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

227 lines
11 KiB
Java
Raw Normal View History

package noppes.npcs;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.PlayerEvent;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.world.WorldServer;
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.EntityInteractEvent;
2024-02-21 00:31:17 -05:00
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;
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.scripted.NpcAPI;
2023-01-12 21:33:06 -05:00
import noppes.npcs.scripted.event.ItemEvent;
import java.util.HashMap;
public class ScriptItemEventHandler {
public ScriptItemEventHandler() {}
@SubscribeEvent
public void invoke(LivingEvent.LivingUpdateEvent event) {
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-02-17 16:02:56 -05: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 invoke(ItemTossEvent event) {
if(event.player == null || event.player.worldObj == null)
return;
if(event.player.worldObj instanceof WorldServer) {
try {
2025-02-17 16:02:56 -05:00
if (NoppesUtilServer.isScriptableItem(event.entityItem.getEntityItem().getItem()) && !event.isCanceled()) {
2025-02-09 01:09:23 -05:00
IItemStack isw = NpcAPI.Instance().getIItemStack(event.entityItem.getEntityItem());
2025-02-17 16:02:56 -05:00
if(isw instanceof IItemCustomizable)
event.setCanceled(EventHooks.onScriptItemTossed((IItemCustomizable) isw, event.player, event.entityItem));
}
} catch (Exception e) {}
}
}
@SubscribeEvent
public void invoke(PlayerEvent.ItemPickupEvent event) {
if(event.player == null || event.player.worldObj == null)
return;
if(event.player.worldObj instanceof WorldServer) {
try {
2025-02-17 16:02:56 -05:00
if (NoppesUtilServer.isScriptableItem(event.player.getHeldItem().getItem()) && !event.isCanceled()) {
2025-02-09 01:09:23 -05:00
IItemStack isw = NpcAPI.Instance().getIItemStack(event.player.getHeldItem());
2025-02-17 16:02:56 -05:00
if(isw instanceof IItemCustomizable)
EventHooks.onScriptItemPickedUp((IItemCustomizable) isw, event.player);
}
} catch(Exception e) {}
}
}
@SubscribeEvent
public void invoke(EntityJoinWorldEvent event) {
if (!event.world.isRemote && event.entity instanceof EntityItem) {
EntityItem entity = (EntityItem)event.entity;
ItemStack stack = entity.getEntityItem();
try {
2025-02-17 16:02:56 -05:00
if (stack.stackSize > 0 && NoppesUtilServer.isScriptableItem(stack.getItem())) {
2025-02-09 01:09:23 -05:00
IItemStack isw = NpcAPI.Instance().getIItemStack(stack);
2025-02-17 16:02:56 -05:00
if(isw instanceof IItemCustomizable){
if(EventHooks.onScriptItemSpawn((IItemCustomizable) isw, entity)){
2025-02-09 01:09:23 -05:00
event.setCanceled(true);
}
}
}
} catch(Exception e) {}
}
}
@SubscribeEvent
public void invoke(EntityInteractEvent event) {
if(event.entityPlayer == null || event.entityPlayer.worldObj == null)
return;
if(!event.entityPlayer.worldObj.isRemote && event.entityPlayer.worldObj instanceof WorldServer) {
try {
2025-02-17 16:02:56 -05:00
if (NoppesUtilServer.isScriptableItem(event.entityPlayer.getHeldItem().getItem()) && !event.isCanceled()) {
2025-02-09 01:09:23 -05:00
IItemStack isw = NpcAPI.Instance().getIItemStack(event.entityPlayer.getHeldItem());
2025-02-17 16:02:56 -05:00
if(isw instanceof IItemCustomizable){
ItemEvent.InteractEvent eve = new ItemEvent.InteractEvent((IItemCustomizable) isw, (IPlayer) NpcAPI.Instance().getIEntity(event.entityPlayer), 2, NpcAPI.Instance().getIEntity(event.target));
event.setCanceled(EventHooks.onScriptItemInteract((IItemCustomizable) isw, eve));
2024-02-21 00:31:17 -05:00
2025-02-17 16:02:56 -05:00
ItemEvent.RightClickEvent rightClickEvent = new ItemEvent.RightClickEvent((IItemCustomizable) isw, (IPlayer) NpcAPI.Instance().getIEntity(event.entityPlayer), 1, NpcAPI.Instance().getIEntity(event.target));
event.setCanceled(EventHooks.onScriptItemRightClick((IItemCustomizable) isw, rightClickEvent));
2025-02-09 01:09:23 -05:00
}
2024-02-21 00:31:17 -05:00
}
} catch(Exception e) {}
}
}
@SubscribeEvent
public void invoke(PlayerInteractEvent event) {
if(event.entityPlayer == null || event.entityPlayer.worldObj == null || event.action == null)
return;
if(event.entityPlayer.worldObj.isRemote || !(event.entityPlayer.worldObj instanceof WorldServer))
2024-02-21 00:31:17 -05:00
return;
2024-02-28 20:58:00 -05:00
if (PlayerDataController.Instance != null) {
if(event.action == PlayerInteractEvent.Action.RIGHT_CLICK_AIR){
PlayerData handler = PlayerDataController.Instance.getPlayerData(event.entityPlayer);
if(handler == null)
return;
if(handler.hadInteract) {
handler.hadInteract = false;
return;
2024-02-21 00:31:17 -05:00
}
2024-02-28 20:58:00 -05:00
try {
2025-02-17 16:02:56 -05:00
if (NoppesUtilServer.isScriptableItem(event.entityPlayer.getHeldItem().getItem()) && !event.isCanceled()) {
2025-02-09 01:09:23 -05:00
IItemStack isw = NpcAPI.Instance().getIItemStack(event.entityPlayer.getHeldItem());
2025-02-17 16:02:56 -05:00
if(isw instanceof IItemCustomizable){
ItemEvent.RightClickEvent eve = new ItemEvent.RightClickEvent((IItemCustomizable) isw, (IPlayer) NpcAPI.Instance().getIEntity(event.entityPlayer), 0, null);
event.setCanceled(EventHooks.onScriptItemRightClick((IItemCustomizable) isw, eve));
2025-02-09 01:09:23 -05:00
}
2024-02-28 20:58:00 -05:00
}
} catch(Exception e) {}
}
else if(event.action == PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK){
PlayerData handler = PlayerDataController.Instance.getPlayerData(event.entityPlayer);
if(handler == null)
return;
handler.hadInteract = true;
try {
2025-02-17 16:02:56 -05:00
if (NoppesUtilServer.isScriptableItem(event.entityPlayer.getHeldItem().getItem()) && !event.isCanceled()) {
2025-02-09 01:09:23 -05:00
IItemStack isw = NpcAPI.Instance().getIItemStack(event.entityPlayer.getHeldItem());
2025-02-17 16:02:56 -05:00
if(isw instanceof IItemCustomizable){
ItemEvent.RightClickEvent eve = new ItemEvent.RightClickEvent((IItemCustomizable) isw, (IPlayer) NpcAPI.Instance().getIEntity(event.entityPlayer), 2, NpcAPI.Instance().getIBlock((IWorld) NpcAPI.Instance().getIWorld(event.world), event.x, event.y, event.z));
event.setCanceled(EventHooks.onScriptItemRightClick((IItemCustomizable) isw, eve));
2025-02-09 01:09:23 -05:00
}
2024-02-28 20:58:00 -05:00
}
} catch(Exception e) {}
}
}
}
@SubscribeEvent
public void invoke(PlayerUseItemEvent.Start event) {
if(event.entityPlayer == null || event.entityPlayer.worldObj == null)
return;
if(event.entityPlayer.worldObj instanceof WorldServer) {
2025-02-09 01:09:23 -05:00
IItemStack isw = NpcAPI.Instance().getIItemStack(event.item);
2025-02-17 16:02:56 -05:00
if(isw instanceof IItemCustomizable){
2025-02-09 01:09:23 -05:00
IPlayer IPlayer = (IPlayer) NpcAPI.Instance().getIEntity(event.entityPlayer);
2025-02-17 16:02:56 -05:00
event.setCanceled(EventHooks.onStartUsingCustomItem((IItemCustomizable) isw, IPlayer, event.duration));
2025-02-09 01:09:23 -05:00
}
}
}
@SubscribeEvent
public void invoke(PlayerUseItemEvent.Tick event) {
if(event.entityPlayer == null || event.entityPlayer.worldObj == null)
return;
2025-02-17 16:02:56 -05:00
if(event.entityPlayer.worldObj instanceof WorldServer && NoppesUtilServer.isScriptableItem(event.item.getItem())) {
2025-02-09 01:09:23 -05:00
IItemStack isw = NpcAPI.Instance().getIItemStack(event.item);
2025-02-17 16:02:56 -05:00
if(isw instanceof IItemCustomizable){
2025-02-09 01:09:23 -05:00
IPlayer IPlayer = (IPlayer) NpcAPI.Instance().getIEntity(event.entityPlayer);
2025-02-17 16:02:56 -05:00
event.setCanceled(EventHooks.onUsingCustomItem((IItemCustomizable) isw, IPlayer, event.duration));
2025-02-09 01:09:23 -05:00
}
}
}
@SubscribeEvent
public void invoke(PlayerUseItemEvent.Stop event) {
if(event.entityPlayer == null || event.entityPlayer.worldObj == null)
return;
2025-02-17 16:02:56 -05:00
if(event.entityPlayer.worldObj instanceof WorldServer && NoppesUtilServer.isScriptableItem(event.item.getItem())) {
2025-02-09 01:09:23 -05:00
IItemStack isw = NpcAPI.Instance().getIItemStack(event.item);
2025-02-17 16:02:56 -05:00
if(isw instanceof IItemCustomizable){
2025-02-09 01:09:23 -05:00
IPlayer IPlayer = (IPlayer) NpcAPI.Instance().getIEntity(event.entityPlayer);
2025-02-17 16:02:56 -05:00
event.setCanceled(EventHooks.onStopUsingCustomItem((IItemCustomizable) isw, IPlayer, event.duration));
2025-02-09 01:09:23 -05:00
}
}
}
@SubscribeEvent
public void invoke(PlayerUseItemEvent.Finish event) {
if(event.entityPlayer == null || event.entityPlayer.worldObj == null)
return;
2025-02-17 16:02:56 -05:00
if(event.entityPlayer.worldObj instanceof WorldServer && NoppesUtilServer.isScriptableItem(event.item.getItem())) {
2025-02-09 01:09:23 -05:00
IItemStack isw = NpcAPI.Instance().getIItemStack(event.item);
2025-02-17 16:02:56 -05:00
if(isw instanceof IItemCustomizable){
2025-02-09 01:09:23 -05:00
IPlayer IPlayer = (IPlayer) NpcAPI.Instance().getIEntity(event.entityPlayer);
2025-02-17 16:02:56 -05:00
EventHooks.onFinishUsingCustomItem((IItemCustomizable) isw, IPlayer, event.duration);
2025-02-09 01:09:23 -05:00
}
}
}
}