Skip to content

Commit

Permalink
fix: vendors incorrectly say insufficient funds when in buy mode
Browse files Browse the repository at this point in the history
closes #98
  • Loading branch information
techno-sam committed Aug 7, 2024
1 parent 19a040d commit 9687054
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,27 +60,42 @@ static IAuthorizationCheckingDeductable getAutomatedAuthorizationChecking(ItemSt
return (IAuthorizationCheckingDeductable) IDeductable.getInternal(stack, owningPlayer == null ? null : Either.right(owningPlayer), reasonHolder, true);
}

@Nullable
static IDeductable getForVendor(ItemStack stack, @Nullable UUID owningPlayer, ReasonHolder reasonHolder) {
return IDeductable.getInternal(stack, owningPlayer == null ? null : Either.right(owningPlayer), reasonHolder, false, true);
}

@Nullable
private static IDeductable getInternal(ItemStack stack, @Nullable Either<Player, UUID> player, ReasonHolder reasonHolder, boolean mustBeAuthorizedDeductible) {
return getInternal(stack, player, reasonHolder, mustBeAuthorizedDeductible, false);
}

@Nullable
private static IDeductable getInternal(ItemStack stack, @Nullable Either<Player, UUID> player, ReasonHolder reasonHolder, boolean mustBeAuthorizedDeductible, boolean allowNullPlayers) {
if (NumismaticsTags.AllItemTags.CARDS.matches(stack)) {
if (player == null)
return null;

Optional<Player> left = player.left();
if (left.isEmpty())
return null;

Player player$ = left.get();
if (player$ instanceof DeployerFakePlayer)
return null;
UUID playerUUID$;
if (left.isEmpty()) {
if (!allowNullPlayers)
return null;
playerUUID$ = player.right().get();
} else {
Player player$ = left.get();
if (player$ instanceof DeployerFakePlayer)
return null;
playerUUID$ = player$.getUUID();
}

if (CardItem.isBound(stack)) {
UUID id = CardItem.get(stack);
BankAccount account = Numismatics.BANK.getAccount(id);

if (account != null && account.isAuthorized(player$)) {
if (account != null && account.isAuthorized(playerUUID$)) {
if (mustBeAuthorizedDeductible) {
return IAuthorizationCheckingDeductable.of(account, new Authorization.Player(player$, UUID.randomUUID()), account);
return IAuthorizationCheckingDeductable.of(account, new Authorization.Player(playerUUID$, UUID.randomUUID()), account);
}
return account;
} else if (account == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,13 @@ public int deductFromSelf(int maxRepetitions, @Nullable IDeductable deductable,
int price = entry.getValue();
int count = getCount.apply(coin);
if (count < price) {
addCoin.accept(coin, -count);
if (!deductable.deduct(coin, price - count, reasonHolder)) {
Numismatics.crashDev("Failed to deduct from self: " + coin + " " + (price - count));
}
} else {
addCoin.accept(coin, -price);
}
addCoin.accept(coin, -price);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package dev.ithundxr.createnumismatics.content.backend.sub_authorization;

import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -45,6 +46,12 @@ public Player(@NotNull net.minecraft.world.entity.player.Player player, @NotNull
this.authorizationID = authorizationID;
}

@ApiStatus.Internal
public Player(@NotNull UUID player, @NotNull UUID authorizationID) {
this.uuid = player;
this.authorizationID = authorizationID;
}

@Override
public boolean isHuman() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public void setChanged() {
private boolean enableAutomatedExtraction = true;
private boolean isFilterSlotLegacy = false;
public final NonNullList<ItemStack> items = NonNullList.withSize(9, ItemStack.EMPTY);
private boolean hasEnoughMoneyFromServer = false;

AbstractComputerBehaviour computerBehaviour;

Expand All @@ -133,7 +134,7 @@ public int getCoinCount(Coin coin) {
public @Nullable IDeductable getDeductable() {
ItemStack card = cardContainer.getItem(0);

return IDeductable.getAutomated(card, owner, ReasonHolder.IGNORED);
return IDeductable.getForVendor(card, owner, ReasonHolder.IGNORED);
}

@Override
Expand Down Expand Up @@ -165,6 +166,10 @@ protected void write(CompoundTag tag, boolean clientPacket) {

tag.putInt("Mode", mode.ordinal());
tag.putBoolean("EnableAutomatedExtraction", enableAutomatedExtraction);

if (clientPacket) {
tag.putBoolean("HasEnoughMoneyFromServer", hasEnoughMoneyFromServer);
}
}

@Override
Expand Down Expand Up @@ -214,6 +219,10 @@ protected void read(CompoundTag tag, boolean clientPacket) {
enableAutomatedExtraction = tag.getBoolean("EnableAutomatedExtraction");
else
enableAutomatedExtraction = true;

if (clientPacket) {
hasEnoughMoneyFromServer = tag.getBoolean("HasEnoughMoneyFromServer");
}
}

@Nullable
Expand Down Expand Up @@ -243,7 +252,7 @@ public boolean isTrustedInternal(Player player) {

public void addCoin(Coin coin, int count) {
UUID depositAccount = getDepositAccount();
if (depositAccount != null) {
if (depositAccount != null && count >= 0) {
BankAccount account = Numismatics.BANK.getAccount(depositAccount);
if (account != null) {
account.deposit(coin, count);
Expand Down Expand Up @@ -294,6 +303,14 @@ public void lazyTick() {
}
}
}

if (getMode() == Mode.BUY) {
boolean hasEnoughMoney = hasEnoughMoney();
if (hasEnoughMoney != hasEnoughMoneyFromServer) {
hasEnoughMoneyFromServer = hasEnoughMoney;
sendData();
}
}
}

void notifyDelayedDataSync() {
Expand Down Expand Up @@ -349,7 +366,7 @@ public boolean addToTooltip(List<Component> tooltip, boolean isPlayerSneaking) {
.style(ChatFormatting.DARK_RED)
.forGoggles(tooltip);
}
} else if (!hasEnoughMoney()) {
} else if (!hasEnoughMoneyFromServer) {
Lang.builder()
.add(Components.translatable("gui.numismatics.vendor.out_of_stock.funds"))
.style(ChatFormatting.DARK_RED)
Expand Down

0 comments on commit 9687054

Please sign in to comment.