Allow /npc horse -c | -b to be used on chestedhorse entities

This commit is contained in:
fullwall 2019-05-24 18:32:51 +08:00
parent d9afd7ebdf
commit 04daf1c053
5 changed files with 55 additions and 19 deletions

View File

@ -476,9 +476,7 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
public void storeNPCs() {
if (saves == null)
return;
for (NPC npc : npcRegistry) {
saves.store(npc);
}
saves.storeAll(npcRegistry);
}
public void storeNPCs(CommandContext args) {

View File

@ -6,6 +6,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import org.bukkit.Bukkit;
@ -615,16 +616,21 @@ public class NPCCommands {
@Command(
aliases = { "npc" },
usage = "horse (--color color) (--type type) (--style style) (-cb)",
desc = "Sets horse modifiers",
help = "Use the -c flag to make the horse have a chest, or the -b flag to stop them from having a chest.",
modifiers = { "horse" },
usage = "horse|llama|donkey|mule (--color color) (--type type) (--style style) (-cb)",
desc = "Sets horse and horse-like entity modifiers",
help = "Use the -c flag to make the NPC have a chest, or the -b flag to stop them from having a chest.",
modifiers = { "horse", "llama", "donkey", "mule" },
min = 1,
max = 1,
flags = "cb",
permission = "citizens.npc.horse")
@Requirements(selected = true, ownership = true, types = EntityType.HORSE)
@Requirements(selected = true, ownership = true)
public void horse(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
Set<EntityType> allowedTypes = Util.optionalEntitySet("HORSE", "LLAMA", "DONKEY", "MULE", "TRADER_LLAMA");
EntityType type = npc.getTrait(MobType.class).getType();
if (!allowedTypes.contains(type)) {
throw new CommandException(CommandMessages.REQUIREMENTS_INVALID_MOB_TYPE, Util.prettyEnum(type));
}
HorseModifiers horse = npc.getTrait(HorseModifiers.class);
String output = "";
if (args.hasFlag('c')) {
@ -634,7 +640,8 @@ public class NPCCommands {
horse.setCarryingChest(false);
output += Messaging.tr(Messages.HORSE_CHEST_UNSET) + " ";
}
if (args.hasValueFlag("color") || args.hasValueFlag("colour")) {
if (type == EntityType.HORSE && (args.hasValueFlag("color") || args.hasValueFlag("colour"))) {
String colorRaw = args.getFlag("color", args.getFlag("colour"));
Color color = Util.matchEnum(Color.values(), colorRaw);
if (color == null) {
@ -644,7 +651,7 @@ public class NPCCommands {
horse.setColor(color);
output += Messaging.tr(Messages.HORSE_COLOR_SET, Util.prettyEnum(color));
}
if (args.hasValueFlag("style")) {
if (type == EntityType.HORSE && args.hasValueFlag("style")) {
Style style = Util.matchEnum(Style.values(), args.getFlag("style"));
if (style == null) {
String valid = Util.listValuesPretty(Style.values());
@ -654,8 +661,8 @@ public class NPCCommands {
output += Messaging.tr(Messages.HORSE_STYLE_SET, Util.prettyEnum(style));
}
if (output.isEmpty()) {
Messaging.sendTr(sender, Messages.HORSE_DESCRIBE, Util.prettyEnum(horse.getColor()),
Util.prettyEnum(horse.getNPC().getEntity().getType()), Util.prettyEnum(horse.getStyle()));
Messaging.sendTr(sender, Messages.HORSE_DESCRIBE, Util.prettyEnum(horse.getColor()), Util.prettyEnum(type),
Util.prettyEnum(horse.getStyle()));
} else {
sender.sendMessage(output);
}

View File

@ -107,13 +107,9 @@ public class EquipmentEditor extends Editor {
EQUIPPERS.put(EntityType.SHEEP, new SheepEquipper());
EQUIPPERS.put(EntityType.ENDERMAN, new EndermanEquipper());
EQUIPPERS.put(EntityType.HORSE, new HorseEquipper());
try {
EQUIPPERS.put(EntityType.valueOf("ZOMBIE_HORSE"), new HorseEquipper());
EQUIPPERS.put(EntityType.valueOf("LLAMA"), new HorseEquipper());
EQUIPPERS.put(EntityType.valueOf("DONKEY"), new HorseEquipper());
EQUIPPERS.put(EntityType.valueOf("MULE"), new HorseEquipper());
EQUIPPERS.put(EntityType.valueOf("SKELETON_HORSE"), new HorseEquipper());
} catch (IllegalArgumentException ex) {
for (EntityType type : Util.optionalEntitySet("ZOMBIE_HORSE", "LLAMA", "TRADER_LLAMA", "DONKEY", "MULE",
"SKELETON_HORSE")) {
EQUIPPERS.put(type, new HorseEquipper());
}
}
}

View File

@ -1,5 +1,8 @@
package net.citizensnpcs.trait;
import java.lang.invoke.MethodHandle;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Horse;
import org.bukkit.entity.Horse.Color;
import org.bukkit.entity.Horse.Style;
@ -8,6 +11,7 @@ import org.bukkit.inventory.ItemStack;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
import net.citizensnpcs.util.NMS;
/**
* Persists various {@link Horse} metadata.
@ -107,5 +111,23 @@ public class HorseModifiers extends Trait {
horse.getInventory().setArmor(armor);
horse.getInventory().setSaddle(saddle);
}
EntityType type = npc.getEntity().getType();
if (type.name().equals("LLAMA") || type.name().equals("TRADER_LLAMA") || type.name().equals("DONKEY")
|| type.name().equals("MULE")) {
try {
CARRYING_CHEST_METHOD.invoke(npc.getEntity(), carryingChest);
} catch (Throwable e) {
}
}
}
private static MethodHandle CARRYING_CHEST_METHOD;
static {
try {
CARRYING_CHEST_METHOD = NMS.getMethodHandle(Class.forName("org.bukkit.entity.ChestedHorse"),
"setCarryingChest", false, boolean.class);
} catch (Throwable e) {
}
}
}

View File

@ -1,6 +1,8 @@
package net.citizensnpcs.util;
import java.util.EnumSet;
import java.util.Random;
import java.util.Set;
import java.util.regex.Pattern;
import org.bukkit.Bukkit;
@ -200,6 +202,17 @@ public class Util {
return false;
}
public static Set<EntityType> optionalEntitySet(String... types) {
Set<EntityType> list = EnumSet.noneOf(EntityType.class);
for (String type : types) {
try {
list.add(EntityType.valueOf(type));
} catch (IllegalArgumentException e) {
}
}
return list;
}
public static String prettyEnum(Enum<?> e) {
return e.name().toLowerCase().replace('_', ' ');
}