Skip to content

Commit

Permalink
Fix bug with /npc snowman /npc pufferfish and make /npc ocelot more d…
Browse files Browse the repository at this point in the history
…escriptive
  • Loading branch information
fullwall committed May 7, 2020
1 parent c720160 commit 3cf8c7c
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 43 deletions.
6 changes: 4 additions & 2 deletions main/src/main/java/net/citizensnpcs/commands/NPCCommands.java
Expand Up @@ -1166,6 +1166,9 @@ public void ocelot(CommandContext args, CommandSender sender, NPC npc) throws Co
throw new CommandException(Messages.INVALID_OCELOT_TYPE, valid);
}
trait.setType(type);
if (!trait.supportsOcelotType()) {
Messaging.sendErrorTr(sender, Messages.OCELOT_TYPE_DEPRECATED);
}
}
}

Expand Down Expand Up @@ -1249,15 +1252,14 @@ public void pathfindingOptions(CommandContext args, CommandSender sender, NPC np
double range = Double.parseDouble(args.getFlag("attack-range"));
if (range < 0)
throw new CommandUsageException();
npc.getNavigator().getDefaultParameters().attackRange(range);
npc.getNavigator().getDefaultParameters().attackRange(Math.pow(range, 2));
output += Messaging.tr(Messages.PATHFINDING_OPTIONS_ATTACK_RANGE_SET, npc.getName(), range);
}
if (args.hasValueFlag("use-new-finder")) {
String raw = args.getFlag("use-new-finder", args.getFlag("unf"));
boolean use = Boolean.parseBoolean(raw);
npc.getNavigator().getDefaultParameters().useNewPathfinder(use);
output += Messaging.tr(Messages.PATHFINDING_OPTIONS_USE_NEW_FINDER, npc.getName(), use);

}
if (output.isEmpty()) {
throw new CommandUsageException();
Expand Down
35 changes: 24 additions & 11 deletions main/src/main/java/net/citizensnpcs/trait/OcelotModifiers.java
Expand Up @@ -24,6 +24,11 @@ public OcelotModifiers() {
super("ocelotmodifiers");
}

private void migrateToCat() {
npc.getTrait(CatTrait.class).setSitting(sitting);
npc.getTrait(CatTrait.class).setType(type);
}

@Override
public void onSpawn() {
updateModifiers();
Expand All @@ -39,18 +44,26 @@ public void setType(Ocelot.Type type) {
updateModifiers();
}

public boolean supportsOcelotType() {
return SUPPORTS_CAT_TYPE;
}

private void updateModifiers() {
if (npc.getEntity() instanceof Ocelot) {
Ocelot ocelot = (Ocelot) npc.getEntity();
try {
ocelot.setCatType(type);
} catch (UnsupportedOperationException ex) {
npc.getTrait(CatTrait.class).setSitting(sitting);
npc.getTrait(CatTrait.class).setType(type);
npc.removeTrait(OcelotModifiers.class);
return;
}
NMS.setSitting(ocelot, sitting);
if (!(npc.getEntity() instanceof Ocelot))
return;
Ocelot ocelot = (Ocelot) npc.getEntity();
NMS.setSitting(ocelot, sitting);
if (!SUPPORTS_CAT_TYPE) {
migrateToCat();
return;
}
try {
ocelot.setCatType(type);
} catch (UnsupportedOperationException ex) {
migrateToCat();
SUPPORTS_CAT_TYPE = false;
}
}

private static boolean SUPPORTS_CAT_TYPE = true;
}
Expand Up @@ -30,15 +30,15 @@ public boolean isLyingDown() {

@Override
public void run() {
if (npc.isSpawned() && npc.getEntity() instanceof Cat) {
Cat cat = (Cat) npc.getEntity();
cat.setSitting(sitting);
cat.setCatType(type);
if (collarColor != null) {
cat.setCollarColor(collarColor);
}
NMS.setLyingDown(cat, lying);
if (!(npc.getEntity() instanceof Cat))
return;
Cat cat = (Cat) npc.getEntity();
cat.setSitting(sitting);
cat.setCatType(type);
if (collarColor != null) {
cat.setCollarColor(collarColor);
}
NMS.setLyingDown(cat, lying);
}

public void setCollarColor(DyeColor color) {
Expand Down
@@ -1,26 +1,20 @@
package net.citizensnpcs.trait.versioned;

import org.bukkit.entity.PufferFish;

import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;

@TraitName("pufferfishtrait")
public class PufferFishTrait extends Trait {
@Persist
private int puffState = 1;
private int puffState = 0;

public PufferFishTrait() {
super("pufferfishtrait");
}

@Override
public void run() {
if (npc.isSpawned() && npc.getEntity() instanceof PufferFish) {
PufferFish puffer = (PufferFish) npc.getEntity();
puffer.setPuffState(puffState);
}
public int getPuffState() {
return puffState;
}

public void setPuffState(int state) {
Expand Down
@@ -1,13 +1,10 @@
package net.citizensnpcs.trait.versioned;

import org.bukkit.DyeColor;
import org.bukkit.entity.Shulker;
import org.bukkit.entity.Snowman;

import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
import net.citizensnpcs.util.NMS;

@TraitName("snowmantrait")
public class SnowmanTrait extends Trait {
Expand Down
1 change: 1 addition & 0 deletions main/src/main/java/net/citizensnpcs/util/Messages.java
Expand Up @@ -222,6 +222,7 @@ public class Messages {
public static final String NPC_TELEPORTED = "citizens.commands.npc.tphere.teleported";
public static final String NPCS_REMOVED = "citizens.commands.npc.remove.npcs-removed";
public static final String NUM_LOADED_NOTIFICATION = "citizens.notifications.npcs-loaded";
public static final String OCELOT_TYPE_DEPRECATED = "citizens.commands.npc.ocelot.deprecated";
public static final String OVER_NPC_LIMIT = "citizens.limits.over-npc-limit";
public static final String OWNER_SET = "citizens.commands.npc.owner.set";
public static final String OWNER_SET_SERVER = "citizens.commands.npc.owner.set-server";
Expand Down
1 change: 1 addition & 0 deletions main/src/main/resources/messages_en.properties
Expand Up @@ -115,6 +115,7 @@ citizens.commands.npc.moveto.teleported=[[{0}]] teleported to [[{1}]].
citizens.commands.npc.mushroomcow.invalid-variant=Invalid variant. Valid values are: [[{0}]].
citizens.commands.npc.mushroomcow.variant-set=[[{0}]]''s variant set to [[{1}]].
citizens.commands.npc.nameplate.toggled=Nameplate visibility toggled.
citizens.commands.npc.ocelot.deprecated=Ocelot types don''t work in this version of Minecraft. Use [[/npc type cat]] if you want to change the colour.
citizens.commands.npc.ocelot.invalid-type=Invalid ocelot type. Valid types are: [[{0}]].
citizens.commands.npc.owner.already-owner={0} is already the owner of [[{1}]].
citizens.commands.npc.owner.owner=[[{0}]]''s owner is [[{1}]].
Expand Down
Expand Up @@ -13,6 +13,7 @@
import net.citizensnpcs.nms.v1_13_R2.util.NMSImpl;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.trait.versioned.PufferFishTrait;
import net.citizensnpcs.util.Util;
import net.minecraft.server.v1_13_R2.BlockPosition;
import net.minecraft.server.v1_13_R2.ControllerMove;
Expand Down Expand Up @@ -77,8 +78,9 @@ public void collide(net.minecraft.server.v1_13_R2.Entity entity) {
// this method is called by both the entities involved - cancelling
// it will not stop the NPC from moving.
super.collide(entity);
if (npc != null)
if (npc != null) {
Util.callCollisionEvent(npc, entity.getBukkitEntity());
}
}

@Override
Expand Down Expand Up @@ -173,6 +175,9 @@ public void mobTick() {
super.mobTick();
if (npc != null) {
npc.update();
if (npc.hasTrait(PufferFishTrait.class)) {
setPuffState(npc.getTrait(PufferFishTrait.class).getPuffState());
}
}
}

Expand Down
Expand Up @@ -13,11 +13,14 @@
import net.citizensnpcs.nms.v1_14_R1.util.NMSImpl;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.trait.versioned.PufferFishTrait;
import net.citizensnpcs.util.Util;
import net.minecraft.server.v1_14_R1.BlockPosition;
import net.minecraft.server.v1_14_R1.ControllerMove;
import net.minecraft.server.v1_14_R1.DamageSource;
import net.minecraft.server.v1_14_R1.EntityPose;
import net.minecraft.server.v1_14_R1.EntityPufferFish;
import net.minecraft.server.v1_14_R1.EntitySize;
import net.minecraft.server.v1_14_R1.EntityTypes;
import net.minecraft.server.v1_14_R1.IBlockData;
import net.minecraft.server.v1_14_R1.NBTTagCompound;
Expand Down Expand Up @@ -58,6 +61,14 @@ protected void a(double d0, boolean flag, IBlockData block, BlockPosition blockp
}
}

@Override
public EntitySize a(EntityPose entitypose) {
if (npc == null) {
return super.a(entitypose);
}
return super.a(entitypose).a(1 / s(getPuffState())).a(0.5F);
}

@Override
public void b(float f, float f1) {
if (npc == null || !npc.isFlyable()) {
Expand All @@ -77,8 +88,9 @@ public void collide(net.minecraft.server.v1_14_R1.Entity entity) {
// this method is called by both the entities involved - cancelling
// it will not stop the NPC from moving.
super.collide(entity);
if (npc != null)
if (npc != null) {
Util.callCollisionEvent(npc, entity.getBukkitEntity());
}
}

@Override
Expand Down Expand Up @@ -205,13 +217,23 @@ public void movementTick() {

@Override
public void tick() {
int lastPuffState = getPuffState();
if (npc != null) {
NMSImpl.resetPuffTicks(this);
}
super.tick();
if (npc != null) {
setPuffState(lastPuffState);
if (npc != null && npc.hasTrait(PufferFishTrait.class)) {
setPuffState(npc.getTrait(PufferFishTrait.class).getPuffState());
}
}

private static float s(int i) {
switch (i) {
case 0:
return 0.5F;
case 1:
return 0.7F;
default:
return 1.0F;
}
}
}
Expand Down
Expand Up @@ -1669,6 +1669,7 @@ public static void minecartItemLogic(EntityMinecartAbstract minecart) {
public static void resetPuffTicks(EntityPufferFish fish) {
try {
PUFFERFISH_C.invoke(fish, 0);
PUFFERFISH_D.invoke(fish, 0);
} catch (Throwable e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -1823,6 +1824,7 @@ public static void updateNavigation(NavigationAbstract navigation) {
private static final MethodHandle PLAYER_CHUNK_MAP_VIEW_DISTANCE_SETTER = NMS.getSetter(PlayerChunkMap.class,
"viewDistance");
private static final MethodHandle PUFFERFISH_C = NMS.getSetter(EntityPufferFish.class, "c");
private static final MethodHandle PUFFERFISH_D = NMS.getSetter(EntityPufferFish.class, "d");
private static final MethodHandle RABBIT_FIELD = NMS.getGetter(EntityRabbit.class, "bz");
private static final Random RANDOM = Util.getFastRandom();
private static final MethodHandle SIZE_FIELD_GETTER = NMS.getGetter(Entity.class, "size");
Expand Down
Expand Up @@ -18,6 +18,7 @@
import net.minecraft.server.v1_15_R1.DamageSource;
import net.minecraft.server.v1_15_R1.DataWatcherObject;
import net.minecraft.server.v1_15_R1.EntityOcelot;
import net.minecraft.server.v1_15_R1.EntityPose;
import net.minecraft.server.v1_15_R1.EntityTypes;
import net.minecraft.server.v1_15_R1.IBlockData;
import net.minecraft.server.v1_15_R1.NBTTagCompound;
Expand Down Expand Up @@ -195,7 +196,12 @@ public boolean isLeashed() {

@Override
public void mobTick() {
EntityPose old = this.getPose();
boolean restorePose = !this.getControllerMove().b();
super.mobTick();
if (restorePose) {
this.setPose(old);
}
if (npc != null) {
NMSImpl.updateMinecraftAIState(npc, this);
npc.update();
Expand Down
Expand Up @@ -14,11 +14,14 @@
import net.citizensnpcs.nms.v1_15_R1.util.PlayerControllerMove;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.trait.versioned.PufferFishTrait;
import net.citizensnpcs.util.Util;
import net.minecraft.server.v1_15_R1.BlockPosition;
import net.minecraft.server.v1_15_R1.ControllerMove;
import net.minecraft.server.v1_15_R1.DamageSource;
import net.minecraft.server.v1_15_R1.EntityPose;
import net.minecraft.server.v1_15_R1.EntityPufferFish;
import net.minecraft.server.v1_15_R1.EntitySize;
import net.minecraft.server.v1_15_R1.EntityTypes;
import net.minecraft.server.v1_15_R1.IBlockData;
import net.minecraft.server.v1_15_R1.NBTTagCompound;
Expand Down Expand Up @@ -61,6 +64,14 @@ protected void a(double d0, boolean flag, IBlockData block, BlockPosition blockp
}
}

@Override
public EntitySize a(EntityPose entitypose) {
if (npc == null) {
return super.a(entitypose);
}
return super.a(entitypose).a(1 / s(getPuffState())).a(0.5F);
}

@Override
public boolean b(float f, float f1) {
if (npc == null || !npc.isFlyable()) {
Expand All @@ -81,15 +92,21 @@ public void collide(net.minecraft.server.v1_15_R1.Entity entity) {
// this method is called by both the entities involved - cancelling
// it will not stop the NPC from moving.
super.collide(entity);
if (npc != null)
if (npc != null) {
Util.callCollisionEvent(npc, entity.getBukkitEntity());
}
}

@Override
public boolean d(NBTTagCompound save) {
return npc == null ? super.d(save) : false;
}

@Override
public boolean doAITick() {
return false;
}

@Override
public void e(Vec3D vec3d) {
if (npc == null || !npc.isFlyable()) {
Expand Down Expand Up @@ -216,13 +233,23 @@ public void movementTick() {

@Override
public void tick() {
int lastPuffState = getPuffState();
if (npc != null) {
NMSImpl.resetPuffTicks(this);
}
super.tick();
if (npc != null) {
setPuffState(lastPuffState);
if (npc != null && npc.hasTrait(PufferFishTrait.class)) {
setPuffState(npc.getTrait(PufferFishTrait.class).getPuffState());
}
}

private static float s(int i) {
switch (i) {
case 0:
return 0.5F;
case 1:
return 0.7F;
default:
return 1.0F;
}
}
}
Expand Down

0 comments on commit 3cf8c7c

Please sign in to comment.