2022-05-11 13:30:13 -05:00
|
|
|
package noppes.npcs;
|
|
|
|
|
|
|
|
|
|
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
|
|
|
|
import cpw.mods.fml.common.gameevent.PlayerEvent;
|
2025-05-20 13:59:51 -04:00
|
|
|
import net.minecraft.block.Block;
|
2022-05-11 13:30:13 -05:00
|
|
|
import net.minecraft.entity.item.EntityItem;
|
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
|
import net.minecraft.item.ItemStack;
|
2025-05-20 13:51:26 -04:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2022-05-11 13:30:13 -05:00
|
|
|
import net.minecraft.world.WorldServer;
|
2025-05-20 13:51:26 -04:00
|
|
|
import net.minecraftforge.common.util.FakePlayer;
|
2022-05-11 13:30:13 -05:00
|
|
|
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
|
|
|
|
|
import net.minecraftforge.event.entity.item.ItemTossEvent;
|
|
|
|
|
import net.minecraftforge.event.entity.living.LivingEvent;
|
2025-07-23 14:22:52 -04:00
|
|
|
import net.minecraftforge.event.entity.player.AnvilRepairEvent;
|
|
|
|
|
import net.minecraftforge.event.entity.player.EntityInteractEvent;
|
|
|
|
|
import net.minecraftforge.event.entity.player.PlayerDestroyItemEvent;
|
|
|
|
|
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
|
|
|
|
|
import net.minecraftforge.event.entity.player.PlayerUseItemEvent;
|
2024-02-21 00:31:17 -05:00
|
|
|
import noppes.npcs.api.IWorld;
|
2023-01-12 21:33:06 -05:00
|
|
|
import noppes.npcs.api.entity.IPlayer;
|
2025-02-17 16:02:56 -05:00
|
|
|
import noppes.npcs.api.item.IItemCustomizable;
|
2022-11-15 15:25:53 -05:00
|
|
|
import noppes.npcs.api.item.IItemStack;
|
2025-05-20 13:59:51 -04:00
|
|
|
import noppes.npcs.blocks.BlockBanner;
|
|
|
|
|
import noppes.npcs.blocks.BlockTallLamp;
|
2025-05-20 13:51:26 -04:00
|
|
|
import noppes.npcs.blocks.tiles.TileColorable;
|
2024-02-21 00:31:17 -05:00
|
|
|
import noppes.npcs.controllers.PlayerDataController;
|
|
|
|
|
import noppes.npcs.controllers.data.PlayerData;
|
2023-01-12 21:33:06 -05:00
|
|
|
import noppes.npcs.entity.EntityCustomNpc;
|
2025-05-20 13:51:26 -04:00
|
|
|
import noppes.npcs.items.ItemNpcTool;
|
2022-05-11 22:08:31 -05:00
|
|
|
import noppes.npcs.scripted.NpcAPI;
|
2023-01-12 21:33:06 -05:00
|
|
|
import noppes.npcs.scripted.event.ItemEvent;
|
2022-05-11 17:12:48 -05:00
|
|
|
|
2022-05-11 13:30:13 -05:00
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
|
|
|
public class ScriptItemEventHandler {
|
2025-03-13 17:14:45 -04:00
|
|
|
public ScriptItemEventHandler() {
|
|
|
|
|
}
|
2022-05-11 13:30:13 -05:00
|
|
|
|
|
|
|
|
@SubscribeEvent
|
2025-05-20 13:51:26 -04:00
|
|
|
public void onLivingUpdate(LivingEvent.LivingUpdateEvent event) {
|
2025-03-13 17:14:45 -04:00
|
|
|
if (event.entityLiving == null || event.entityLiving.worldObj == null || event.entityLiving instanceof EntityPlayer || event.entityLiving.ticksExisted % 10 != 0)
|
2022-05-11 13:30:13 -05:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (event.entityLiving instanceof EntityCustomNpc) {
|
|
|
|
|
HashMap<Integer, ItemStack> armor = ((EntityCustomNpc) event.entityLiving).inventory.armor;
|
|
|
|
|
HashMap<Integer, ItemStack> weapons = ((EntityCustomNpc) event.entityLiving).inventory.weapons;
|
2023-06-02 23:21:14 -05:00
|
|
|
HashMap<Integer, ItemStack>[] inventories = new HashMap[]{armor, weapons};
|
2022-05-11 13:30:13 -05:00
|
|
|
|
|
|
|
|
for (HashMap<Integer, ItemStack> inventory : inventories) {
|
|
|
|
|
for (ItemStack stack : inventory.values()) {
|
2025-02-17 16:02:56 -05:00
|
|
|
if (stack != null && NoppesUtilServer.isScriptableItem(stack.getItem())) {
|
2022-05-11 13:30:13 -05:00
|
|
|
IItemStack istack = NpcAPI.Instance().getIItemStack(stack);
|
2025-02-17 16:02:56 -05:00
|
|
|
EventHooks.onScriptItemUpdate((IItemCustomizable) istack, event.entityLiving);
|
2022-05-11 13:30:13 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
} else if (event.entityLiving.getHeldItem() != null && NoppesUtilServer.isScriptableItem(event.entityLiving.getHeldItem().getItem()) && !event.isCanceled()) {
|
2022-05-11 13:30:13 -05:00
|
|
|
IItemStack itemStack = NpcAPI.Instance().getIItemStack(event.entityLiving.getHeldItem());
|
2025-02-17 16:02:56 -05:00
|
|
|
EventHooks.onScriptItemUpdate((IItemCustomizable) itemStack, event.entityLiving);
|
2022-05-11 13:30:13 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@SubscribeEvent
|
2025-05-20 13:51:26 -04:00
|
|
|
public void onItemToss(ItemTossEvent e) {
|
|
|
|
|
if (!isValidContext(e.player) || e.isCanceled())
|
2022-05-11 13:30:13 -05:00
|
|
|
return;
|
|
|
|
|
|
2025-05-20 13:53:26 -04:00
|
|
|
if (e.entityItem == null)
|
2025-05-20 13:51:26 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
EntityItem entityItem = e.entityItem;
|
|
|
|
|
IItemCustomizable c = getCustomizable(entityItem.getEntityItem());
|
|
|
|
|
if (c != null) {
|
|
|
|
|
e.setCanceled(EventHooks.onScriptItemTossed(c, e.player, entityItem));
|
2022-05-11 13:30:13 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-20 13:51:26 -04:00
|
|
|
|
2022-05-11 13:30:13 -05:00
|
|
|
@SubscribeEvent
|
2025-05-20 13:51:26 -04:00
|
|
|
public void onItemPickup(PlayerEvent.ItemPickupEvent e) {
|
|
|
|
|
if (!isValidContext(e.player) || e.isCanceled())
|
2022-05-11 13:30:13 -05:00
|
|
|
return;
|
|
|
|
|
|
2025-05-20 13:51:26 -04:00
|
|
|
IItemCustomizable c = getCustomizable(e.player.getHeldItem());
|
|
|
|
|
if (c != null) {
|
|
|
|
|
EventHooks.onScriptItemPickedUp(c, e.player);
|
2022-05-11 13:30:13 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@SubscribeEvent
|
2025-05-20 13:51:26 -04:00
|
|
|
public void onEntityJoinWorld(EntityJoinWorldEvent e) {
|
|
|
|
|
if (e.world.isRemote || !(e.entity instanceof EntityItem))
|
|
|
|
|
return;
|
|
|
|
|
|
2025-05-20 13:53:26 -04:00
|
|
|
EntityItem itemEnt = (EntityItem) e.entity;
|
2025-05-20 13:51:26 -04:00
|
|
|
IItemCustomizable c = getCustomizable(itemEnt.getEntityItem());
|
|
|
|
|
if (c != null) {
|
|
|
|
|
e.setCanceled(EventHooks.onScriptItemSpawn(c, itemEnt));
|
2022-05-11 13:30:13 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@SubscribeEvent
|
2025-05-20 13:51:26 -04:00
|
|
|
public void onEntityInteract(EntityInteractEvent e) {
|
|
|
|
|
EntityPlayer player = e.entityPlayer;
|
|
|
|
|
if (!isValidContext(player) || e.isCanceled())
|
2022-05-11 13:30:13 -05:00
|
|
|
return;
|
|
|
|
|
|
2025-05-20 13:51:26 -04:00
|
|
|
IItemCustomizable c = getCustomizable(player.getHeldItem());
|
|
|
|
|
IPlayer ip = NoppesUtilServer.getIPlayer(player);
|
|
|
|
|
if (c != null && ip != null) {
|
|
|
|
|
boolean cancelA = EventHooks.onScriptItemInteract(
|
|
|
|
|
c, new ItemEvent.InteractEvent(c, ip, 2, NpcAPI.Instance().getIEntity(e.target))
|
|
|
|
|
);
|
|
|
|
|
boolean cancelB = EventHooks.onScriptItemRightClick(
|
|
|
|
|
c, new ItemEvent.RightClickEvent(c, ip, 1, NpcAPI.Instance().getIEntity(e.target))
|
|
|
|
|
);
|
|
|
|
|
if (cancelA || cancelB) {
|
|
|
|
|
e.setCanceled(true);
|
2025-03-13 17:14:45 -04:00
|
|
|
}
|
2024-02-21 00:31:17 -05:00
|
|
|
}
|
|
|
|
|
}
|
2025-05-20 13:53:26 -04:00
|
|
|
|
2024-02-21 00:31:17 -05:00
|
|
|
@SubscribeEvent
|
2025-05-20 13:51:26 -04:00
|
|
|
public void onPlayerInteract(PlayerInteractEvent e) {
|
|
|
|
|
EntityPlayer player = e.entityPlayer;
|
|
|
|
|
if (player == null || e.action == null) return;
|
2024-02-21 00:31:17 -05:00
|
|
|
|
2025-05-20 13:53:26 -04:00
|
|
|
if (!isValidContext(e.entityPlayer))
|
2024-02-21 00:31:17 -05:00
|
|
|
return;
|
|
|
|
|
|
2025-05-20 13:51:26 -04:00
|
|
|
// Paintbrush on LEFT_CLICK_BLOCK
|
|
|
|
|
if (e.action == PlayerInteractEvent.Action.LEFT_CLICK_BLOCK) {
|
|
|
|
|
ItemStack held = player.getHeldItem();
|
|
|
|
|
if (ItemNpcTool.isPaintbrush(held)
|
|
|
|
|
&& CustomNpcsPermissions.hasPermission(player, CustomNpcsPermissions.NPC_BUILD)) {
|
|
|
|
|
|
2025-05-20 13:59:51 -04:00
|
|
|
int x = e.x;
|
|
|
|
|
int y = e.y;
|
|
|
|
|
int z = e.z;
|
|
|
|
|
|
|
|
|
|
Block block = player.worldObj.getBlock(x, y, z);
|
|
|
|
|
if (block instanceof BlockTallLamp || block instanceof BlockBanner) {
|
|
|
|
|
int meta = player.worldObj.getBlockMetadata(x, y, z);
|
|
|
|
|
if (meta >= 7)
|
|
|
|
|
y--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TileEntity tile = player.worldObj.getTileEntity(x, y, z);
|
|
|
|
|
if (tile instanceof TileColorable) {
|
|
|
|
|
int color = ((TileColorable) tile).color;
|
2025-05-20 13:51:26 -04:00
|
|
|
if (!held.hasTagCompound()) {
|
|
|
|
|
held.setTagCompound(new NBTTagCompound());
|
2024-02-28 20:58:00 -05:00
|
|
|
}
|
2025-05-20 13:59:51 -04:00
|
|
|
|
2025-05-20 13:51:26 -04:00
|
|
|
ItemNpcTool.setColor(held.getTagCompound(), color);
|
|
|
|
|
e.setCanceled(true);
|
2025-06-02 14:56:29 -04:00
|
|
|
return;
|
2025-03-13 17:14:45 -04:00
|
|
|
}
|
2024-02-28 20:58:00 -05:00
|
|
|
}
|
2022-05-11 13:30:13 -05:00
|
|
|
}
|
2022-05-11 14:24:12 -05:00
|
|
|
|
2025-05-20 13:51:26 -04:00
|
|
|
// Scriptable-item RIGHT_CLICK hooks
|
|
|
|
|
if (PlayerDataController.Instance == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
PlayerData pd = PlayerData.get(player);
|
|
|
|
|
if (pd == null) {
|
2022-05-11 14:24:12 -05:00
|
|
|
return;
|
2025-05-20 13:51:26 -04:00
|
|
|
}
|
2022-05-11 14:24:12 -05:00
|
|
|
|
2025-05-20 13:51:26 -04:00
|
|
|
ItemStack held = player.getHeldItem();
|
|
|
|
|
IItemCustomizable c = getCustomizable(held);
|
|
|
|
|
IPlayer ip = NoppesUtilServer.getIPlayer(player);
|
|
|
|
|
if (c == null || ip == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-02-09 01:09:23 -05:00
|
|
|
|
2025-05-20 13:51:26 -04:00
|
|
|
if (e.action == PlayerInteractEvent.Action.RIGHT_CLICK_AIR) {
|
|
|
|
|
if (pd.hadInteract) {
|
|
|
|
|
pd.hadInteract = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (EventHooks.onScriptItemRightClick(
|
|
|
|
|
c, new ItemEvent.RightClickEvent(c, ip, 0, null))) {
|
|
|
|
|
e.setCanceled(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else if (e.action == PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK) {
|
|
|
|
|
pd.hadInteract = true;
|
2025-05-20 13:53:26 -04:00
|
|
|
IWorld iw = (IWorld) NpcAPI.Instance().getIWorld(e.world);
|
2025-05-20 13:51:26 -04:00
|
|
|
Object blockCtx = NpcAPI.Instance().getIBlock(iw, e.x, e.y, e.z);
|
|
|
|
|
if (EventHooks.onScriptItemRightClick(
|
|
|
|
|
c, new ItemEvent.RightClickEvent(c, ip, 2, blockCtx))) {
|
|
|
|
|
e.setCanceled(true);
|
2025-02-09 01:09:23 -05:00
|
|
|
}
|
2022-05-11 14:24:12 -05:00
|
|
|
}
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2022-05-11 14:24:12 -05:00
|
|
|
@SubscribeEvent
|
2025-05-20 13:51:26 -04:00
|
|
|
public void onStartUseItem(PlayerUseItemEvent.Start e) {
|
2025-05-20 13:53:26 -04:00
|
|
|
if (!isValidContext(e.entityPlayer))
|
2022-05-11 14:24:12 -05:00
|
|
|
return;
|
|
|
|
|
|
2025-05-20 13:51:26 -04:00
|
|
|
IItemCustomizable c = getCustomizable(e.item);
|
|
|
|
|
IPlayer ip = NoppesUtilServer.getIPlayer(e.entityPlayer);
|
|
|
|
|
if (c != null && ip != null && EventHooks.onStartUsingCustomItem(c, ip, e.duration)) {
|
|
|
|
|
e.setCanceled(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@SubscribeEvent
|
|
|
|
|
public void onUseItemTick(PlayerUseItemEvent.Tick e) {
|
2025-05-20 13:53:26 -04:00
|
|
|
if (!isValidContext(e.entityPlayer))
|
2025-05-20 13:51:26 -04:00
|
|
|
return;
|
2025-02-09 01:09:23 -05:00
|
|
|
|
2025-05-20 13:51:26 -04:00
|
|
|
IItemCustomizable c = getCustomizable(e.item);
|
|
|
|
|
IPlayer ip = NoppesUtilServer.getIPlayer(e.entityPlayer);
|
|
|
|
|
if (c != null && ip != null && EventHooks.onUsingCustomItem(c, ip, e.duration)) {
|
|
|
|
|
e.setCanceled(true);
|
2022-05-11 14:24:12 -05:00
|
|
|
}
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2022-05-11 14:24:12 -05:00
|
|
|
@SubscribeEvent
|
2025-05-20 13:51:26 -04:00
|
|
|
public void onStopUseItem(PlayerUseItemEvent.Stop e) {
|
2025-05-20 13:53:26 -04:00
|
|
|
if (!isValidContext(e.entityPlayer))
|
2022-05-11 14:24:12 -05:00
|
|
|
return;
|
|
|
|
|
|
2025-05-20 13:51:26 -04:00
|
|
|
IItemCustomizable c = getCustomizable(e.item);
|
|
|
|
|
IPlayer ip = NoppesUtilServer.getIPlayer(e.entityPlayer);
|
|
|
|
|
if (c != null && ip != null
|
|
|
|
|
&& EventHooks.onStopUsingCustomItem(c, ip, e.duration)) {
|
|
|
|
|
e.setCanceled(true);
|
2022-05-11 14:24:12 -05:00
|
|
|
}
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2022-05-11 14:24:12 -05:00
|
|
|
@SubscribeEvent
|
2025-05-20 13:51:26 -04:00
|
|
|
public void onFinishUseItem(PlayerUseItemEvent.Finish e) {
|
2025-05-20 13:53:26 -04:00
|
|
|
if (!isValidContext(e.entityPlayer))
|
2022-05-11 14:24:12 -05:00
|
|
|
return;
|
|
|
|
|
|
2025-05-20 13:51:26 -04:00
|
|
|
IItemCustomizable c = getCustomizable(e.item);
|
|
|
|
|
IPlayer ip = NoppesUtilServer.getIPlayer(e.entityPlayer);
|
|
|
|
|
if (c != null && ip != null) {
|
|
|
|
|
EventHooks.onFinishUsingCustomItem(c, ip, e.duration);
|
2022-05-11 14:24:12 -05:00
|
|
|
}
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2025-02-17 21:39:18 -06:00
|
|
|
@SubscribeEvent
|
2025-05-20 13:51:26 -04:00
|
|
|
public void onAnvilRepair(AnvilRepairEvent e) {
|
2025-05-20 13:53:26 -04:00
|
|
|
if (!isValidContext(e.entityPlayer))
|
2025-05-20 13:51:26 -04:00
|
|
|
return;
|
|
|
|
|
|
2025-05-20 13:53:26 -04:00
|
|
|
if (e.output == null)
|
2025-05-20 13:51:26 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
IItemStack out = NpcAPI.Instance().getIItemStack(e.output);
|
|
|
|
|
if (out instanceof IItemCustomizable) {
|
|
|
|
|
IPlayer ip = NoppesUtilServer.getIPlayer(e.entityPlayer);
|
2025-05-20 13:53:26 -04:00
|
|
|
IItemStack left = NpcAPI.Instance().getIItemStack(e.left);
|
2025-05-20 13:51:26 -04:00
|
|
|
IItemStack right = NpcAPI.Instance().getIItemStack(e.right);
|
2025-05-20 13:53:26 -04:00
|
|
|
EventHooks.onRepairCustomItem((IItemCustomizable) out, ip, left, right, e.breakChance);
|
2025-02-17 21:39:18 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@SubscribeEvent
|
2025-05-20 13:51:26 -04:00
|
|
|
public void onDestroyItem(PlayerDestroyItemEvent e) {
|
2025-05-20 13:53:26 -04:00
|
|
|
if (!isValidContext(e.entityPlayer))
|
2025-05-20 13:51:26 -04:00
|
|
|
return;
|
|
|
|
|
|
2025-05-20 13:53:26 -04:00
|
|
|
if (e.original == null)
|
2025-05-20 13:51:26 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
IItemStack orig = NpcAPI.Instance().getIItemStack(e.original);
|
|
|
|
|
if (orig instanceof IItemCustomizable) {
|
|
|
|
|
IPlayer ip = NoppesUtilServer.getIPlayer(e.entityPlayer);
|
2025-05-20 13:53:26 -04:00
|
|
|
EventHooks.onBreakCustomItem((IItemCustomizable) orig, ip);
|
2025-02-17 21:39:18 -06:00
|
|
|
}
|
|
|
|
|
}
|
2025-05-20 13:51:26 -04:00
|
|
|
|
|
|
|
|
private boolean isValidContext(EntityPlayer player) {
|
|
|
|
|
return player != null
|
|
|
|
|
&& player.worldObj instanceof WorldServer
|
|
|
|
|
&& !player.worldObj.isRemote
|
|
|
|
|
&& !(player instanceof FakePlayer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IItemCustomizable getCustomizable(ItemStack stack) {
|
|
|
|
|
if (stack == null || stack.getItem() == null || !NoppesUtilServer.isScriptableItem(stack.getItem())) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IItemStack raw = NpcAPI.Instance().getIItemStack(stack);
|
|
|
|
|
return (raw instanceof IItemCustomizable) ? (IItemCustomizable) raw : null;
|
|
|
|
|
}
|
2022-05-11 13:30:13 -05:00
|
|
|
}
|