From 15569c529fc7c9b32bccd16a576a8232595f4376 Mon Sep 17 00:00:00 2001 From: fullwall Date: Mon, 4 Sep 2017 19:16:49 +0800 Subject: [PATCH] Add /npc shulker --- .../citizensnpcs/commands/NPCCommands.java | 26 ++++++++++++++- .../npc/CitizensTraitFactory.java | 2 ++ .../net/citizensnpcs/trait/ShulkerTrait.java | 33 +++++++++++++++++++ .../java/net/citizensnpcs/util/Messages.java | 1 + .../main/java/net/citizensnpcs/util/NMS.java | 6 +++- .../java/net/citizensnpcs/util/NMSBridge.java | 3 ++ .../src/main/resources/messages_en.properties | 1 + .../nms/v1_10_R1/util/NMSImpl.java | 7 ++++ .../nms/v1_11_R1/util/NMSImpl.java | 11 ++++--- .../nms/v1_12_R1/util/NMSImpl.java | 7 ++++ 10 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 main/src/main/java/net/citizensnpcs/trait/ShulkerTrait.java diff --git a/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java b/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java index 28cf32c32..a428676c4 100644 --- a/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java +++ b/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java @@ -82,6 +82,7 @@ import net.citizensnpcs.trait.Powered; import net.citizensnpcs.trait.RabbitType; import net.citizensnpcs.trait.ScriptTrait; import net.citizensnpcs.trait.SheepTrait; +import net.citizensnpcs.trait.ShulkerTrait; import net.citizensnpcs.trait.SkinLayers; import net.citizensnpcs.trait.SkinLayers.Layer; import net.citizensnpcs.trait.SlimeSize; @@ -1441,10 +1442,33 @@ public class NPCCommands { } } + @Command( + aliases = { "npc" }, + usage = "shulker (--peek [peek])", + desc = "Sets shulker modifiers.", + modifiers = { "shulker" }, + min = 1, + max = 1, + permission = "citizens.npc.shulker") + @Requirements(selected = true, ownership = true, types = { EntityType.SHULKER }) + public void shulker(CommandContext args, CommandSender sender, NPC npc) throws CommandException { + ShulkerTrait trait = npc.getTrait(ShulkerTrait.class); + boolean hasArg = false; + if (args.hasValueFlag("peek")) { + int peek = (byte) args.getFlagInteger("peek"); + trait.setPeek(peek); + Messaging.sendTr(sender, Messages.SHULKER_PEEK_SET, npc.getName(), peek); + hasArg = true; + } + if (!hasArg) { + throw new CommandException(); + } + } + @Command( aliases = { "npc" }, usage = "skin (-c -p -f) [name]", - desc = "Sets an NPC's skin name, Use -p to save a skin snapshot that won't change", + desc = "Sets an NPC's skin name. Use -p to save a skin snapshot that won't change", modifiers = { "skin" }, min = 1, max = 2, diff --git a/main/src/main/java/net/citizensnpcs/npc/CitizensTraitFactory.java b/main/src/main/java/net/citizensnpcs/npc/CitizensTraitFactory.java index 60430cfd8..92bac7244 100644 --- a/main/src/main/java/net/citizensnpcs/npc/CitizensTraitFactory.java +++ b/main/src/main/java/net/citizensnpcs/npc/CitizensTraitFactory.java @@ -39,6 +39,7 @@ import net.citizensnpcs.trait.RabbitType; import net.citizensnpcs.trait.Saddle; import net.citizensnpcs.trait.ScriptTrait; import net.citizensnpcs.trait.SheepTrait; +import net.citizensnpcs.trait.ShulkerTrait; import net.citizensnpcs.trait.SkinLayers; import net.citizensnpcs.trait.SlimeSize; import net.citizensnpcs.trait.VillagerProfession; @@ -72,6 +73,7 @@ public class CitizensTraitFactory implements TraitFactory { registerTrait(TraitInfo.create(Saddle.class)); registerTrait(TraitInfo.create(ScriptTrait.class)); registerTrait(TraitInfo.create(SheepTrait.class)); + registerTrait(TraitInfo.create(ShulkerTrait.class)); registerTrait(TraitInfo.create(SkinLayers.class)); registerTrait(TraitInfo.create(MountTrait.class)); registerTrait(TraitInfo.create(SlimeSize.class)); diff --git a/main/src/main/java/net/citizensnpcs/trait/ShulkerTrait.java b/main/src/main/java/net/citizensnpcs/trait/ShulkerTrait.java new file mode 100644 index 000000000..204043f91 --- /dev/null +++ b/main/src/main/java/net/citizensnpcs/trait/ShulkerTrait.java @@ -0,0 +1,33 @@ +package net.citizensnpcs.trait; + +import org.bukkit.entity.Shulker; + +import net.citizensnpcs.api.persistence.Persist; +import net.citizensnpcs.api.trait.Trait; +import net.citizensnpcs.api.trait.TraitName; +import net.citizensnpcs.util.NMS; + +@TraitName("shulkertrait") +public class ShulkerTrait extends Trait { + @Persist("peek") + private int peek = 0; + + public ShulkerTrait() { + super("shulkertrait"); + } + + @Override + public void onSpawn() { + } + + @Override + public void run() { + if (npc.getEntity() instanceof Shulker) { + NMS.setShulkerPeek((Shulker) npc.getEntity(), peek); + } + } + + public void setPeek(int peek) { + this.peek = peek; + } +} diff --git a/main/src/main/java/net/citizensnpcs/util/Messages.java b/main/src/main/java/net/citizensnpcs/util/Messages.java index 3ec4e2e7e..2112daf7c 100644 --- a/main/src/main/java/net/citizensnpcs/util/Messages.java +++ b/main/src/main/java/net/citizensnpcs/util/Messages.java @@ -204,6 +204,7 @@ public class Messages { public static final String SHEARED_SET = "citizens.editors.equipment.sheared-set"; public static final String SHEARED_STOPPED = "citizens.editors.equipment.sheared-stopped"; public static final String SHEEP_COLOR_SET = "citizens.commands.npc.sheep.color-set"; + public static final String SHULKER_PEEK_SET = "citizens.commands.npc.shulker.peek-set"; public static final String SIZE_DESCRIPTION = "citizens.commands.npc.size.description"; public static final String SIZE_SET = "citizens.commands.npc.size.set"; public static final String SKELETON_TYPE_SET = "citizens.commands.npc.skeletontype.set"; diff --git a/main/src/main/java/net/citizensnpcs/util/NMS.java b/main/src/main/java/net/citizensnpcs/util/NMS.java index 483ccaef4..2559e5329 100644 --- a/main/src/main/java/net/citizensnpcs/util/NMS.java +++ b/main/src/main/java/net/citizensnpcs/util/NMS.java @@ -13,6 +13,7 @@ import org.bukkit.entity.FishHook; import org.bukkit.entity.Horse; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; +import org.bukkit.entity.Shulker; import org.bukkit.entity.Tameable; import org.bukkit.entity.Wither; import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason; @@ -261,6 +262,10 @@ public class NMS { BRIDGE.setShouldJump(entity); } + public static void setShulkerPeek(Shulker shulker, int peek) { + BRIDGE.setShulkerPeek(shulker, peek); + } + public static void setSitting(Tameable tameable, boolean sitting) { BRIDGE.setSitting(tameable, sitting); } @@ -306,6 +311,5 @@ public class NMS { } private static NMSBridge BRIDGE; - private static Field MODIFIERS_FIELD = NMS.getField(Field.class, "modifiers"); } diff --git a/main/src/main/java/net/citizensnpcs/util/NMSBridge.java b/main/src/main/java/net/citizensnpcs/util/NMSBridge.java index e2b023d52..6b36d19d4 100644 --- a/main/src/main/java/net/citizensnpcs/util/NMSBridge.java +++ b/main/src/main/java/net/citizensnpcs/util/NMSBridge.java @@ -12,6 +12,7 @@ import org.bukkit.entity.FishHook; import org.bukkit.entity.Horse; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; +import org.bukkit.entity.Shulker; import org.bukkit.entity.Tameable; import org.bukkit.entity.Wither; import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason; @@ -125,6 +126,8 @@ public interface NMSBridge { public void setShouldJump(Entity entity); + public void setShulkerPeek(Shulker shulker, int peek); + public void setSitting(Tameable tameable, boolean sitting); public void setStepHeight(Entity entity, float height); diff --git a/main/src/main/resources/messages_en.properties b/main/src/main/resources/messages_en.properties index 628a445ce..59a0f8459 100644 --- a/main/src/main/resources/messages_en.properties +++ b/main/src/main/resources/messages_en.properties @@ -110,6 +110,7 @@ citizens.commands.npc.respawn.describe=Respawn delay is currently [[{0}]]. citizens.commands.npc.select.already-selected=You already have that NPC selected. citizens.commands.npc.script.invalid-file=Unknown or unavailable script ''[[{0}]]''. citizens.commands.npc.script.current-scripts=[[{0}]]''s current scripts are [[{1}]]. +citizens.commands.npc.shulker.peek-set=[[{0}]]''s peek amount set to [[{1}]]. citizens.commands.npc.skin.set=[[{0}]]''s skin name set to [[{1}]]. citizens.commands.npc.skin.cleared=[[{0}]]''s skin name was cleared. citizens.commands.npc.skin.layers-set=[[{0}]]''s skin layers: cape - [[{1}]], hat - [[{2}]], jacket - [[{3}]], sleeves - [[{4}]], pants - [[{5}]]. diff --git a/v1_10_R1/src/main/java/net/citizensnpcs/nms/v1_10_R1/util/NMSImpl.java b/v1_10_R1/src/main/java/net/citizensnpcs/nms/v1_10_R1/util/NMSImpl.java index d53b3eb66..81c8019c3 100644 --- a/v1_10_R1/src/main/java/net/citizensnpcs/nms/v1_10_R1/util/NMSImpl.java +++ b/v1_10_R1/src/main/java/net/citizensnpcs/nms/v1_10_R1/util/NMSImpl.java @@ -35,6 +35,7 @@ import org.bukkit.entity.FishHook; import org.bukkit.entity.Horse; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; +import org.bukkit.entity.Shulker; import org.bukkit.entity.Tameable; import org.bukkit.entity.Wither; import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason; @@ -168,6 +169,7 @@ import net.minecraft.server.v1_10_R1.EntityMinecartAbstract; import net.minecraft.server.v1_10_R1.EntityPlayer; import net.minecraft.server.v1_10_R1.EntityPolarBear; import net.minecraft.server.v1_10_R1.EntityRabbit; +import net.minecraft.server.v1_10_R1.EntityShulker; import net.minecraft.server.v1_10_R1.EntityTameableAnimal; import net.minecraft.server.v1_10_R1.EntityTracker; import net.minecraft.server.v1_10_R1.EntityTrackerEntry; @@ -876,6 +878,11 @@ public class NMSImpl implements NMSBridge { } } + @Override + public void setShulkerPeek(Shulker shulker, int peek) { + ((EntityShulker) getHandle(shulker)).a((byte) peek); + } + @Override public void setSitting(Tameable tameable, boolean sitting) { ((EntityTameableAnimal) NMSImpl.getHandle((LivingEntity) tameable)).setSitting(sitting); diff --git a/v1_11_R1/src/main/java/net/citizensnpcs/nms/v1_11_R1/util/NMSImpl.java b/v1_11_R1/src/main/java/net/citizensnpcs/nms/v1_11_R1/util/NMSImpl.java index 3a7ffee7d..5a1af59b3 100644 --- a/v1_11_R1/src/main/java/net/citizensnpcs/nms/v1_11_R1/util/NMSImpl.java +++ b/v1_11_R1/src/main/java/net/citizensnpcs/nms/v1_11_R1/util/NMSImpl.java @@ -35,6 +35,7 @@ import org.bukkit.entity.FishHook; import org.bukkit.entity.Horse; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; +import org.bukkit.entity.Shulker; import org.bukkit.entity.Tameable; import org.bukkit.entity.Wither; import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason; @@ -188,6 +189,7 @@ import net.minecraft.server.v1_11_R1.EntityMinecartAbstract; import net.minecraft.server.v1_11_R1.EntityPlayer; import net.minecraft.server.v1_11_R1.EntityPolarBear; import net.minecraft.server.v1_11_R1.EntityRabbit; +import net.minecraft.server.v1_11_R1.EntityShulker; import net.minecraft.server.v1_11_R1.EntityTameableAnimal; import net.minecraft.server.v1_11_R1.EntityTracker; import net.minecraft.server.v1_11_R1.EntityTrackerEntry; @@ -937,6 +939,11 @@ public class NMSImpl implements NMSBridge { } } + @Override + public void setShulkerPeek(Shulker shulker, int peek) { + ((EntityShulker) getHandle(shulker)).a((byte) peek); + } + @Override public void setSitting(Tameable tameable, boolean sitting) { ((EntityTameableAnimal) NMSImpl.getHandle((LivingEntity) tameable)).setSitting(sitting); @@ -1486,13 +1493,9 @@ public class NMSImpl implements NMSBridge { public static final Location PACKET_CACHE_LOCATION = new Location(null, 0, 0, 0); private static Field PATHFINDING_RANGE = NMS.getField(NavigationAbstract.class, "f"); private static final Field RABBIT_FIELD = NMS.getField(EntityRabbit.class, "bw"); - private static final Random RANDOM = Util.getFastRandom(); - private static Field SKULL_PROFILE_FIELD; - private static Field TRACKED_ENTITY_SET = NMS.getField(EntityTracker.class, "c"); - private static final Field WITHER_BOSS_BAR_FIELD = NMS.getField(EntityWither.class, "bF"); static { diff --git a/v1_12_R1/src/main/java/net/citizensnpcs/nms/v1_12_R1/util/NMSImpl.java b/v1_12_R1/src/main/java/net/citizensnpcs/nms/v1_12_R1/util/NMSImpl.java index f3d5fc123..f2e469bfa 100644 --- a/v1_12_R1/src/main/java/net/citizensnpcs/nms/v1_12_R1/util/NMSImpl.java +++ b/v1_12_R1/src/main/java/net/citizensnpcs/nms/v1_12_R1/util/NMSImpl.java @@ -35,6 +35,7 @@ import org.bukkit.entity.FishHook; import org.bukkit.entity.Horse; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; +import org.bukkit.entity.Shulker; import org.bukkit.entity.Tameable; import org.bukkit.entity.Wither; import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason; @@ -192,6 +193,7 @@ import net.minecraft.server.v1_12_R1.EntityMinecartAbstract; import net.minecraft.server.v1_12_R1.EntityPlayer; import net.minecraft.server.v1_12_R1.EntityPolarBear; import net.minecraft.server.v1_12_R1.EntityRabbit; +import net.minecraft.server.v1_12_R1.EntityShulker; import net.minecraft.server.v1_12_R1.EntityTameableAnimal; import net.minecraft.server.v1_12_R1.EntityTracker; import net.minecraft.server.v1_12_R1.EntityTrackerEntry; @@ -952,6 +954,11 @@ public class NMSImpl implements NMSBridge { } } + @Override + public void setShulkerPeek(Shulker shulker, int peek) { + ((EntityShulker) getHandle(shulker)).a((byte) peek); + } + @Override public void setSitting(Tameable tameable, boolean sitting) { ((EntityTameableAnimal) NMSImpl.getHandle((LivingEntity) tameable)).setSitting(sitting);