Skip to content

Commit

Permalink
fix: sneak-wrenching blaze bankers bypassed extra break check
Browse files Browse the repository at this point in the history
fixes #77
  • Loading branch information
techno-sam committed Jul 21, 2024
1 parent d92a4d2 commit a93f5eb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.simibubi.create.foundation.gui.AllIcons;
import com.simibubi.create.foundation.utility.Components;
import com.simibubi.create.foundation.utility.Couple;
import com.simibubi.create.foundation.utility.Pair;
import dev.ithundxr.createnumismatics.registry.NumismaticsIcons;
import dev.ithundxr.createnumismatics.registry.NumismaticsItems;
import dev.ithundxr.createnumismatics.util.TextUtils;
Expand Down Expand Up @@ -169,4 +168,8 @@ public static Coin getCoinFromName(String name){
}
return selectedCoin;
}

public static Iterable<Coin> valuesHighToLow() {
return Arrays.stream(values()).sorted(Comparator.comparingInt(c -> -c.value))::iterator;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@
import com.simibubi.create.foundation.blockEntity.SmartBlockEntity;
import com.simibubi.create.foundation.blockEntity.behaviour.BehaviourType;
import com.simibubi.create.foundation.blockEntity.behaviour.BlockEntityBehaviour;
import com.simibubi.create.foundation.utility.Couple;
import dev.ithundxr.createnumismatics.Numismatics;
import dev.ithundxr.createnumismatics.content.backend.BankAccount;
import dev.ithundxr.createnumismatics.content.backend.Coin;
import dev.ithundxr.createnumismatics.util.Utils;
import net.minecraft.core.NonNullList;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.Containers;
import net.minecraft.world.item.ItemStack;

import java.util.UUID;

Expand Down Expand Up @@ -77,8 +83,47 @@ public BehaviourType<?> getType() {
public void destroy() {
super.destroy();
BankAccount oldAccount = Numismatics.BANK.accounts.remove(accountUUID);
if (oldAccount != null)
if (oldAccount != null) {
oldAccount.setLabel(null);

if (oldAccount.getBalance() != 0) {
// Drop coins
NonNullList<ItemStack> stacks = NonNullList.create();
int spurs = oldAccount.getBalance();
for (Coin coin : Coin.valuesHighToLow()) {
if (spurs == 0)
break;

Couple<Integer> amount = coin.convert(spurs);
spurs = amount.getSecond();

int coinAmount = amount.getFirst();

while (coinAmount > 64) {
stacks.add(coin.asStack(64));
coinAmount -= 64;
}
if (coinAmount > 0)
stacks.add(coin.asStack(coinAmount));
}
if (!stacks.isEmpty()) {
Containers.dropContents(getWorld(), getPos(), stacks);
}

{
long start = System.currentTimeMillis();
Numismatics.LOGGER.error("Bank account behaviour removed with non-zero balance"); // set breakpoint here when developing
if (Utils.isDevEnv()) {
long end = System.currentTimeMillis();
if (end - start < 50) { // crash if breakpoint wasn't set
throw new RuntimeException("Bank account behaviour removed with non-zero balance, please set a breakpoint above");
}
} else {
Numismatics.LOGGER.error("Stacktrace: ", new RuntimeException("Bank account behaviour removed with non-zero balance"));
}
}
}
}
Numismatics.BANK.markBankDirty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ public void setPlacedBy(@NotNull Level level, @NotNull BlockPos pos, @NotNull Bl
public InteractionResult onSneakWrenched(BlockState state, UseOnContext context) {
if (!isTrusted(context.getPlayer(), context.getLevel(), context.getClickedPos()))
return InteractionResult.FAIL;
if (!mayBreak(context.getLevel(), context.getClickedPos(), state, context.getPlayer()))
return InteractionResult.FAIL;
return IWrenchable.super.onSneakWrenched(state, context);
}

Expand Down

0 comments on commit a93f5eb

Please sign in to comment.