add default properties

This commit is contained in:
Pyrbu 2023-08-26 22:17:56 +02:00
parent 793959b59a
commit 9bb74de8e6
5 changed files with 34 additions and 3 deletions

@ -82,6 +82,8 @@ public class CitizensImporter implements DataImporter {
world = Bukkit.getWorlds().get(0).getName(); world = Bukkit.getWorlds().get(0).getName();
} }
NpcImpl npc = new NpcImpl(uuid, propertyRegistry, configManager, packetFactory, textSerializer, world, typeRegistry.getByName("armor_stand"), new NpcLocation(0, 0, 0, 0, 0)); NpcImpl npc = new NpcImpl(uuid, propertyRegistry, configManager, packetFactory, textSerializer, world, typeRegistry.getByName("armor_stand"), new NpcLocation(0, 0, 0, 0, 0));
npc.getType().applyDefaultProperties(npc);
npc.getHologram().addTextLineComponent(textSerializer.deserialize(name)); npc.getHologram().addTextLineComponent(textSerializer.deserialize(name));
ConfigurationSection traits = npcSection.getConfigurationSection("traits"); ConfigurationSection traits = npcSection.getConfigurationSection("traits");
if (traits != null) { if (traits != null) {

@ -103,6 +103,7 @@ public class ZNpcImporter implements DataImporter {
NpcLocation location = new NpcLocation(oldLoc.getX(), oldLoc.getY(), oldLoc.getZ(), oldLoc.getYaw(), oldLoc.getPitch()); NpcLocation location = new NpcLocation(oldLoc.getX(), oldLoc.getY(), oldLoc.getZ(), oldLoc.getYaw(), oldLoc.getPitch());
UUID uuid = model.getUuid() == null ? UUID.randomUUID() : model.getUuid(); UUID uuid = model.getUuid() == null ? UUID.randomUUID() : model.getUuid();
NpcImpl npc = new NpcImpl(uuid, propertyRegistry, configManager, packetFactory, textSerializer, oldLoc.getWorld(), typeRegistry.getByName(type), location); NpcImpl npc = new NpcImpl(uuid, propertyRegistry, configManager, packetFactory, textSerializer, oldLoc.getWorld(), typeRegistry.getByName(type), location);
npc.getType().applyDefaultProperties(npc);
HologramImpl hologram = npc.getHologram(); HologramImpl hologram = npc.getHologram();
hologram.setOffset(model.getHologramHeight()); hologram.setOffset(model.getHologramHeight());

@ -139,6 +139,7 @@ public class NpcRegistryImpl implements NpcRegistry {
id = id.toLowerCase(); id = id.toLowerCase();
if (npcIdLookupMap.containsKey(id)) throw new IllegalArgumentException("An npc with the id " + id + " already exists!"); if (npcIdLookupMap.containsKey(id)) throw new IllegalArgumentException("An npc with the id " + id + " already exists!");
NpcImpl npc = new NpcImpl(UUID.randomUUID(), propertyRegistry, configManager, textSerializer, world, type, location, packetFactory); NpcImpl npc = new NpcImpl(UUID.randomUUID(), propertyRegistry, configManager, textSerializer, world, type, location, packetFactory);
type.applyDefaultProperties(npc);
NpcEntryImpl entry = new NpcEntryImpl(id, npc); NpcEntryImpl entry = new NpcEntryImpl(id, npc);
register(entry); register(entry);
return entry; return entry;

@ -16,14 +16,16 @@ import java.util.stream.Collectors;
public class NpcTypeImpl implements NpcType { public class NpcTypeImpl implements NpcType {
private final EntityType type; private final EntityType type;
private final Set<EntityPropertyImpl<?>> allowedProperties; private final Set<EntityPropertyImpl<?>> allowedProperties;
private final Map<EntityPropertyImpl<?>, Object> defaultProperties;
private final String name; private final String name;
private final double hologramOffset; private final double hologramOffset;
private NpcTypeImpl(String name, EntityType type, double hologramOffset, Set<EntityPropertyImpl<?>> allowedProperties) { private NpcTypeImpl(String name, EntityType type, double hologramOffset, Set<EntityPropertyImpl<?>> allowedProperties, Map<EntityPropertyImpl<?>, Object> defaultProperties) {
this.name = name.toLowerCase(); this.name = name.toLowerCase();
this.type = type; this.type = type;
this.hologramOffset = hologramOffset; this.hologramOffset = hologramOffset;
this.allowedProperties = allowedProperties; this.allowedProperties = allowedProperties;
this.defaultProperties = defaultProperties;
} }
public String getName() { public String getName() {
@ -42,6 +44,12 @@ public class NpcTypeImpl implements NpcType {
return allowedProperties.stream().map(property -> (EntityProperty<?>) property).collect(Collectors.toSet()); return allowedProperties.stream().map(property -> (EntityProperty<?>) property).collect(Collectors.toSet());
} }
public void applyDefaultProperties(NpcImpl npc) {
for (Map.Entry<EntityPropertyImpl<?>, Object> entry : defaultProperties.entrySet()) {
npc.UNSAFE_setProperty(entry.getKey(), entry.getValue());
}
}
protected static final class Builder { protected static final class Builder {
private final static Logger logger = Logger.getLogger("NpcTypeBuilder"); private final static Logger logger = Logger.getLogger("NpcTypeBuilder");
@ -49,6 +57,7 @@ public class NpcTypeImpl implements NpcType {
private final String name; private final String name;
private final EntityType type; private final EntityType type;
private final List<EntityPropertyImpl<?>> allowedProperties = new ArrayList<>(); private final List<EntityPropertyImpl<?>> allowedProperties = new ArrayList<>();
private final Map<EntityPropertyImpl<?>, Object> defaultProperties = new HashMap<>();
private double hologramOffset = 0; private double hologramOffset = 0;
Builder(EntityPropertyRegistryImpl propertyRegistry, String name, EntityType type) { Builder(EntityPropertyRegistryImpl propertyRegistry, String name, EntityType type) {
@ -81,6 +90,17 @@ public class NpcTypeImpl implements NpcType {
return this; return this;
} }
@SuppressWarnings("unchecked")
public <T> Builder addDefaultProperty(String name, T value) {
EntityPropertyImpl<T> property = (EntityPropertyImpl<T>) propertyRegistry.getByName(name);
if (property == null) {
logger.warning("Tried to register the non-existent \"" + name + "\" default property to the \"" + this.name + "\" npc type");
return this;
}
defaultProperties.put(property, value);
return this;
}
public Builder setHologramOffset(double hologramOffset) { public Builder setHologramOffset(double hologramOffset) {
this.hologramOffset = hologramOffset; this.hologramOffset = hologramOffset;
return this; return this;
@ -109,7 +129,7 @@ public class NpcTypeImpl implements NpcType {
} else if (version.isOlderThan(ServerVersion.V_1_11) && type.equals(EntityTypes.HORSE)) { } else if (version.isOlderThan(ServerVersion.V_1_11) && type.equals(EntityTypes.HORSE)) {
addProperties("has_chest"); addProperties("has_chest");
} }
return new NpcTypeImpl(name, type, hologramOffset, new HashSet<>(allowedProperties)); return new NpcTypeImpl(name, type, hologramOffset, new HashSet<>(allowedProperties), defaultProperties);
} }
} }
} }

@ -36,7 +36,14 @@ public class NpcTypeRegistryImpl implements NpcTypeRegistry {
register(builder(p, "player", EntityTypes.PLAYER) register(builder(p, "player", EntityTypes.PLAYER)
.setHologramOffset(-0.15D) .setHologramOffset(-0.15D)
.addEquipmentProperties() .addEquipmentProperties()
.addProperties("skin_cape", "skin_jacket", "skin_left_sleeve", "skin_right_sleeve", "skin_left_leg", "skin_right_leg", "skin_hat", "shoulder_entity_left", "shoulder_entity_right")); .addProperties("skin_cape", "skin_jacket", "skin_left_sleeve", "skin_right_sleeve", "skin_left_leg", "skin_right_leg", "skin_hat", "shoulder_entity_left", "shoulder_entity_right")
.addDefaultProperty("skin_cape", true)
.addDefaultProperty("skin_jacket", true)
.addDefaultProperty("skin_left_sleeve", true)
.addDefaultProperty("skin_right_sleeve", true)
.addDefaultProperty("skin_left_leg", true)
.addDefaultProperty("skin_right_leg", true)
.addDefaultProperty("skin_hat", true));
// Most hologram offsets generated using Entity#getHeight() in 1.19.4 // Most hologram offsets generated using Entity#getHeight() in 1.19.4