implement villager properties

This commit is contained in:
Pyrbu 2023-09-13 01:59:52 +02:00
parent fb3a22a355
commit df5fd8078f
7 changed files with 134 additions and 34 deletions

@ -2,28 +2,28 @@ package lol.pyr.znpcsplus.util;
public enum VillagerProfession {
NONE(0),
ARMORER(3),
BUTCHER(4),
CARTOGRAPHER(1),
CLERIC(2),
FARMER(0),
FISHERMAN(0),
FLETCHER(0),
LEATHER_WORKER(4),
LIBRARIAN(1),
MASON(-1),
NITWIT(5),
SHEPHERD(0),
TOOL_SMITH(3),
WEAPON_SMITH(3);
ARMORER(1),
BUTCHER(2),
CARTOGRAPHER(3),
CLERIC(4),
FARMER(5),
FISHERMAN(6),
FLETCHER(7),
LEATHER_WORKER(8),
LIBRARIAN(9),
MASON(10),
NITWIT(11),
SHEPHERD(12),
TOOL_SMITH(13),
WEAPON_SMITH(14);
private final int legacyId;
private final int id;
VillagerProfession(int legacyId) {
this.legacyId = legacyId;
VillagerProfession(int id) {
this.id = id;
}
public int getLegacyId() {
return legacyId;
public int getId() {
return id;
}
}

@ -1,11 +1,20 @@
package lol.pyr.znpcsplus.util;
public enum VillagerType {
DESERT,
JUNGLE,
PLAINS,
SAVANNA,
SNOW,
SWAMP,
TAIGA
DESERT(0),
JUNGLE(1),
PLAINS(2),
SAVANNA(3),
SNOW(4),
SWAMP(5),
TAIGA(6);
private final int id;
VillagerType(int id) {
this.id = id;
}
public int getId() {
return id;
}
}

@ -11,6 +11,9 @@ import lol.pyr.znpcsplus.api.entity.EntityProperty;
import lol.pyr.znpcsplus.api.entity.EntityPropertyRegistry;
import lol.pyr.znpcsplus.api.skin.SkinDescriptor;
import lol.pyr.znpcsplus.entity.properties.*;
import lol.pyr.znpcsplus.entity.properties.villager.VillagerLevelProperty;
import lol.pyr.znpcsplus.entity.properties.villager.VillagerProfessionProperty;
import lol.pyr.znpcsplus.entity.properties.villager.VillagerTypeProperty;
import lol.pyr.znpcsplus.entity.serializers.*;
import lol.pyr.znpcsplus.packets.PacketFactory;
import lol.pyr.znpcsplus.skin.cache.MojangSkinCache;
@ -109,11 +112,6 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry {
registerType("wolf_collar_color", DyeColor.RED); // TODO
registerType("wolf_angry", false); // TODO
// Villager
registerType("villager_type", VillagerType.PLAINS);
registerType("villager_profession", VillagerProfession.NONE);
registerType("villager_level", VillagerLevel.STONE);
// Show Golem
registerType("pumpkin", true); // TODO
@ -395,6 +393,19 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry {
// Pose
register(new NpcPoseProperty());
// Villager
final int villagerIndex;
if (ver.isNewerThanOrEquals(ServerVersion.V_1_17)) villagerIndex = 18;
else if (ver.isNewerThanOrEquals(ServerVersion.V_1_15)) villagerIndex = 17;
else if (ver.isNewerThanOrEquals(ServerVersion.V_1_14)) villagerIndex = 16;
else if (ver.isNewerThanOrEquals(ServerVersion.V_1_10)) villagerIndex = 13;
else if (ver.isNewerThanOrEquals(ServerVersion.V_1_9)) villagerIndex = 12;
else villagerIndex = 16;
register(new VillagerTypeProperty("villager_type", villagerIndex, VillagerType.PLAINS));
register(new VillagerProfessionProperty("villager_profession", villagerIndex, VillagerProfession.NONE));
register(new VillagerLevelProperty("villager_level", villagerIndex, VillagerLevel.STONE));
linkProperties("villager_type", "villager_profession", "villager_level");
// Cat
int catIndex;
if (ver.isNewerThanOrEquals(ServerVersion.V_1_17)) catIndex = 19;
@ -470,11 +481,10 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry {
register(new BooleanProperty("piglin_dancing", piglinIndex, false, legacyBooleans));
// Pillager
int pillagerIndex = zombificationIndex;
register(new BooleanProperty("pillager_charging", pillagerIndex, false, legacyBooleans));
register(new BooleanProperty("pillager_charging", zombificationIndex, false, legacyBooleans));
// Vindicator
int vindicatorIndex = pillagerIndex-1;
int vindicatorIndex = zombificationIndex -1;
register(new BooleanProperty("celebrating", vindicatorIndex, false, legacyBooleans));
if (!ver.isNewerThanOrEquals(ServerVersion.V_1_17)) return;

@ -0,0 +1,31 @@
package lol.pyr.znpcsplus.entity.properties.villager;
import com.github.retrooper.packetevents.protocol.entity.data.EntityData;
import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes;
import com.github.retrooper.packetevents.protocol.entity.villager.VillagerData;
import com.github.retrooper.packetevents.protocol.entity.villager.profession.VillagerProfessions;
import com.github.retrooper.packetevents.protocol.entity.villager.type.VillagerTypes;
import lol.pyr.znpcsplus.entity.EntityPropertyImpl;
import lol.pyr.znpcsplus.entity.PacketEntity;
import org.bukkit.entity.Player;
import java.util.Map;
public abstract class VillagerDataProperty<T> extends EntityPropertyImpl<T> {
private final int index;
@SuppressWarnings("unchecked")
public VillagerDataProperty(String name, int index, T def) {
super(name, def, (Class<T>) def.getClass());
this.index = index;
}
@Override
public void apply(Player player, PacketEntity entity, boolean isSpawned, Map<Integer, EntityData> properties) {
EntityData oldData = properties.get(index);
VillagerData old = oldData == null ? new VillagerData(VillagerTypes.PLAINS, VillagerProfessions.NONE, 1) : (VillagerData) oldData.getValue();
properties.put(index, newEntityData(index, EntityDataTypes.VILLAGER_DATA, apply(old, entity.getProperty(this))));
}
protected abstract VillagerData apply(VillagerData data, T value);
}

@ -0,0 +1,16 @@
package lol.pyr.znpcsplus.entity.properties.villager;
import com.github.retrooper.packetevents.protocol.entity.villager.VillagerData;
import lol.pyr.znpcsplus.util.VillagerLevel;
public class VillagerLevelProperty extends VillagerDataProperty<VillagerLevel> {
public VillagerLevelProperty(String name, int index, VillagerLevel def) {
super(name, index, def);
}
@Override
protected VillagerData apply(VillagerData data, VillagerLevel value) {
data.setLevel(value.ordinal() + 1);
return data;
}
}

@ -0,0 +1,17 @@
package lol.pyr.znpcsplus.entity.properties.villager;
import com.github.retrooper.packetevents.protocol.entity.villager.VillagerData;
import com.github.retrooper.packetevents.protocol.entity.villager.profession.VillagerProfessions;
import lol.pyr.znpcsplus.util.VillagerProfession;
public class VillagerProfessionProperty extends VillagerDataProperty<VillagerProfession> {
public VillagerProfessionProperty(String name, int index, VillagerProfession def) {
super(name, index, def);
}
@Override
protected VillagerData apply(VillagerData data, VillagerProfession value) {
data.setProfession(VillagerProfessions.getById(value.getId()));
return data;
}
}

@ -0,0 +1,17 @@
package lol.pyr.znpcsplus.entity.properties.villager;
import com.github.retrooper.packetevents.protocol.entity.villager.VillagerData;
import com.github.retrooper.packetevents.protocol.entity.villager.type.VillagerTypes;
import lol.pyr.znpcsplus.util.VillagerType;
public class VillagerTypeProperty extends VillagerDataProperty<VillagerType> {
public VillagerTypeProperty(String name, int index, VillagerType def) {
super(name, index, def);
}
@Override
protected VillagerData apply(VillagerData data, VillagerType value) {
data.setType(VillagerTypes.getById(value.getId()));
return data;
}
}