Files
CustomNPC-Plus/src/main/java/noppes/npcs/attribute/AttributeController.java
T

66 lines
2.2 KiB
Java
Raw Normal View History

2025-02-24 16:08:22 -05:00
package noppes.npcs.attribute;
2025-02-26 16:13:43 -05:00
import net.minecraft.entity.player.EntityPlayer;
import noppes.npcs.attribute.player.PlayerAttributeTracker;
2025-02-24 16:08:22 -05:00
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
2025-02-26 16:13:43 -05:00
import java.util.UUID;
2025-02-24 16:08:22 -05:00
/**
2025-02-24 20:07:32 -05:00
* AttributeController is the central registry for attribute definitions.
* It ensures that keys are unique.
2025-02-24 16:08:22 -05:00
*/
public class AttributeController {
2025-02-26 12:51:22 -05:00
public AttributeController Instance;
public ModAttributes attributes;
2025-02-26 16:13:43 -05:00
// Tracking Player Attributes
private static final Map<UUID, PlayerAttributeTracker> trackers = new HashMap<>();
2025-02-24 16:08:22 -05:00
private static final Map<String, AttributeDefinition> definitions = new HashMap<>();
2025-02-26 12:51:22 -05:00
public AttributeController(){
this.Instance = this;
definitions.clear();
2025-02-26 16:13:43 -05:00
trackers.clear();
2025-02-26 12:51:22 -05:00
attributes = new ModAttributes();
}
2025-02-27 00:30:39 -05:00
public static AttributeDefinition registerAttribute(String key, String displayName, char colorCode, AttributeValueType valueType, AttributeDefinition.AttributeSection section) {
2025-02-24 16:08:22 -05:00
if (definitions.containsKey(key)) {
2025-02-24 20:07:32 -05:00
throw new IllegalArgumentException("Attribute already registered with key: " + key);
2025-02-24 16:08:22 -05:00
}
2025-02-27 00:30:39 -05:00
AttributeDefinition def = new AttributeDefinition(key, displayName, colorCode, valueType, section);
2025-02-24 16:08:22 -05:00
definitions.put(key, def);
return def;
}
public static AttributeDefinition getAttribute(String key) {
return definitions.get(key);
}
public static Collection<AttributeDefinition> getAllAttributes() {
return definitions.values();
}
2025-02-26 16:13:43 -05:00
2025-02-26 22:49:26 -05:00
public static PlayerAttributeTracker getTracker(EntityPlayer player) {
return trackers.computeIfAbsent(player.getUniqueID(), id -> new PlayerAttributeTracker(id));
2025-02-26 16:13:43 -05:00
}
public static void removeTracker(UUID playerId) {
trackers.remove(playerId);
}
/**
* Update all trackers (e.g. called every 10 ticks for all online players).
*/
public static void updateAllTrackers(Iterable<EntityPlayer> players) {
for (EntityPlayer player : players) {
2025-02-26 22:49:26 -05:00
PlayerAttributeTracker tracker = getTracker(player);
2025-02-26 16:13:43 -05:00
tracker.updateIfChanged(player);
}
}
2025-02-24 16:08:22 -05:00
}