diff --git a/src/main/java/noppes/npcs/EventHooks.java b/src/main/java/noppes/npcs/EventHooks.java index 49d402c3e..d9c755ee3 100644 --- a/src/main/java/noppes/npcs/EventHooks.java +++ b/src/main/java/noppes/npcs/EventHooks.java @@ -36,10 +36,7 @@ import noppes.npcs.scripted.event.PlayerEvent.LoginEvent; import noppes.npcs.scripted.event.PlayerEvent.LogoutEvent; import noppes.npcs.scripted.event.PlayerEvent.PickUpEvent; import noppes.npcs.scripted.event.PlayerEvent.DropEvent; -import noppes.npcs.scripted.interfaces.ICustomGui; -import noppes.npcs.scripted.interfaces.IEntity; -import noppes.npcs.scripted.interfaces.IItemStack; -import noppes.npcs.scripted.interfaces.IWorld; +import noppes.npcs.scripted.interfaces.*; import noppes.npcs.scripted.item.ScriptCustomItem; import noppes.npcs.scripted.item.ScriptItemStack; import noppes.npcs.scripted.wrapper.WrapperNpcAPI; @@ -96,6 +93,30 @@ public class EventHooks { return WrapperNpcAPI.EVENT_BUS.post(event); } + public static boolean onStartUsingCustomItem(ScriptCustomItem handler, IPlayer player, int duration) { + ItemEvent.StartUsingItem event = new ItemEvent.StartUsingItem(handler, player, duration); + handler.callScript(EnumScriptType.START_USING_ITEM, event); + return WrapperNpcAPI.EVENT_BUS.post(event); + } + + public static boolean onUsingCustomItem(ScriptCustomItem handler, IPlayer player, int duration) { + ItemEvent.UsingItem event = new ItemEvent.UsingItem(handler, player, duration); + handler.callScript(EnumScriptType.USING_ITEM, event); + return WrapperNpcAPI.EVENT_BUS.post(event); + } + + public static boolean onStopUsingCustomItem(ScriptCustomItem handler, IPlayer player, int duration) { + ItemEvent.StopUsingItem event = new ItemEvent.StopUsingItem(handler, player, duration); + handler.callScript(EnumScriptType.STOP_USING_ITEM, event); + return WrapperNpcAPI.EVENT_BUS.post(event); + } + + public static boolean onFinishUsingCustomItem(ScriptCustomItem handler, IPlayer player, int duration) { + ItemEvent.FinishUsingItem event = new ItemEvent.FinishUsingItem(handler, player, duration); + handler.callScript(EnumScriptType.FINISH_USING_ITEM, event); + return WrapperNpcAPI.EVENT_BUS.post(event); + } + public static void onNPCInit(EntityNPCInterface npc) { if(npc == null || npc.wrappedNPC == null) return; diff --git a/src/main/java/noppes/npcs/ScriptItemEventHandler.java b/src/main/java/noppes/npcs/ScriptItemEventHandler.java index e21ddd8dc..c5de605c2 100644 --- a/src/main/java/noppes/npcs/ScriptItemEventHandler.java +++ b/src/main/java/noppes/npcs/ScriptItemEventHandler.java @@ -12,10 +12,13 @@ import net.minecraftforge.event.entity.item.ItemTossEvent; import net.minecraftforge.event.entity.living.LivingEvent; import net.minecraftforge.event.entity.living.LivingHurtEvent; import net.minecraftforge.event.entity.player.EntityInteractEvent; +import net.minecraftforge.event.entity.player.PlayerUseItemEvent; import noppes.npcs.controllers.ScriptController; +import noppes.npcs.controllers.data.PlayerDataScript; import noppes.npcs.entity.EntityCustomNpc; import noppes.npcs.items.ItemScripted; import noppes.npcs.scripted.NpcAPI; +import noppes.npcs.scripted.entity.ScriptPlayer; import noppes.npcs.scripted.event.ItemEvent; import noppes.npcs.scripted.interfaces.IItemStack; import noppes.npcs.scripted.interfaces.IPlayer; @@ -109,4 +112,49 @@ public class ScriptItemEventHandler { } 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) { + ScriptCustomItem handler = ItemScripted.GetWrapper(event.item); + ScriptPlayer scriptPlayer = (ScriptPlayer) ScriptController.Instance.getScriptForEntity(event.entityPlayer); + event.setCanceled(EventHooks.onStartUsingCustomItem(handler, scriptPlayer, event.duration)); + } + } + @SubscribeEvent + public void invoke(PlayerUseItemEvent.Tick event) { + if(event.entityPlayer == null || event.entityPlayer.worldObj == null) + return; + + if(event.entityPlayer.worldObj instanceof WorldServer && event.item.getItem() == CustomItems.scripted_item) { + ScriptCustomItem handler = ItemScripted.GetWrapper(event.item); + ScriptPlayer scriptPlayer = (ScriptPlayer) ScriptController.Instance.getScriptForEntity(event.entityPlayer); + event.setCanceled(EventHooks.onUsingCustomItem(handler, scriptPlayer, event.duration)); + } + } + @SubscribeEvent + public void invoke(PlayerUseItemEvent.Stop event) { + if(event.entityPlayer == null || event.entityPlayer.worldObj == null) + return; + + if(event.entityPlayer.worldObj instanceof WorldServer && event.item.getItem() == CustomItems.scripted_item) { + ScriptCustomItem handler = ItemScripted.GetWrapper(event.item); + ScriptPlayer scriptPlayer = (ScriptPlayer) ScriptController.Instance.getScriptForEntity(event.entityPlayer); + event.setCanceled(EventHooks.onStopUsingCustomItem(handler, scriptPlayer, event.duration)); + } + } + @SubscribeEvent + public void invoke(PlayerUseItemEvent.Finish event) { + if(event.entityPlayer == null || event.entityPlayer.worldObj == null) + return; + + if(event.entityPlayer.worldObj instanceof WorldServer && event.item.getItem() == CustomItems.scripted_item) { + ScriptCustomItem handler = ItemScripted.GetWrapper(event.item); + ScriptPlayer scriptPlayer = (ScriptPlayer) ScriptController.Instance.getScriptForEntity(event.entityPlayer); + EventHooks.onFinishUsingCustomItem(handler, scriptPlayer, event.duration); + } + } } diff --git a/src/main/java/noppes/npcs/client/gui/script/GuiScriptInterface.java b/src/main/java/noppes/npcs/client/gui/script/GuiScriptInterface.java index 7e6100906..21183687c 100644 --- a/src/main/java/noppes/npcs/client/gui/script/GuiScriptInterface.java +++ b/src/main/java/noppes/npcs/client/gui/script/GuiScriptInterface.java @@ -173,6 +173,10 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb hookList.add("spawn"); hookList.add("interact"); hookList.add("attack"); + hookList.add("startItem"); + hookList.add("usingItem"); + hookList.add("stopItem"); + hookList.add("finishItem"); //hookList.add(""); //hookList.add(""); } diff --git a/src/main/java/noppes/npcs/client/renderer/CustomItemRenderer.java b/src/main/java/noppes/npcs/client/renderer/CustomItemRenderer.java index d875ec1d4..488ecc496 100644 --- a/src/main/java/noppes/npcs/client/renderer/CustomItemRenderer.java +++ b/src/main/java/noppes/npcs/client/renderer/CustomItemRenderer.java @@ -6,7 +6,6 @@ import net.minecraft.client.renderer.OpenGlHelper; import net.minecraft.client.renderer.Tessellator; import net.minecraft.client.renderer.entity.RenderManager; import net.minecraft.client.renderer.texture.TextureManager; -import net.minecraft.client.renderer.texture.TextureMap; import net.minecraft.client.resources.IResource; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.item.EntityItem; @@ -33,7 +32,8 @@ public class CustomItemRenderer implements IItemRenderer { private static final ResourceLocation enchant = new ResourceLocation("textures/misc/enchanted_item_glint.png"); private final Random random = new Random(); - private int itemRenderTicks = 0; + private int entityRenderTicks = 1; + private int item3dRenderTicks = 1; @Override public boolean handleRenderType(ItemStack item, ItemRenderType type) { @@ -80,7 +80,22 @@ public class CustomItemRenderer implements IItemRenderer { } if (type == ItemRenderType.ENTITY) { - ((ItemScripted) itemStack.getItem()).renderCustomAttributes(itemStack); + GL11.glRotatef(scriptCustomItem.rotationX, 1, 0, 0); + GL11.glRotatef(scriptCustomItem.rotationY, 0, 1, 0); + GL11.glRotatef(scriptCustomItem.rotationZ, 0, 0, 1); + + GL11.glRotatef(scriptCustomItem.rotationXRate * entityRenderTicks%360, 1, 0, 0); + GL11.glRotatef(scriptCustomItem.rotationYRate * entityRenderTicks%360, 0, 1, 0); + GL11.glRotatef(scriptCustomItem.rotationZRate * entityRenderTicks%360, 0, 0, 1); + + GL11.glScalef(scriptCustomItem.scaleX, scriptCustomItem.scaleY, scriptCustomItem.scaleZ); + + int color = scriptCustomItem.getColor(); + float itemRed = (color >> 16 & 255) / 255f; + float itemGreen = (color >> 8 & 255) / 255f; + float itemBlue = (color & 255) / 255f; + GL11.glColor4f(itemRed, itemGreen, itemBlue, 1.0F); + EntityItem entityItem = (EntityItem) data[1]; renderEntityCustomItem(scriptCustomItem, itemStack, entityItem); return; @@ -91,7 +106,21 @@ public class CustomItemRenderer implements IItemRenderer { ((ItemRenderInterface) itemStack.getItem()).renderSpecial(); GL11.glTranslatef(scriptCustomItem.translateX, scriptCustomItem.translateY, scriptCustomItem.translateZ); - ((ItemScripted) itemStack.getItem()).renderCustomAttributes(itemStack); + GL11.glRotatef(scriptCustomItem.rotationX, 1, 0, 0); + GL11.glRotatef(scriptCustomItem.rotationY, 0, 1, 0); + GL11.glRotatef(scriptCustomItem.rotationZ, 0, 0, 1); + + GL11.glRotatef(scriptCustomItem.rotationXRate * item3dRenderTicks %360, 1, 0, 0); + GL11.glRotatef(scriptCustomItem.rotationYRate * item3dRenderTicks %360, 0, 1, 0); + GL11.glRotatef(scriptCustomItem.rotationZRate * item3dRenderTicks %360, 0, 0, 1); + + GL11.glScalef(scriptCustomItem.scaleX, scriptCustomItem.scaleY, scriptCustomItem.scaleZ); + + int color = scriptCustomItem.getColor(); + float itemRed = (color >> 16 & 255) / 255f; + float itemGreen = (color >> 8 & 255) / 255f; + float itemBlue = (color & 255) / 255f; + GL11.glColor4f(itemRed, itemGreen, itemBlue, 1.0F); GL11.glRotatef(-20.0F, 0.0F, 0.0F, 1.0F); GL11.glRotatef(-50.0F, 0.0F, 1.0F, 0.0F); @@ -102,9 +131,9 @@ public class CustomItemRenderer implements IItemRenderer { } public void renderEntityCustomItem(ScriptCustomItem scriptCustomItem, ItemStack itemStack, EntityItem entityItem) { - this.itemRenderTicks++; + this.entityRenderTicks++; int pass = 0; - float bobbingY = (float)(Math.sin((float)this.itemRenderTicks/40.0F) + 1) * scriptCustomItem.scaleY/6.0F; + float bobbingY = (float)(Math.sin((float)this.entityRenderTicks /40.0F) + 1) * scriptCustomItem.scaleY/6.0F; GL11.glPushMatrix(); GL11.glTranslatef(0.0F, bobbingY, 0.0F); @@ -342,6 +371,8 @@ public class CustomItemRenderer implements IItemRenderer { } public void renderItem3d(ScriptCustomItem scriptCustomItem, EntityLivingBase entityLivingBase, ItemStack itemStack) { + item3dRenderTicks++; + Minecraft mc = Minecraft.getMinecraft(); TextureManager texturemanager = mc.getTextureManager(); int par3 = 0; diff --git a/src/main/java/noppes/npcs/items/ItemScripted.java b/src/main/java/noppes/npcs/items/ItemScripted.java index 43b941229..510c8fbbc 100644 --- a/src/main/java/noppes/npcs/items/ItemScripted.java +++ b/src/main/java/noppes/npcs/items/ItemScripted.java @@ -4,6 +4,7 @@ import cpw.mods.fml.common.registry.GameRegistry; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import net.minecraft.block.Block; +import net.minecraft.client.Minecraft; import net.minecraft.client.entity.EntityClientPlayerMP; import net.minecraft.client.renderer.texture.IIconRegister; import net.minecraft.entity.Entity; @@ -161,19 +162,4 @@ public class ItemScripted extends Item implements ItemRenderInterface { public void renderSpecial() { GL11.glTranslatef(0.135F, 0.2F, 0.07F); } - - public void renderCustomAttributes(ItemStack itemStack) { - ScriptCustomItem scriptCustomItem = GetWrapper(itemStack); - - GL11.glRotatef(scriptCustomItem.rotationX, 1, 0, 0); - GL11.glRotatef(scriptCustomItem.rotationY, 0, 1, 0); - GL11.glRotatef(scriptCustomItem.rotationZ, 0, 0, 1); - GL11.glScalef(scriptCustomItem.scaleX, scriptCustomItem.scaleY, scriptCustomItem.scaleZ); - - int color = scriptCustomItem.getColor(); - float itemRed = (color >> 16 & 255) / 255f; - float itemGreen = (color >> 8 & 255) / 255f; - float itemBlue = (color & 255) / 255f; - GL11.glColor4f(itemRed, itemGreen, itemBlue, 1.0F); - } } diff --git a/src/main/java/noppes/npcs/scripted/event/ItemEvent.java b/src/main/java/noppes/npcs/scripted/event/ItemEvent.java index 6bbb8c4e3..87663eada 100644 --- a/src/main/java/noppes/npcs/scripted/event/ItemEvent.java +++ b/src/main/java/noppes/npcs/scripted/event/ItemEvent.java @@ -1,20 +1,23 @@ package noppes.npcs.scripted.event; import cpw.mods.fml.common.eventhandler.Cancelable; -import noppes.npcs.scripted.entity.ScriptEntity; +import net.minecraft.item.ItemStack; +import noppes.npcs.scripted.NpcAPI; +import noppes.npcs.scripted.interfaces.ICustomItem; import noppes.npcs.scripted.interfaces.IEntity; +import noppes.npcs.scripted.interfaces.IItemStack; import noppes.npcs.scripted.interfaces.IPlayer; import noppes.npcs.scripted.item.ScriptCustomItem; public class ItemEvent extends CustomNPCsEvent { - public ScriptCustomItem item; + public ICustomItem item; - public ItemEvent(ScriptCustomItem item) { + public ItemEvent(ICustomItem item) { this.item = item; } public static class InitEvent extends ItemEvent { - public InitEvent(ScriptCustomItem item) { + public InitEvent(ICustomItem item) { super(item); } } @@ -22,7 +25,7 @@ public class ItemEvent extends CustomNPCsEvent { public static class UpdateEvent extends ItemEvent { public IEntity entity; - public UpdateEvent(ScriptCustomItem item, IEntity entity) { + public UpdateEvent(ICustomItem item, IEntity entity) { super(item); this.entity = entity; } @@ -33,7 +36,7 @@ public class ItemEvent extends CustomNPCsEvent { public IEntity entity; public IPlayer player; - public TossedEvent(ScriptCustomItem item, IPlayer player, IEntity entity) { + public TossedEvent(ICustomItem item, IPlayer player, IEntity entity) { super(item); this.entity = entity; this.player = player; @@ -43,7 +46,7 @@ public class ItemEvent extends CustomNPCsEvent { public static class PickedUpEvent extends ItemEvent { public IPlayer player; - public PickedUpEvent(ScriptCustomItem item, IPlayer player) { + public PickedUpEvent(ICustomItem item, IPlayer player) { super(item); this.player = player; } @@ -52,7 +55,7 @@ public class ItemEvent extends CustomNPCsEvent { @Cancelable public static class SpawnEvent extends ItemEvent { public IEntity entity; - public SpawnEvent(ScriptCustomItem item, IEntity entity){ + public SpawnEvent(ICustomItem item, IEntity entity){ super(item); this.entity = entity; } @@ -64,7 +67,7 @@ public class ItemEvent extends CustomNPCsEvent { public final Object target; public IPlayer player; - public InteractEvent(ScriptCustomItem item, IPlayer player, int type, Object target) { + public InteractEvent(ICustomItem item, IPlayer player, int type, Object target) { super(item); this.type = type; this.target = target; @@ -74,11 +77,11 @@ public class ItemEvent extends CustomNPCsEvent { @Cancelable public static class AttackEvent extends ItemEvent { - public final int type;//1: Hit, 2: Whiff //TODO: Implement type 2 + public final int type;//1: Hit, 2: Whiff public final Object target; public IEntity swingingEntity; - public AttackEvent(ScriptCustomItem item, IEntity swingingEntity, int type, Object target) { + public AttackEvent(ICustomItem item, IEntity swingingEntity, int type, Object target) { super(item); this.type = type; this.target = target; @@ -86,4 +89,44 @@ public class ItemEvent extends CustomNPCsEvent { } } + public static class StartUsingItem extends ItemEvent { + public final IPlayer player; + public final int duration; + + public StartUsingItem(ICustomItem item, IPlayer player, int duration){ + super(item); + this.player = player; + this.duration = duration; + } + } + public static class UsingItem extends ItemEvent { + public final IPlayer player; + public final int duration; + + public UsingItem(ICustomItem item, IPlayer player, int duration){ + super(item); + this.player = player; + this.duration = duration; + } + } + public static class StopUsingItem extends ItemEvent { + public final IPlayer player; + public final int duration; + + public StopUsingItem(ICustomItem item, IPlayer player, int duration){ + super(item); + this.player = player; + this.duration = duration; + } + } + public static class FinishUsingItem extends ItemEvent { + public final IPlayer player; + public final int duration; + + public FinishUsingItem(ICustomItem item, IPlayer player, int duration){ + super(item); + this.player = player; + this.duration = duration; + } + } } diff --git a/src/main/java/noppes/npcs/scripted/item/ScriptCustomItem.java b/src/main/java/noppes/npcs/scripted/item/ScriptCustomItem.java index b9320b729..b3233840f 100644 --- a/src/main/java/noppes/npcs/scripted/item/ScriptCustomItem.java +++ b/src/main/java/noppes/npcs/scripted/item/ScriptCustomItem.java @@ -41,6 +41,7 @@ public class ScriptCustomItem extends ScriptItemStack implements ICustomItem, IS public float translateX, translateY, translateZ; public float scaleX = 1.0F, scaleY = 1.0F, scaleZ = 1.0F; public float rotationX, rotationY, rotationZ; + public float rotationXRate, rotationYRate, rotationZRate; public ScriptCustomItem(ItemStack item) { super(item); @@ -273,6 +274,13 @@ public class ScriptCustomItem extends ScriptItemStack implements ICustomItem, IS saveItemData(); } + public void setRotationRate(float rotationXRate, float rotationYRate, float rotationZRate){ + this.rotationXRate = rotationXRate; + this.rotationYRate = rotationYRate; + this.rotationZRate = rotationZRate; + saveItemData(); + } + public void setScale(float scaleX, float scaleY, float scaleZ){ this.scaleX = scaleX; this.scaleY = scaleY; @@ -302,6 +310,21 @@ public class ScriptCustomItem extends ScriptItemStack implements ICustomItem, IS saveItemData(); } + public void setRotationXRate(float rotationXRate) { + this.rotationXRate = rotationXRate; + saveItemData(); + } + + public void setRotationYRate(float rotationYRate) { + this.rotationYRate = rotationYRate; + saveItemData(); + } + + public void setRotationZRate(float rotationZRate) { + this.rotationZRate = rotationZRate; + saveItemData(); + } + public void setScaleX(float scaleX) { this.scaleX = scaleX; saveItemData(); @@ -344,6 +367,18 @@ public class ScriptCustomItem extends ScriptItemStack implements ICustomItem, IS return rotationZ; } + public float getRotationXRate() { + return rotationXRate; + } + + public float getRotationYRate() { + return rotationYRate; + } + + public float getRotationZRate() { + return rotationZRate; + } + public float getScaleX() { return scaleX; } @@ -392,6 +427,10 @@ public class ScriptCustomItem extends ScriptItemStack implements ICustomItem, IS compound.setFloat("RotationY",this.rotationY); compound.setFloat("RotationZ",this.rotationZ); + compound.setFloat("RotationXRate",this.rotationXRate); + compound.setFloat("RotationYRate",this.rotationYRate); + compound.setFloat("RotationZRate",this.rotationZRate); + compound.setFloat("ScaleX",this.scaleX); compound.setFloat("ScaleY",this.scaleY); compound.setFloat("ScaleZ",this.scaleZ); @@ -427,6 +466,10 @@ public class ScriptCustomItem extends ScriptItemStack implements ICustomItem, IS this.rotationY = compound.getFloat("RotationY"); this.rotationZ = compound.getFloat("RotationZ"); + this.rotationXRate = compound.getFloat("RotationXRate"); + this.rotationYRate = compound.getFloat("RotationYRate"); + this.rotationZRate = compound.getFloat("RotationZRate"); + this.scaleX = compound.getFloat("ScaleX"); this.scaleY = compound.getFloat("ScaleY"); this.scaleZ = compound.getFloat("ScaleZ");