Fixed the Drill sometimes producing Ghost Blocks and added lots of Comments

This commit is contained in:
Ellpeck 2015-07-17 05:11:49 +02:00
parent cb584b6e93
commit 8def70fa04

View file

@ -13,6 +13,7 @@ import ellpeck.actuallyadditions.util.ModUtil;
import ellpeck.actuallyadditions.util.WorldUtil; import ellpeck.actuallyadditions.util.WorldUtil;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.material.Material; import net.minecraft.block.material.Material;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.IIconRegister; import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.enchantment.Enchantment; import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.enchantment.EnchantmentHelper;
@ -21,12 +22,15 @@ import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.SharedMonsterAttributes; import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.attributes.AttributeModifier; import net.minecraft.entity.ai.attributes.AttributeModifier;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.init.Blocks; import net.minecraft.init.Blocks;
import net.minecraft.item.EnumRarity; import net.minecraft.item.EnumRarity;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList; import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.play.client.C07PacketPlayerDigging;
import net.minecraft.network.play.server.S23PacketBlockChange;
import net.minecraft.util.ChatComponentText; import net.minecraft.util.ChatComponentText;
import net.minecraft.util.IIcon; import net.minecraft.util.IIcon;
import net.minecraft.util.MovingObjectPosition; import net.minecraft.util.MovingObjectPosition;
@ -47,22 +51,28 @@ public class ItemDrill extends ItemEnergy implements INameableItem{
public static int energyUsePerBlockOrHit = ConfigIntValues.DRILL_ENERGY_USE.getValue(); public static int energyUsePerBlockOrHit = ConfigIntValues.DRILL_ENERGY_USE.getValue();
@Override @Override
//Places Blocks if the Placing Upgrade is installed
public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int hitSide, float hitX, float hitY, float hitZ){ public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int hitSide, float hitX, float hitY, float hitZ){
ItemStack upgrade = this.getHasUpgradeAsStack(stack, ItemDrillUpgrade.UpgradeType.PLACER); ItemStack upgrade = this.getHasUpgradeAsStack(stack, ItemDrillUpgrade.UpgradeType.PLACER);
if(upgrade != null){ if(upgrade != null){
int slot = ItemDrillUpgrade.getSlotToPlaceFrom(upgrade); int slot = ItemDrillUpgrade.getSlotToPlaceFrom(upgrade);
if(slot >= 0 && slot < InventoryPlayer.getHotbarSize()){ if(slot >= 0 && slot < InventoryPlayer.getHotbarSize()){
ItemStack anEquip =player.inventory.getStackInSlot(slot); ItemStack anEquip = player.inventory.getStackInSlot(slot);
if(anEquip != null && anEquip != stack){ if(anEquip != null && anEquip != stack){
ItemStack equip = anEquip.copy(); ItemStack equip = anEquip.copy();
if(!world.isRemote){ if(!world.isRemote){
//tryPlaceItemIntoWorld could throw an Exception
try{ try{
//Places the Block into the World
if(equip.tryPlaceItemIntoWorld(player, world, x, y, z, hitSide, hitX, hitY, hitZ)){ if(equip.tryPlaceItemIntoWorld(player, world, x, y, z, hitSide, hitX, hitY, hitZ)){
if(!player.capabilities.isCreativeMode) player.inventory.setInventorySlotContents(slot, equip.stackSize <= 0 ? null : equip.copy()); if(!player.capabilities.isCreativeMode)
player.inventory.setInventorySlotContents(slot, equip.stackSize <= 0 ? null : equip.copy());
//Synchronizes the Client
player.inventoryContainer.detectAndSendChanges(); player.inventoryContainer.detectAndSendChanges();
return true; return true;
} }
} }
//Notify the Player and log the Exception
catch(Exception e){ catch(Exception e){
player.addChatComponentMessage(new ChatComponentText("Ouch! That really hurt! You must have done something wrong, don't do that again please!")); player.addChatComponentMessage(new ChatComponentText("Ouch! That really hurt! You must have done something wrong, don't do that again please!"));
ModUtil.LOGGER.log(Level.ERROR, "Player "+player.getDisplayName()+" who should place a Block using a Drill at "+player.posX+", "+player.posY+", "+player.posZ+" in World "+world.provider.dimensionId+" threw an Exception! Don't let that happen again!"); ModUtil.LOGGER.log(Level.ERROR, "Player "+player.getDisplayName()+" who should place a Block using a Drill at "+player.posX+", "+player.posY+", "+player.posZ+" in World "+world.provider.dimensionId+" threw an Exception! Don't let that happen again!");
@ -75,6 +85,8 @@ public class ItemDrill extends ItemEnergy implements INameableItem{
return false; return false;
} }
//Checks for Energy Containers in the Upgrade Slots and charges the Drill from them
@Override
public void onUpdate(ItemStack stack, World world, Entity entity, int par4, boolean par5){ public void onUpdate(ItemStack stack, World world, Entity entity, int par4, boolean par5){
ItemStack[] slots = this.getSlotsFromNBT(stack); ItemStack[] slots = this.getSlotsFromNBT(stack);
if(slots != null && slots.length > 0){ if(slots != null && slots.length > 0){
@ -83,6 +95,7 @@ public class ItemDrill extends ItemEnergy implements INameableItem{
if(this.getEnergyStored(stack) < this.getMaxEnergyStored(stack)){ if(this.getEnergyStored(stack) < this.getMaxEnergyStored(stack)){
int energy = ((IEnergyContainerItem)slotStack.getItem()).getEnergyStored(slotStack); int energy = ((IEnergyContainerItem)slotStack.getItem()).getEnergyStored(slotStack);
if(energy > 0){ if(energy > 0){
//Charge the Drill and discharge the "Upgrade"
int toReceive = ((IEnergyContainerItem)stack.getItem()).receiveEnergy(stack, energy, true); int toReceive = ((IEnergyContainerItem)stack.getItem()).receiveEnergy(stack, energy, true);
int actualReceive = ((IEnergyContainerItem)slotStack.getItem()).extractEnergy(slotStack, toReceive, false); int actualReceive = ((IEnergyContainerItem)slotStack.getItem()).extractEnergy(slotStack, toReceive, false);
((IEnergyContainerItem)stack.getItem()).receiveEnergy(stack, actualReceive, false); ((IEnergyContainerItem)stack.getItem()).receiveEnergy(stack, actualReceive, false);
@ -94,6 +107,11 @@ public class ItemDrill extends ItemEnergy implements INameableItem{
} }
} }
/**
* Gets the Mining Speed of the Drill
* @param stack The Drill
* @return The Mining Speed depending on the Speed Upgrades
*/
public float getEfficiencyFromUpgrade(ItemStack stack){ public float getEfficiencyFromUpgrade(ItemStack stack){
float efficiency = defaultEfficiency; float efficiency = defaultEfficiency;
if(this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.SPEED)){ if(this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.SPEED)){
@ -106,27 +124,40 @@ public class ItemDrill extends ItemEnergy implements INameableItem{
return efficiency; return efficiency;
} }
/**
* Gets the Energy that is used per Block broken
* @param stack The Drill
* @return The Energy use per Block
*/
public int getEnergyUsePerBlock(ItemStack stack){ public int getEnergyUsePerBlock(ItemStack stack){
int use = energyUsePerBlockOrHit; int use = energyUsePerBlockOrHit;
//Speed
if(this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.SPEED)){ if(this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.SPEED)){
use += ConfigIntValues.DRILL_SPEED_EXTRA_USE.getValue(); use += ConfigIntValues.DRILL_SPEED_EXTRA_USE.getValue();
if(this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.SPEED_II)){ if(this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.SPEED_II)){
use += ConfigIntValues.DRILL_SPEED_II_EXTRA_USE.getValue(); use += ConfigIntValues.DRILL_SPEED_II_EXTRA_USE.getValue();
if(this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.SPEED_III)) use += ConfigIntValues.DRILL_SPEED_III_EXTRA_USE.getValue(); if(this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.SPEED_III))
use += ConfigIntValues.DRILL_SPEED_III_EXTRA_USE.getValue();
} }
} }
if(this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.SILK_TOUCH)) use += ConfigIntValues.DRILL_SILK_EXTRA_USE.getValue(); //Silk Touch
if(this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.SILK_TOUCH))
use += ConfigIntValues.DRILL_SILK_EXTRA_USE.getValue();
//Fortune
if(this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.FORTUNE)){ if(this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.FORTUNE)){
use += ConfigIntValues.DRILL_FORTUNE_EXTRA_USE.getValue(); use += ConfigIntValues.DRILL_FORTUNE_EXTRA_USE.getValue();
if(this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.FORTUNE_II)) use += ConfigIntValues.DRILL_FORTUNE_II_EXTRA_USE.getValue(); if(this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.FORTUNE_II))
use += ConfigIntValues.DRILL_FORTUNE_II_EXTRA_USE.getValue();
} }
//Size
if(this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.THREE_BY_THREE)){ if(this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.THREE_BY_THREE)){
use += ConfigIntValues.DRILL_THREE_BY_THREE_EXTRA_USE.getValue(); use += ConfigIntValues.DRILL_THREE_BY_THREE_EXTRA_USE.getValue();
if(this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.FIVE_BY_FIVE)) use += ConfigIntValues.DRILL_FIVE_BY_FIVE_EXTRA_USE.getValue(); if(this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.FIVE_BY_FIVE))
use += ConfigIntValues.DRILL_FIVE_BY_FIVE_EXTRA_USE.getValue();
} }
return use; return use;
@ -137,10 +168,22 @@ public class ItemDrill extends ItemEnergy implements INameableItem{
return EnumRarity.epic; return EnumRarity.epic;
} }
/**
* Checks if a certain Upgrade is applied
* @param stack The Drill
* @param upgrade The Upgrade to be checked
* @return Is the Upgrade applied?
*/
public boolean getHasUpgrade(ItemStack stack, ItemDrillUpgrade.UpgradeType upgrade){ public boolean getHasUpgrade(ItemStack stack, ItemDrillUpgrade.UpgradeType upgrade){
return this.getHasUpgradeAsStack(stack, upgrade) != null; return this.getHasUpgradeAsStack(stack, upgrade) != null;
} }
/**
* Checks if a certain Upgrade is installed and returns it as an ItemStack
* @param stack The Drill
* @param upgrade The Upgrade to be checked
* @return The Upgrade, if it's installed
*/
public ItemStack getHasUpgradeAsStack(ItemStack stack, ItemDrillUpgrade.UpgradeType upgrade){ public ItemStack getHasUpgradeAsStack(ItemStack stack, ItemDrillUpgrade.UpgradeType upgrade){
NBTTagCompound compound = stack.getTagCompound(); NBTTagCompound compound = stack.getTagCompound();
if(compound == null) return null; if(compound == null) return null;
@ -164,9 +207,14 @@ public class ItemDrill extends ItemEnergy implements INameableItem{
@Override @Override
@SideOnly(Side.CLIENT) @SideOnly(Side.CLIENT)
public void registerIcons(IIconRegister iconReg){ public void registerIcons(IIconRegister iconReg){
this.itemIcon = iconReg.registerIcon(ModUtil.MOD_ID_LOWER + ":" + this.getName()); this.itemIcon = iconReg.registerIcon(ModUtil.MOD_ID_LOWER+":"+this.getName());
} }
/**
* Writes all of the Slots to NBT
* @param slots The Slots
* @param stack The Drill
*/
public void writeSlotsToNBT(ItemStack[] slots, ItemStack stack){ public void writeSlotsToNBT(ItemStack[] slots, ItemStack stack){
NBTTagCompound compound = stack.getTagCompound(); NBTTagCompound compound = stack.getTagCompound();
if(compound == null) compound = new NBTTagCompound(); if(compound == null) compound = new NBTTagCompound();
@ -187,6 +235,12 @@ public class ItemDrill extends ItemEnergy implements INameableItem{
stack.setTagCompound(compound); stack.setTagCompound(compound);
} }
/**
* Gets all of the Slots from NBT
* @param stack The Drill
* @return All of the Slots
*/
public ItemStack[] getSlotsFromNBT(ItemStack stack){ public ItemStack[] getSlotsFromNBT(ItemStack stack){
NBTTagCompound compound = stack.getTagCompound(); NBTTagCompound compound = stack.getTagCompound();
if(compound == null) return null; if(compound == null) return null;
@ -207,14 +261,27 @@ public class ItemDrill extends ItemEnergy implements INameableItem{
return slots; return slots;
} }
/**
* Breaks Blocks in a certain Radius
* Has to be called on both Server and Client
* @param stack The Drill
* @param radius The Radius to break Blocks in (0 means only 1 Block will be broken!)
* @param world The World
* @param x The X Coord of the main Block to break
* @param y The Y Coord of the main Block to break
* @param z The Z Coord of the main Block to break
* @param player The Player who breaks the Blocks
*/
public void breakBlocks(ItemStack stack, int radius, World world, int x, int y, int z, EntityPlayer player){ public void breakBlocks(ItemStack stack, int radius, World world, int x, int y, int z, EntityPlayer player){
int xRange = radius; int xRange = radius;
int yRange = radius; int yRange = radius;
int zRange = 0; int zRange = 0;
//Block hit
MovingObjectPosition pos = WorldUtil.getNearestBlockWithDefaultReachDistance(world, player); MovingObjectPosition pos = WorldUtil.getNearestBlockWithDefaultReachDistance(world, player);
if(pos != null){ if(pos != null){
//Corrects Blocks to hit depending on Side of original Block hit
int side = pos.sideHit; int side = pos.sideHit;
if(side == 0 || side == 1){ if(side == 0 || side == 1){
zRange = radius; zRange = radius;
@ -225,6 +292,7 @@ public class ItemDrill extends ItemEnergy implements INameableItem{
zRange = radius; zRange = radius;
} }
//Not defined later because main Block is getting broken below
float mainHardness = world.getBlock(x, y, z).getBlockHardness(world, x, y, z); float mainHardness = world.getBlock(x, y, z).getBlockHardness(world, x, y, z);
//Break Middle Block first //Break Middle Block first
@ -241,7 +309,7 @@ public class ItemDrill extends ItemEnergy implements INameableItem{
for(int zPos = z-zRange; zPos <= z+zRange; zPos++){ for(int zPos = z-zRange; zPos <= z+zRange; zPos++){
if(!(x == xPos && y == yPos && z == zPos)){ if(!(x == xPos && y == yPos && z == zPos)){
if(this.getEnergyStored(stack) >= use){ if(this.getEnergyStored(stack) >= use){
//Only break Blocks that are (about) as hard or softer //Only break Blocks around that are (about) as hard or softer
if(world.getBlock(xPos, yPos, zPos).getBlockHardness(world, xPos, yPos, zPos) <= mainHardness+5.0F){ if(world.getBlock(xPos, yPos, zPos).getBlockHardness(world, xPos, yPos, zPos) <= mainHardness+5.0F){
this.tryHarvestBlock(world, xPos, yPos, zPos, true, stack, player, use); this.tryHarvestBlock(world, xPos, yPos, zPos, true, stack, player, use);
} }
@ -255,6 +323,19 @@ public class ItemDrill extends ItemEnergy implements INameableItem{
} }
} }
/**
* Tries to harvest a certain Block
* Breaks the Block, drops Particles etc.
* Has to be called on both Server and Client
* @param world The World
* @param xPos The X Position of the Block to break
* @param yPos The Y Position of the Block to break
* @param zPos The Z Position of the Block to break
* @param isExtra If the Block is the Block that was looked at when breaking or an additional Block
* @param stack The Drill
* @param player The Player breaking the Blocks
* @param use The Energy that should be extracted per Block
*/
private void tryHarvestBlock(World world, int xPos, int yPos, int zPos, boolean isExtra, ItemStack stack, EntityPlayer player, int use){ private void tryHarvestBlock(World world, int xPos, int yPos, int zPos, boolean isExtra, ItemStack stack, EntityPlayer player, int use){
Block block = world.getBlock(xPos, yPos, zPos); Block block = world.getBlock(xPos, yPos, zPos);
float hardness = block.getBlockHardness(world, xPos, yPos, zPos); float hardness = block.getBlockHardness(world, xPos, yPos, zPos);
@ -262,20 +343,38 @@ public class ItemDrill extends ItemEnergy implements INameableItem{
if(hardness >= 0.0F && (!isExtra || (this.canHarvestBlock(block, stack) && !block.hasTileEntity(meta)))){ if(hardness >= 0.0F && (!isExtra || (this.canHarvestBlock(block, stack) && !block.hasTileEntity(meta)))){
this.extractEnergy(stack, use, false); this.extractEnergy(stack, use, false);
if(!world.isRemote){
//Server-Side only, special cases
block.onBlockHarvested(world, xPos, yPos, zPos, meta, player); block.onBlockHarvested(world, xPos, yPos, zPos, meta, player);
}
else{
//Shows the Harvest Particles and plays the Block's Sound
world.playAuxSFX(2001, xPos, yPos, zPos, Block.getIdFromBlock(block)+(meta << 12));
}
//Actually removes the Block from the World
if(block.removedByPlayer(world, player, xPos, yPos, zPos, true)){ if(block.removedByPlayer(world, player, xPos, yPos, zPos, true)){
//Before the Block is destroyed, special cases
block.onBlockDestroyedByPlayer(world, xPos, yPos, zPos, meta); block.onBlockDestroyedByPlayer(world, xPos, yPos, zPos, meta);
if(!player.capabilities.isCreativeMode){ if(!world.isRemote && !player.capabilities.isCreativeMode){
//Actually drops the Block's Items etc.
block.harvestBlock(world, player, xPos, yPos, zPos, meta); block.harvestBlock(world, player, xPos, yPos, zPos, meta);
//Only drop XP when no Silk Touch is applied
if(!EnchantmentHelper.getSilkTouchModifier(player)){ if(!EnchantmentHelper.getSilkTouchModifier(player)){
//Drop XP depending on Fortune Level
block.dropXpOnBlockBreak(world, xPos, yPos, zPos, block.getExpDrop(world, meta, EnchantmentHelper.getFortuneModifier(player))); block.dropXpOnBlockBreak(world, xPos, yPos, zPos, block.getExpDrop(world, meta, EnchantmentHelper.getFortuneModifier(player)));
} }
} }
} }
if(isExtra){ if(!world.isRemote){
world.playAuxSFX(2001, xPos, yPos, zPos, Block.getIdFromBlock(block)+(meta << 12)); //Update the Client of a Block Change
((EntityPlayerMP)player).playerNetServerHandler.sendPacket(new S23PacketBlockChange(xPos, yPos, zPos, world));
}
else{
//Check the Server if a Block that changed on the Client really changed, if not, revert the change
Minecraft.getMinecraft().getNetHandler().addToSendQueue(new C07PacketPlayerDigging(2, xPos, yPos, zPos, Minecraft.getMinecraft().objectMouseOver.sideHit));
} }
} }
} }
@ -289,7 +388,7 @@ public class ItemDrill extends ItemEnergy implements INameableItem{
public boolean onBlockStartBreak(ItemStack stack, int x, int y, int z, EntityPlayer player){ public boolean onBlockStartBreak(ItemStack stack, int x, int y, int z, EntityPlayer player){
int use = this.getEnergyUsePerBlock(stack); int use = this.getEnergyUsePerBlock(stack);
if(this.getEnergyStored(stack) >= use){ if(this.getEnergyStored(stack) >= use){
if(!player.worldObj.isRemote){ //Enchants the Drill depending on the Upgrades it has
if(this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.SILK_TOUCH)) if(this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.SILK_TOUCH))
stack.addEnchantment(Enchantment.silkTouch, 1); stack.addEnchantment(Enchantment.silkTouch, 1);
else{ else{
@ -297,6 +396,7 @@ public class ItemDrill extends ItemEnergy implements INameableItem{
stack.addEnchantment(Enchantment.fortune, this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.FORTUNE_II) ? 3 : 1); stack.addEnchantment(Enchantment.fortune, this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.FORTUNE_II) ? 3 : 1);
} }
//Breaks the Blocks
if(!player.isSneaking() && this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.THREE_BY_THREE)){ if(!player.isSneaking() && this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.THREE_BY_THREE)){
if(this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.FIVE_BY_FIVE)){ if(this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.FIVE_BY_FIVE)){
this.breakBlocks(stack, 2, player.worldObj, x, y, z, player); this.breakBlocks(stack, 2, player.worldObj, x, y, z, player);
@ -305,6 +405,7 @@ public class ItemDrill extends ItemEnergy implements INameableItem{
} }
else this.breakBlocks(stack, 0, player.worldObj, x, y, z, player); else this.breakBlocks(stack, 0, player.worldObj, x, y, z, player);
//Removes Enchantments added above
NBTTagList ench = stack.getEnchantmentTagList(); NBTTagList ench = stack.getEnchantmentTagList();
if(ench != null){ if(ench != null){
for(int i = 0; i < ench.tagCount(); i++){ for(int i = 0; i < ench.tagCount(); i++){
@ -314,12 +415,9 @@ public class ItemDrill extends ItemEnergy implements INameableItem{
} }
} }
} }
}
return true; return true;
} }
else return false;
}
else return true;
}
@Override @Override
public boolean hitEntity(ItemStack stack, EntityLivingBase entity1, EntityLivingBase entity2){ public boolean hitEntity(ItemStack stack, EntityLivingBase entity1, EntityLivingBase entity2){