From 8f16e6b20fd9493853d68f9076baaebc2f4ecb52 Mon Sep 17 00:00:00 2001 From: "Luis C.M" Date: Fri, 10 Jun 2022 17:25:58 -0500 Subject: [PATCH] Added IDialogImage, a scripted object which is used by IDialog. Used to add dialog images. IDialog: -Added several new functions for dialogs' new visual attributes, as well as a way to add, get, create, remove, and clear dialog images. Global NPC events: -Added fields and variables from the old ScriptEvent events, in preparation to replace the old events with NpcEvent instances on local NPC scripts. -Fixed issue where some event hooks would run on remote worlds. Global NPC killed event: -Added droppedItems and expDropped fields. IWorld and NpcAPI: -Now always uses a non-remove WorldServer object to create ScriptWorld objects and add them to the world cache. --- src/main/java/noppes/npcs/DataInventory.java | 111 +++++++----- src/main/java/noppes/npcs/EventHooks.java | 3 +- .../java/noppes/npcs/NoppesUtilPlayer.java | 23 +-- .../java/noppes/npcs/NoppesUtilServer.java | 9 +- .../client/gui/SubGuiNpcDialogVisual.java | 4 +- .../client/gui/player/GuiDialogInteract.java | 13 +- .../npcs/controllers/data/DataScript.java | 2 +- .../noppes/npcs/controllers/data/Dialog.java | 160 +++++++++++++++++- .../npcs/controllers/data/DialogImage.java | 139 ++++++++++++++- .../npcs/entity/EntityNPCInterface.java | 104 +++++++----- .../java/noppes/npcs/scripted/NpcAPI.java | 8 +- .../noppes/npcs/scripted/ScriptWorld.java | 11 +- .../npcs/scripted/entity/ScriptEntity.java | 5 +- .../noppes/npcs/scripted/event/NpcEvent.java | 154 ++++++++++++++++- .../scripted/interfaces/AbstractNpcAPI.java | 2 +- .../scripted/interfaces/entity/IEntity.java | 3 +- .../interfaces/handler/data/IDialog.java | 87 +++++++++- 17 files changed, 710 insertions(+), 128 deletions(-) diff --git a/src/main/java/noppes/npcs/DataInventory.java b/src/main/java/noppes/npcs/DataInventory.java index 924ebd969..9d2ff9c8e 100644 --- a/src/main/java/noppes/npcs/DataInventory.java +++ b/src/main/java/noppes/npcs/DataInventory.java @@ -93,7 +93,9 @@ public class DataInventory implements IInventory{ weapons.put(2, item); } - public void dropStuff(Entity entity, DamageSource damagesource) { + public ArrayList getDroppedItems(DamageSource damagesource) { + ArrayList drops = new ArrayList<>(); + ArrayList list = new ArrayList(); for (int i : items.keySet()) { ItemStack item = items.get(i); @@ -109,53 +111,76 @@ public class DataInventory implements IInventory{ list.add(e); } } - + for (EntityItem e : list) { + drops.add(e.getEntityItem()); + } + int enchant = 0; - if (damagesource.getEntity() instanceof EntityPlayer){ - enchant = EnchantmentHelper.getLootingModifier((EntityLivingBase)damagesource.getEntity()); - } - - if (!net.minecraftforge.common.ForgeHooks.onLivingDrops(npc, damagesource, list, enchant, true, 0)){ - for (EntityItem item : list){ - if(lootMode == 1 && entity instanceof EntityPlayer){ - EntityPlayer player = (EntityPlayer)entity; - item.delayBeforeCanPickup = 2; - npc.worldObj.spawnEntityInWorld(item); - ItemStack stack = item.getEntityItem(); - int i = stack.stackSize; + if (damagesource.getEntity() instanceof EntityPlayer){ + enchant = EnchantmentHelper.getLootingModifier((EntityLivingBase)damagesource.getEntity()); + } - if (player.inventory.addItemStackToInventory(stack)) { - npc.worldObj.playSoundAtEntity(item, - "random.pop", - 0.2F, - ((npc.getRNG().nextFloat() - npc.getRNG().nextFloat()) * 0.7F + 1.0F) * 2.0F); - player.onItemPickup(item, i); + if (!net.minecraftforge.common.ForgeHooks.onLivingDrops(npc, damagesource, list, enchant, true, 0)){ + return drops; + } else { + return new ArrayList<>(); + } + } - if (stack.stackSize <= 0) { - item.setDead(); - } - } - } - else - npc.worldObj.spawnEntityInWorld(item); - } - } - - int var1 = minExp; + public int getDroppedXp() { + int droppedXp = minExp; if (maxExp - minExp > 0) - var1 += npc.worldObj.rand.nextInt(maxExp - minExp); + droppedXp += npc.worldObj.rand.nextInt(maxExp - minExp); - while (var1 > 0){ - int var2 = EntityXPOrb.getXPSplit(var1); - var1 -= var2; - if(lootMode == 1 && entity instanceof EntityPlayer){ - npc.worldObj.spawnEntityInWorld(new EntityXPOrb(entity.worldObj, entity.posX, entity.posY, entity.posZ, var2)); - } - else{ - npc.worldObj.spawnEntityInWorld(new EntityXPOrb(npc.worldObj, npc.posX, npc.posY, npc.posZ, var2)); - } - } - + return droppedXp; + } + + public void dropXp(Entity entity, int droppedXp) { + while (droppedXp > 0){ + int var2 = EntityXPOrb.getXPSplit(droppedXp); + droppedXp -= var2; + if(lootMode == 1 && entity instanceof EntityPlayer){ + npc.worldObj.spawnEntityInWorld(new EntityXPOrb(entity.worldObj, entity.posX, entity.posY, entity.posZ, var2)); + } + else{ + npc.worldObj.spawnEntityInWorld(new EntityXPOrb(npc.worldObj, npc.posX, npc.posY, npc.posZ, var2)); + } + } + } + + public void dropItems(Entity entity, ArrayList itemList) { + ArrayList list = new ArrayList(); + for (ItemStack item : itemList) { + if(item == null) + continue; + EntityItem e = getEntityItem(item.copy()); + if(e != null) + list.add(e); + } + + for (EntityItem item : list){ + if(lootMode == 1 && entity instanceof EntityPlayer){ + EntityPlayer player = (EntityPlayer)entity; + item.delayBeforeCanPickup = 2; + npc.worldObj.spawnEntityInWorld(item); + ItemStack stack = item.getEntityItem(); + int i = stack.stackSize; + + if (player.inventory.addItemStackToInventory(stack)) { + npc.worldObj.playSoundAtEntity(item, + "random.pop", + 0.2F, + ((npc.getRNG().nextFloat() - npc.getRNG().nextFloat()) * 0.7F + 1.0F) * 2.0F); + player.onItemPickup(item, i); + + if (stack.stackSize <= 0) { + item.setDead(); + } + } + } + else + npc.worldObj.spawnEntityInWorld(item); + } } public EntityItem getEntityItem(ItemStack itemstack) { diff --git a/src/main/java/noppes/npcs/EventHooks.java b/src/main/java/noppes/npcs/EventHooks.java index 4da9356f7..564196051 100644 --- a/src/main/java/noppes/npcs/EventHooks.java +++ b/src/main/java/noppes/npcs/EventHooks.java @@ -236,11 +236,10 @@ public class EventHooks { return result; } - public static boolean onNPCKilled(EntityNPCInterface npc, DamageSource damagesource, Entity entity, ScriptEventKilled killedEvent){ + public static boolean onNPCKilled(EntityNPCInterface npc, NpcEvent.DiedEvent event, ScriptEventKilled killedEvent){ if(npc == null || npc.wrappedNPC == null) return false; - NpcEvent.DiedEvent event = new NpcEvent.DiedEvent(npc.wrappedNPC,damagesource,entity); ScriptController.Instance.npcScripts.callScript(EnumScriptType.KILLED, event); boolean result = npc.script.callScript(EnumScriptType.KILLED, "event", killedEvent); NpcAPI.EVENT_BUS.post(event); diff --git a/src/main/java/noppes/npcs/NoppesUtilPlayer.java b/src/main/java/noppes/npcs/NoppesUtilPlayer.java index 8357d655a..200240b78 100644 --- a/src/main/java/noppes/npcs/NoppesUtilPlayer.java +++ b/src/main/java/noppes/npcs/NoppesUtilPlayer.java @@ -259,18 +259,19 @@ public class NoppesUtilPlayer { Dialog dialog = DialogController.instance.dialogs.get(dialogId); if(dialog == null) return; - - EventHooks.onDialogOption(new DialogEvent.DialogOption((IPlayer) NpcAPI.Instance().getIEntity(player), dialog)); - EventHooks.onNPCDialogClosed(npc,player,dialogId,optionId+1,dialog); - - if(!dialog.hasDialogs(player) && !dialog.hasOtherOptions()) { - EventHooks.onDialogClosed(new DialogEvent.DialogClosed((IPlayer) NpcAPI.Instance().getIEntity(player), dialog)); - return; - } DialogOption option = dialog.options.get(optionId); - if(option == null || option.optionType == EnumOptionType.DialogOption && (!option.isAvailable(player) || !option.hasDialog()) || option.optionType == EnumOptionType.Disabled || option.optionType == EnumOptionType.QuitOption) { - EventHooks.onDialogClosed(new DialogEvent.DialogClosed((IPlayer) NpcAPI.Instance().getIEntity(player), dialog)); - return; + if (!npc.isRemote()) { + EventHooks.onDialogOption(new DialogEvent.DialogOption((IPlayer) NpcAPI.Instance().getIEntity(player), dialog)); + EventHooks.onNPCDialogClosed(npc, player, dialogId, optionId + 1, dialog); + + if (!dialog.hasDialogs(player) && !dialog.hasOtherOptions()) { + EventHooks.onDialogClosed(new DialogEvent.DialogClosed((IPlayer) NpcAPI.Instance().getIEntity(player), dialog)); + return; + } + if (option == null || option.optionType == EnumOptionType.DialogOption && (!option.isAvailable(player) || !option.hasDialog()) || option.optionType == EnumOptionType.Disabled || option.optionType == EnumOptionType.QuitOption) { + EventHooks.onDialogClosed(new DialogEvent.DialogClosed((IPlayer) NpcAPI.Instance().getIEntity(player), dialog)); + return; + } } if(option.optionType == EnumOptionType.RoleOption){ if(npc.roleInterface != null) diff --git a/src/main/java/noppes/npcs/NoppesUtilServer.java b/src/main/java/noppes/npcs/NoppesUtilServer.java index c56bff429..363d31516 100644 --- a/src/main/java/noppes/npcs/NoppesUtilServer.java +++ b/src/main/java/noppes/npcs/NoppesUtilServer.java @@ -181,7 +181,9 @@ public class NoppesUtilServer { public static void openDialog(EntityPlayer player, EntityNPCInterface npc, Dialog dia, int optionId){ Dialog dialog = dia.copy(player); - EventHooks.onDialogOpen(new DialogEvent.DialogOpen((IPlayer) NpcAPI.Instance().getIEntity(player), dialog)); + if (!npc.isRemote()) { + EventHooks.onDialogOpen(new DialogEvent.DialogOpen((IPlayer) NpcAPI.Instance().getIEntity(player), dialog)); + } if(npc instanceof EntityDialogNpc){ dialog.hideNPC = true; @@ -201,7 +203,10 @@ public class NoppesUtilServer { if(!data.dialogsRead.contains(dialog.id)) data.dialogsRead.add(dialog.id); setEditingNpc(player, npc); - EventHooks.onNPCDialog(npc, player, dialog.id, optionId, dialog); + + if (!npc.isRemote()) { + EventHooks.onNPCDialog(npc, player, dialog.id, optionId, dialog); + } } public static void runCommand(EntityPlayer player, String name, String command){ runCommand(player, name, command, player); diff --git a/src/main/java/noppes/npcs/client/gui/SubGuiNpcDialogVisual.java b/src/main/java/noppes/npcs/client/gui/SubGuiNpcDialogVisual.java index 78be626bf..c44832313 100644 --- a/src/main/java/noppes/npcs/client/gui/SubGuiNpcDialogVisual.java +++ b/src/main/java/noppes/npcs/client/gui/SubGuiNpcDialogVisual.java @@ -325,7 +325,7 @@ public class SubGuiNpcDialogVisual extends SubGuiInterface implements ISubGuiLis if (button.id == 1) { int addId = 0; if (selected != -1 && imageScroll.getSelected() != null) { - DialogImage selectedImage = dialog.dialogImages.get(Integer.valueOf(imageScroll.getSelected())); + DialogImage selectedImage = (DialogImage) dialog.dialogImages.get(Integer.valueOf(imageScroll.getSelected())); ArrayList keys = new ArrayList<>(dialog.dialogImages.keySet()); int keyIndex = keys.indexOf(selectedImage.id); do { @@ -417,7 +417,7 @@ public class SubGuiNpcDialogVisual extends SubGuiInterface implements ISubGuiLis private DialogImage getSelectedImage() { if (selected > -1) { - return dialog.dialogImages.get((dialog.dialogImages.keySet().toArray(new Integer[0]))[selected]); + return (DialogImage) dialog.dialogImages.get((dialog.dialogImages.keySet().toArray(new Integer[0]))[selected]); } else { return null; } diff --git a/src/main/java/noppes/npcs/client/gui/player/GuiDialogInteract.java b/src/main/java/noppes/npcs/client/gui/player/GuiDialogInteract.java index 47490533e..0ac6df680 100644 --- a/src/main/java/noppes/npcs/client/gui/player/GuiDialogInteract.java +++ b/src/main/java/noppes/npcs/client/gui/player/GuiDialogInteract.java @@ -28,6 +28,7 @@ import noppes.npcs.controllers.data.DialogImage; import noppes.npcs.controllers.data.DialogOption; import noppes.npcs.entity.EntityNPCInterface; +import noppes.npcs.scripted.interfaces.handler.data.IDialogImage; import org.lwjgl.input.Keyboard; import org.lwjgl.input.Mouse; import org.lwjgl.opengl.GL11; @@ -165,8 +166,8 @@ public class GuiDialogInteract extends GuiNPCInterface implements IGuiClose drawString(fontRendererObj, "Text Sound: " + (textSoundEnabled ? "On" : "Off"), 10, 20, 0xFFFFFF); } - for (DialogImage dialogImage : dialog.dialogImages.values()) { - GuiDialogImage image = new GuiDialogImage(dialogImage); + for (IDialogImage dialogImage : dialog.dialogImages.values()) { + GuiDialogImage image = new GuiDialogImage((DialogImage) dialogImage); if (image.imageType != 0) continue; @@ -184,8 +185,8 @@ public class GuiDialogInteract extends GuiNPCInterface implements IGuiClose } GL11.glPushMatrix(); - for (DialogImage dialogImage : dialog.dialogImages.values()) { - GuiDialogImage image = new GuiDialogImage(dialogImage); + for (IDialogImage dialogImage : dialog.dialogImages.values()) { + GuiDialogImage image = new GuiDialogImage((DialogImage) dialogImage); if (image.imageType != 1) continue; @@ -447,8 +448,8 @@ public class GuiDialogInteract extends GuiNPCInterface implements IGuiClose } GL11.glPushMatrix(); - for (DialogImage dialogImage : dialog.dialogImages.values()) { - GuiDialogImage image = new GuiDialogImage(dialogImage); + for (IDialogImage dialogImage : dialog.dialogImages.values()) { + GuiDialogImage image = new GuiDialogImage((DialogImage) dialogImage); if (image.imageType != 2) continue; diff --git a/src/main/java/noppes/npcs/controllers/data/DataScript.java b/src/main/java/noppes/npcs/controllers/data/DataScript.java index 1fdaee109..a300229cf 100644 --- a/src/main/java/noppes/npcs/controllers/data/DataScript.java +++ b/src/main/java/noppes/npcs/controllers/data/DataScript.java @@ -116,7 +116,7 @@ public class DataScript implements IScriptHandler { } if(!isEnabled()) return false; - if(!hasInited){ + if(!hasInited && !npc.isRemote()){ hasInited = true; EventHooks.onNPCInit(this.npc); callScript(EnumScriptType.INIT); diff --git a/src/main/java/noppes/npcs/controllers/data/Dialog.java b/src/main/java/noppes/npcs/controllers/data/Dialog.java index 5bc716d90..01cfa4ece 100644 --- a/src/main/java/noppes/npcs/controllers/data/Dialog.java +++ b/src/main/java/noppes/npcs/controllers/data/Dialog.java @@ -57,7 +57,7 @@ public class Dialog implements ICompatibilty, IDialog { public float npcScale = 1.0F; public int npcOffsetX, npcOffsetY; - public HashMap dialogImages = new HashMap<>(); + public HashMap dialogImages = new HashMap<>(); public boolean hasDialogs(EntityPlayer player) { for(DialogOption option: options.values()) @@ -108,7 +108,7 @@ public class Dialog implements ICompatibilty, IDialog { this.options = newoptions; NBTTagList images = compound.getTagList("Images", 10); - HashMap newImages = new HashMap<>(); + HashMap newImages = new HashMap<>(); for(int i = 0; i < images.tagCount(); i++){ NBTTagCompound imageCompound = images.getCompoundTagAt(i); int id = imageCompound.getInteger("ID"); @@ -220,8 +220,8 @@ public class Dialog implements ICompatibilty, IDialog { compound.setInteger("NPCOffsetY", npcOffsetY); NBTTagList images = new NBTTagList(); - for (DialogImage dialogImage : dialogImages.values()) { - NBTTagCompound imageCompound = dialogImage.writeToNBT(new NBTTagCompound()); + for (IDialogImage dialogImage : dialogImages.values()) { + NBTTagCompound imageCompound = ((DialogImage) dialogImage).writeToNBT(new NBTTagCompound()); images.appendTag(imageCompound); } compound.setTag("Images",images); @@ -398,4 +398,156 @@ public class Dialog implements ICompatibilty, IDialog { public String getSound() { return this.sound; } + + public void setColor(int color) { + this.color = color; + } + public int getColor() { + return this.color; + } + + public void setTitleColor(int titleColor) { + this.titleColor = titleColor; + } + public int getTitleColor() { + return titleColor; + } + + public void renderGradual(boolean gradual) { + this.renderGradual = gradual; + } + public boolean renderGradual() { + return renderGradual; + } + + public void showPreviousBlocks(boolean show) { + this.showPreviousBlocks = show; + } + public boolean showPreviousBlocks() { + return showPreviousBlocks; + } + + public void showOptionLine(boolean show) { + this.showOptionLine = show; + } + public boolean showOptionLine() { + return showOptionLine; + } + + public void setTextSound(String textSound) { + this.textSound = textSound; + } + public String getTextSound() { + return textSound; + } + + public void setTextPitch(float textPitch) { + this.textPitch = textPitch; + } + public float getTextPitch() { + return textPitch; + } + + public void setTitlePos(int pos) { + this.titlePos = pos; + } + public int getTitlePos() { + return this.titlePos; + } + + public void setNPCScale(float scale) { + this.npcScale = scale; + } + public float getNpcScale() { + return npcScale; + } + + public void setNpcOffset(int offsetX, int offsetY) { + this.npcOffsetX = offsetX; + this.npcOffsetY = offsetY; + } + public int getNpcOffsetX() { + return npcOffsetX; + } + public int getNpcOffsetY() { + return npcOffsetY; + } + + public void textWidthHeight(int textWidth, int textHeight) { + this.textWidth = textWidth; + this.textHeight = textHeight; + } + public int getTextWidth() { + return textWidth; + } + public int setTextHeight() { + return textHeight; + } + + public void setTextOffset(int offsetX, int offsetY) { + this.textOffsetX = offsetX; + this.textOffsetY = offsetY; + } + public int getTextOffsetX() { + return textOffsetX; + } + public int getTextOffsetY() { + return textOffsetY; + } + + public void setTitleOffset(int offsetX, int offsetY) { + this.titleOffsetX = offsetX; + this.titleOffsetY = offsetY; + } + public int getTitleOffsetX() { + return titleOffsetX; + } + public int getTitleOffsetY() { + return titleOffsetY; + } + + public void setOptionOffset(int offsetX, int offsetY) { + this.optionOffsetX = offsetX; + this.optionOffsetY = offsetY; + } + public int getOptionOffsetX() { + return optionOffsetX; + } + public int getOptionOffsetY() { + return optionOffsetY; + } + + public void setOptionSpacing(int spaceX, int spaceY) { + this.optionSpaceX = spaceX; + this.optionSpaceY = spaceY; + } + public int getOptionSpaceX() { + return optionSpaceX; + } + public int getOptionSpaceY() { + return optionSpaceY; + } + + public void addImage(int id, IDialogImage image) { + ((DialogImage) image).id = id; + dialogImages.put(id, image); + } + public IDialogImage getImage(int id) { + return dialogImages.get(id); + } + public IDialogImage createImage() { + return new DialogImage(); + } + public IDialogImage[] getImages() { + return (new ArrayList<>(dialogImages.values())).toArray(new IDialogImage[0]); + } + public boolean hasImage(int id) { + return dialogImages.containsKey(id); + } + public void removeImage(int id) { + dialogImages.remove(id); + } + public void clearImages() { + dialogImages.clear(); + } } diff --git a/src/main/java/noppes/npcs/controllers/data/DialogImage.java b/src/main/java/noppes/npcs/controllers/data/DialogImage.java index e02957fda..068836a8c 100644 --- a/src/main/java/noppes/npcs/controllers/data/DialogImage.java +++ b/src/main/java/noppes/npcs/controllers/data/DialogImage.java @@ -1,8 +1,9 @@ package noppes.npcs.controllers.data; import net.minecraft.nbt.NBTTagCompound; +import noppes.npcs.scripted.interfaces.handler.data.IDialogImage; -public class DialogImage { +public class DialogImage implements IDialogImage { public int id; public String texture = ""; public int x = 0; @@ -21,6 +22,9 @@ public class DialogImage { public int imageType = 1; //0 - Default, 1 - Text, 2 - Option public int alignment = 0; + public DialogImage() { + } + public DialogImage(int id) { this.id = id; } @@ -62,4 +66,137 @@ public class DialogImage { imageType = compound.getInteger("ImageType"); alignment = compound.getInteger("Alignment"); } + + @Override + public int getId() { + return id; + } + + @Override + public void setTexture(String texture) { + this.texture = texture; + } + + @Override + public String getTexture() { + return texture; + } + + @Override + public void setPosition(int x, int y) { + this.x = x; + this.y = y; + } + + @Override + public int getX() { + return x; + } + + @Override + public int getY() { + return y; + } + + @Override + public void setWidthHeight(int width, int height) { + this.width = width; + this.height = height; + } + + @Override + public int getWidth() { + return width; + } + + @Override + public int getHeight() { + return height; + } + + @Override + public void setTextureOffset(int offsetX, int offsetY) { + this.textureX = offsetX; + this.textureY = offsetY; + } + + @Override + public int getTextureX() { + return textureX; + } + + @Override + public int getTextureY() { + return textureY; + } + + @Override + public void setColor(int color) { + this.color = color; + } + + @Override + public int getColor() { + return color; + } + + @Override + public void setSelectedColor(int color) { + this.selectedColor = color; + } + + @Override + public int getSelectedColor() { + return selectedColor; + } + + @Override + public void setScale(float scale) { + this.scale = scale; + } + + @Override + public float getScale() { + return scale; + } + + @Override + public void setAlpha(float alpha) { + this.alpha = alpha; + } + + @Override + public float getAlpha() { + return alpha; + } + + @Override + public void setRotation(float rotation) { + this.rotation = rotation; + } + + @Override + public float getRotation() { + return rotation; + } + + @Override + public void setImageType(int imageType) { + this.imageType = imageType; + } + + @Override + public int getImageType() { + return imageType; + } + + @Override + public void setAlignment(int alignment) { + this.alignment = alignment; + } + + @Override + public int getAlignment() { + return alignment; + } } diff --git a/src/main/java/noppes/npcs/entity/EntityNPCInterface.java b/src/main/java/noppes/npcs/entity/EntityNPCInterface.java index b637b4f17..ba0a1a3b9 100644 --- a/src/main/java/noppes/npcs/entity/EntityNPCInterface.java +++ b/src/main/java/noppes/npcs/entity/EntityNPCInterface.java @@ -75,11 +75,9 @@ import noppes.npcs.roles.RoleCompanion; import noppes.npcs.roles.RoleFollower; import noppes.npcs.roles.RoleInterface; import noppes.npcs.scripted.entity.ScriptNpc; -import noppes.npcs.scripted.event.ScriptEventAttack; -import noppes.npcs.scripted.event.ScriptEventDamaged; -import noppes.npcs.scripted.event.ScriptEventKilled; -import noppes.npcs.scripted.event.ScriptEventTarget; +import noppes.npcs.scripted.event.*; import noppes.npcs.scripted.interfaces.entity.ICustomNpc; +import noppes.npcs.scripted.interfaces.item.IItemStack; import noppes.npcs.util.GameProfileAlt; import cpw.mods.fml.common.registry.IEntityAdditionalSpawnData; @@ -211,10 +209,12 @@ public abstract class EntityNPCInterface extends EntityCreature implements IEnti @Override public void onUpdate(){ super.onUpdate(); - if(this.ticksExisted % 10 == 0) { - EventHooks.onNPCUpdate(this); + if (!isRemote()) { + if (this.ticksExisted % 10 == 0) { + EventHooks.onNPCUpdate(this); + } + this.timers.update(); } - this.timers.update(); } public void setWorld(World world){ @@ -234,7 +234,7 @@ public abstract class EntityNPCInterface extends EntityCreature implements IEnti if (stats.attackSpeed < 10){ par1Entity.hurtResistantTime = 0; } - if(par1Entity instanceof EntityLivingBase){ + if(par1Entity instanceof EntityLivingBase && !isRemote()){ ScriptEventAttack event = new ScriptEventAttack(f, (EntityLivingBase)par1Entity, false); if(EventHooks.onNPCMeleeAttack(this, f, (EntityLivingBase)par1Entity)) return false; @@ -542,20 +542,22 @@ public abstract class EntityNPCInterface extends EntityCreature implements IEnti public void setAttackTarget(EntityLivingBase entity){ if(entity instanceof EntityPlayer && ((EntityPlayer)entity).capabilities.disableDamage || entity != null && entity == getOwner()) return; - if(getAttackTarget() != entity && entity != null){ - ScriptEventTarget event = new ScriptEventTarget(entity); - if(EventHooks.onNPCTarget(this,entity)) - return; - - if(event.getTarget() == null) - entity = null; - else - entity = event.getTarget().getMCEntity(); - } - if (entity != null && entity != this && ai.onAttack != 3 && !isAttacking() && !isRemote()){ - Line line = advanced.getAttackLine(); - if(line != null) - saySurrounding(line.formatTarget(entity)); + if (!isRemote()) { + if (getAttackTarget() != entity && entity != null) { + ScriptEventTarget event = new ScriptEventTarget(entity); + if (EventHooks.onNPCTarget(this, entity)) + return; + + if (event.getTarget() == null) + entity = null; + else + entity = event.getTarget().getMCEntity(); + } + if (entity != null && entity != this && ai.onAttack != 3 && !isAttacking()) { + Line line = advanced.getAttackLine(); + if (line != null) + saySurrounding(line.formatTarget(entity)); + } } super.setAttackTarget(entity); @@ -568,16 +570,16 @@ public abstract class EntityNPCInterface extends EntityCreature implements IEnti updateTasks(); return; } - ScriptEventAttack event = new ScriptEventAttack(stats.pDamage, entity, true); - if(EventHooks.onNPCRangedAttack(this, f, (EntityLivingBase)entity)) - return; - for(int i = 0; i < this.stats.shotCount; i++) - { - EntityProjectile projectile = shoot(entity, stats.accuracy, proj, f == 1); - projectile.damage = event.getDamage(); + if (!isRemote()) { + ScriptEventAttack event = new ScriptEventAttack(stats.pDamage, entity, true); + if (EventHooks.onNPCRangedAttack(this, f, (EntityLivingBase) entity)) + return; + for (int i = 0; i < this.stats.shotCount; i++) { + EntityProjectile projectile = shoot(entity, stats.accuracy, proj, f == 1); + projectile.damage = event.getDamage(); + } + this.playSound(this.stats.fireSound, 2.0F, 1.0f); } - this.playSound(this.stats.fireSound, 2.0F, 1.0f); - } public EntityProjectile shoot(EntityLivingBase entity, int accuracy, ItemStack proj, boolean indirect){ @@ -1052,7 +1054,9 @@ public abstract class EntityNPCInterface extends EntityCreature implements IEnti if(jobInterface != null) jobInterface.reset(); - EventHooks.onNPCInit(this); + if (!isRemote()) { + EventHooks.onNPCInit(this); + } } public void onCollide() { @@ -1072,12 +1076,14 @@ public abstract class EntityNPCInterface extends EntityCreature implements IEnti if(list == null) return; - for (int i = 0; i < list.size(); ++i){ - Entity entity = (Entity)list.get(i); - if (entity.isEntityAlive()) { - EventHooks.onNPCCollide(this,entity); + if (!isRemote()) { + for (int i = 0; i < list.size(); ++i) { + Entity entity = (Entity) list.get(i); + if (entity.isEntityAlive()) { + EventHooks.onNPCCollide(this, entity); + } } - } + } } @@ -1202,12 +1208,26 @@ public abstract class EntityNPCInterface extends EntityCreature implements IEnti else if ((entity instanceof EntityThrowable)) attackingEntity = ((EntityThrowable) entity).getThrower(); - ScriptEventKilled result = new ScriptEventKilled(attackingEntity, damagesource); - if(EventHooks.onNPCKilled(this,damagesource,attackingEntity,result)) - return; + + int droppedXp = inventory.getDroppedXp(); + ArrayList droppedItems = inventory.getDroppedItems(damagesource); + if(!isRemote()){ - if(this.recentlyHit > 0) - inventory.dropStuff(entity, damagesource); + ScriptEventKilled result = new ScriptEventKilled(attackingEntity, damagesource); + NpcEvent.DiedEvent event = new NpcEvent.DiedEvent(this.wrappedNPC,damagesource,entity,droppedItems, droppedXp); + if(EventHooks.onNPCKilled(this, event, result)) + return; + + droppedItems.clear(); + for (IItemStack iItemStack : event.droppedItems) { + droppedItems.add(iItemStack.getMCItemStack()); + } + droppedXp = event.expDropped; + + if(this.recentlyHit > 0) { + inventory.dropItems(entity, droppedItems); + inventory.dropXp(entity, droppedXp); + } Line line = advanced.getKilledLine(); if(line != null) saySurrounding(line.formatTarget(attackingEntity)); diff --git a/src/main/java/noppes/npcs/scripted/NpcAPI.java b/src/main/java/noppes/npcs/scripted/NpcAPI.java index 5afa158e2..cbf368d20 100644 --- a/src/main/java/noppes/npcs/scripted/NpcAPI.java +++ b/src/main/java/noppes/npcs/scripted/NpcAPI.java @@ -207,13 +207,13 @@ public class NpcAPI extends AbstractNpcAPI { return getIItemStack(new ItemStack(item, size, damage)); } - public IWorld getIWorld(WorldServer world) { - ScriptWorld w = (ScriptWorld)worldCache.get(world.provider.dimensionId); + public IWorld getIWorld(World world) { + ScriptWorld w = worldCache.get(world.provider.dimensionId); if (w != null) { - w.world = world; + w.world.provider.dimensionId = world.provider.dimensionId; return w; } else { - worldCache.put(world.provider.dimensionId, w = ScriptWorld.createNew(world)); + worldCache.put(world.provider.dimensionId, w = ScriptWorld.createNew(world.provider.dimensionId)); return w; } } diff --git a/src/main/java/noppes/npcs/scripted/ScriptWorld.java b/src/main/java/noppes/npcs/scripted/ScriptWorld.java index 9c01455ee..77eb0cc8b 100644 --- a/src/main/java/noppes/npcs/scripted/ScriptWorld.java +++ b/src/main/java/noppes/npcs/scripted/ScriptWorld.java @@ -21,6 +21,7 @@ import net.minecraft.tileentity.TileEntitySign; import net.minecraft.util.Vec3; import net.minecraft.util.math.BlockPos; import net.minecraft.world.WorldServer; +import noppes.npcs.CustomNpcs; import noppes.npcs.NoppesUtilServer; import noppes.npcs.blocks.tiles.TileBigSign; import noppes.npcs.controllers.ScriptController; @@ -41,7 +42,15 @@ public class ScriptWorld implements IWorld { this.world = world; } - public static ScriptWorld createNew(WorldServer world) { + public static ScriptWorld createNew(int dimensionId) { + WorldServer[] worlds = CustomNpcs.getServer().worldServers; + + WorldServer world = worlds[0]; + for (WorldServer w : worlds) { + if (w.provider.dimensionId == dimensionId) { + world = w; + } + } return new ScriptWorld(world); } diff --git a/src/main/java/noppes/npcs/scripted/entity/ScriptEntity.java b/src/main/java/noppes/npcs/scripted/entity/ScriptEntity.java index 4bdbe7951..72e94f68c 100644 --- a/src/main/java/noppes/npcs/scripted/entity/ScriptEntity.java +++ b/src/main/java/noppes/npcs/scripted/entity/ScriptEntity.java @@ -29,6 +29,7 @@ import noppes.npcs.scripted.ScriptBlockPos; import noppes.npcs.scripted.ScriptWorld; import noppes.npcs.scripted.constants.EntityType; import noppes.npcs.scripted.interfaces.IParticle; +import noppes.npcs.scripted.interfaces.IWorld; import noppes.npcs.scripted.interfaces.entity.IEntity; import noppes.npcs.scripted.interfaces.INbt; import noppes.npcs.scripted.interfaces.IPos; @@ -755,7 +756,7 @@ public class ScriptEntity implements IEntity { } } - public ScriptWorld getWorld() { - return (ScriptWorld) NpcAPI.Instance().getIWorld((WorldServer) entity.worldObj); + public IWorld getWorld() { + return NpcAPI.Instance().getIWorld(entity.worldObj); } } \ No newline at end of file diff --git a/src/main/java/noppes/npcs/scripted/event/NpcEvent.java b/src/main/java/noppes/npcs/scripted/event/NpcEvent.java index e1a5c69f0..56f4ecff1 100644 --- a/src/main/java/noppes/npcs/scripted/event/NpcEvent.java +++ b/src/main/java/noppes/npcs/scripted/event/NpcEvent.java @@ -9,6 +9,7 @@ import cpw.mods.fml.common.eventhandler.Cancelable; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; import net.minecraft.util.DamageSource; import noppes.npcs.controllers.data.Dialog; import noppes.npcs.scripted.interfaces.*; @@ -19,6 +20,8 @@ import noppes.npcs.scripted.interfaces.entity.IPlayer; import noppes.npcs.scripted.interfaces.item.IItemStack; import noppes.npcs.scripted.NpcAPI; +import java.util.ArrayList; + public class NpcEvent extends CustomNPCsEvent { public final ICustomNpc npc; @@ -33,6 +36,10 @@ public class NpcEvent extends CustomNPCsEvent { super(npc); this.id = id; } + + public int getId(){ + return id; + } } public static class CollideEvent extends NpcEvent { @@ -49,13 +56,49 @@ public class NpcEvent extends CustomNPCsEvent { public final IDamageSource damageSource; public final IEntity source; public float damage; - public boolean clearTarget = false; + private boolean clearTarget = false; + + public boolean clear = false; + public final DamageSource damagesource; public DamagedEvent(ICustomNpc npc, Entity source, float damage, DamageSource damagesource) { super(npc); this.source = NpcAPI.Instance().getIEntity(source); this.damage = damage; this.damageSource = NpcAPI.Instance().getIDamageSource(damagesource); + + //Old CNPC Script compatibility + this.damagesource = damagesource; + } + + /** + * @return The source of the damage + */ + public IEntity getSource(){ + return source; + } + + /** + * @return Returns the damage value + */ + public float getDamage(){ + return damage; + } + + /** + * @param damage The new damage value + */ + public void setDamage(float damage){ + this.damage = damage; + } + + public void setClearTarget(boolean bo){ + this.clearTarget = bo; + this.clear = bo; + } + + public boolean getClearTarget(){ + return clearTarget && clear; } public String getType(){ @@ -103,13 +146,33 @@ public class NpcEvent extends CustomNPCsEvent { public IItemStack[] droppedItems; public int expDropped; - public DiedEvent(ICustomNpc npc, DamageSource damagesource, Entity entity) { + public DiedEvent(ICustomNpc npc, DamageSource damagesource, Entity entity, ArrayList droppedItems, int expDropped) { super(npc); this.damageSource = NpcAPI.Instance().getIDamageSource(damagesource); this.type = damagesource.damageType; this.source = NpcAPI.Instance().getIEntity(entity); + + ArrayList iItemStacks = new ArrayList<>(); + for (ItemStack itemStack : droppedItems) { + IItemStack iItemStack = NpcAPI.Instance().getIItemStack(itemStack); + iItemStacks.add(iItemStack); + } + this.droppedItems = iItemStacks.toArray(new IItemStack[0]); + this.expDropped = expDropped; } + /** + * @return The source of the damage + */ + public IEntity getSource(){ + return source; + } + + public IDamageSource getDamageSource() { return damageSource; } + + /** + * @return Returns the damage type + */ public String getType(){ return damageSource.getMCDamageSource().damageType; } @@ -129,14 +192,43 @@ public class NpcEvent extends CustomNPCsEvent { public final IPlayer player; public final int id; public final int optionId; - public final Dialog dialog; + public final Dialog dialogObj; + + public final int dialog; + public final int option; public DialogEvent(ICustomNpc npc, EntityPlayer player, int id, int optionId, Dialog dialog) { super(npc); this.player = (IPlayer)NpcAPI.Instance().getIEntity(player); this.id = id; this.optionId = optionId; - this.dialog = dialog; + this.dialogObj = dialog; + + this.dialog = id; + this.option = optionId; + } + + public IPlayer getPlayer() { + return player; + } + + public Dialog getDialog() { + return dialogObj; + } + + public int getDialogId() { + return dialog; + } + + public int getOptionId() { + return option; + } + + /** + * @deprecated + */ + public boolean isClosing(){ + return true; } } @@ -173,6 +265,17 @@ public class NpcEvent extends CustomNPCsEvent { super(npc); this.entity = (IEntityLivingBase) NpcAPI.Instance().getIEntity(entity); } + + /** + * @return The target source + */ + public IEntityLivingBase getTarget(){ + return entity; + } + + public void setTarget(IEntityLivingBase entity){ + this.entity = entity; + } } public static class UpdateEvent extends NpcEvent { @@ -186,4 +289,47 @@ public class NpcEvent extends CustomNPCsEvent { super(npc); } } + + public static class AttackEvent extends NpcEvent{ + private float damage; + private IEntityLivingBase target; + private boolean isRanged; + + public AttackEvent(ICustomNpc npc, float damage, EntityLivingBase target, boolean isRanged){ + super(npc); + this.damage = damage; + this.isRanged = isRanged; + this.target = (IEntityLivingBase) NpcAPI.Instance().getIEntity(target); + } + + /** + * @return The source of the damage + * @deprecated + */ + public IEntityLivingBase getTarget(){ + return target; + } + + + /** + * @return Returns the damage value + */ + public float getDamage(){ + return damage; + } + + /** + * @param damage The new damage value + */ + public void setDamage(float damage){ + this.damage = damage; + } + + /** + * @return Returns the damage type + */ + public boolean isRange(){ + return isRanged; + } + } } diff --git a/src/main/java/noppes/npcs/scripted/interfaces/AbstractNpcAPI.java b/src/main/java/noppes/npcs/scripted/interfaces/AbstractNpcAPI.java index 81d8ad3c3..2a169f994 100644 --- a/src/main/java/noppes/npcs/scripted/interfaces/AbstractNpcAPI.java +++ b/src/main/java/noppes/npcs/scripted/interfaces/AbstractNpcAPI.java @@ -77,7 +77,7 @@ public abstract class AbstractNpcAPI { public abstract IItemStack getIItemStack(ItemStack var1); - public abstract IWorld getIWorld(WorldServer var1); + public abstract IWorld getIWorld(World var1); public abstract IWorld getIWorld(int var1); diff --git a/src/main/java/noppes/npcs/scripted/interfaces/entity/IEntity.java b/src/main/java/noppes/npcs/scripted/interfaces/entity/IEntity.java index 0c266d515..2d35aaf9c 100644 --- a/src/main/java/noppes/npcs/scripted/interfaces/entity/IEntity.java +++ b/src/main/java/noppes/npcs/scripted/interfaces/entity/IEntity.java @@ -10,6 +10,7 @@ import noppes.npcs.scripted.ScriptWorld; import noppes.npcs.scripted.interfaces.INbt; import noppes.npcs.scripted.interfaces.IParticle; import noppes.npcs.scripted.interfaces.IPos; +import noppes.npcs.scripted.interfaces.IWorld; import noppes.npcs.scripted.interfaces.item.IItemStack; public interface IEntity { @@ -342,5 +343,5 @@ public interface IEntity { void storeAsClone(int tab, String name); - ScriptWorld getWorld(); + IWorld getWorld(); } diff --git a/src/main/java/noppes/npcs/scripted/interfaces/handler/data/IDialog.java b/src/main/java/noppes/npcs/scripted/interfaces/handler/data/IDialog.java index e7e5bb6cc..21f8cf9e5 100644 --- a/src/main/java/noppes/npcs/scripted/interfaces/handler/data/IDialog.java +++ b/src/main/java/noppes/npcs/scripted/interfaces/handler/data/IDialog.java @@ -4,7 +4,6 @@ // package noppes.npcs.scripted.interfaces.handler.data; - import java.util.List; public interface IDialog { @@ -50,4 +49,90 @@ public interface IDialog { String getSound(); void save(); + + void setColor(int color); + + int getColor(); + + void setTitleColor(int titleColor); + + int getTitleColor(); + + void renderGradual(boolean gradual); + + boolean renderGradual(); + + void showPreviousBlocks(boolean show); + + boolean showPreviousBlocks(); + + void showOptionLine(boolean show); + + boolean showOptionLine(); + + void setTextSound(String textSound); + + String getTextSound(); + + void setTextPitch(float textPitch); + + float getTextPitch(); + + void setTitlePos(int pos); + + int getTitlePos(); + + void setNPCScale(float scale); + + float getNpcScale(); + + void setNpcOffset(int offsetX, int offsetY); + + int getNpcOffsetX(); + + int getNpcOffsetY(); + + void textWidthHeight(int textWidth, int textHeight); + + int getTextWidth(); + + int setTextHeight(); + + void setTextOffset(int offsetX, int offsetY); + + int getTextOffsetX(); + + int getTextOffsetY(); + + void setTitleOffset(int offsetX, int offsetY); + + int getTitleOffsetX(); + + int getTitleOffsetY(); + + void setOptionOffset(int offsetX, int offsetY); + + int getOptionOffsetX(); + + int getOptionOffsetY(); + + void setOptionSpacing(int spaceX, int spaceY); + + int getOptionSpaceX(); + + int getOptionSpaceY(); + + void addImage(int id, IDialogImage image); + + IDialogImage getImage(int id); + + IDialogImage createImage(); + + IDialogImage[] getImages(); + + boolean hasImage(int id); + + void removeImage(int id); + + void clearImages(); }