Skip to content

Commit

Permalink
Fix divide by zero error (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
DonovanDMC committed Jan 30, 2023
1 parent d2bf64f commit 7c4c701
Showing 1 changed file with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.minecraft.fluid.Fluids;
import net.minecraft.item.BucketItem;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.ITickableTileEntity;
import net.minecraft.util.ActionResultType;
Expand Down Expand Up @@ -186,7 +187,9 @@ public ItemStack getStackInSlot(int slot) {
if (slot != 0 || itemStack.isEmpty()) return ItemStack.EMPTY;
@Nullable IKnowledgeProvider provider = Util.getKnowledgeProvider(owner);
if (provider == null) return ItemStack.EMPTY;
BigInteger maxCount = provider.getEmc().divide(BigInteger.valueOf(ProjectEAPI.getEMCProxy().getValue(itemStack))).min(BigInteger.valueOf(Integer.MAX_VALUE));
BigInteger val = BigInteger.valueOf(ProjectEAPI.getEMCProxy().getValue(itemStack));
if(val.equals(BigInteger.ZERO)) return ItemStack.EMPTY;
BigInteger maxCount = provider.getEmc().divide(val).min(BigInteger.valueOf(Integer.MAX_VALUE));
int count = maxCount.intValueExact();
if (count <= 0) return ItemStack.EMPTY;

Expand Down Expand Up @@ -238,6 +241,7 @@ public ItemStack extractItemInternal(int slot, int amount, boolean simulate, boo
if (slot != 0 || remainingExport <= 0 || owner == null || itemStack.isEmpty() || Util.getPlayer(owner) == null) return ItemStack.EMPTY;

BigInteger itemValue = BigInteger.valueOf(ProjectEAPI.getEMCProxy().getValue(itemStack));
if(itemValue.equals(BigInteger.ZERO)) return ItemStack.EMPTY;
@Nullable IKnowledgeProvider provider = Util.getKnowledgeProvider(owner);
if (provider == null) return ItemStack.EMPTY;
BigInteger maxCount = provider.getEmc().divide(itemValue).min(BigInteger.valueOf(Integer.MAX_VALUE));
Expand Down Expand Up @@ -280,7 +284,10 @@ public boolean isItemValid(int slot, @Nonnull ItemStack stack) {

private double getFluidCostPer() {
try {
return (ProjectEAPI.getEMCProxy().getValue(itemStack) - ((ProjectEAPI.getEMCProxy().getValue(net.minecraft.item.Items.BUCKET) * matter.getFluidEfficiencyPercentage()) / 100F)) / 1000D;
long fullCost = ProjectEAPI.getEMCProxy().getValue(itemStack);
long bucketCost = ProjectEAPI.getEMCProxy().getValue(Items.BUCKET);
if(bucketCost == 0 && fullCost == 0) return 0D;
return (fullCost - ((bucketCost * matter.getFluidEfficiencyPercentage()) / 100F)) / 1000D;
} catch(ArithmeticException ignore) {
return Long.MAX_VALUE;
}
Expand All @@ -304,7 +311,7 @@ public int getTanks() {
@Override
public FluidStack getFluidInTank(int tank) {
Fluid fluid = getFluid();
if(fluid == null) return FluidStack.EMPTY;
if(fluid == null || getFluidCostPer() == 0D) return FluidStack.EMPTY;
return new FluidStack(fluid, remainingFluid);
}

Expand All @@ -327,15 +334,15 @@ public int fill(FluidStack resource, FluidAction action) {
@Override
public FluidStack drain(FluidStack resource, FluidAction action) {
Fluid fluid = getFluid();
if(fluid != null && resource.getFluid().equals(fluid)) return drain(resource.getAmount(), action);
if(fluid != null && getFluidCostPer() != 0D && resource.getFluid().equals(fluid)) return drain(resource.getAmount(), action);
return FluidStack.EMPTY;
}

@Nonnull
@Override
public FluidStack drain(int maxDrain, FluidAction action) {
Fluid fluid = getFluid();
if(fluid == null || Util.getPlayer(owner) == null) return FluidStack.EMPTY;
if(fluid == null || getFluidCostPer() == 0D || Util.getPlayer(owner) == null) return FluidStack.EMPTY;
if(maxDrain > remainingFluid) maxDrain = remainingFluid;
long cost = getFluidCost(maxDrain);
@Nullable IKnowledgeProvider provider = Util.getKnowledgeProvider(owner);
Expand Down Expand Up @@ -392,7 +399,7 @@ public ActionResultType handleActivation(PlayerEntity player, Hand hand) {
}

Fluid fluid = getFluid();
if(fluid != null && inHand.getItem() instanceof BucketItem && ((BucketItem) inHand.getItem()).getFluid() == Fluids.EMPTY) {
if(fluid != null && getFluidCostPer() != 0D && inHand.getItem() instanceof BucketItem && ((BucketItem) inHand.getItem()).getFluid() == Fluids.EMPTY) {
BucketItem bucketItem = (BucketItem) inHand.getItem();
if(Config.limitEmcLinkVendor.get() && remainingExport < 1000) {
player.displayClientMessage(new TranslationTextComponent("block.projectexpansion.emc_link.no_export_remaining").setStyle(ColorStyle.RED), true);
Expand Down

0 comments on commit 7c4c701

Please sign in to comment.