added rabbit_type property

This commit is contained in:
D3v1s0m 2023-10-02 10:02:52 +05:30
parent 82cd0d1a81
commit 58d81a740c
No known key found for this signature in database
GPG Key ID: FA1F770C7B1D40C1
5 changed files with 83 additions and 1 deletions

@ -0,0 +1,22 @@
package lol.pyr.znpcsplus.util;
public enum RabbitType {
BROWN(0),
WHITE(1),
BLACK(2),
BLACK_AND_WHITE(3),
GOLD(4),
SALT_AND_PEPPER(5),
THE_KILLER_BUNNY(99),
TOAST(100);
private final int id;
RabbitType(int id) {
this.id = id;
}
public int getId() {
return id;
}
}

@ -280,6 +280,7 @@ public class ZNpcsPlus extends JavaPlugin {
registerEnumParser(manager, LookType.class, incorrectUsageMessage);
registerEnumParser(manager, TropicalFishVariant.TropicalFishPattern.class, incorrectUsageMessage);
registerEnumParser(manager, SnifferState.class, incorrectUsageMessage);
registerEnumParser(manager, RabbitType.class, incorrectUsageMessage);
manager.registerCommand("npc", new MultiCommand(loadHelpMessage("root"))
.addSubcommand("center", new CenterCommand(npcRegistry))

@ -78,6 +78,7 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry {
registerEnumSerializer(PuffState.class);
registerEnumSerializer(TropicalFishVariant.TropicalFishPattern.class);
registerEnumSerializer(SnifferState.class);
registerEnumSerializer(RabbitType.class);
registerPrimitiveSerializers(Integer.class, Boolean.class, Double.class, Float.class, Long.class, Short.class, Byte.class, String.class);
@ -339,6 +340,16 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry {
else pigIndex = 16;
register(new BooleanProperty("pig_saddled", pigIndex, false, legacyBooleans));
// Rabbit
int rabbitIndex;
if (ver.isNewerThanOrEquals(ServerVersion.V_1_17)) rabbitIndex = 17;
else if (ver.isNewerThanOrEquals(ServerVersion.V_1_15)) rabbitIndex = 16;
else if (ver.isNewerThanOrEquals(ServerVersion.V_1_14)) rabbitIndex = 15;
else if (ver.isNewerThanOrEquals(ServerVersion.V_1_10)) rabbitIndex = 13;
else if (ver.isNewerThanOrEquals(ServerVersion.V_1_9)) rabbitIndex = 12;
else rabbitIndex = 18;
register(new RabbitTypeProperty(rabbitIndex, legacyBooleans, legacyNames, optionalComponents));
if (!ver.isNewerThanOrEquals(ServerVersion.V_1_10)) return;
// Polar Bear
int polarBearIndex;

@ -0,0 +1,47 @@
package lol.pyr.znpcsplus.entity.properties;
import com.github.retrooper.packetevents.protocol.entity.data.EntityData;
import com.github.retrooper.packetevents.protocol.entity.data.EntityDataType;
import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes;
import com.github.retrooper.packetevents.util.adventure.AdventureSerializer;
import lol.pyr.znpcsplus.entity.EntityPropertyImpl;
import lol.pyr.znpcsplus.entity.PacketEntity;
import lol.pyr.znpcsplus.util.RabbitType;
import net.kyori.adventure.text.Component;
import org.bukkit.entity.Player;
import java.util.Map;
import java.util.Optional;
public class RabbitTypeProperty extends EntityPropertyImpl<RabbitType> {
private final int index;
private final boolean legacyBooleans;
private final Object serialized;
private final EntityDataType<?> type;
public RabbitTypeProperty(int index, boolean legacyBooleans, boolean legacyNames, boolean optional) {
super("rabbit_type", RabbitType.BROWN, RabbitType.class);
this.index = index;
this.legacyBooleans = legacyBooleans;
Component name = Component.text("Toast");
String serialized = legacyNames ?
AdventureSerializer.getLegacyGsonSerializer().serialize(name) :
AdventureSerializer.getGsonSerializer().serialize(name);
this.serialized = optional ? Optional.of(serialized) : serialized;
this.type = optional ? EntityDataTypes.OPTIONAL_COMPONENT : EntityDataTypes.STRING;
}
@Override
public void apply(Player player, PacketEntity entity, boolean isSpawned, Map<Integer, EntityData> properties) {
RabbitType rabbitType = entity.getProperty(this);
if (rabbitType == null) return;
if (!rabbitType.equals(RabbitType.TOAST)) {
properties.put(index, legacyBooleans ?
newEntityData(index, EntityDataTypes.BYTE, (byte) rabbitType.getId()) :
newEntityData(index, EntityDataTypes.INT, rabbitType.getId()));
properties.put(2, new EntityData(2, type, null));
} else {
properties.put(2, new EntityData(2, type, serialized));
}
}
}

@ -115,7 +115,8 @@ public class NpcTypeRegistryImpl implements NpcTypeRegistry {
.addProperties("pig_saddled"));
register(builder(p, "rabbit", EntityTypes.RABBIT)
.setHologramOffset(-1.475));
.setHologramOffset(-1.475)
.addProperties("rabbit_type"));
register(builder(p, "sheep", EntityTypes.SHEEP)
.setHologramOffset(-0.675));