Merge pull request #299 from ljfa-ag/bound-pick

Make item dropping from bound tools more efficient
This commit is contained in:
WayofTime 2015-04-10 19:46:58 -04:00
commit afa3988a21
5 changed files with 278 additions and 167 deletions

View file

@ -0,0 +1,87 @@
package WayofTime.alchemicalWizardry.common;
import java.util.Objects;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
/**
* Represents an item together with metadata and NBT tag.
*
* @author ljfa-ag
*/
public class ItemType
{
public final Item item;
public final int meta;
public final NBTTagCompound nbtTag;
public ItemType(Item item, int meta, NBTTagCompound nbtTag)
{
this.item = Objects.requireNonNull(item);
this.meta = meta;
this.nbtTag = nbtTag;
}
public ItemType(Item item, int meta)
{
this(item, meta, null);
}
public ItemType(Item item)
{
this(item, 0, null);
}
public ItemType(Block block, int meta, NBTTagCompound nbtTag)
{
this(Item.getItemFromBlock(block), meta, nbtTag);
}
public ItemType(Block block, int meta)
{
this(block, meta, null);
}
public ItemType(Block block)
{
this(block, 0, null);
}
public ItemStack createStack(int count)
{
ItemStack result = new ItemStack(item, count, meta);
result.stackTagCompound = nbtTag;
return result;
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass())
return false;
ItemType other = (ItemType) obj;
return item == other.item && meta == other.meta && Objects.equals(nbtTag, other.nbtTag);
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + item.hashCode();
result = prime * result + meta;
result = prime * result + ((nbtTag == null) ? 0 : nbtTag.hashCode());
return result;
}
public static ItemType fromStack(ItemStack stack)
{
return new ItemType(stack.getItem(), stack.getItemDamage(), stack.stackTagCompound);
}
}

View file

@ -2,6 +2,7 @@ package WayofTime.alchemicalWizardry.common.items;
import WayofTime.alchemicalWizardry.AlchemicalWizardry;
import WayofTime.alchemicalWizardry.api.items.interfaces.IBindable;
import WayofTime.alchemicalWizardry.common.ItemType;
import WayofTime.alchemicalWizardry.common.spell.complex.effect.SpellHelper;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@ -25,6 +26,8 @@ import net.minecraftforge.common.ForgeHooks;
import java.util.ArrayList;
import java.util.List;
import com.google.common.collect.HashMultiset;
public class BoundAxe extends ItemAxe implements IBindable
{
public float efficiencyOnProperMaterial = 12.0F;
@ -113,6 +116,11 @@ public class BoundAxe extends ItemAxe implements IBindable
return par1ItemStack;
}
if (par2World.isRemote)
{
return par1ItemStack;
}
if (!getActivated(par1ItemStack) || SpellHelper.isFakePlayer(par2World, par3EntityPlayer))
{
return par1ItemStack;
@ -135,6 +143,8 @@ public class BoundAxe extends ItemAxe implements IBindable
boolean silkTouch = EnchantmentHelper.getSilkTouchModifier(par3EntityPlayer);
int fortuneLvl = EnchantmentHelper.getFortuneModifier(par3EntityPlayer);
HashMultiset<ItemType> dropMultiset = HashMultiset.create();
for (int i = -5; i <= 5; i++)
{
for (int j = 0; j <= 10; j++)
@ -152,27 +162,18 @@ public class BoundAxe extends ItemAxe implements IBindable
{
if (silkTouch && block.canSilkHarvest(par2World, par3EntityPlayer, posX + i, posY + j, posZ + k, meta))
{
ItemStack droppedItem = new ItemStack(block, 1, meta);
if (!par2World.isRemote)
{
par2World.spawnEntityInWorld(new EntityItem(par2World, posX, posY + par3EntityPlayer.getEyeHeight(), posZ, droppedItem));
}
dropMultiset.add(new ItemType(block, meta));
} else
{
ArrayList<ItemStack> itemDropList = block.getDrops(par2World, posX + i, posY + j, posZ + k, meta, fortuneLvl);
if (itemDropList != null)
{
for (ItemStack item : itemDropList)
{
if (!par2World.isRemote)
{
par2World.spawnEntityInWorld(new EntityItem(par2World, posX, posY + par3EntityPlayer.getEyeHeight(), posZ, item));
}
}
for (ItemStack stack : itemDropList)
dropMultiset.add(ItemType.fromStack(stack), stack.stackSize);
}
}
par2World.setBlockToAir(posX + i, posY + j, posZ + k);
}
}
@ -180,6 +181,7 @@ public class BoundAxe extends ItemAxe implements IBindable
}
}
BoundPickaxe.dropMultisetStacks(dropMultiset, par2World, posX, posY + par3EntityPlayer.getEyeHeight(), posZ);
return par1ItemStack;
}

View file

@ -1,10 +1,8 @@
package WayofTime.alchemicalWizardry.common.items;
import WayofTime.alchemicalWizardry.AlchemicalWizardry;
import WayofTime.alchemicalWizardry.api.items.interfaces.IBindable;
import WayofTime.alchemicalWizardry.common.spell.complex.effect.SpellHelper;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.enchantment.EnchantmentHelper;
@ -20,9 +18,16 @@ import net.minecraft.util.StatCollector;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeHooks;
import WayofTime.alchemicalWizardry.AlchemicalWizardry;
import WayofTime.alchemicalWizardry.api.items.interfaces.IBindable;
import WayofTime.alchemicalWizardry.common.ItemType;
import WayofTime.alchemicalWizardry.common.spell.complex.effect.SpellHelper;
import java.util.ArrayList;
import java.util.List;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class BoundPickaxe extends ItemPickaxe implements IBindable
{
@ -115,6 +120,11 @@ public class BoundPickaxe extends ItemPickaxe implements IBindable
return par1ItemStack;
}
if (par2World.isRemote)
{
return par1ItemStack;
}
if (!getActivated(par1ItemStack) || SpellHelper.isFakePlayer(par2World, par3EntityPlayer))
{
return par1ItemStack;
@ -125,11 +135,6 @@ public class BoundPickaxe extends ItemPickaxe implements IBindable
return par1ItemStack;
}
if (par2World.isRemote)
{
return par1ItemStack;
}
if(!EnergyItems.syphonBatteries(par1ItemStack, par3EntityPlayer, 10000))
{
return par1ItemStack;
@ -142,6 +147,8 @@ public class BoundPickaxe extends ItemPickaxe implements IBindable
boolean silkTouch = EnchantmentHelper.getSilkTouchModifier(par3EntityPlayer);
int fortuneLvl = EnchantmentHelper.getFortuneModifier(par3EntityPlayer);
HashMultiset<ItemType> dropMultiset = HashMultiset.create();
for (int i = -5; i <= 5; i++)
{
for (int j = -5; j <= 5; j++)
@ -159,25 +166,15 @@ public class BoundPickaxe extends ItemPickaxe implements IBindable
{
if (silkTouch && block.canSilkHarvest(par2World, par3EntityPlayer, posX + i, posY + j, posZ + k, meta))
{
ItemStack droppedItem = new ItemStack(block, 1, meta);
if (!par2World.isRemote)
{
par2World.spawnEntityInWorld(new EntityItem(par2World, posX, posY + par3EntityPlayer.getEyeHeight(), posZ, droppedItem));
}
dropMultiset.add(new ItemType(block, meta));
} else
{
ArrayList<ItemStack> itemDropList = block.getDrops(par2World, posX + i, posY + j, posZ + k, meta, fortuneLvl);
if (itemDropList != null)
{
for (ItemStack item : itemDropList)
{
if (!par2World.isRemote)
{
par2World.spawnEntityInWorld(new EntityItem(par2World, posX, posY + par3EntityPlayer.getEyeHeight(), posZ, item));
}
}
for (ItemStack stack : itemDropList)
dropMultiset.add(ItemType.fromStack(stack), stack.stackSize);
}
}
@ -188,9 +185,31 @@ public class BoundPickaxe extends ItemPickaxe implements IBindable
}
}
dropMultisetStacks(dropMultiset, par2World, posX, posY + par3EntityPlayer.getEyeHeight(), posZ);
return par1ItemStack;
}
public static void dropMultisetStacks(Multiset<ItemType> dropMultiset, World world, double x, double y, double z)
{
for (Multiset.Entry<ItemType> entry : dropMultiset.entrySet())
{
int count = entry.getCount();
ItemType type = entry.getElement();
int maxStackSize = type.item.getItemStackLimit(type.createStack(1));
//Drop in groups of maximum size
while (count >= maxStackSize)
{
world.spawnEntityInWorld(new EntityItem(world, x, y, z, type.createStack(maxStackSize)));
count -= maxStackSize;
}
//Drop remainder
if (count > 0)
world.spawnEntityInWorld(new EntityItem(world, x, y, z, type.createStack(count)));
}
}
@Override
public void onUpdate(ItemStack par1ItemStack, World par2World, Entity par3Entity, int par4, boolean par5)
{

View file

@ -2,8 +2,10 @@ package WayofTime.alchemicalWizardry.common.items;
import WayofTime.alchemicalWizardry.AlchemicalWizardry;
import WayofTime.alchemicalWizardry.api.items.interfaces.IBindable;
import WayofTime.alchemicalWizardry.common.ItemType;
import WayofTime.alchemicalWizardry.common.spell.complex.effect.SpellHelper;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multimap;
import cpw.mods.fml.relauncher.Side;
@ -119,6 +121,11 @@ public class BoundShovel extends ItemSpade implements IBindable
return par1ItemStack;
}
if (par2World.isRemote)
{
return par1ItemStack;
}
if (!getActivated(par1ItemStack) || SpellHelper.isFakePlayer(par2World, par3EntityPlayer))
{
return par1ItemStack;
@ -141,6 +148,8 @@ public class BoundShovel extends ItemSpade implements IBindable
boolean silkTouch = EnchantmentHelper.getSilkTouchModifier(par3EntityPlayer);
int fortuneLvl = EnchantmentHelper.getFortuneModifier(par3EntityPlayer);
HashMultiset<ItemType> dropMultiset = HashMultiset.create();
for (int i = -5; i <= 5; i++)
{
for (int j = 0; j <= 10; j++)
@ -158,25 +167,15 @@ public class BoundShovel extends ItemSpade implements IBindable
{
if (silkTouch && block.canSilkHarvest(par2World, par3EntityPlayer, posX + i, posY + j, posZ + k, meta))
{
ItemStack droppedItem = new ItemStack(block, 1, meta);
if (!par2World.isRemote)
{
par2World.spawnEntityInWorld(new EntityItem(par2World, posX, posY + par3EntityPlayer.getEyeHeight(), posZ, droppedItem));
}
dropMultiset.add(new ItemType(block, meta));
} else
{
ArrayList<ItemStack> itemDropList = block.getDrops(par2World, posX + i, posY + j, posZ + k, meta, fortuneLvl);
if (itemDropList != null)
{
for (ItemStack item : itemDropList)
{
if (!par2World.isRemote)
{
par2World.spawnEntityInWorld(new EntityItem(par2World, posX, posY + par3EntityPlayer.getEyeHeight(), posZ, item));
}
}
for (ItemStack stack : itemDropList)
dropMultiset.add(ItemType.fromStack(stack), stack.stackSize);
}
}
@ -187,6 +186,8 @@ public class BoundShovel extends ItemSpade implements IBindable
}
}
BoundPickaxe.dropMultisetStacks(dropMultiset, par2World, posX, posY + par3EntityPlayer.getEyeHeight(), posZ);
return par1ItemStack;
}

View file

@ -10,6 +10,7 @@ import net.minecraftforge.common.util.Constants;
/**
* Base class for tile entities with inventory
*
* @author ljfa-ag
*/
public abstract class TEInventory extends TileEntity implements IInventory
@ -135,7 +136,8 @@ public abstract class TEInventory extends TileEntity implements IInventory
public void readFromNBT(NBTTagCompound tag)
{
super.readFromNBT(tag);
NBTTagList invList = tag.getTagList("Inventory", Constants.NBT.TAG_COMPOUND);
NBTTagList invList = tag.getTagList("Inventory",
Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < invList.tagCount(); i++)
{
NBTTagCompound stackTag = invList.getCompoundTagAt(i);