From 8ca307073552c9c2e8f6ed6b12763ea271dd9018 Mon Sep 17 00:00:00 2001 From: Robert S Date: Mon, 5 May 2014 17:14:18 -0400 Subject: [PATCH] Created some basic tool prefabs which includes support for energy & tool modes --- .../electrical/laser/gun/ItemMiningLaser.java | 102 +++------- .../core/prefab/items/ItemEnergyTool.java | 181 ++++++++++++++++++ .../core/prefab/items/ItemTool.java | 91 +++++++++ 3 files changed, 300 insertions(+), 74 deletions(-) create mode 100644 src/main/scala/resonantinduction/core/prefab/items/ItemEnergyTool.java create mode 100644 src/main/scala/resonantinduction/core/prefab/items/ItemTool.java diff --git a/electrical/src/main/scala/resonantinduction/electrical/laser/gun/ItemMiningLaser.java b/electrical/src/main/scala/resonantinduction/electrical/laser/gun/ItemMiningLaser.java index 9ace997d..1f8566a9 100644 --- a/electrical/src/main/scala/resonantinduction/electrical/laser/gun/ItemMiningLaser.java +++ b/electrical/src/main/scala/resonantinduction/electrical/laser/gun/ItemMiningLaser.java @@ -4,17 +4,13 @@ import java.awt.Color; import java.util.HashMap; import java.util.List; -import resonantinduction.core.ResonantInduction; import net.minecraft.block.Block; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.entity.Entity; -import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.EnumAction; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.potion.Potion; -import net.minecraft.potion.PotionEffect; import net.minecraft.util.DamageSource; import net.minecraft.util.EnumMovingObjectType; import net.minecraft.util.MathHelper; @@ -23,8 +19,9 @@ import net.minecraft.util.Vec3; import net.minecraft.world.World; import net.minecraftforge.common.ForgeDirection; import net.minecraftforge.common.MinecraftForge; +import resonantinduction.core.ResonantInduction; +import resonantinduction.core.prefab.items.ItemEnergyTool; import universalelectricity.api.UniversalClass; -import universalelectricity.api.item.ItemElectric; import universalelectricity.api.vector.IVector3; import universalelectricity.api.vector.Vector3; import calclavia.api.resonantinduction.electrical.LaserEvent; @@ -47,16 +44,18 @@ import cpw.mods.fml.relauncher.SideOnly; * * @author DarkGuardsman */ @UniversalClass -public class ItemMiningLaser extends ItemElectric +public class ItemMiningLaser extends ItemEnergyTool { - long batterySize = 500000; + /** Cost per tick of using the item */ float wattPerShot = 1; + /** Damage to entities hit by the laser */ float damageToEntities = 3.3f; + /** Range of the laser ray trace */ int blockRange = 50; - int firingDelay = 5; + /** Time to break a single block */ int breakTime = 15; - boolean createLava = true, setFire = true; + /** Map of players and how long they have focused the laser on a single block */ HashMap> miningMap = new HashMap>(); public ItemMiningLaser(int id) @@ -81,41 +80,23 @@ public class ItemMiningLaser extends ItemElectric } @Override - public void onUpdate(ItemStack itemStack, World par2World, Entity entity, int par4, boolean par5) + public void onUpdate(ItemStack itemStack, World world, Entity entity, int slot, boolean currentHeldItem) { - //Slow any entity that carries this down as a side effect of using heavy mining gear - if (entity instanceof EntityLivingBase) + //Remove player from mining map if he puts the laser gun away + if (!currentHeldItem && entity instanceof EntityPlayer && this.miningMap.containsKey(entity)) { - boolean flag = entity instanceof EntityPlayer && ((EntityPlayer) entity).capabilities.isCreativeMode; - - if (!flag) - { - //((EntityLivingBase) entity).addPotionEffect(new PotionEffect(Potion.moveSlowdown.id, 5, 0)); - } - else - { - //((EntityPlayer) entity).setItemInUse(itemStack, this.getMaxItemUseDuration(itemStack)); - } - } - } - - @Override - public void onCreated(ItemStack stack, World par2World, EntityPlayer entityPlayer) - { - this.setEnergy(stack, 0); - if (stack.getTagCompound() == null) - { - stack.setTagCompound(new NBTTagCompound()); - } - if (entityPlayer != null) - { - stack.getTagCompound().setString("Creator", entityPlayer.username); + this.miningMap.remove(entity); } } @Override public void onUsingItemTick(ItemStack stack, EntityPlayer player, int count) { + //Small delay to prevent unwanted usage of the item + //TODO increase this delay to simulate warm up time + //TODO increase break time longer the laser has been running + //TODO match hardness of block for break time + //TODO add audio if (count > 5) { Vec3 playerPosition = Vec3.createVectorHelper(player.posX, player.posY + player.getEyeHeight(), player.posZ); @@ -125,7 +106,6 @@ public class ItemMiningLaser extends ItemElectric Vec3 playerViewOffset = Vec3.createVectorHelper(playerPosition.xCoord + playerLook.xCoord * blockRange, playerPosition.yCoord + playerLook.yCoord * blockRange, playerPosition.zCoord + playerLook.zCoord * blockRange); MovingObjectPosition hit = RayTraceHelper.do_rayTraceFromEntity(player, new Vector3().toVec3(), blockRange, true); - //TODO fix sound if (hit != null) { LaserEvent event = new LaserEvent.LaserFiredPlayerEvent(player, hit, stack); @@ -189,9 +169,16 @@ public class ItemMiningLaser extends ItemElectric @Override public ItemStack onItemRightClick(ItemStack itemStack, World par2World, EntityPlayer player) { - if (player.capabilities.isCreativeMode || this.getEnergy(itemStack) > this.wattPerShot) + if (!player.isSneaking()) { - player.setItemInUse(itemStack, this.getMaxItemUseDuration(itemStack)); + if (player.capabilities.isCreativeMode || this.getEnergy(itemStack) > this.wattPerShot) + { + player.setItemInUse(itemStack, this.getMaxItemUseDuration(itemStack)); + } + } + else + { + } return itemStack; } @@ -202,45 +189,12 @@ public class ItemMiningLaser extends ItemElectric if (miningMap.containsKey(player)) { Pair vec = miningMap.get(player); - if(vec != null && vec.left() != null) + if (vec != null && vec.left() != null) { player.worldObj.destroyBlockInWorldPartially(player.entityId, vec.left().intX(), vec.left().intY(), vec.left().intZ(), -1); } - miningMap.remove(player); + miningMap.remove(player); } } - @Override - public ItemStack onEaten(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer) - { - return par1ItemStack; - } - - @SideOnly(Side.CLIENT) - @Override - public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean par4) - { - super.addInformation(stack, player, list, par4); - if (stack.getTagCompound() == null) - { - stack.setTagCompound(new NBTTagCompound()); - } - String creator = stack.getTagCompound().getString("Creator"); - if (!creator.equalsIgnoreCase("creative") && creator != "") - { - list.add("Created by: " + creator); - } - else if (creator.equalsIgnoreCase("creative")) - { - list.add("Created by Magic Dwarfs"); - } - - } - - @Override - public long getEnergyCapacity(ItemStack theItem) - { - return this.batterySize; - } - } diff --git a/src/main/scala/resonantinduction/core/prefab/items/ItemEnergyTool.java b/src/main/scala/resonantinduction/core/prefab/items/ItemEnergyTool.java new file mode 100644 index 00000000..41d23957 --- /dev/null +++ b/src/main/scala/resonantinduction/core/prefab/items/ItemEnergyTool.java @@ -0,0 +1,181 @@ +package resonantinduction.core.prefab.items; + +import java.util.List; + +import calclavia.lib.render.EnumColor; +import calclavia.lib.utility.LanguageUtility; +import net.minecraft.creativetab.CreativeTabs; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.world.World; +import resonantinduction.electrical.battery.TileBattery; +import universalelectricity.api.CompatibilityModule; +import universalelectricity.api.UniversalElectricity; +import universalelectricity.api.energy.UnitDisplay; +import universalelectricity.api.energy.UnitDisplay.Unit; +import universalelectricity.api.item.IEnergyItem; +import universalelectricity.api.item.IVoltageItem; + +/** Prefab for all eletric based tools + * + * @author DarkGurdsman */ +public class ItemEnergyTool extends ItemTool implements IEnergyItem, IVoltageItem +{ + protected long batterySize = 500000; + protected boolean hasTier = false; + protected boolean showEnergy = true; + + public ItemEnergyTool(int id) + { + super(id); + } + + @SuppressWarnings("unchecked") + @Override + public void addInformation(ItemStack itemStack, EntityPlayer entityPlayer, List list, boolean par4) + { + if (hasTier) + list.add(LanguageUtility.getLocal("tooltip.tier") + ": " + (getTier(itemStack) + 1)); + + if (showEnergy) + { + String color = ""; + long joules = this.getEnergy(itemStack); + + if (joules <= this.getEnergyCapacity(itemStack) / 3) + { + color = "\u00a74"; + } + else if (joules > this.getEnergyCapacity(itemStack) * 2 / 3) + { + color = "\u00a72"; + } + else + { + color = "\u00a76"; + } + itemStack.getItemDamageForDisplay(); + list.add(LanguageUtility.getLocal("tooltip.battery.energy").replace("%0", color).replace("%1", EnumColor.GREY.toString()).replace("%v0", UnitDisplay.getDisplayShort(joules, Unit.JOULES)).replace("%v1", UnitDisplay.getDisplayShort(this.getEnergyCapacity(itemStack), Unit.JOULES))); + } + } + + @Override + public void onCreated(ItemStack itemStack, World world, EntityPlayer player) + { + super.onCreated(itemStack, world, player); + this.setEnergy(itemStack, 0); + } + + @Override + public long recharge(ItemStack itemStack, long energy, boolean doReceive) + { + long rejectedElectricity = Math.max((this.getEnergy(itemStack) + energy) - this.getEnergyCapacity(itemStack), 0); + long energyToReceive = Math.min(energy - rejectedElectricity, getTransferRate(itemStack)); + + if (doReceive) + { + this.setEnergy(itemStack, this.getEnergy(itemStack) + energyToReceive); + } + + return energyToReceive; + } + + @Override + public long discharge(ItemStack itemStack, long energy, boolean doTransfer) + { + long energyToExtract = Math.min(Math.min(this.getEnergy(itemStack), energy), getTransferRate(itemStack)); + + if (doTransfer) + { + this.setEnergy(itemStack, this.getEnergy(itemStack) - energyToExtract); + } + + return energyToExtract; + } + + @Override + public long getVoltage(ItemStack itemStack) + { + return UniversalElectricity.DEFAULT_VOLTAGE; + } + + @Override + public void setEnergy(ItemStack itemStack, long joules) + { + if (itemStack.getTagCompound() == null) + { + itemStack.setTagCompound(new NBTTagCompound()); + } + + long electricityStored = Math.max(Math.min(joules, this.getEnergyCapacity(itemStack)), 0); + itemStack.getTagCompound().setLong("electricity", electricityStored); + } + + public long getTransfer(ItemStack itemStack) + { + return this.getEnergyCapacity(itemStack) - this.getEnergy(itemStack); + } + + /** Gets the energy stored in the item. Energy is stored using item NBT */ + @Override + public long getEnergy(ItemStack itemStack) + { + if (itemStack.getTagCompound() == null) + { + itemStack.setTagCompound(new NBTTagCompound()); + } + + long energyStored = itemStack.getTagCompound().getLong("electricity"); + return energyStored; + } + + @Override + public int getDisplayDamage(ItemStack stack) + { + return (int) (100 - ((double) this.getEnergy(stack) / (double) getEnergyCapacity(stack)) * 100); + } + + @Override + public long getEnergyCapacity(ItemStack theItem) + { + return TileBattery.getEnergyForTier(getTier(theItem)); + } + + public long getTransferRate(ItemStack itemStack) + { + return this.getEnergyCapacity(itemStack) / 100; + } + + public static ItemStack setTier(ItemStack itemStack, byte tier) + { + if (itemStack.getTagCompound() == null) + { + itemStack.setTagCompound(new NBTTagCompound()); + } + + itemStack.getTagCompound().setByte("tier", tier); + return itemStack; + } + + public static byte getTier(ItemStack itemStack) + { + if (itemStack.getTagCompound() == null) + { + itemStack.setTagCompound(new NBTTagCompound()); + } + return itemStack.getTagCompound().getByte("tier"); + } + + @SuppressWarnings("unchecked") + @Override + public void getSubItems(int par1, CreativeTabs par2CreativeTabs, List par3List) + { + for (byte tier = 0; tier <= TileBattery.MAX_TIER; tier++) + { + par3List.add(CompatibilityModule.getItemWithCharge(setTier(new ItemStack(this), tier), 0)); + par3List.add(CompatibilityModule.getItemWithCharge(setTier(new ItemStack(this), tier), TileBattery.getEnergyForTier(tier))); + } + } + +} diff --git a/src/main/scala/resonantinduction/core/prefab/items/ItemTool.java b/src/main/scala/resonantinduction/core/prefab/items/ItemTool.java new file mode 100644 index 00000000..c345fb26 --- /dev/null +++ b/src/main/scala/resonantinduction/core/prefab/items/ItemTool.java @@ -0,0 +1,91 @@ +package resonantinduction.core.prefab.items; + +import java.util.List; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.world.World; +import calclavia.components.tool.ToolMode; +import calclavia.lib.utility.LanguageUtility; +import calclavia.lib.utility.nbt.NBTUtility; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; + +/** Prefab for all tool based items + * + * @author Darkguardsman */ +public class ItemTool extends Item +{ + protected boolean hasModes = false; + protected String[] toolModes = new String[] { "Remove" }; + + public ItemTool(int par1) + { + super(par1); + } + + @Override + public void onCreated(ItemStack stack, World par2World, EntityPlayer entityPlayer) + { + //Save who crafted the tool + if (entityPlayer != null) + { + NBTUtility.getNBTTagCompound(stack).setString("Creator", entityPlayer.username); + } + } + + @SideOnly(Side.CLIENT) + @Override + public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean par4) + { + super.addInformation(stack, player, list, par4); + + //Item creator + String creator = NBTUtility.getNBTTagCompound(stack).getString("Creator"); + if (!creator.equalsIgnoreCase("creative") && creator != "") + { + list.add("Created by: " + creator); + } + else if (creator.equalsIgnoreCase("creative")) + { + list.add("Created by Magic Dwarfs"); + } + + } + + @Override + public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer player) + { + if (hasModes && toolModes != null && toolModes.length > 1) + { + //TODO replace with middle mouse wheel + //Changes the mod of the tool + if (player.isSneaking()) + { + setMode(itemStack, (getMode(itemStack) + 1) % toolModes.length); + + if (!world.isRemote) + player.addChatMessage("Set tool mode to: " + toolModes[getMode(itemStack)]); + + } + } + return itemStack; + } + + @Override + public boolean shouldPassSneakingClickToBlock(World world, int x, int y, int z) + { + return true; + } + + public int getMode(ItemStack itemStack) + { + return NBTUtility.getNBTTagCompound(itemStack).getInteger("mode"); + } + + public void setMode(ItemStack itemStack, int mode) + { + NBTUtility.getNBTTagCompound(itemStack).setInteger("mode", mode); + } +}