2015-11-02 12:39:44 -08:00
|
|
|
package WayofTime.bloodmagic.util;
|
2015-10-29 20:22:14 -07:00
|
|
|
|
2015-12-02 16:02:18 -08:00
|
|
|
import net.minecraft.block.Block;
|
2016-01-06 07:13:56 -05:00
|
|
|
import net.minecraft.enchantment.EnchantmentHelper;
|
|
|
|
import net.minecraft.entity.EntityLivingBase;
|
2015-11-12 13:05:23 -08:00
|
|
|
import net.minecraft.entity.item.EntityItem;
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2015-12-02 16:02:18 -08:00
|
|
|
import net.minecraft.init.Blocks;
|
2016-01-09 18:17:38 -05:00
|
|
|
import net.minecraft.inventory.IInventory;
|
|
|
|
import net.minecraft.inventory.ISidedInventory;
|
2015-11-12 13:05:23 -08:00
|
|
|
import net.minecraft.item.ItemStack;
|
2016-01-06 07:13:56 -05:00
|
|
|
import net.minecraft.potion.Potion;
|
|
|
|
import net.minecraft.util.DamageSource;
|
2016-01-09 18:17:38 -05:00
|
|
|
import net.minecraft.util.EnumFacing;
|
2015-12-23 20:19:06 -05:00
|
|
|
import WayofTime.bloodmagic.api.altar.EnumAltarComponent;
|
|
|
|
import WayofTime.bloodmagic.registry.ModBlocks;
|
|
|
|
import WayofTime.bloodmagic.tile.TileInventory;
|
2015-11-12 13:05:23 -08:00
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
public class Utils
|
|
|
|
{
|
|
|
|
public static boolean isInteger(String integer)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2015-10-29 20:22:14 -07:00
|
|
|
Integer.parseInt(integer);
|
2015-12-30 15:34:40 -05:00
|
|
|
} catch (NumberFormatException e)
|
|
|
|
{
|
2015-10-29 20:22:14 -07:00
|
|
|
return false;
|
2015-12-30 15:34:40 -05:00
|
|
|
} catch (NullPointerException e)
|
|
|
|
{
|
2015-10-29 20:22:14 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// only got here if we didn't return false
|
|
|
|
return true;
|
|
|
|
}
|
2015-11-12 13:05:23 -08:00
|
|
|
|
|
|
|
/**
|
2016-01-01 10:52:42 -08:00
|
|
|
* @see #insertItemToTile(TileInventory, EntityPlayer, int)
|
2016-01-02 17:56:37 -05:00
|
|
|
*
|
2015-12-30 15:34:40 -05:00
|
|
|
* @param tile
|
2016-01-02 17:56:37 -05:00
|
|
|
* - The {@link TileInventory} to input the item to
|
2015-12-30 15:34:40 -05:00
|
|
|
* @param player
|
2016-01-02 17:56:37 -05:00
|
|
|
* - The player to take the item from.
|
|
|
|
*
|
|
|
|
* @return {@code true} if the ItemStack is inserted, {@code false}
|
|
|
|
* otherwise
|
2015-11-12 13:05:23 -08:00
|
|
|
*/
|
2015-12-30 15:34:40 -05:00
|
|
|
public static boolean insertItemToTile(TileInventory tile, EntityPlayer player)
|
|
|
|
{
|
2015-12-23 20:19:06 -05:00
|
|
|
return insertItemToTile(tile, player, 0);
|
|
|
|
}
|
2015-12-30 15:34:40 -05:00
|
|
|
|
2016-01-01 10:52:42 -08:00
|
|
|
/**
|
|
|
|
* Used for inserting an ItemStack with a stacksize of 1 to a tile's
|
|
|
|
* inventory at slot 0
|
2016-01-02 17:56:37 -05:00
|
|
|
*
|
2016-01-01 10:52:42 -08:00
|
|
|
* EG: Block Altar
|
2016-01-02 17:56:37 -05:00
|
|
|
*
|
2016-01-01 10:52:42 -08:00
|
|
|
* @param tile
|
2016-01-02 17:56:37 -05:00
|
|
|
* - The {@link TileInventory} to input the item to
|
2016-01-01 10:52:42 -08:00
|
|
|
* @param player
|
2016-01-02 17:56:37 -05:00
|
|
|
* - The player to take the item from.
|
2016-01-01 10:52:42 -08:00
|
|
|
* @param slot
|
2016-01-02 17:56:37 -05:00
|
|
|
* - The slot to attempt to insert to
|
|
|
|
*
|
|
|
|
* @return {@code true} if the ItemStack is inserted, {@code false}
|
|
|
|
* otherwise
|
2016-01-01 10:52:42 -08:00
|
|
|
*/
|
2015-12-30 15:34:40 -05:00
|
|
|
public static boolean insertItemToTile(TileInventory tile, EntityPlayer player, int slot)
|
|
|
|
{
|
|
|
|
if (tile.getStackInSlot(slot) == null && player.getHeldItem() != null)
|
|
|
|
{
|
2015-11-12 13:05:23 -08:00
|
|
|
ItemStack input = player.getHeldItem().copy();
|
|
|
|
input.stackSize = 1;
|
|
|
|
player.getHeldItem().stackSize--;
|
2015-12-23 20:19:06 -05:00
|
|
|
tile.setInventorySlotContents(slot, input);
|
2015-11-27 20:15:19 -05:00
|
|
|
return true;
|
2015-12-30 15:34:40 -05:00
|
|
|
} else if (tile.getStackInSlot(slot) != null && player.getHeldItem() == null)
|
|
|
|
{
|
|
|
|
if (!tile.getWorld().isRemote)
|
|
|
|
{
|
2015-12-23 20:19:06 -05:00
|
|
|
EntityItem invItem = new EntityItem(tile.getWorld(), player.posX, player.posY + 0.25, player.posZ, tile.getStackInSlot(slot));
|
2015-11-12 13:05:23 -08:00
|
|
|
tile.getWorld().spawnEntityInWorld(invItem);
|
|
|
|
}
|
|
|
|
tile.clear();
|
2015-11-27 20:15:19 -05:00
|
|
|
return false;
|
2015-11-12 13:05:23 -08:00
|
|
|
}
|
2015-11-27 20:15:19 -05:00
|
|
|
|
|
|
|
return false;
|
2015-11-12 13:05:23 -08:00
|
|
|
}
|
2015-12-02 16:02:18 -08:00
|
|
|
|
2016-01-01 10:52:42 -08:00
|
|
|
/**
|
|
|
|
* Gets a default block for each type of {@link EnumAltarComponent}
|
2016-01-02 17:56:37 -05:00
|
|
|
*
|
2016-01-01 10:52:42 -08:00
|
|
|
* @param component
|
2016-01-02 17:56:37 -05:00
|
|
|
* - The Component to provide a block for.
|
|
|
|
*
|
2016-01-01 10:52:42 -08:00
|
|
|
* @return The default Block for the EnumAltarComponent
|
|
|
|
*/
|
2015-12-30 15:34:40 -05:00
|
|
|
public static Block getBlockForComponent(EnumAltarComponent component)
|
|
|
|
{
|
|
|
|
switch (component)
|
|
|
|
{
|
|
|
|
case GLOWSTONE:
|
|
|
|
return Blocks.glowstone;
|
|
|
|
case BLOODSTONE:
|
|
|
|
return ModBlocks.bloodStoneBrick;
|
|
|
|
case BEACON:
|
|
|
|
return Blocks.beacon;
|
|
|
|
case BLOODRUNE:
|
|
|
|
return ModBlocks.bloodRune;
|
|
|
|
case CRYSTAL:
|
|
|
|
return ModBlocks.crystal;
|
|
|
|
case NOTAIR:
|
|
|
|
return Blocks.stonebrick;
|
|
|
|
default:
|
|
|
|
return Blocks.air;
|
2015-12-02 16:02:18 -08:00
|
|
|
}
|
|
|
|
}
|
2016-01-06 07:13:56 -05:00
|
|
|
|
|
|
|
public static float getModifiedDamage(EntityLivingBase attackedEntity, DamageSource source, float amount)
|
|
|
|
{
|
|
|
|
if (!attackedEntity.isEntityInvulnerable(source))
|
|
|
|
{
|
|
|
|
if (amount <= 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
amount = net.minecraftforge.common.ISpecialArmor.ArmorProperties.applyArmor(attackedEntity, attackedEntity.getInventory(), source, amount);
|
|
|
|
if (amount <= 0)
|
|
|
|
return 0;
|
|
|
|
amount = applyPotionDamageCalculations(attackedEntity, source, amount);
|
|
|
|
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static float applyPotionDamageCalculations(EntityLivingBase attackedEntity, DamageSource source, float damage)
|
|
|
|
{
|
|
|
|
if (source.isDamageAbsolute())
|
|
|
|
{
|
|
|
|
return damage;
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
if (attackedEntity.isPotionActive(Potion.resistance) && source != DamageSource.outOfWorld)
|
|
|
|
{
|
|
|
|
int i = (attackedEntity.getActivePotionEffect(Potion.resistance).getAmplifier() + 1) * 5;
|
|
|
|
int j = 25 - i;
|
|
|
|
float f = damage * (float) j;
|
|
|
|
damage = f / 25.0F;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (damage <= 0.0F)
|
|
|
|
{
|
|
|
|
return 0.0F;
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
int k = EnchantmentHelper.getEnchantmentModifierDamage(attackedEntity.getInventory(), source);
|
|
|
|
|
|
|
|
if (k > 20)
|
|
|
|
{
|
|
|
|
k = 20;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (k > 0 && k <= 20)
|
|
|
|
{
|
|
|
|
int l = 25 - k;
|
|
|
|
float f1 = damage * (float) l;
|
|
|
|
damage = f1 / 25.0F;
|
|
|
|
}
|
|
|
|
|
|
|
|
return damage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-09 18:17:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to determine if stack1 can be placed into stack2. If stack2 is null
|
|
|
|
* and stack1 isn't null, returns true. Ignores stack size
|
|
|
|
*
|
|
|
|
* @param stack1
|
|
|
|
* Stack that is placed into a slot
|
|
|
|
* @param stack2
|
|
|
|
* Slot content that stack1 is placed into
|
|
|
|
* @return True if they can be combined
|
|
|
|
*/
|
|
|
|
public static boolean canCombine(ItemStack stack1, ItemStack stack2)
|
|
|
|
{
|
|
|
|
if (stack1 == null)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stack2 == null)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stack1.isItemStackDamageable() ^ stack2.isItemStackDamageable())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return stack1.getItem() == stack2.getItem() && stack1.getItemDamage() == stack2.getItemDamage() && ItemStack.areItemStackTagsEqual(stack1, stack2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param stack1
|
|
|
|
* Stack that is placed into a slot
|
|
|
|
* @param stack2
|
|
|
|
* Slot content that stack1 is placed into
|
|
|
|
* @return Stacks after stacking
|
|
|
|
*/
|
|
|
|
public static ItemStack[] combineStacks(ItemStack stack1, ItemStack stack2, int transferMax)
|
|
|
|
{
|
|
|
|
ItemStack[] returned = new ItemStack[2];
|
|
|
|
|
|
|
|
if (canCombine(stack1, stack2))
|
|
|
|
{
|
|
|
|
int transferedAmount = Math.min(transferMax, stack2 == null ? stack1.stackSize : Math.min(stack2.getMaxStackSize() - stack2.stackSize, stack1.stackSize));
|
|
|
|
if (transferedAmount > 0)
|
|
|
|
{
|
|
|
|
ItemStack copyStack = stack1.splitStack(transferedAmount);
|
|
|
|
if (stack2 == null)
|
|
|
|
{
|
|
|
|
stack2 = copyStack;
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
stack2.stackSize += transferedAmount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
returned[0] = stack1;
|
|
|
|
returned[1] = stack2;
|
|
|
|
|
|
|
|
return returned;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param stack1
|
|
|
|
* Stack that is placed into a slot
|
|
|
|
* @param stack2
|
|
|
|
* Slot content that stack1 is placed into
|
|
|
|
* @return Stacks after stacking
|
|
|
|
*/
|
|
|
|
public static ItemStack[] combineStacks(ItemStack stack1, ItemStack stack2)
|
|
|
|
{
|
|
|
|
ItemStack[] returned = new ItemStack[2];
|
|
|
|
|
|
|
|
if (canCombine(stack1, stack2))
|
|
|
|
{
|
|
|
|
int transferedAmount = stack2 == null ? stack1.stackSize : Math.min(stack2.getMaxStackSize() - stack2.stackSize, stack1.stackSize);
|
|
|
|
if (transferedAmount > 0)
|
|
|
|
{
|
|
|
|
ItemStack copyStack = stack1.splitStack(transferedAmount);
|
|
|
|
if (stack2 == null)
|
|
|
|
{
|
|
|
|
stack2 = copyStack;
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
stack2.stackSize += transferedAmount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
returned[0] = stack1;
|
|
|
|
returned[1] = stack2;
|
|
|
|
|
|
|
|
return returned;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ItemStack insertStackIntoInventory(ItemStack stack, IInventory inventory, EnumFacing dir)
|
|
|
|
{
|
|
|
|
if (stack == null)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean[] canBeInserted = new boolean[inventory.getSizeInventory()];
|
|
|
|
|
|
|
|
if (inventory instanceof ISidedInventory)
|
|
|
|
{
|
|
|
|
int[] array = ((ISidedInventory) inventory).getSlotsForFace(dir);
|
|
|
|
for (int in : array)
|
|
|
|
{
|
|
|
|
canBeInserted[in] = inventory.isItemValidForSlot(in, stack) && ((ISidedInventory) inventory).canInsertItem(in, stack, dir);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
for (int i = 0; i < canBeInserted.length; i++)
|
|
|
|
{
|
|
|
|
canBeInserted[i] = inventory.isItemValidForSlot(i, stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < inventory.getSizeInventory(); i++)
|
|
|
|
{
|
|
|
|
if (!canBeInserted[i])
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ItemStack[] combinedStacks = combineStacks(stack, inventory.getStackInSlot(i));
|
|
|
|
stack = combinedStacks[0];
|
|
|
|
inventory.setInventorySlotContents(i, combinedStacks[1]);
|
|
|
|
|
|
|
|
if (stack.stackSize <= 0)
|
|
|
|
{
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean canInsertStackFullyIntoInventory(ItemStack stack, IInventory inventory, EnumFacing dir)
|
|
|
|
{
|
|
|
|
return canInsertStackFullyIntoInventory(stack, inventory, dir, false, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean canInsertStackFullyIntoInventory(ItemStack stack, IInventory inventory, EnumFacing dir, boolean fillToLimit, int limit)
|
|
|
|
{
|
|
|
|
if (stack == null)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int itemsLeft = stack.stackSize;
|
|
|
|
|
|
|
|
boolean[] canBeInserted = new boolean[inventory.getSizeInventory()];
|
|
|
|
|
|
|
|
if (inventory instanceof ISidedInventory)
|
|
|
|
{
|
|
|
|
int[] array = ((ISidedInventory) inventory).getSlotsForFace(dir);
|
|
|
|
for (int in : array)
|
|
|
|
{
|
|
|
|
canBeInserted[in] = inventory.isItemValidForSlot(in, stack) && ((ISidedInventory) inventory).canInsertItem(in, stack, dir);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
for (int i = 0; i < canBeInserted.length; i++)
|
|
|
|
{
|
|
|
|
canBeInserted[i] = inventory.isItemValidForSlot(i, stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int numberMatching = 0;
|
|
|
|
|
|
|
|
if (fillToLimit)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < inventory.getSizeInventory(); i++)
|
|
|
|
{
|
|
|
|
if (!canBeInserted[i])
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ItemStack invStack = inventory.getStackInSlot(i);
|
|
|
|
|
|
|
|
if (invStack != null && canCombine(stack, invStack))
|
|
|
|
{
|
|
|
|
numberMatching += invStack.stackSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fillToLimit && limit < stack.stackSize + numberMatching)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < inventory.getSizeInventory(); i++)
|
|
|
|
{
|
|
|
|
if (!canBeInserted[i])
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ItemStack invStack = inventory.getStackInSlot(i);
|
|
|
|
boolean canCombine = canCombine(stack, invStack);
|
|
|
|
if (canCombine)
|
|
|
|
{
|
|
|
|
if (invStack == null)
|
|
|
|
{
|
|
|
|
itemsLeft = 0;
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
itemsLeft -= (invStack.getMaxStackSize() - invStack.stackSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (itemsLeft <= 0)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ItemStack insertStackIntoInventory(ItemStack stack, IInventory inventory, EnumFacing dir, int limit)
|
|
|
|
{
|
|
|
|
if (stack == null)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean[] canBeInserted = new boolean[inventory.getSizeInventory()];
|
|
|
|
|
|
|
|
if (inventory instanceof ISidedInventory)
|
|
|
|
{
|
|
|
|
int[] array = ((ISidedInventory) inventory).getSlotsForFace(dir);
|
|
|
|
for (int in : array)
|
|
|
|
{
|
|
|
|
canBeInserted[in] = ((ISidedInventory) inventory).canInsertItem(in, stack, dir);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
for (int i = 0; i < canBeInserted.length; i++)
|
|
|
|
{
|
|
|
|
canBeInserted[i] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int numberMatching = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < inventory.getSizeInventory(); i++)
|
|
|
|
{
|
|
|
|
if (!canBeInserted[i])
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ItemStack invStack = inventory.getStackInSlot(i);
|
|
|
|
|
|
|
|
if (invStack != null && canCombine(stack, invStack))
|
|
|
|
{
|
|
|
|
numberMatching += invStack.stackSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (numberMatching >= limit)
|
|
|
|
{
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
|
|
|
int newLimit = limit - numberMatching;
|
|
|
|
|
|
|
|
for (int i = 0; i < inventory.getSizeInventory(); i++)
|
|
|
|
{
|
|
|
|
if (!canBeInserted[i])
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int prevStackSize = stack.stackSize;
|
|
|
|
|
|
|
|
ItemStack[] combinedStacks = combineStacks(stack, inventory.getStackInSlot(i), newLimit);
|
|
|
|
stack = combinedStacks[0];
|
|
|
|
inventory.setInventorySlotContents(i, combinedStacks[1]);
|
|
|
|
|
|
|
|
newLimit -= (prevStackSize - stack.stackSize);
|
|
|
|
|
|
|
|
if (newLimit <= 0 || stack.stackSize <= 0)
|
|
|
|
{
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stack;
|
|
|
|
}
|
2015-10-29 20:22:14 -07:00
|
|
|
}
|