Skip to content

Commit

Permalink
Merge pull request #21
Browse files Browse the repository at this point in the history
v2.0.7+1.20.1
  • Loading branch information
skycatminepokie authored Apr 12, 2024
2 parents 7e24032 + cc9deea commit 03d1660
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 103 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:
env:
MINECRAFT_VERSION: 1.20.1
JAVA_VERSION: 17
VERSION: 2.0.6+1.20.1
VERSION: 2.0.7+1.20.1
VERSION_TYPE: beta

permissions:
Expand Down
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
- Fixed crash when loading multiple worlds in singleplayer (#18)
- Automatically generate points config file (#18)
- Added the icon to the mod
- Fixed Economy API compatibility (thanks to @basiqueevangelist for open-sourcing their code)
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ yarn_mappings=1.20.1+build.10
loader_version=0.14.22

# Mod Properties
mod_version=2.0.6+1.20.1
mod_version=2.0.7+1.20.1
maven_group=com.skycat
archives_base_name=wbshop

Expand Down
115 changes: 59 additions & 56 deletions src/main/java/com/skycat/wbshop/econ/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,7 @@
import java.util.UUID;

public class Account implements EconomyAccount {
private final UUID owner;
/**
* The balance of this account. Modify only via {@link Account#setBalance(long)}.
*/
private long balance;
private final HashMap<Item, Long> donatedItemCounts;
private long totalItemsDonated;

public static final String POINTS_ACCOUNT = "points_account";
public static final Codec<Account> CODEC = RecordCodecBuilder.create((account) -> account.group(
Uuids.CODEC.fieldOf("owner").forGetter(Account::owner),
Codec.LONG.fieldOf("balance").forGetter(Account::balance),
Expand All @@ -43,13 +36,37 @@ public class Account implements EconomyAccount {
"count"
).fieldOf("donatedItemCounts").forGetter(Account::getDonatedItemCounts)
).apply(account, Account::new));
public static final Identifier ID = Identifier.of(WBShop.MOD_ID, POINTS_ACCOUNT);
private final UUID owner;
private final HashMap<Item, Long> donatedItemCounts;
/**
* The balance of this account. Modify only via {@link Account#setBalance(long)}.
*/
private long balance;
private long totalItemsDonated;


public HashMap<Item, Long> getDonatedItemCounts() {
return donatedItemCounts;
public Account(UUID owner) {
this(owner, 0);
}

public Account(UUID owner, long balance) {
this(owner, balance, new HashMap<>());
}

public Account(UUID owner, long balance, HashMap<Item, Long> donatedItemCounts) {
this.owner = owner;
this.balance = balance;
this.donatedItemCounts = donatedItemCounts;
totalItemsDonated = 0;
for (Long l : donatedItemCounts.values()) {
totalItemsDonated += l;
}
}

/**
* Record items as donated and award points.
*
* @param items The stacks of items to donate.
* @return The number of points awarded for donating.
*/
Expand All @@ -63,6 +80,7 @@ public long donateItems(Collection<ItemStack> items) {

/**
* Record items as donated and award points.
*
* @param stack The stack of items to donate.
* @return The number of points awarded for donating.
*/
Expand All @@ -77,66 +95,32 @@ public long donateItems(ItemStack stack) {
return value;
}

public Account(UUID owner) {
this(owner, 0);
}

public Account(UUID owner, long balance) {
this(owner, balance, new HashMap<>());
}

public Account(UUID owner, long balance, HashMap<Item, Long> donatedItemCounts) {
this.owner = owner;
this.balance = balance;
this.donatedItemCounts = donatedItemCounts;
totalItemsDonated = 0;
for (Long l : donatedItemCounts.values()) {
totalItemsDonated += l;
}
}

public void addBalance(long value) {
setBalance(balance + value);
}

public long getTotalItemsDonated() {
return totalItemsDonated;
public HashMap<Item, Long> getDonatedItemCounts() {
return donatedItemCounts;
}

public void removeBalance(long value) {
addBalance(-value);
public long getTotalItemsDonated() {
return totalItemsDonated;
}

//<editor-fold desc="Patbox's Economy API Handling">
@Override
public Text name() {
return Text.of("Worldborder Shop Account");
}
} // TODO: add player's name

@Override
public UUID owner() {
return owner;
}

@Override
public Identifier id() { // I believe this is the type of account this is
return defaultId();
}

/**
* Used for withdrawing points to a voucher.
* @param amount The amount to take from the player's account and grant as a voucher.
* @param player The player to grant the voucher to. This can be someone other than the account owner, but I don't see why you'd do that.
* @return {@code false} if the account does not have enough points, {@code true} on success.
*/
public boolean withdraw(long amount, ServerPlayerEntity player) {
if (amount <= 0) return false;
if (amount > balance) return false;
ItemStack voucher = Economy.makeVoucher(amount);

player.getInventory().offerOrDrop(voucher);
removeBalance(amount);
return true;
public Identifier id() {
return ID;
}

@Override
Expand Down Expand Up @@ -179,7 +163,7 @@ public EconomyCurrency currency() {
}

public EconomyTransaction tryTransaction(long value) {
if (balance + value >= 0){
if (balance + value >= 0) {
long old = balance;
addBalance(value);
return new EconomyTransaction.Simple(true,
Expand All @@ -190,11 +174,30 @@ public EconomyTransaction tryTransaction(long value) {
this
);
}
return new EconomyTransaction.Simple(false, Text.of("Transaction failed"), balance, balance,value, this);
}
public static Identifier defaultId() {
return Identifier.of(WBShop.MOD_ID, "points_account");
return new EconomyTransaction.Simple(false, Text.of("Transaction failed"), balance, balance, value, this);
}
//</editor-fold>


/**
* Used for withdrawing points to a voucher.
*
* @param amount The amount to take from the player's account and grant as a voucher.
* @param player The player to grant the voucher to. This can be someone other than the account owner, but I don't see why you'd do that.
* @return {@code false} if the account does not have enough points, {@code true} on success.
*/
public boolean withdraw(long amount, ServerPlayerEntity player) {
if (amount <= 0) return false;
if (amount > balance) return false;
ItemStack voucher = Economy.makeVoucher(amount);

player.getInventory().offerOrDrop(voucher);
removeBalance(amount);
return true;
}

public void removeBalance(long value) {
addBalance(-value);
}

}
85 changes: 43 additions & 42 deletions src/main/java/com/skycat/wbshop/econ/Economy.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,6 @@ private Economy(int configVersion, List<Account> accountList, String borderFunct
.build();
}

public static Economy readFromNbt(NbtCompound nbt) {
var result = CODEC.decode(NbtOps.INSTANCE, nbt.get("economy")).result();
if (result.isEmpty()) {
Utils.log("WBShop couldn't load the economy. This is normal when you start a new world.");
return null;
}
return result.get().getFirst();
}

@NotNull
public static ItemStack makeVoucher(long amount) {
if (amount <= 0) {
Expand All @@ -93,36 +84,13 @@ public static ItemStack makeVoucher(long amount) {
return voucher;
}

public String getBorderFunctionString() {
return borderFunctionString;
}

/**
* Try to set the border function.
*
* @param newExpression The string expression to parse.
* @param server The server to set the border on
* @return True if the function is valid, false if the function is not valid.
*/
public boolean setBorderFunction(String newExpression, @NonNull MinecraftServer server) {
Expression newFunction = new ExpressionBuilder(newExpression).variable("points").build();
newFunction.setVariable("points", getTotalPoints());
if (newFunction.validate().isValid()) {
borderFunction = newFunction;
borderFunctionString = newExpression;
markDirty();
WBShop.updateBorder(server);
return true;
}
return false;
}

public long getTotalPoints() {
long total = 0;
for (Account account : accounts.values()) {
total += account.balance();
public static Economy readFromNbt(NbtCompound nbt) {
var result = CODEC.decode(NbtOps.INSTANCE, nbt.get("economy")).result();
if (result.isEmpty()) {
Utils.log("WBShop couldn't load the economy. This is normal when you start a new world.");
return null;
}
return total;
return result.get().getFirst();
}

/**
Expand Down Expand Up @@ -150,6 +118,10 @@ public ArrayList<Account> getAccountList() {
return new ArrayList<>(accounts.values());
}

public String getBorderFunctionString() {
return borderFunctionString;
}

public int getConfigVersion() {
return this.configVersion;
}
Expand Down Expand Up @@ -178,20 +150,21 @@ public Text name() {

@Override
public @Nullable EconomyAccount getAccount(MinecraftServer server, GameProfile profile, String accountId) {
if (accountId.equals(Account.defaultId().toString())) {
if (accountId.equals(Account.POINTS_ACCOUNT)) {
return accounts.get(profile.getId());
}
return null;
}

@Override
public Collection<EconomyAccount> getAccounts(MinecraftServer server, GameProfile profile) {
return List.of(accounts.get(profile.getId()));
EconomyAccount account = getAccount(server, profile, Account.POINTS_ACCOUNT);
return account == null ? Collections.emptySet() : Collections.singleton(account);
}

@Override
public @Nullable EconomyCurrency getCurrency(MinecraftServer server, String currencyId) {
if (CURRENCY.id().toString().equals(currencyId)) {
if (Points.STRING_ID.equals(currencyId)) {
return CURRENCY;
}
return null;
Expand All @@ -205,7 +178,7 @@ public Collection<EconomyCurrency> getCurrencies(MinecraftServer server) {
@Override
public @Nullable String defaultAccount(MinecraftServer server, GameProfile profile, EconomyCurrency currency) {
if (currency == CURRENCY) {
return Account.defaultId().toString();
return Account.POINTS_ACCOUNT;
}
return null;
}
Expand Down Expand Up @@ -237,6 +210,34 @@ public long pointValueOf(Item itemType) {
return WBShop.globalConfig.getItemValue(itemType);
}

/**
* Try to set the border function.
*
* @param newExpression The string expression to parse.
* @param server The server to set the border on
* @return True if the function is valid, false if the function is not valid.
*/
public boolean setBorderFunction(String newExpression, @NonNull MinecraftServer server) {
Expression newFunction = new ExpressionBuilder(newExpression).variable("points").build();
newFunction.setVariable("points", getTotalPoints());
if (newFunction.validate().isValid()) {
borderFunction = newFunction;
borderFunctionString = newExpression;
markDirty();
WBShop.updateBorder(server);
return true;
}
return false;
}

public long getTotalPoints() {
long total = 0;
for (Account account : accounts.values()) {
total += account.balance();
}
return total;
}

@Override
public NbtCompound writeNbt(NbtCompound nbt) {
var encodedResult = CODEC.encode(this, NbtOps.INSTANCE, NbtOps.INSTANCE.empty()).result();
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/skycat/wbshop/econ/Points.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
import net.minecraft.util.Identifier;

public class Points implements EconomyCurrency {
public static final Identifier ID = Identifier.of(WBShop.MOD_ID, "points");
public static final String STRING_ID = "points";
@Override
public Text name() {
return Text.of("Points");
}

@Override
public Identifier id() {
return Identifier.of(WBShop.MOD_ID, "points");
return ID;
}

@Override
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/skycat/wbshop/econ/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* Handling for the economy (how points are transferred and stored).
* Thanks to @basiqueevangelist, I used a lot from <a href="https://github.com/BasiqueEvangelist/CommonBridge">CommonBridge</a> to fix problems with Patbox's Common Economy API compatibility
*/
package com.skycat.wbshop.econ;
Binary file added src/main/resources/assets/wbshop/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 03d1660

Please sign in to comment.