|
21 | 21 | import com.simibubi.create.foundation.blockEntity.SmartBlockEntity;
|
22 | 22 | import com.simibubi.create.foundation.blockEntity.behaviour.BehaviourType;
|
23 | 23 | import com.simibubi.create.foundation.blockEntity.behaviour.BlockEntityBehaviour;
|
| 24 | +import com.simibubi.create.foundation.utility.Couple; |
24 | 25 | import dev.ithundxr.createnumismatics.Numismatics;
|
25 | 26 | import dev.ithundxr.createnumismatics.content.backend.BankAccount;
|
| 27 | +import dev.ithundxr.createnumismatics.content.backend.Coin; |
| 28 | +import dev.ithundxr.createnumismatics.util.Utils; |
| 29 | +import net.minecraft.core.NonNullList; |
26 | 30 | import net.minecraft.nbt.CompoundTag;
|
| 31 | +import net.minecraft.world.Containers; |
| 32 | +import net.minecraft.world.item.ItemStack; |
27 | 33 |
|
28 | 34 | import java.util.UUID;
|
29 | 35 |
|
@@ -77,8 +83,47 @@ public BehaviourType<?> getType() {
|
77 | 83 | public void destroy() {
|
78 | 84 | super.destroy();
|
79 | 85 | BankAccount oldAccount = Numismatics.BANK.accounts.remove(accountUUID);
|
80 |
| - if (oldAccount != null) |
| 86 | + if (oldAccount != null) { |
81 | 87 | oldAccount.setLabel(null);
|
| 88 | + |
| 89 | + if (oldAccount.getBalance() != 0) { |
| 90 | + // Drop coins |
| 91 | + NonNullList<ItemStack> stacks = NonNullList.create(); |
| 92 | + int spurs = oldAccount.getBalance(); |
| 93 | + for (Coin coin : Coin.valuesHighToLow()) { |
| 94 | + if (spurs == 0) |
| 95 | + break; |
| 96 | + |
| 97 | + Couple<Integer> amount = coin.convert(spurs); |
| 98 | + spurs = amount.getSecond(); |
| 99 | + |
| 100 | + int coinAmount = amount.getFirst(); |
| 101 | + |
| 102 | + while (coinAmount > 64) { |
| 103 | + stacks.add(coin.asStack(64)); |
| 104 | + coinAmount -= 64; |
| 105 | + } |
| 106 | + if (coinAmount > 0) |
| 107 | + stacks.add(coin.asStack(coinAmount)); |
| 108 | + } |
| 109 | + if (!stacks.isEmpty()) { |
| 110 | + Containers.dropContents(getWorld(), getPos(), stacks); |
| 111 | + } |
| 112 | + |
| 113 | + { |
| 114 | + long start = System.currentTimeMillis(); |
| 115 | + Numismatics.LOGGER.error("Bank account behaviour removed with non-zero balance"); // set breakpoint here when developing |
| 116 | + if (Utils.isDevEnv()) { |
| 117 | + long end = System.currentTimeMillis(); |
| 118 | + if (end - start < 50) { // crash if breakpoint wasn't set |
| 119 | + throw new RuntimeException("Bank account behaviour removed with non-zero balance, please set a breakpoint above"); |
| 120 | + } |
| 121 | + } else { |
| 122 | + Numismatics.LOGGER.error("Stacktrace: ", new RuntimeException("Bank account behaviour removed with non-zero balance")); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + } |
82 | 127 | Numismatics.BANK.markBankDirty();
|
83 | 128 | }
|
84 | 129 | }
|
0 commit comments