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 */ /** 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>>(); 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) public ItemMiningLaser(int id)
{ {
super(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 @Override
public void onUpdate(ItemStack itemStack, World world, Entity entity, int slot, boolean currentHeldItem) 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 //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 increase break time longer the laser has been running
//TODO match hardness of block for break time //TODO match hardness of block for break time
//TODO add audio //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 playerPosition = Vec3.createVectorHelper(player.posX, player.posY + player.getEyeHeight(), player.posZ);
Vec3 playerLook = RayTraceHelper.getLook(player, 1.0f); Vec3 playerLook = RayTraceHelper.getLook(player, 1.0f);
Vec3 p = Vec3.createVectorHelper(playerPosition.xCoord + playerLook.xCoord, playerPosition.yCoord + playerLook.yCoord, playerPosition.zCoord + playerLook.zCoord); 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); 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); 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) if (hit != null)
{ {
LaserEvent event = new LaserEvent.LaserFiredPlayerEvent(player, hit, stack); LaserEvent event = new LaserEvent.LaserFiredPlayerEvent(player, hit, stack);
@ -148,7 +148,7 @@ public class ItemMiningLaser extends ItemEnergyTool
} }
playerViewOffset = hit.hitVec; playerViewOffset = hit.hitVec;
}
//TODO make beam brighter the longer it has been used //TODO make beam brighter the longer it has been used
//TODO adjust the laser for the end of the gun //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 x = (float) (MathHelper.cos((float) (player.rotationYawHead * 0.0174532925)) * (-.4) - MathHelper.sin((float) (player.rotationYawHead * 0.0174532925)) * (-.1));
@ -156,29 +156,25 @@ public class ItemMiningLaser extends ItemEnergyTool
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, -.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); ResonantInduction.proxy.renderBeam(player.worldObj, (IVector3) new Vector3(p).translate(new Vector3(x, -.45, z)), (IVector3) new Vector3(playerViewOffset), Color.ORANGE, 1);
} }
}
} }
@Override @Override
public ItemStack onItemRightClick(ItemStack itemStack, World par2World, EntityPlayer player) public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer player)
{ {
if (!player.isSneaking()) if (!player.isSneaking())
{ {
if (player.capabilities.isCreativeMode || this.getEnergy(itemStack) > this.joulesPerTick) player.addChatMessage("Laser turns on Client:" + world.isRemote);
{
player.setItemInUse(itemStack, this.getMaxItemUseDuration(itemStack)); player.setItemInUse(itemStack, this.getMaxItemUseDuration(itemStack));
} }
} return super.onItemRightClick(itemStack, world, player);
else
{
}
return itemStack;
} }
@Override @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)) if (miningMap.containsKey(player))
{ {
Pair<Vector3, Integer> vec = miningMap.get(player); Pair<Vector3, Integer> vec = miningMap.get(player);
@ -188,6 +184,26 @@ public class ItemMiningLaser extends ItemEnergyTool
} }
miningMap.remove(player); 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 universalelectricity.api.item.IVoltageItem;
import calclavia.lib.render.EnumColor; import calclavia.lib.render.EnumColor;
import calclavia.lib.utility.LanguageUtility; import calclavia.lib.utility.LanguageUtility;
import calclavia.lib.utility.nbt.NBTUtility;
/** Prefab for all eletric based tools /** Prefab for all eletric based tools
* *
@ -61,7 +62,6 @@ public class ItemEnergyTool extends ItemTool implements IEnergyItem, IVoltageIte
{ {
color = "\u00a76"; 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))); 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 @Override
public void setEnergy(ItemStack itemStack, long joules) 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); 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) public long getEnergySpace(ItemStack itemStack)
@ -127,13 +122,7 @@ public class ItemEnergyTool extends ItemTool implements IEnergyItem, IVoltageIte
@Override @Override
public long getEnergy(ItemStack itemStack) public long getEnergy(ItemStack itemStack)
{ {
if (itemStack.getTagCompound() == null) return NBTUtility.getNBTTagCompound(itemStack).getLong("electricity");
{
itemStack.setTagCompound(new NBTTagCompound());
}
long energyStored = itemStack.getTagCompound().getLong("electricity");
return energyStored;
} }
@Override @Override
@ -155,22 +144,13 @@ public class ItemEnergyTool extends ItemTool implements IEnergyItem, IVoltageIte
public static ItemStack setTier(ItemStack itemStack, int tier) public static ItemStack setTier(ItemStack itemStack, int tier)
{ {
if (itemStack.getTagCompound() == null) NBTUtility.getNBTTagCompound(itemStack).setByte("tier", (byte) tier);
{
itemStack.setTagCompound(new NBTTagCompound());
}
itemStack.getTagCompound().setByte("tier", (byte) tier);
return itemStack; return itemStack;
} }
public static byte getTier(ItemStack itemStack) public static byte getTier(ItemStack itemStack)
{ {
if (itemStack.getTagCompound() == null) return NBTUtility.getNBTTagCompound(itemStack).getByte("tier");
{
itemStack.setTagCompound(new NBTTagCompound());
}
return itemStack.getTagCompound().getByte("tier");
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")

View file

@ -77,12 +77,6 @@ public class ItemTool extends Item
return itemStack; return itemStack;
} }
@Override
public boolean shouldPassSneakingClickToBlock(World world, int x, int y, int z)
{
return true;
}
public int getMode(ItemStack itemStack) public int getMode(ItemStack itemStack)
{ {
return NBTUtility.getNBTTagCompound(itemStack).getInteger("mode"); return NBTUtility.getNBTTagCompound(itemStack).getInteger("mode");