Add command to set any block as an NPC helmet

This commit is contained in:
fullwall 2016-01-25 22:59:45 +08:00
parent 4613839a8c
commit 3ca1e03efe
3 changed files with 43 additions and 13 deletions

View File

@ -4,20 +4,18 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.util.Messages;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.util.Messages;
public abstract class Editor implements Listener {
public abstract void begin();
public abstract void end();
private static final Map<String, Editor> EDITING = new HashMap<String, Editor>();
private static void enter(Player player, Editor editor) {
editor.begin();
player.getServer().getPluginManager().registerEvents(editor,
@ -29,13 +27,14 @@ public abstract class Editor implements Listener {
if (editor == null)
return;
Editor edit = EDITING.get(player.getName());
if (edit == null)
if (edit == null) {
enter(player, editor);
else if (edit.getClass() == editor.getClass())
} else if (edit.getClass() == editor.getClass()) {
leave(player);
else
} else {
Messaging.sendErrorTr(player, Messages.ALREADY_IN_EDITOR);
}
}
public static boolean hasEditor(Player player) {
return EDITING.containsKey(player.getName());
@ -56,4 +55,6 @@ public abstract class Editor implements Listener {
}
EDITING.clear();
}
private static final Map<String, Editor> EDITING = new HashMap<String, Editor>();
}

View File

@ -2,18 +2,24 @@ package net.citizensnpcs.editor;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.Event.Result;
import org.bukkit.event.EventHandler;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import com.google.common.collect.Maps;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.trait.trait.Equipment;
import net.citizensnpcs.api.trait.trait.Equipment.EquipmentSlot;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.util.Messages;
@ -36,6 +42,28 @@ public class EquipmentEditor extends Editor {
Messaging.sendTr(player, Messages.EQUIPMENT_EDITOR_END);
}
@EventHandler(ignoreCancelled = true)
public void onPlayerChat(final AsyncPlayerChatEvent event) {
if (!event.getMessage().equals("helmet")
|| !event.getPlayer().hasPermission("citizens.npc.edit.equip.any-helmet"))
return;
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {
if (!event.getPlayer().isValid())
return;
ItemStack hand = event.getPlayer().getItemInHand();
if (hand.getType() == Material.AIR || hand.getAmount() <= 0) {
return;
}
npc.getTrait(Equipment.class).set(EquipmentSlot.HELMET,
new ItemStack(event.getPlayer().getItemInHand().getType(), 1));
hand.setAmount(hand.getAmount() - 1);
event.getPlayer().setItemInHand(hand);
}
});
}
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
if (event.getAction() == Action.RIGHT_CLICK_AIR && Editor.hasEditor(event.getPlayer())) {

View File

@ -6,6 +6,7 @@ import org.bukkit.inventory.ItemStack;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.trait.trait.Equipment;
import net.citizensnpcs.api.trait.trait.Equipment.EquipmentSlot;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.util.Messages;
@ -14,7 +15,7 @@ public class GenericEquipper implements Equipper {
public void equip(Player equipper, NPC toEquip) {
ItemStack hand = equipper.getItemInHand();
Equipment trait = toEquip.getTrait(Equipment.class);
int slot = 0;
EquipmentSlot slot = EquipmentSlot.HAND;
Material type = hand == null ? Material.AIR : hand.getType();
// First, determine the slot to edit
switch (type) {
@ -27,7 +28,7 @@ public class GenericEquipper implements Equipper {
case IRON_HELMET:
case DIAMOND_HELMET:
if (!equipper.isSneaking())
slot = 1;
slot = EquipmentSlot.HELMET;
break;
case LEATHER_CHESTPLATE:
case CHAINMAIL_CHESTPLATE:
@ -35,7 +36,7 @@ public class GenericEquipper implements Equipper {
case IRON_CHESTPLATE:
case DIAMOND_CHESTPLATE:
if (!equipper.isSneaking())
slot = 2;
slot = EquipmentSlot.CHESTPLATE;
break;
case LEATHER_LEGGINGS:
case CHAINMAIL_LEGGINGS:
@ -43,7 +44,7 @@ public class GenericEquipper implements Equipper {
case IRON_LEGGINGS:
case DIAMOND_LEGGINGS:
if (!equipper.isSneaking())
slot = 3;
slot = EquipmentSlot.LEGGINGS;
break;
case LEATHER_BOOTS:
case CHAINMAIL_BOOTS:
@ -51,7 +52,7 @@ public class GenericEquipper implements Equipper {
case IRON_BOOTS:
case DIAMOND_BOOTS:
if (!equipper.isSneaking())
slot = 4;
slot = EquipmentSlot.BOOTS;
break;
case AIR:
for (int i = 0; i < 5; i++) {