Files
CustomNPC-Plus/src/main/java/noppes/npcs/ScriptItemEventHandler.java
T

280 lines
10 KiB
Java
Raw Normal View History

package noppes.npcs;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.PlayerEvent;
import kamkeel.npcs.network.PacketClient;
import kamkeel.npcs.network.packets.request.item.ColorBrushPacket;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.util.FakePlayer;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.entity.item.ItemTossEvent;
import net.minecraftforge.event.entity.living.LivingEvent;
import net.minecraftforge.event.entity.player.*;
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;
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;
import noppes.npcs.items.ItemNpcTool;
import noppes.npcs.scripted.NpcAPI;
2023-01-12 21:33:06 -05:00
import noppes.npcs.scripted.event.ItemEvent;
import java.util.HashMap;
public class ScriptItemEventHandler {
2025-03-13 17:14:45 -04:00
public ScriptItemEventHandler() {
}
@SubscribeEvent
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)
return;
if (event.entityLiving instanceof EntityCustomNpc) {
HashMap<Integer, ItemStack> armor = ((EntityCustomNpc) event.entityLiving).inventory.armor;
HashMap<Integer, ItemStack> weapons = ((EntityCustomNpc) event.entityLiving).inventory.weapons;
HashMap<Integer, ItemStack>[] inventories = new HashMap[]{armor, weapons};
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())) {
IItemStack istack = NpcAPI.Instance().getIItemStack(stack);
2025-02-17 16:02:56 -05:00
EventHooks.onScriptItemUpdate((IItemCustomizable) istack, event.entityLiving);
}
}
}
2025-03-13 17:14:45 -04:00
} else if (event.entityLiving.getHeldItem() != null && NoppesUtilServer.isScriptableItem(event.entityLiving.getHeldItem().getItem()) && !event.isCanceled()) {
IItemStack itemStack = NpcAPI.Instance().getIItemStack(event.entityLiving.getHeldItem());
2025-02-17 16:02:56 -05:00
EventHooks.onScriptItemUpdate((IItemCustomizable) itemStack, event.entityLiving);
}
}
@SubscribeEvent
public void onItemToss(ItemTossEvent e) {
if (!isValidContext(e.player) || e.isCanceled())
return;
if(e.entityItem == null)
return;
EntityItem entityItem = e.entityItem;
IItemCustomizable c = getCustomizable(entityItem.getEntityItem());
if (c != null) {
e.setCanceled(EventHooks.onScriptItemTossed(c, e.player, entityItem));
}
}
@SubscribeEvent
public void onItemPickup(PlayerEvent.ItemPickupEvent e) {
if (!isValidContext(e.player) || e.isCanceled())
return;
IItemCustomizable c = getCustomizable(e.player.getHeldItem());
if (c != null) {
EventHooks.onScriptItemPickedUp(c, e.player);
}
}
@SubscribeEvent
public void onEntityJoinWorld(EntityJoinWorldEvent e) {
if (e.world.isRemote || !(e.entity instanceof EntityItem))
return;
EntityItem itemEnt = (EntityItem)e.entity;
IItemCustomizable c = getCustomizable(itemEnt.getEntityItem());
if (c != null) {
e.setCanceled(EventHooks.onScriptItemSpawn(c, itemEnt));
}
}
@SubscribeEvent
public void onEntityInteract(EntityInteractEvent e) {
EntityPlayer player = e.entityPlayer;
if (!isValidContext(player) || e.isCanceled())
return;
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
}
}
@SubscribeEvent
public void onPlayerInteract(PlayerInteractEvent e) {
EntityPlayer player = e.entityPlayer;
if (player == null || e.action == null) return;
2024-02-21 00:31:17 -05:00
if(!isValidContext(e.entityPlayer))
2024-02-21 00:31:17 -05:00
return;
// 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)) {
TileEntity te = e.world.getTileEntity(e.x, e.y, e.z);
if (te instanceof TileColorable) {
int color = ((TileColorable)te).color;
if (!held.hasTagCompound()) {
held.setTagCompound(new NBTTagCompound());
2024-02-28 20:58:00 -05:00
}
ItemNpcTool.setColor(held.getTagCompound(), color);
e.setCanceled(true);
2024-02-28 20:58:00 -05:00
return;
2025-03-13 17:14:45 -04:00
}
2024-02-28 20:58:00 -05:00
}
}
// Scriptable-item RIGHT_CLICK hooks
if (PlayerDataController.Instance == null) {
return;
}
PlayerData pd = PlayerData.get(player);
if (pd == null) {
return;
}
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
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;
IWorld iw = (IWorld)NpcAPI.Instance().getIWorld(e.world);
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
}
}
}
2025-03-13 17:14:45 -04:00
@SubscribeEvent
public void onStartUseItem(PlayerUseItemEvent.Start e) {
if(!isValidContext(e.entityPlayer))
return;
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) {
if(!isValidContext(e.entityPlayer))
return;
2025-02-09 01:09:23 -05: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);
}
}
2025-03-13 17:14:45 -04:00
@SubscribeEvent
public void onStopUseItem(PlayerUseItemEvent.Stop e) {
if(!isValidContext(e.entityPlayer))
return;
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);
}
}
2025-03-13 17:14:45 -04:00
@SubscribeEvent
public void onFinishUseItem(PlayerUseItemEvent.Finish e) {
if(!isValidContext(e.entityPlayer))
return;
IItemCustomizable c = getCustomizable(e.item);
IPlayer ip = NoppesUtilServer.getIPlayer(e.entityPlayer);
if (c != null && ip != null) {
EventHooks.onFinishUsingCustomItem(c, ip, e.duration);
}
}
2025-03-13 17:14:45 -04:00
@SubscribeEvent
public void onAnvilRepair(AnvilRepairEvent e) {
if(!isValidContext(e.entityPlayer))
return;
if(e.output == null)
return;
IItemStack out = NpcAPI.Instance().getIItemStack(e.output);
if (out instanceof IItemCustomizable) {
IPlayer ip = NoppesUtilServer.getIPlayer(e.entityPlayer);
IItemStack left = NpcAPI.Instance().getIItemStack(e.left);
IItemStack right = NpcAPI.Instance().getIItemStack(e.right);
EventHooks.onRepairCustomItem((IItemCustomizable)out, ip, left, right, e.breakChance);
}
}
@SubscribeEvent
public void onDestroyItem(PlayerDestroyItemEvent e) {
if(!isValidContext(e.entityPlayer))
return;
if(e.original == null)
return;
IItemStack orig = NpcAPI.Instance().getIItemStack(e.original);
if (orig instanceof IItemCustomizable) {
IPlayer ip = NoppesUtilServer.getIPlayer(e.entityPlayer);
EventHooks.onBreakCustomItem((IItemCustomizable)orig, ip);
}
}
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;
}
}