Fixed sync issues with laser when discharging its battery

This commit is contained in:
Robert S 2014-05-05 22:29:17 -04:00
parent ecf0ea78ac
commit aa7e8fdde7
3 changed files with 57 additions and 67 deletions

View file

@ -50,32 +50,28 @@ public class ItemMiningLaser extends ItemEnergyTool
/** Map of players and how long they have focused the laser on a single block */
HashMap<EntityPlayer, Pair<Vector3, Integer>> miningMap = new HashMap<EntityPlayer, Pair<Vector3, Integer>>();
/** Used to track energy used while the player uses the laser rather then direct editing the nbt */
HashMap<EntityPlayer, Long> energyUsedMap = new HashMap<EntityPlayer, Long>();
public ItemMiningLaser(int id)
{
super(id);
}
@Override
public EnumAction getItemUseAction(ItemStack par1ItemStack)
{
return EnumAction.bow;
}
@Override
public int getMaxItemUseDuration(ItemStack par1ItemStack)
{
//TODO change render of the laser too show it slowly over heat, when it over heats eg gets to max use damage the player, and tool
return 1000;
}
@Override
public void onUpdate(ItemStack itemStack, World world, Entity entity, int slot, boolean currentHeldItem)
{
//Remove player from mining map if he puts the laser gun away
if (!currentHeldItem && entity instanceof EntityPlayer && this.miningMap.containsKey(entity))
if (entity instanceof EntityPlayer)
{
this.miningMap.remove(entity);
EntityPlayer player = (EntityPlayer) entity;
if (!currentHeldItem)
{
if (this.miningMap.containsKey(player))
{
this.miningMap.remove(player);
}
}
}
}
@ -87,11 +83,8 @@ public class ItemMiningLaser extends ItemEnergyTool
//TODO increase break time longer the laser has been running
//TODO match hardness of block for break time
//TODO add audio
if ((player.capabilities.isCreativeMode || discharge(stack, joulesPerTick, false) > joulesPerTick) && count > 20)
if (count > 5 && (player.capabilities.isCreativeMode || discharge(stack, joulesPerTick, false) >= joulesPerTick && (!this.energyUsedMap.containsKey(player) || this.energyUsedMap.get(player) <= this.getEnergy(stack))))
{
if(!player.capabilities.isCreativeMode)
discharge(stack, joulesPerTick, true);
Vec3 playerPosition = Vec3.createVectorHelper(player.posX, player.posY + player.getEyeHeight(), player.posZ);
Vec3 playerLook = RayTraceHelper.getLook(player, 1.0f);
Vec3 p = Vec3.createVectorHelper(playerPosition.xCoord + playerLook.xCoord, playerPosition.yCoord + playerLook.yCoord, playerPosition.zCoord + playerLook.zCoord);
@ -99,6 +92,13 @@ public class ItemMiningLaser extends ItemEnergyTool
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);
if (!player.capabilities.isCreativeMode)
{
long energyUsed = this.energyUsedMap.containsKey(player) ? this.energyUsedMap.get(player) : 0;
energyUsed += joulesPerTick;
this.energyUsedMap.put(player, energyUsed);
}
if (hit != null)
{
LaserEvent event = new LaserEvent.LaserFiredPlayerEvent(player, hit, stack);
@ -148,37 +148,33 @@ public class ItemMiningLaser extends ItemEnergyTool
}
playerViewOffset = hit.hitVec;
//TODO make beam brighter the longer it has been used
//TODO adjust the laser for the end of the gun
float x = (float) (MathHelper.cos((float) (player.rotationYawHead * 0.0174532925)) * (-.4) - MathHelper.sin((float) (player.rotationYawHead * 0.0174532925)) * (-.1));
float z = (float) (MathHelper.sin((float) (player.rotationYawHead * 0.0174532925)) * (-.4) + MathHelper.cos((float) (player.rotationYawHead * 0.0174532925)) * (-.1));
ResonantInduction.proxy.renderBeam(player.worldObj, (IVector3) new Vector3(p).translate(new Vector3(x, -.25, z)), (IVector3) new Vector3(playerViewOffset), Color.ORANGE, 1);
ResonantInduction.proxy.renderBeam(player.worldObj, (IVector3) new Vector3(p).translate(new Vector3(x, -.45, z)), (IVector3) new Vector3(playerViewOffset), Color.ORANGE, 1);
}
//TODO make beam brighter the longer it has been used
//TODO adjust the laser for the end of the gun
float x = (float) (MathHelper.cos((float) (player.rotationYawHead * 0.0174532925)) * (-.4) - MathHelper.sin((float) (player.rotationYawHead * 0.0174532925)) * (-.1));
float z = (float) (MathHelper.sin((float) (player.rotationYawHead * 0.0174532925)) * (-.4) + MathHelper.cos((float) (player.rotationYawHead * 0.0174532925)) * (-.1));
ResonantInduction.proxy.renderBeam(player.worldObj, (IVector3) new Vector3(p).translate(new Vector3(x, -.25, z)), (IVector3) new Vector3(playerViewOffset), Color.ORANGE, 1);
ResonantInduction.proxy.renderBeam(player.worldObj, (IVector3) new Vector3(p).translate(new Vector3(x, -.45, z)), (IVector3) new Vector3(playerViewOffset), Color.ORANGE, 1);
}
}
@Override
public ItemStack onItemRightClick(ItemStack itemStack, World par2World, EntityPlayer player)
public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer player)
{
if (!player.isSneaking())
{
if (player.capabilities.isCreativeMode || this.getEnergy(itemStack) > this.joulesPerTick)
{
player.setItemInUse(itemStack, this.getMaxItemUseDuration(itemStack));
}
player.addChatMessage("Laser turns on Client:" + world.isRemote);
player.setItemInUse(itemStack, this.getMaxItemUseDuration(itemStack));
}
else
{
}
return itemStack;
return super.onItemRightClick(itemStack, world, player);
}
@Override
public void onPlayerStoppedUsing(ItemStack par1ItemStack, World par2World, EntityPlayer player, int par4)
public void onPlayerStoppedUsing(ItemStack stack, World world, EntityPlayer player, int par4)
{
player.addChatMessage("Laser turns off Client:" + world.isRemote);
if (miningMap.containsKey(player))
{
Pair<Vector3, Integer> vec = miningMap.get(player);
@ -188,6 +184,26 @@ public class ItemMiningLaser extends ItemEnergyTool
}
miningMap.remove(player);
}
if (this.energyUsedMap.containsKey(player))
discharge(stack, this.energyUsedMap.get(player), true);
}
@Override
public ItemStack onEaten(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
{
return par1ItemStack;
}
@Override
public int getMaxItemUseDuration(ItemStack par1ItemStack)
{
return 72000;
}
@Override
public EnumAction getItemUseAction(ItemStack par1ItemStack)
{
return EnumAction.bow;
}
}

View file

@ -16,6 +16,7 @@ import universalelectricity.api.item.IEnergyItem;
import universalelectricity.api.item.IVoltageItem;
import calclavia.lib.render.EnumColor;
import calclavia.lib.utility.LanguageUtility;
import calclavia.lib.utility.nbt.NBTUtility;
/** Prefab for all eletric based tools
*
@ -61,7 +62,6 @@ public class ItemEnergyTool extends ItemTool implements IEnergyItem, IVoltageIte
{
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)));
}
}
@ -109,13 +109,8 @@ public class ItemEnergyTool extends ItemTool implements IEnergyItem, IVoltageIte
@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);
NBTUtility.getNBTTagCompound(itemStack).setLong("electricity", electricityStored);
}
public long getEnergySpace(ItemStack itemStack)
@ -127,13 +122,7 @@ public class ItemEnergyTool extends ItemTool implements IEnergyItem, IVoltageIte
@Override
public long getEnergy(ItemStack itemStack)
{
if (itemStack.getTagCompound() == null)
{
itemStack.setTagCompound(new NBTTagCompound());
}
long energyStored = itemStack.getTagCompound().getLong("electricity");
return energyStored;
return NBTUtility.getNBTTagCompound(itemStack).getLong("electricity");
}
@Override
@ -155,22 +144,13 @@ public class ItemEnergyTool extends ItemTool implements IEnergyItem, IVoltageIte
public static ItemStack setTier(ItemStack itemStack, int tier)
{
if (itemStack.getTagCompound() == null)
{
itemStack.setTagCompound(new NBTTagCompound());
}
itemStack.getTagCompound().setByte("tier", (byte) tier);
NBTUtility.getNBTTagCompound(itemStack).setByte("tier", (byte) tier);
return itemStack;
}
public static byte getTier(ItemStack itemStack)
{
if (itemStack.getTagCompound() == null)
{
itemStack.setTagCompound(new NBTTagCompound());
}
return itemStack.getTagCompound().getByte("tier");
return NBTUtility.getNBTTagCompound(itemStack).getByte("tier");
}
@SuppressWarnings("unchecked")

View file

@ -77,12 +77,6 @@ public class ItemTool extends Item
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");