Skip to content

Commit

Permalink
Merge branch 'refs/heads/1.20.1/dev' into 1.20.1/authorized_cards
Browse files Browse the repository at this point in the history
# Conflicts:
#	common/src/main/java/dev/ithundxr/createnumismatics/content/backend/Coin.java
  • Loading branch information
techno-sam committed Jul 21, 2024
2 parents 80702c6 + a93f5eb commit 05acc43
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,8 @@ public static void provideLang(BiConsumer<String, String> consumer) {
consumer.accept(coin.getTranslationKey() + ".plural", coin.getDefaultLangName()+"s");
}
}

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 05acc43

Please sign in to comment.