mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-17 21:35:10 +00:00
Hook Up Linked Item to existing Events
This commit is contained in:
1 parent
f84dd5477d
commit
690f2ca10d
6 files changed
+62
-47
No files matched your search
@@ -21,7 +21,6 @@ import noppes.npcs.api.gui.ICustomGui;
|
||||
import noppes.npcs.api.gui.IItemSlot;
|
||||
import noppes.npcs.api.handler.data.IAnimation;
|
||||
import noppes.npcs.api.handler.data.IFrame;
|
||||
import noppes.npcs.api.item.IItemCustom;
|
||||
import noppes.npcs.api.item.IItemCustomizable;
|
||||
import noppes.npcs.api.item.IItemStack;
|
||||
import noppes.npcs.constants.EnumScriptType;
|
||||
@@ -34,7 +33,6 @@ import noppes.npcs.entity.EntityProjectile;
|
||||
import noppes.npcs.scripted.NpcAPI;
|
||||
import noppes.npcs.scripted.event.*;
|
||||
import noppes.npcs.scripted.event.PlayerEvent.*;
|
||||
import noppes.npcs.scripted.item.ScriptCustomItem;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
@@ -74,6 +74,10 @@ public class NoppesUtilServer {
|
||||
return entity instanceof EntityPlayer ? ((EntityPlayer) entity).getGameProfile().getId() : entity.getUniqueID();
|
||||
}
|
||||
|
||||
public static boolean isScriptableItem(Item item){
|
||||
return item == CustomItems.scripted_item || item == CustomItems.linked_item;
|
||||
}
|
||||
|
||||
public static void setEditingNpc(EntityPlayer player, EntityNPCInterface npc){
|
||||
PlayerData data = PlayerDataController.Instance.getPlayerData(player);
|
||||
data.editingNpc = npc;
|
||||
|
||||
@@ -14,15 +14,13 @@ import net.minecraftforge.event.entity.player.PlayerInteractEvent;
|
||||
import net.minecraftforge.event.entity.player.PlayerUseItemEvent;
|
||||
import noppes.npcs.api.IWorld;
|
||||
import noppes.npcs.api.entity.IPlayer;
|
||||
import noppes.npcs.api.item.IItemCustom;
|
||||
import noppes.npcs.api.item.IItemCustomizable;
|
||||
import noppes.npcs.api.item.IItemStack;
|
||||
import noppes.npcs.controllers.PlayerDataController;
|
||||
import noppes.npcs.controllers.data.PlayerData;
|
||||
import noppes.npcs.entity.EntityCustomNpc;
|
||||
import noppes.npcs.items.ItemScripted;
|
||||
import noppes.npcs.scripted.NpcAPI;
|
||||
import noppes.npcs.scripted.event.ItemEvent;
|
||||
import noppes.npcs.scripted.item.ScriptCustomItem;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
@@ -41,15 +39,15 @@ public class ScriptItemEventHandler {
|
||||
|
||||
for (HashMap<Integer, ItemStack> inventory : inventories) {
|
||||
for (ItemStack stack : inventory.values()) {
|
||||
if (stack != null && stack.getItem() == CustomItems.scripted_item) {
|
||||
if (stack != null && NoppesUtilServer.isScriptableItem(stack.getItem())) {
|
||||
IItemStack istack = NpcAPI.Instance().getIItemStack(stack);
|
||||
EventHooks.onScriptItemUpdate((IItemCustom) istack, event.entityLiving);
|
||||
EventHooks.onScriptItemUpdate((IItemCustomizable) istack, event.entityLiving);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if(event.entityLiving.getHeldItem() != null && event.entityLiving.getHeldItem().getItem() == CustomItems.scripted_item && !event.isCanceled()) {
|
||||
} else if(event.entityLiving.getHeldItem() != null && NoppesUtilServer.isScriptableItem(event.entityLiving.getHeldItem().getItem()) && !event.isCanceled()) {
|
||||
IItemStack itemStack = NpcAPI.Instance().getIItemStack(event.entityLiving.getHeldItem());
|
||||
EventHooks.onScriptItemUpdate((IItemCustom) itemStack, event.entityLiving);
|
||||
EventHooks.onScriptItemUpdate((IItemCustomizable) itemStack, event.entityLiving);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,10 +58,10 @@ public class ScriptItemEventHandler {
|
||||
|
||||
if(event.player.worldObj instanceof WorldServer) {
|
||||
try {
|
||||
if (event.entityItem.getEntityItem().getItem() == CustomItems.scripted_item && !event.isCanceled()) {
|
||||
if (NoppesUtilServer.isScriptableItem(event.entityItem.getEntityItem().getItem()) && !event.isCanceled()) {
|
||||
IItemStack isw = NpcAPI.Instance().getIItemStack(event.entityItem.getEntityItem());
|
||||
if(isw instanceof IItemCustom)
|
||||
event.setCanceled(EventHooks.onScriptItemTossed((IItemCustom) isw, event.player, event.entityItem));
|
||||
if(isw instanceof IItemCustomizable)
|
||||
event.setCanceled(EventHooks.onScriptItemTossed((IItemCustomizable) isw, event.player, event.entityItem));
|
||||
}
|
||||
} catch (Exception e) {}
|
||||
}
|
||||
@@ -76,10 +74,10 @@ public class ScriptItemEventHandler {
|
||||
|
||||
if(event.player.worldObj instanceof WorldServer) {
|
||||
try {
|
||||
if (event.player.getHeldItem().getItem() == CustomItems.scripted_item && !event.isCanceled()) {
|
||||
if (NoppesUtilServer.isScriptableItem(event.player.getHeldItem().getItem()) && !event.isCanceled()) {
|
||||
IItemStack isw = NpcAPI.Instance().getIItemStack(event.player.getHeldItem());
|
||||
if(isw instanceof IItemCustom)
|
||||
EventHooks.onScriptItemPickedUp((IItemCustom) isw, event.player);
|
||||
if(isw instanceof IItemCustomizable)
|
||||
EventHooks.onScriptItemPickedUp((IItemCustomizable) isw, event.player);
|
||||
}
|
||||
} catch(Exception e) {}
|
||||
}
|
||||
@@ -92,10 +90,10 @@ public class ScriptItemEventHandler {
|
||||
ItemStack stack = entity.getEntityItem();
|
||||
|
||||
try {
|
||||
if (stack.stackSize > 0 && stack.getItem() == CustomItems.scripted_item) {
|
||||
if (stack.stackSize > 0 && NoppesUtilServer.isScriptableItem(stack.getItem())) {
|
||||
IItemStack isw = NpcAPI.Instance().getIItemStack(stack);
|
||||
if(isw instanceof IItemCustom){
|
||||
if(EventHooks.onScriptItemSpawn((IItemCustom) isw, entity)){
|
||||
if(isw instanceof IItemCustomizable){
|
||||
if(EventHooks.onScriptItemSpawn((IItemCustomizable) isw, entity)){
|
||||
event.setCanceled(true);
|
||||
}
|
||||
}
|
||||
@@ -111,14 +109,14 @@ public class ScriptItemEventHandler {
|
||||
|
||||
if(!event.entityPlayer.worldObj.isRemote && event.entityPlayer.worldObj instanceof WorldServer) {
|
||||
try {
|
||||
if (event.entityPlayer.getHeldItem().getItem() == CustomItems.scripted_item && !event.isCanceled()) {
|
||||
if (NoppesUtilServer.isScriptableItem(event.entityPlayer.getHeldItem().getItem()) && !event.isCanceled()) {
|
||||
IItemStack isw = NpcAPI.Instance().getIItemStack(event.entityPlayer.getHeldItem());
|
||||
if(isw instanceof IItemCustom){
|
||||
ItemEvent.InteractEvent eve = new ItemEvent.InteractEvent((IItemCustom) isw, (IPlayer) NpcAPI.Instance().getIEntity(event.entityPlayer), 2, NpcAPI.Instance().getIEntity(event.target));
|
||||
event.setCanceled(EventHooks.onScriptItemInteract((IItemCustom) isw, eve));
|
||||
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));
|
||||
|
||||
ItemEvent.RightClickEvent rightClickEvent = new ItemEvent.RightClickEvent((IItemCustom) isw, (IPlayer) NpcAPI.Instance().getIEntity(event.entityPlayer), 1, NpcAPI.Instance().getIEntity(event.target));
|
||||
event.setCanceled(EventHooks.onScriptItemRightClick((IItemCustom) isw, rightClickEvent));
|
||||
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));
|
||||
}
|
||||
}
|
||||
} catch(Exception e) {}
|
||||
@@ -144,11 +142,11 @@ public class ScriptItemEventHandler {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
if (event.entityPlayer.getHeldItem().getItem() == CustomItems.scripted_item && !event.isCanceled()) {
|
||||
if (NoppesUtilServer.isScriptableItem(event.entityPlayer.getHeldItem().getItem()) && !event.isCanceled()) {
|
||||
IItemStack isw = NpcAPI.Instance().getIItemStack(event.entityPlayer.getHeldItem());
|
||||
if(isw instanceof IItemCustom){
|
||||
ItemEvent.RightClickEvent eve = new ItemEvent.RightClickEvent((IItemCustom) isw, (IPlayer) NpcAPI.Instance().getIEntity(event.entityPlayer), 0, null);
|
||||
event.setCanceled(EventHooks.onScriptItemRightClick((IItemCustom) isw, eve));
|
||||
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));
|
||||
}
|
||||
}
|
||||
} catch(Exception e) {}
|
||||
@@ -160,11 +158,11 @@ public class ScriptItemEventHandler {
|
||||
|
||||
handler.hadInteract = true;
|
||||
try {
|
||||
if (event.entityPlayer.getHeldItem().getItem() == CustomItems.scripted_item && !event.isCanceled()) {
|
||||
if (NoppesUtilServer.isScriptableItem(event.entityPlayer.getHeldItem().getItem()) && !event.isCanceled()) {
|
||||
IItemStack isw = NpcAPI.Instance().getIItemStack(event.entityPlayer.getHeldItem());
|
||||
if(isw instanceof IItemCustom){
|
||||
ItemEvent.RightClickEvent eve = new ItemEvent.RightClickEvent((IItemCustom) 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((IItemCustom) isw, eve));
|
||||
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));
|
||||
}
|
||||
}
|
||||
} catch(Exception e) {}
|
||||
@@ -180,9 +178,9 @@ public class ScriptItemEventHandler {
|
||||
if(event.entityPlayer.worldObj instanceof WorldServer) {
|
||||
|
||||
IItemStack isw = NpcAPI.Instance().getIItemStack(event.item);
|
||||
if(isw instanceof IItemCustom){
|
||||
if(isw instanceof IItemCustomizable){
|
||||
IPlayer IPlayer = (IPlayer) NpcAPI.Instance().getIEntity(event.entityPlayer);
|
||||
event.setCanceled(EventHooks.onStartUsingCustomItem((IItemCustom) isw, IPlayer, event.duration));
|
||||
event.setCanceled(EventHooks.onStartUsingCustomItem((IItemCustomizable) isw, IPlayer, event.duration));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -191,12 +189,12 @@ public class ScriptItemEventHandler {
|
||||
if(event.entityPlayer == null || event.entityPlayer.worldObj == null)
|
||||
return;
|
||||
|
||||
if(event.entityPlayer.worldObj instanceof WorldServer && event.item.getItem() == CustomItems.scripted_item) {
|
||||
if(event.entityPlayer.worldObj instanceof WorldServer && NoppesUtilServer.isScriptableItem(event.item.getItem())) {
|
||||
|
||||
IItemStack isw = NpcAPI.Instance().getIItemStack(event.item);
|
||||
if(isw instanceof IItemCustom){
|
||||
if(isw instanceof IItemCustomizable){
|
||||
IPlayer IPlayer = (IPlayer) NpcAPI.Instance().getIEntity(event.entityPlayer);
|
||||
event.setCanceled(EventHooks.onUsingCustomItem((IItemCustom) isw, IPlayer, event.duration));
|
||||
event.setCanceled(EventHooks.onUsingCustomItem((IItemCustomizable) isw, IPlayer, event.duration));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -205,11 +203,11 @@ public class ScriptItemEventHandler {
|
||||
if(event.entityPlayer == null || event.entityPlayer.worldObj == null)
|
||||
return;
|
||||
|
||||
if(event.entityPlayer.worldObj instanceof WorldServer && event.item.getItem() == CustomItems.scripted_item) {
|
||||
if(event.entityPlayer.worldObj instanceof WorldServer && NoppesUtilServer.isScriptableItem(event.item.getItem())) {
|
||||
IItemStack isw = NpcAPI.Instance().getIItemStack(event.item);
|
||||
if(isw instanceof IItemCustom){
|
||||
if(isw instanceof IItemCustomizable){
|
||||
IPlayer IPlayer = (IPlayer) NpcAPI.Instance().getIEntity(event.entityPlayer);
|
||||
event.setCanceled(EventHooks.onStopUsingCustomItem((IItemCustom) isw, IPlayer, event.duration));
|
||||
event.setCanceled(EventHooks.onStopUsingCustomItem((IItemCustomizable) isw, IPlayer, event.duration));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -218,11 +216,11 @@ public class ScriptItemEventHandler {
|
||||
if(event.entityPlayer == null || event.entityPlayer.worldObj == null)
|
||||
return;
|
||||
|
||||
if(event.entityPlayer.worldObj instanceof WorldServer && event.item.getItem() == CustomItems.scripted_item) {
|
||||
if(event.entityPlayer.worldObj instanceof WorldServer && NoppesUtilServer.isScriptableItem(event.item.getItem())) {
|
||||
IItemStack isw = NpcAPI.Instance().getIItemStack(event.item);
|
||||
if(isw instanceof IItemCustom){
|
||||
if(isw instanceof IItemCustomizable){
|
||||
IPlayer IPlayer = (IPlayer) NpcAPI.Instance().getIEntity(event.entityPlayer);
|
||||
EventHooks.onFinishUsingCustomItem((IItemCustom) isw, IPlayer, event.duration);
|
||||
EventHooks.onFinishUsingCustomItem((IItemCustomizable) isw, IPlayer, event.duration);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import net.minecraftforge.event.entity.living.*;
|
||||
import net.minecraftforge.event.entity.player.*;
|
||||
import net.minecraftforge.event.world.BlockEvent;
|
||||
import noppes.npcs.api.entity.IPlayer;
|
||||
import noppes.npcs.api.item.IItemCustomizable;
|
||||
import noppes.npcs.constants.EnumQuestType;
|
||||
import noppes.npcs.controllers.CustomEffectController;
|
||||
import noppes.npcs.controllers.PartyController;
|
||||
@@ -53,8 +54,8 @@ public class ScriptPlayerEventHandler {
|
||||
|
||||
for(int i = 0; i < player.inventory.getSizeInventory(); i++){
|
||||
ItemStack item = player.inventory.getStackInSlot(i);
|
||||
if(item != null && item.getItem() == CustomItems.scripted_item){
|
||||
ScriptCustomItem isw = (ScriptCustomItem) NpcAPI.Instance().getIItemStack(item);
|
||||
if(item != null && NoppesUtilServer.isScriptableItem(item.getItem())){
|
||||
IItemCustomizable isw = (IItemCustomizable) NpcAPI.Instance().getIItemStack(item);
|
||||
EventHooks.onScriptItemUpdate(isw, player);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package noppes.npcs.scripted;
|
||||
|
||||
import cpw.mods.fml.common.eventhandler.EventBus;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import foxz.command.ScriptedCommand;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.command.CommandHandler;
|
||||
@@ -483,9 +484,23 @@ public class NpcAPI extends AbstractNpcAPI {
|
||||
} else {
|
||||
scriptStack = new ScriptItemStack(itemstack);
|
||||
}
|
||||
scriptItemCache.put(itemstack, new CacheHashMap.CachedObject<>(scriptStack));
|
||||
}
|
||||
|
||||
// Version Check Linked Item
|
||||
if(scriptStack instanceof ScriptLinkedItem && CustomNpcs.side() == Side.SERVER){
|
||||
ScriptLinkedItem scriptLinkedItem = (ScriptLinkedItem) scriptStack;
|
||||
LinkedItem linkedItem = LinkedItemController.getInstance().get(scriptLinkedItem.linkedItem.getId());
|
||||
if(linkedItem != null && scriptLinkedItem.linkedVersion != linkedItem.version){
|
||||
scriptLinkedItem.linkedItem = linkedItem.clone();
|
||||
scriptLinkedItem.linkedVersion = linkedItem.version;
|
||||
scriptLinkedItem.saveItemData();
|
||||
// Send Version Change Event
|
||||
// TODO: Version Change Event
|
||||
} else {
|
||||
// Destroy Linked Item
|
||||
scriptStack.item.stackSize = 0;
|
||||
}
|
||||
}
|
||||
return scriptStack;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import cpw.mods.fml.common.eventhandler.Cancelable;
|
||||
import noppes.npcs.api.entity.IEntity;
|
||||
import noppes.npcs.api.entity.IPlayer;
|
||||
import noppes.npcs.api.event.IItemEvent;
|
||||
import noppes.npcs.api.item.IItemCustom;
|
||||
import noppes.npcs.api.item.IItemCustomizable;
|
||||
import noppes.npcs.constants.EnumScriptType;
|
||||
|
||||
|
||||
Reference in new issue
Block a user