Dartcraft/src/main/java/ley/modding/dartcraft/util/ForceConsumerUtils.java

168 lines
6.4 KiB
Java
Raw Normal View History

2021-04-01 14:32:58 +02:00
package ley.modding.dartcraft.util;
2021-04-07 15:37:47 +02:00
import ley.modding.dartcraft.Config;
2021-04-17 16:07:41 +02:00
import ley.modding.dartcraft.Dartcraft;
2021-04-01 14:32:58 +02:00
import ley.modding.dartcraft.api.IForceConsumer;
2021-04-07 15:37:47 +02:00
import ley.modding.dartcraft.api.inventory.ItemInventory;
import ley.modding.dartcraft.item.DartItems;
2021-04-17 16:07:41 +02:00
import net.minecraft.entity.player.EntityPlayer;
2021-04-01 14:32:58 +02:00
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
2021-04-17 16:07:41 +02:00
import net.minecraftforge.fluids.Fluid;
2021-04-07 15:37:47 +02:00
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import java.util.Random;
import java.util.logging.Logger;
2021-04-01 14:32:58 +02:00
public class ForceConsumerUtils {
public static int getStoredForce(ItemStack stack) {
return stack != null && stack.hasTagCompound()
&& stack.getTagCompound().hasKey("storedForce")
? stack.getTagCompound().getInteger("storedForce")
: 0;
2021-04-01 14:32:58 +02:00
}
public static void initializeForceConsumer(ItemStack stack) {
if (!stack.hasTagCompound()) {
2021-04-01 14:32:58 +02:00
stack.setTagCompound(new NBTTagCompound());
}
NBTTagCompound comp = stack.getTagCompound();
NBTTagList contents = new NBTTagList();
for (int i = 0; i < 1; ++i) {
2021-04-01 14:32:58 +02:00
NBTTagCompound itemComp = new NBTTagCompound();
itemComp.setByte("Slot", (byte) i);
2021-04-01 14:32:58 +02:00
contents.appendTag(itemComp);
}
comp.setTag("consumerContents", contents);
comp.setInteger("ID", (new Random()).nextInt());
}
public static boolean attemptRepair(ItemStack stack) {
boolean repaired = false;
IForceConsumer consumer = null;
if (stack != null && stack.getItemDamage() > 0
&& stack.getItem() instanceof IForceConsumer) {
consumer = (IForceConsumer) stack.getItem();
2021-04-01 14:32:58 +02:00
}
if (consumer != null) {
for (int use = consumer.amountUsedBase(stack);
stack.getItemDamage() > 0 && consumer.getStored(stack) >= use;
repaired = true) {
2021-04-01 14:32:58 +02:00
stack.setItemDamage(stack.getItemDamage() - 1);
consumer.useForce(stack, use, true);
}
}
return repaired;
}
2021-04-07 15:37:47 +02:00
public static boolean useForce(ItemStack stack, int amount, boolean use) {
if ((stack == null) || (!stack.hasTagCompound()) || (amount < 0)
|| (stack.getItem() == null)
|| (!(stack.getItem() instanceof IForceConsumer))) {
2021-04-07 15:37:47 +02:00
return false;
}
boolean canUse = stack.getTagCompound().getInteger("storedForce") >= amount;
try {
if (use) {
stack.getTagCompound().setInteger(
"storedForce",
stack.getTagCompound().getInteger("storedForce") - amount
);
2021-04-07 15:37:47 +02:00
ItemInventory inv = new ItemInventory(1, stack, "consumerContents");
IForceConsumer consumer = (IForceConsumer) stack.getItem();
2021-04-07 15:37:47 +02:00
ItemStack invStack = inv.getStackInSlot(0);
int defecit = consumer.getMaxStored(stack)
- stack.getTagCompound().getInteger("storedForce");
2021-04-07 15:37:47 +02:00
if ((defecit > 0) && (invStack != null) && (invStack.stackSize > 0)) {
FluidStack liquid = FluidContainerRegistry.getFluidForFilledItem(
new ItemStack(invStack.getItem(), 1, invStack.getItemDamage())
);
2021-04-07 15:37:47 +02:00
if ((liquid == null) && (invStack.getItem() == DartItems.forcegem)) {
liquid = new FluidStack(
FluidRegistry.getFluid("liquidforce"),
(int) (1000.0F * Config.gemValue)
);
2021-04-07 15:37:47 +02:00
}
if ((liquid != null)
&& (liquid.getFluid().getName().equalsIgnoreCase("liquidforce")
)) {
while ((defecit >= liquid.amount) && (invStack.stackSize > 0)) {
stack.getTagCompound().setInteger(
"storedForce",
stack.getTagCompound().getInteger("storedForce")
+ liquid.amount
);
2021-04-07 15:37:47 +02:00
invStack.stackSize -= 1;
if (invStack.stackSize <= 0) {
inv.setInventorySlotContents(
0,
invStack.getItem().hasContainerItem()
? new ItemStack(
invStack.getItem().getContainerItem()
)
: (ItemStack) null
);
2021-04-07 15:37:47 +02:00
}
inv.save();
defecit = consumer.getMaxStored(stack)
- stack.getTagCompound().getInteger("storedForce");
2021-04-07 15:37:47 +02:00
}
}
}
}
} catch (Exception e) {
Logger.getLogger("DartCraft")
.info("There was a problem in IForceConsumer implementation.");
2021-04-07 15:37:47 +02:00
e.printStackTrace();
}
return canUse;
}
public static boolean isForceContainer(ItemStack stack) {
2021-04-17 16:07:41 +02:00
if (stack == null) {
return false;
}
FluidStack liquid = FluidContainerRegistry.getFluidForFilledItem(stack);
Fluid liquidForce = FluidRegistry.getFluid("liquidforce");
if ((liquid != null) && (liquidForce != null)
&& (liquid.getFluidID() == liquidForce.getID())) {
2021-04-17 16:07:41 +02:00
return true;
}
return (stack.getItem() == DartItems.forcegem)
|| (stack.getItem() == DartItems.forceshard);
2021-04-17 16:07:41 +02:00
}
public static boolean openForceConsumerGui(EntityPlayer player, ItemStack stack) {
if ((stack == null) || (stack.getItem() == null) || (!stack.hasTagCompound())
|| (!(stack.getItem() instanceof IForceConsumer)) || (player == null)) {
2021-04-17 16:07:41 +02:00
return false;
}
player.openGui(
Dartcraft.instance,
16,
player.worldObj,
(int) player.posX,
(int) player.posY,
(int) player.posZ
);
2021-04-17 16:07:41 +02:00
return true;
}
2021-04-01 14:32:58 +02:00
}