RightClick Event for Scripted Items

This commit is contained in:
Kam committed 2024-02-21 00:31:17 -05:00
1 parent 4a58702d16
commit 7f2a375323
9 files changed
+101 -14

No files matched your search

@@ -85,6 +85,11 @@ public class EventHooks {
return NpcAPI.EVENT_BUS.post(event);
}
public static boolean onScriptItemRightClick(IItemCustom handler, noppes.npcs.scripted.event.ItemEvent.RightClickEvent event) {
((ScriptCustomItem) handler).callScript(EnumScriptType.RIGHT_CLICK, event);
return NpcAPI.EVENT_BUS.post(event);
}
public static boolean onScriptItemAttack(IItemCustom handler, noppes.npcs.scripted.event.ItemEvent.AttackEvent event) {
((ScriptCustomItem) handler).callScript(EnumScriptType.ATTACK, event);
return NpcAPI.EVENT_BUS.post(event);
@@ -10,10 +10,15 @@ 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;
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.IItemStack;
import noppes.npcs.controllers.PlayerDataController;
import noppes.npcs.controllers.data.PlayerData;
import noppes.npcs.controllers.data.PlayerDataScript;
import noppes.npcs.entity.EntityCustomNpc;
import noppes.npcs.items.ItemScripted;
import noppes.npcs.scripted.NpcAPI;
@@ -103,6 +108,44 @@ public class ScriptItemEventHandler {
IItemCustom isw = ItemScripted.GetWrapper(event.entityPlayer.getHeldItem());
ItemEvent.InteractEvent eve = new ItemEvent.InteractEvent(isw, (IPlayer) NpcAPI.Instance().getIEntity(event.entityPlayer), 2, NpcAPI.Instance().getIEntity(event.target));
event.setCanceled(EventHooks.onScriptItemInteract(isw, eve));
ItemEvent.RightClickEvent rightClickEvent = new ItemEvent.RightClickEvent(isw, (IPlayer) NpcAPI.Instance().getIEntity(event.entityPlayer), 1, NpcAPI.Instance().getIEntity(event.target));
event.setCanceled(EventHooks.onScriptItemRightClick(isw, rightClickEvent));
}
} 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)
return;
if(event.action == PlayerInteractEvent.Action.RIGHT_CLICK_AIR){
PlayerDataScript handler = PlayerDataController.Instance.getPlayerData(event.entityPlayer).scriptData;
if(handler.hadInteract) {
handler.hadInteract = false;
return;
}
try {
if (event.entityPlayer.getHeldItem().getItem() == CustomItems.scripted_item && !event.isCanceled()) {
IItemCustom isw = ItemScripted.GetWrapper(event.entityPlayer.getHeldItem());
ItemEvent.RightClickEvent eve = new ItemEvent.RightClickEvent(isw, (IPlayer) NpcAPI.Instance().getIEntity(event.entityPlayer), 0, null);
event.setCanceled(EventHooks.onScriptItemRightClick(isw, eve));
}
} catch(Exception e) {}
}
else if(event.action == PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK){
PlayerDataScript handler = PlayerDataController.Instance.getPlayerData(event.entityPlayer).scriptData;
handler.hadInteract = true;
try {
if (event.entityPlayer.getHeldItem().getItem() == CustomItems.scripted_item && !event.isCanceled()) {
IItemCustom isw = ItemScripted.GetWrapper(event.entityPlayer.getHeldItem());
ItemEvent.RightClickEvent eve = new ItemEvent.RightClickEvent(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(isw, eve));
}
} catch(Exception e) {}
}
@@ -17,6 +17,7 @@ public class GuiScriptItem extends GuiScriptInterface {
hookList.add("pickedUp");
hookList.add("spawn");
hookList.add("interact");
hookList.add("rightClick");
hookList.add("attack");
hookList.add("startItem");
hookList.add("usingItem");
@@ -37,4 +38,4 @@ public class GuiScriptItem extends GuiScriptInterface {
super.save();
Client.sendData(EnumPacketServer.ScriptItemDataSave, new Object[]{this.item.getMCNbt()});
}
}
}
@@ -20,6 +20,7 @@ public enum EnumScriptType {
ATTACKED("attacked"),
ATTACK_MELEE("meleeAttack"),
ATTACK_SWING("meleeSwing"),
RIGHT_CLICK("rightClick"),
ROLE("role"),
RANGED_CHARGE("rangedCharge"),
RANGED_LAUNCHED("rangedLaunched"),
@@ -101,4 +102,4 @@ public enum EnumScriptType {
private EnumScriptType(String function) {
this.function = function;
}
}
}
@@ -12,10 +12,7 @@ import net.minecraft.util.ChatComponentText;
import noppes.npcs.CustomNpcs;
import noppes.npcs.LogWriter;
import noppes.npcs.config.ConfigMain;
import noppes.npcs.controllers.data.Bank;
import noppes.npcs.controllers.data.PlayerBankData;
import noppes.npcs.controllers.data.PlayerData;
import noppes.npcs.controllers.data.PlayerMail;
import noppes.npcs.controllers.data.*;
import noppes.npcs.util.CacheHashMap;
import noppes.npcs.util.NBTJsonUtil;
@@ -292,7 +289,10 @@ public class PlayerDataController {
public PlayerData getPlayerData(EntityPlayer player){
PlayerData data = getPlayerDataCache(player.getUniqueID().toString());
if(data != null){
data.player = player;
if(data.player == null){
data.player = player;
data.scriptData = new PlayerDataScript(player);
}
return data;
}
@@ -300,9 +300,14 @@ public class PlayerDataController {
if(data == null){
player.registerExtendedProperties("CustomNpcsData", data = new PlayerData());
data.player = player;
data.scriptData = new PlayerDataScript(player);
data.load();
}
data.player = player;
if(data.player == null){
data.player = player;
data.scriptData = new PlayerDataScript(player);
}
return data;
}
@@ -41,6 +41,7 @@ public class PlayerData implements IExtendedEntityProperties, IPlayerData {
public PlayerItemGiverData itemgiverData = new PlayerItemGiverData(this);
public PlayerMailData mailData = new PlayerMailData(this);
public AnimationData animationData = new AnimationData(this);
public PlayerDataScript scriptData;
public DataTimers timers = new DataTimers(this);
public DataSkinOverlays skinOverlays = new DataSkinOverlays(this);
@@ -1,7 +1,3 @@
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package noppes.npcs.controllers.data;
import com.google.common.base.Preconditions;
@@ -30,6 +26,7 @@ public class PlayerDataScript implements INpcScriptHandler {
private IPlayer playerAPI;
private long lastPlayerUpdate = 0L;
public long lastInited = -1L;
public boolean hadInteract = true;
public boolean enabled = false;
public PlayerDataScript(EntityPlayer player) {
@@ -320,4 +317,4 @@ public class PlayerDataScript implements INpcScriptHandler {
public static PlayerDataScript.ToStringHelper toStringHelper(Object self) {
return new PlayerDataScript.ToStringHelper(self.getClass().getSimpleName());
}
}
}
@@ -138,6 +138,40 @@ public class ItemEvent extends CustomNPCsEvent implements IItemEvent {
}
}
@Cancelable
public static class RightClickEvent extends ItemEvent implements IItemEvent.RightClickEvent {
/**
* 0:air, 1:entity, 2:block
*/
public final int type;
public final Object target;
public final IPlayer player;
public RightClickEvent(IItemCustom item, IPlayer player, int type, Object target) {
super(item);
this.type = type;
this.target = target;
this.player = player;
}
public String getHookName() {
return EnumScriptType.INTERACT.function;
}
public int getType() {
return type;
}
public Object getTarget() {
return target;
}
public IPlayer getPlayer() {
return player;
}
}
@Cancelable
public static class AttackEvent extends ItemEvent implements IItemEvent.AttackEvent {
public final int type;//1: Hit, 2: Whiff