Skip to content

Commit

Permalink
Try to catch most expected NPE's from ProjectE (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
DonovanDMC committed Oct 28, 2022
1 parent 1ef07cf commit 13361b2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,12 @@ public void tickServer(Level level, BlockPos pos, BlockState state, BlockEntityE
resetLimits();
if (emc.equals(BigInteger.ZERO)) return;
ServerPlayer player = Util.getPlayer(level, owner);
IKnowledgeProvider provider = ProjectEAPI.getTransmutationProxy().getKnowledgeProviderFor(owner);
IKnowledgeProvider provider;
try {
provider = ProjectEAPI.getTransmutationProxy().getKnowledgeProviderFor(owner);
} catch (NullPointerException ignore) {
return;
}

provider.setEmc(provider.getEmc().add(emc));
if (player != null) provider.syncEmc(player);
Expand Down Expand Up @@ -183,7 +188,12 @@ public int getSlots() {
@Override
public ItemStack getStackInSlot(int slot) {
if (slot != 0 || itemStack.isEmpty()) return ItemStack.EMPTY;
IKnowledgeProvider provider = ProjectEAPI.getTransmutationProxy().getKnowledgeProviderFor(owner);
IKnowledgeProvider provider;
try {
provider = ProjectEAPI.getTransmutationProxy().getKnowledgeProviderFor(owner);
} catch (NullPointerException ignore) {
return ItemStack.EMPTY;
}
BigInteger maxCount = provider.getEmc().divide(BigInteger.valueOf(ProjectEAPI.getEMCProxy().getValue(itemStack))).min(BigInteger.valueOf(Integer.MAX_VALUE));
int count = maxCount.intValueExact();
if (count <= 0) return ItemStack.EMPTY;
Expand All @@ -207,7 +217,12 @@ public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate
int insertCount = Math.min(count, remainingImport);
if (!simulate) {
long itemValue = ProjectEAPI.getEMCProxy().getSellValue(stack);
IKnowledgeProvider provider = ProjectEAPI.getTransmutationProxy().getKnowledgeProviderFor(owner);
IKnowledgeProvider provider;
try {
provider = ProjectEAPI.getTransmutationProxy().getKnowledgeProviderFor(owner);
} catch (NullPointerException ignore) {
return stack;
}
BigInteger totalValue = BigInteger.valueOf(itemValue).multiply(BigInteger.valueOf(insertCount));
provider.setEmc(provider.getEmc().add(totalValue));
ServerPlayer player = Util.getPlayer(owner);
Expand Down Expand Up @@ -236,7 +251,12 @@ 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));
IKnowledgeProvider provider = ProjectEAPI.getTransmutationProxy().getKnowledgeProviderFor(owner);
IKnowledgeProvider provider;
try {
provider = ProjectEAPI.getTransmutationProxy().getKnowledgeProviderFor(owner);
} catch (NullPointerException ignore) {
return ItemStack.EMPTY;
}
BigInteger maxCount = provider.getEmc().divide(itemValue).min(BigInteger.valueOf(Integer.MAX_VALUE));
int extractCount = Math.min(amount, limit ? Math.min(maxCount.intValueExact(), remainingExport) : maxCount.intValueExact());
if (extractCount <= 0) return ItemStack.EMPTY;
Expand Down Expand Up @@ -334,7 +354,12 @@ public FluidStack drain(int maxDrain, FluidAction action) {
if(fluid == null || Util.getPlayer(owner) == null) return FluidStack.EMPTY;
if(maxDrain > remainingFluid) maxDrain = remainingFluid;
long cost = getFluidCost(maxDrain);
IKnowledgeProvider provider = ProjectEAPI.getTransmutationProxy().getKnowledgeProviderFor(owner);
IKnowledgeProvider provider;
try {
provider = ProjectEAPI.getTransmutationProxy().getKnowledgeProviderFor(owner);
} catch (NullPointerException ignore) {
return FluidStack.EMPTY;
}
BigInteger emc = provider.getEmc();
BigDecimal dEMC = new BigDecimal(emc);
if(dEMC.compareTo(BigDecimal.valueOf(getFluidCostPer())) < 0) return FluidStack.EMPTY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ private ItemInfo[] fetchKnowledge() {
}

private int getMaxCount(int slot) {
IKnowledgeProvider provider = ProjectEAPI.getTransmutationProxy().getKnowledgeProviderFor(owner);
IKnowledgeProvider provider;
try {
provider = ProjectEAPI.getTransmutationProxy().getKnowledgeProviderFor(owner);
} catch (NullPointerException ignore) {
return 0;
}
BigInteger playerEmc = provider.getEmc();
if (playerEmc.compareTo(BigInteger.ZERO) < 1) return 0;
BigInteger targetItemEmc = BigInteger.valueOf(ProjectEAPI.getEMCProxy().getValue(fetchKnowledge()[slot]));
Expand Down Expand Up @@ -115,7 +120,12 @@ public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate
if (simulate) return ItemStack.EMPTY;

long emcValue = ProjectEAPI.getEMCProxy().getSellValue(stack);
IKnowledgeProvider provider = ProjectEAPI.getTransmutationProxy().getKnowledgeProviderFor(owner);
IKnowledgeProvider provider;
try {
provider = ProjectEAPI.getTransmutationProxy().getKnowledgeProviderFor(owner);
} catch (NullPointerException ignore) {
return ItemStack.EMPTY;
}
BigInteger totalEmcValue = BigInteger.valueOf(emcValue).multiply(BigInteger.valueOf(count));
provider.setEmc(provider.getEmc().add(totalEmcValue));

Expand Down Expand Up @@ -143,7 +153,12 @@ public ItemStack extractItem(int slot, int amount, boolean simulate) {
if (simulate) return item;
long emcValue = ProjectEAPI.getEMCProxy().getValue(info[slot - 1]);
BigInteger totalEmcCost = BigInteger.valueOf(emcValue).multiply(BigInteger.valueOf(amount));
IKnowledgeProvider provider = ProjectEAPI.getTransmutationProxy().getKnowledgeProviderFor(owner);
IKnowledgeProvider provider;
try {
provider = ProjectEAPI.getTransmutationProxy().getKnowledgeProviderFor(owner);
} catch (NullPointerException ignore) {
return ItemStack.EMPTY;
}
provider.setEmc(provider.getEmc().subtract(totalEmcCost));
ServerPlayer player = Util.getPlayer(level, owner);
if (player != null) provider.syncEmc(player);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public static void onTick(TickEvent.ServerTickEvent event) {
IKnowledgeProvider provider;
try {
provider = ProjectEAPI.getTransmutationProxy().getKnowledgeProviderFor(uuid);
} catch(NullPointerException ignore) { continue; }
} catch(NullPointerException ignore) {
continue;
}
provider.setEmc(provider.getEmc().add(amount));
provider.syncEmc(player);
toRemove.add(uuid);
Expand Down

0 comments on commit 13361b2

Please sign in to comment.