Add enderman equipment GUI

This commit is contained in:
fullwall 2021-01-31 23:40:42 +08:00
parent f1c2fcbe37
commit f776638508
4 changed files with 86 additions and 113 deletions

View File

@ -1,67 +0,0 @@
package net.citizensnpcs.editor;
import org.bukkit.Material;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Enderman;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.MaterialData;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.trait.trait.Equipment;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.api.util.SpigotUtil;
import net.citizensnpcs.util.Messages;
public class EndermanEquipper implements Equipper {
@Override
public void equip(Player equipper, NPC npc) {
ItemStack hand = equipper.getInventory().getItemInHand();
if (!hand.getType().isBlock()) {
Messaging.sendErrorTr(equipper, Messages.EQUIPMENT_EDITOR_INVALID_BLOCK);
return;
}
if (SpigotUtil.isUsing1_13API()) {
BlockData carried = ((Enderman) npc.getEntity()).getCarriedBlock();
if (carried == null || carried.getMaterial() == Material.AIR) {
if (hand.getType() == Material.AIR) {
Messaging.sendErrorTr(equipper, Messages.EQUIPMENT_EDITOR_INVALID_BLOCK);
return;
}
} else {
equipper.getWorld().dropItemNaturally(npc.getEntity().getLocation(),
new ItemStack(carried.getMaterial(), 1));
((Enderman) npc.getEntity()).setCarriedBlock(hand.getType().createBlockData());
// TODO: copy block data info from itemstack?
}
ItemStack set = hand.clone();
if (set.getType() != Material.AIR) {
set.setAmount(1);
hand.setAmount(hand.getAmount() - 1);
equipper.getInventory().setItemInHand(hand);
}
npc.getOrAddTrait(Equipment.class).set(0, set);
} else {
MaterialData carried = ((Enderman) npc.getEntity()).getCarriedMaterial();
if (carried.getItemType() == Material.AIR) {
if (hand.getType() == Material.AIR) {
Messaging.sendErrorTr(equipper, Messages.EQUIPMENT_EDITOR_INVALID_BLOCK);
return;
}
} else {
equipper.getWorld().dropItemNaturally(npc.getEntity().getLocation(), carried.toItemStack(1));
((Enderman) npc.getEntity()).setCarriedMaterial(hand.getData());
}
ItemStack set = hand.clone();
if (set.getType() != Material.AIR) {
set.setAmount(1);
hand.setAmount(hand.getAmount() - 1);
equipper.getInventory().setItemInHand(hand);
}
npc.getOrAddTrait(Equipment.class).set(0, set);
}
}
}

View File

@ -0,0 +1,84 @@
package net.citizensnpcs.editor;
import org.bukkit.Material;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Enderman;
import org.bukkit.event.Event.Result;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.MaterialData;
import net.citizensnpcs.api.gui.CitizensInventoryClickEvent;
import net.citizensnpcs.api.gui.ClickHandler;
import net.citizensnpcs.api.gui.InjectContext;
import net.citizensnpcs.api.gui.InventoryMenuPage;
import net.citizensnpcs.api.gui.InventoryMenuSlot;
import net.citizensnpcs.api.gui.Menu;
import net.citizensnpcs.api.gui.MenuContext;
import net.citizensnpcs.api.gui.MenuPattern;
import net.citizensnpcs.api.gui.MenuSlot;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.SpigotUtil;
@Menu(title = "NPC Equipment", type = InventoryType.HOPPER, dimensions = { 0, 5 })
@MenuSlot(
slot = { 0, 0 },
material = Material.ENDER_PEARL,
amount = 1,
lore = "Place a block to hold here ->",
filter = InventoryAction.UNKNOWN)
@MenuPattern(
offset = { 0, 2 },
slots = { @MenuSlot(
filter = InventoryAction.UNKNOWN,
pat = 'x',
material = Material.GLASS_PANE,
title = "<4>Unused") },
value = "xxx")
public class EndermanEquipperGUI extends InventoryMenuPage {
@MenuSlot(slot = { 0, 1 })
private InventoryMenuSlot hand;
@InjectContext
private NPC npc;
@SuppressWarnings("deprecation")
private Material getCarriedMaterial() {
if (SpigotUtil.isUsing1_13API()) {
BlockData carried = ((Enderman) npc.getEntity()).getCarriedBlock();
return carried == null ? null : carried.getMaterial();
} else {
MaterialData carried = ((Enderman) npc.getEntity()).getCarriedMaterial();
return carried == null ? null : carried.getItemType();
}
}
@Override
public void initialise(MenuContext ctx) {
Material mat = getCarriedMaterial();
if (mat == null)
return;
hand.setItemStack(new ItemStack(mat, 1));
}
@ClickHandler(slot = { 0, 1 }, filter = { InventoryAction.PICKUP_ALL, InventoryAction.PLACE_ALL })
public void setHand(InventoryMenuSlot slot, CitizensInventoryClickEvent event) {
if (event.getAction() == InventoryAction.PICKUP_ALL && event.getCursor() != null) {
event.setResult(Result.DENY);
return;
}
if (event.getAction() == InventoryAction.PLACE_ALL && (event.getCurrentItem() != null
|| !event.getCursor().getType().isBlock() || event.getCursorNonNull().getAmount() > 1)) {
event.setResult(Result.DENY);
return;
}
if (SpigotUtil.isUsing1_13API()) {
((Enderman) npc.getEntity()).setCarriedBlock(
event.getAction() == InventoryAction.PLACE_ALL ? event.getResultItem().getType().createBlockData()
: null);
} else {
((Enderman) npc.getEntity()).setCarriedMaterial(
event.getAction() == InventoryAction.PLACE_ALL ? event.getResultItem().getData() : null);
}
}
}

View File

@ -2,8 +2,6 @@ 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;
@ -11,10 +9,8 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.block.Action;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
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;
@ -22,8 +18,6 @@ import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.gui.InventoryMenu;
import net.citizensnpcs.api.gui.InventoryMenuPage;
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;
import net.citizensnpcs.util.Util;
@ -76,43 +70,6 @@ public class EquipmentEditor extends Editor {
}
}
@EventHandler(ignoreCancelled = true)
public void onPlayerChat(final AsyncPlayerChatEvent event) {
if (!event.getPlayer().equals(player))
return;
EquipmentSlot slot = Util.matchEnum(EquipmentSlot.values(), event.getMessage());
if (slot == null) {
return;
}
if (!event.getPlayer().hasPermission("citizens.npc.edit.equip." + slot.name().toLowerCase().replace(" ", ""))
&& (slot != EquipmentSlot.HELMET
|| !event.getPlayer().hasPermission("citizens.npc.edit.equip.any-helmet"))) {
return;
}
final EquipmentSlot finalSlot = slot;
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {
if (!event.getPlayer().isValid())
return;
ItemStack hand = event.getPlayer().getInventory().getItemInHand();
if (hand.getType() == Material.AIR || hand.getAmount() <= 0) {
return;
}
ItemStack old = npc.getOrAddTrait(Equipment.class).get(finalSlot);
if (old != null && old.getType() != Material.AIR) {
event.getPlayer().getWorld().dropItemNaturally(event.getPlayer().getLocation(), old);
}
ItemStack newStack = hand.clone();
newStack.setAmount(1);
npc.getOrAddTrait(Equipment.class).set(finalSlot, newStack);
hand.setAmount(hand.getAmount() - 1);
event.getPlayer().getInventory().setItemInHand(hand);
}
});
event.setCancelled(true);
}
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
if (event.getAction() == Action.RIGHT_CLICK_AIR && event.getPlayer().equals(player)) {
@ -136,8 +93,8 @@ public class EquipmentEditor extends Editor {
static {
EQUIPPER_GUIS.put(EntityType.PIG, PigEquipperGUI.class);
EQUIPPER_GUIS.put(EntityType.ENDERMAN, EndermanEquipperGUI.class);
EQUIPPERS.put(EntityType.SHEEP, new SheepEquipper());
EQUIPPERS.put(EntityType.ENDERMAN, new EndermanEquipper());
EQUIPPERS.put(EntityType.HORSE, new HorseEquipper());
for (EntityType type : Util.optionalEntitySet("ZOMBIE_HORSE", "LLAMA", "TRADER_LLAMA", "DONKEY", "MULE",
"SKELETON_HORSE")) {

View File

@ -9,7 +9,6 @@ import org.bukkit.material.Dye;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.api.util.SpigotUtil;
import net.citizensnpcs.trait.SheepTrait;
import net.citizensnpcs.trait.WoolColor;
import net.citizensnpcs.util.Messages;
@ -22,7 +21,7 @@ public class SheepEquipper implements Equipper {
if (hand.getType() == Material.SHEARS) {
Messaging.sendTr(equipper, toEquip.getOrAddTrait(SheepTrait.class).toggleSheared() ? Messages.SHEARED_SET
: Messages.SHEARED_STOPPED, toEquip.getName());
} else if (hand.getType() == (SpigotUtil.isUsing1_13API() ? Material.INK_SAC : Material.valueOf("INK_SACK"))) {
} else if (hand.getType() != null && hand.getType().name().contains("INK_SAC")) {
Dye dye = (Dye) hand.getData();
if (sheep.getColor() == dye.getColor())
return;