rewrite buy/sell system close #120 close #124

This commit is contained in:
Flemmli97 2022-02-01 18:50:55 +01:00
parent 4a1e884728
commit 828a406702
8 changed files with 347 additions and 114 deletions

View File

@ -14,6 +14,9 @@ Flan 1.7.0
- defaultEnterMessage and defaultLeaveMessage:
Automatically sets the enter/leave message to this
The claims name is passed on to this so "Entering %s" -> "Entering <Claim>"
- Rewritten buy/sell system.
Its now possible to specify money, items or xp points as a "currency" value
For more info see the https://github.com/Flemmli97/Flan/wiki/Config#buysell-handler
Flan 1.6.9
======================

View File

@ -18,7 +18,6 @@ import io.github.flemmli97.flan.claim.PermHelper;
import io.github.flemmli97.flan.config.ConfigHandler;
import io.github.flemmli97.flan.gui.ClaimMenuScreenHandler;
import io.github.flemmli97.flan.gui.PersonalGroupScreenHandler;
import io.github.flemmli97.flan.integration.currency.CommandCurrency;
import io.github.flemmli97.flan.integration.permissions.PermissionNodeHandler;
import io.github.flemmli97.flan.player.EnumDisplayType;
import io.github.flemmli97.flan.player.EnumEditMode;
@ -84,9 +83,9 @@ public class CommandClaim {
.then(Commands.literal("giveClaimBlocks").requires(src -> PermissionNodeHandler.perm(src, PermissionNodeHandler.cmdAdminGive, true)).then(Commands.argument("players", GameProfileArgument.gameProfile())
.then(Commands.argument("amount", IntegerArgumentType.integer()).executes(CommandClaim::giveClaimBlocks))))
.then(Commands.literal("buyBlocks").requires(src -> PermissionNodeHandler.perm(src, PermissionNodeHandler.cmdBuy, false))
.then(Commands.argument("amount", IntegerArgumentType.integer()).executes(CommandCurrency::buyClaimBlocks)))
.then(Commands.argument("amount", IntegerArgumentType.integer()).executes(CommandClaim::buyClaimBlocks)))
.then(Commands.literal("sellBlocks").requires(src -> PermissionNodeHandler.perm(src, PermissionNodeHandler.cmdSell, false))
.then(Commands.argument("amount", IntegerArgumentType.integer()).executes(CommandCurrency::sellClaimBlocks)))
.then(Commands.argument("amount", IntegerArgumentType.integer()).executes(CommandClaim::sellClaimBlocks)))
.then(Commands.literal("claimMessage").then(Commands.argument("type", StringArgumentType.word()).suggests((ctx, b) -> SharedSuggestionProvider.suggest(new String[]{"enter", "leave"}, b))
.then(Commands.argument("title", StringArgumentType.word()).suggests((ctx, b) -> SharedSuggestionProvider.suggest(new String[]{"title", "subtitle"}, b))
.then(Commands.literal("text").then(Commands.argument("component", ComponentArgument.textComponent()).executes(ctx -> CommandClaim.editClaimMessages(ctx, ComponentArgument.getComponent(ctx, "component")))))
@ -801,4 +800,14 @@ public class CommandClaim {
context.getSource().sendSuccess(cmdFeed, false);
return Command.SINGLE_SUCCESS;
}
private static int sellClaimBlocks(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
boolean b = ConfigHandler.config.buySellHandler.sell(context.getSource().getPlayerOrException(), Math.max(0, IntegerArgumentType.getInteger(context, "amount")), m -> context.getSource().sendSuccess(m, false));
return b ? Command.SINGLE_SUCCESS : 0;
}
private static int buyClaimBlocks(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
boolean b = ConfigHandler.config.buySellHandler.buy(context.getSource().getPlayerOrException(), Math.max(0, IntegerArgumentType.getInteger(context, "amount")), m -> context.getSource().sendSuccess(m, false));
return b ? Command.SINGLE_SUCCESS : 0;
}
}

View File

@ -0,0 +1,215 @@
package io.github.flemmli97.flan.config;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import io.github.flemmli97.flan.claim.PermHelper;
import io.github.flemmli97.flan.integration.currency.CommandCurrency;
import io.github.flemmli97.flan.player.PlayerClaimData;
import net.minecraft.ChatFormatting;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
public class BuySellHandler {
private static int[] xpCalc;
private Type buyType = Type.MONEY;
private Type sellType = Type.MONEY;
private float buyAmount = -1;
private float sellAmount = -1;
private Ingredient ingredient = Ingredient.EMPTY;
public boolean buy(ServerPlayer player, int blocks, Consumer<Component> message) {
if (this.buyAmount == -1) {
message.accept(PermHelper.simpleColoredText(ConfigHandler.langManager.get("buyDisabled"), ChatFormatting.DARK_RED));
return false;
}
switch (this.buyType) {
case MONEY -> {
return CommandCurrency.buyClaimBlocks(player, blocks, this.buyAmount, message);
}
case ITEM -> {
int deduct = Mth.ceil(blocks * this.buyAmount);
List<ItemStack> matching = new ArrayList<>();
int i = 0;
for (ItemStack stack : player.getInventory().items) {
if (this.ingredient.test(stack)) {
if (stack.isDamageableItem()) {
if (stack.getDamageValue() != 0) {
continue;
}
}
//Ignore "special" items
if (!this.isJustRenamedItem(stack)) {
continue;
}
matching.add(stack);
i += stack.getCount();
}
}
if (i < deduct) {
message.accept(PermHelper.simpleColoredText(ConfigHandler.langManager.get("buyFailItem"), ChatFormatting.DARK_RED));
return false;
}
i = deduct;
for (ItemStack stack : matching) {
if (i > stack.getCount()) {
int count = stack.getCount();
stack.setCount(0);
i -= count;
} else {
stack.shrink(i);
break;
}
}
PlayerClaimData data = PlayerClaimData.get(player);
data.setAdditionalClaims(data.getAdditionalClaims() + blocks);
message.accept(PermHelper.simpleColoredText(String.format(ConfigHandler.langManager.get("buySuccessItem"), blocks, deduct)));
return true;
}
case XP -> {
int deduct = Mth.ceil(blocks * this.buyAmount);
if (deduct < totalXpPointsForLevel(player.experienceLevel) + player.experienceProgress * xpForLevel(player.experienceLevel + 1)) {
player.giveExperiencePoints(-deduct);
PlayerClaimData data = PlayerClaimData.get(player);
data.setAdditionalClaims(data.getAdditionalClaims() + blocks);
message.accept(PermHelper.simpleColoredText(String.format(ConfigHandler.langManager.get("buySuccessXP"), blocks, deduct)));
return true;
}
message.accept(PermHelper.simpleColoredText(ConfigHandler.langManager.get("buyFailXP"), ChatFormatting.DARK_RED));
return false;
}
}
return false;
}
public boolean sell(ServerPlayer player, int blocks, Consumer<Component> message) {
if (this.sellAmount == -1) {
message.accept(PermHelper.simpleColoredText(ConfigHandler.langManager.get("sellDisabled"), ChatFormatting.DARK_RED));
return false;
}
PlayerClaimData data = PlayerClaimData.get(player);
if (data.getAdditionalClaims() - Math.max(0, data.usedClaimBlocks() - data.getClaimBlocks()) < blocks) {
message.accept(PermHelper.simpleColoredText(ConfigHandler.langManager.get("sellFail"), ChatFormatting.DARK_RED));
return false;
}
switch (this.sellType) {
case MONEY -> {
return CommandCurrency.sellClaimBlocks(player, blocks, this.sellAmount, message);
}
case ITEM -> {
if (this.ingredient.getItems().length == 0) {
return false;
}
ItemStack stack = this.ingredient.getItems()[0];
int amount = Mth.floor(blocks * this.sellAmount);
while (amount > 0) {
ItemStack toGive = stack.copy();
if (amount > 64) {
toGive.setCount(64);
amount -= 64;
} else {
toGive.setCount(amount);
amount = 0;
}
boolean bl = player.getInventory().add(toGive);
if (!bl || !toGive.isEmpty()) {
ItemEntity itemEntity = player.drop(toGive, false);
if (itemEntity != null) {
itemEntity.setNoPickUpDelay();
itemEntity.setOwner(player.getUUID());
}
}
}
data.setAdditionalClaims(data.getAdditionalClaims() - blocks);
message.accept(new TranslatableComponent(ConfigHandler.langManager.get("sellSuccessItem"), blocks, amount, new TranslatableComponent(stack.getDescriptionId()).withStyle(ChatFormatting.AQUA)));
return true;
}
case XP -> {
int amount = Mth.floor(blocks * this.buyAmount);
player.giveExperiencePoints(amount);
data.setAdditionalClaims(data.getAdditionalClaims() - blocks);
message.accept(PermHelper.simpleColoredText(String.format(ConfigHandler.langManager.get("sellSuccessXP"), blocks, amount)));
return false;
}
}
return false;
}
private boolean isJustRenamedItem(ItemStack stack) {
if (!stack.hasTag())
return true;
if (stack.getTag().getAllKeys()
.stream().allMatch(s -> s.equals("Damage") || s.equals("RepairCost") || s.equals("display"))) {
CompoundTag tag = stack.getTag().getCompound("display");
return tag.contains("Name") && tag.size() == 1;
}
return true;
}
private static int totalXpPointsForLevel(int level) {
if (xpCalc == null || level > xpCalc.length) {
xpCalc = new int[level + 50];
xpCalc[0] = 0;
for (int i = 1; i < xpCalc.length; i++) {
xpCalc[i] = xpForLevel(i) + xpCalc[i - 1];
}
}
return xpCalc[level];
}
/**
* See {@link Player#getXpNeededForNextLevel()}
*/
private static int xpForLevel(int level) {
level -= 1;
if (level >= 30) {
return 112 + (level - 30) * 9;
}
if (level >= 15) {
return 37 + (level - 15) * 5;
}
return 7 + level * 2;
}
public JsonObject toJson() {
JsonObject obj = new JsonObject();
obj.addProperty("buyType", this.buyType.toString());
obj.addProperty("sellType", this.sellType.toString());
obj.addProperty("buyValue", this.buyAmount);
obj.addProperty("sellValue", this.sellAmount);
obj.add("ingredient", this.ingredient.toJson());
return obj;
}
public void fromJson(JsonObject object) {
this.buyType = Type.valueOf(ConfigHandler.fromJson(object, "buyType", this.buyType.toString()));
this.sellType = Type.valueOf(ConfigHandler.fromJson(object, "sellType", this.sellType.toString()));
this.buyAmount = object.has("buyValue") ? object.get("buyValue").getAsFloat() : this.buyAmount;
this.sellAmount = object.has("sellValue") ? object.get("sellValue").getAsFloat() : this.sellAmount;
try {
this.ingredient = object.has("ingredient") ? Ingredient.fromJson(object.get("ingredient")) : Ingredient.EMPTY;
} catch (JsonParseException e) {
this.ingredient = Ingredient.EMPTY;
}
}
enum Type {
MONEY,
ITEM,
XP
}
}

View File

@ -47,8 +47,7 @@ public class Config {
public int claimDisplayTime = 1000;
public int permissionLevel = 2;
public int sellPrice = -1;
public int buyPrice = -1;
public BuySellHandler buySellHandler = new BuySellHandler();
public boolean lenientBlockEntityCheck;
public List<String> breakBlockBlacklist = Lists.newArrayList(
@ -167,8 +166,7 @@ public class Config {
this.claimDisplayTime = ConfigHandler.fromJson(obj, "claimDisplayTime", this.claimDisplayTime);
this.permissionLevel = ConfigHandler.fromJson(obj, "permissionLevel", this.permissionLevel);
this.sellPrice = ConfigHandler.fromJson(obj, "sellPrice", this.sellPrice);
this.buyPrice = ConfigHandler.fromJson(obj, "buyPrice", this.buyPrice);
this.buySellHandler.fromJson(ConfigHandler.fromJson(obj, "buySellHandler"));
this.lenientBlockEntityCheck = ConfigHandler.fromJson(obj, "lenientBlockEntityCheck", this.lenientBlockEntityCheck);
this.breakBlockBlacklist.clear();
@ -262,8 +260,7 @@ public class Config {
obj.addProperty("claimDisplayTime", this.claimDisplayTime);
obj.addProperty("permissionLevel", this.permissionLevel);
obj.addProperty("sellPrice", this.sellPrice);
obj.addProperty("buyPrice", this.buyPrice);
obj.add("buySellHandler", this.buySellHandler.toJson());
obj.addProperty("lenientBlockEntityCheck", this.lenientBlockEntityCheck);
JsonArray blocksBreak = new JsonArray();

View File

@ -24,6 +24,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
@ -152,8 +153,15 @@ public class LangManager {
this.defaultTranslation.put("buyDisabled", "Claimblocks purchasing is disabled");
this.defaultTranslation.put("sellFail", "Not enough claimblocks to sell");
this.defaultTranslation.put("buyFail", "Not enough money");
this.defaultTranslation.put("buyFailItem", "Not enough items");
this.defaultTranslation.put("buyFailXP", "Not enough experience points");
this.defaultTranslation.put("sellSuccess", "Sold %1$s claimblocks for %2$s");
this.defaultTranslation.put("sellSuccessItem", "Sold %1$s claimblocks for %3$s x%2$s");
this.defaultTranslation.put("sellSuccessXP", "Sold %1$s claimblocks for %2$s experience points");
this.defaultTranslation.put("buySuccess", "Bought %1$s claimblocks for %2$s");
this.defaultTranslation.put("buySuccessItem", "Bought %1$s claimblocks with %2$s items");
this.defaultTranslation.put("buySuccessXP", "Bought %1$s claimblocks with %2$s experience points");
this.defaultTranslation.put("currencyMissing", "Missing a supported currency mod");
this.defaultTranslation.put("trappedRescue", "Rescuing. Don't move for 5 seconds");
@ -214,8 +222,8 @@ public class LangManager {
this.defaultTranslationArray.put("command.giveClaimBlocks", new String[]{"giveClaimBlocks <amount>", "Gives a player additional claim blocks."});
}
private final Map<String, String> translation = new LinkedHashMap<>();
private final Map<String, String[]> translationArr = new LinkedHashMap<>();
private final Map<String, String> translation = new HashMap<>();
private final Map<String, String[]> translationArr = new HashMap<>();
private final Path confDir;
@ -260,20 +268,25 @@ public class LangManager {
FileReader reader = new FileReader(legacy);
JsonObject obj = GSON.fromJson(reader, JsonObject.class);
reader.close();
translation = new LinkedHashMap<>();
translationArr = new LinkedHashMap<>();
Map<String, String> fromConf = new HashMap<>();
Map<String, String[]> fromConfArr = new HashMap<>();
obj.entrySet().forEach(e -> {
if (e.getKey().equals("commands")) {
JsonObject commands = e.getValue().getAsJsonObject();
commands.entrySet().forEach(c -> translationArr.put("command." + c.getKey(), GSON.fromJson(c.getValue(), String[].class)));
commands.entrySet().forEach(c -> fromConfArr.put("command." + c.getKey(), GSON.fromJson(c.getValue(), String[].class)));
} else if (legacyPermissionGetter(e.getKey())) {
if (e.getValue().isJsonArray())
translationArr.put(e.getKey(), GSON.fromJson(e.getValue(), String[].class));
fromConfArr.put(e.getKey(), GSON.fromJson(e.getValue(), String[].class));
else
translationArr.put(e.getKey(), new String[]{e.getValue().getAsString()});
fromConfArr.put(e.getKey(), new String[]{e.getValue().getAsString()});
} else
translation.put(e.getKey(), e.getValue().getAsString());
fromConf.put(e.getKey(), e.getValue().getAsString());
});
//To preserve order
translation = new LinkedHashMap<>();
translationArr = new LinkedHashMap<>();
this.defaultTranslation.forEach((key, t)-> translation.put(key, fromConf.getOrDefault(key, t)));
this.defaultTranslationArray.forEach((key, t)-> translationArr.put(key, fromConfArr.getOrDefault(key, t)));
} else {
translation = this.defaultTranslation;
translationArr = this.defaultTranslationArray;
@ -311,9 +324,12 @@ public class LangManager {
});
//en_us is basically used as a default modifiable file
if (lang.equals("en_us")) {
this.defaultTranslation.forEach(this.translation::putIfAbsent);
this.defaultTranslationArray.forEach(this.translationArr::putIfAbsent);
saveTo(this.confDir.resolve("en_us.json").toFile(), this.translation, this.translationArr);
//To preserve order
Map<String, String> ordered = new LinkedHashMap<>();
Map<String, String[]> orderedArr = new LinkedHashMap<>();
this.defaultTranslation.forEach((key, t)-> ordered.put(key, this.translation.getOrDefault(key, t)));
this.defaultTranslationArray.forEach((key, t)-> orderedArr.put(key, this.translationArr.getOrDefault(key, t)));
saveTo(this.confDir.resolve("en_us.json").toFile(), ordered, orderedArr);
}
} catch (IOException e) {
if (lang.equals("en_us"))

View File

@ -1,19 +1,20 @@
package io.github.flemmli97.flan.integration.currency;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.architectury.injectables.annotations.ExpectPlatform;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import java.util.function.Consumer;
public class CommandCurrency {
@ExpectPlatform
public static int sellClaimBlocks(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
public static boolean sellClaimBlocks(ServerPlayer player, int blocks, float value, Consumer<Component> message) {
throw new AssertionError();
}
@ExpectPlatform
public static int buyClaimBlocks(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
public static boolean buyClaimBlocks(ServerPlayer player, int blocks, float value, Consumer<Component> message) {
throw new AssertionError();
}
}

View File

@ -1,9 +1,5 @@
package io.github.flemmli97.flan.integration.currency.fabric;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import io.github.flemmli97.flan.Flan;
import io.github.flemmli97.flan.claim.PermHelper;
import io.github.flemmli97.flan.config.ConfigHandler;
@ -11,57 +7,57 @@ import io.github.flemmli97.flan.player.PlayerClaimData;
import io.github.gunpowder.entities.StoredBalance;
import io.github.gunpowder.modelhandlers.BalanceHandler;
import net.minecraft.ChatFormatting;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import java.math.BigDecimal;
import java.util.function.Consumer;
public class CommandCurrencyImpl {
public static int sellClaimBlocks(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
if (!Flan.gunpowder) {
context.getSource().sendSuccess(PermHelper.simpleColoredText(ConfigHandler.langManager.get("currencyMissing"), ChatFormatting.DARK_RED), false);
return 0;
public static boolean sellClaimBlocks(ServerPlayer player, int blocks, float value, Consumer<Component> message) {
if (value == -1) {
message.accept(PermHelper.simpleColoredText(ConfigHandler.langManager.get("sellDisabled"), ChatFormatting.DARK_RED));
return false;
}
if (ConfigHandler.config.sellPrice == -1) {
context.getSource().sendSuccess(PermHelper.simpleColoredText(ConfigHandler.langManager.get("sellDisabled"), ChatFormatting.DARK_RED), false);
return 0;
if (Flan.gunpowder) {
PlayerClaimData data = PlayerClaimData.get(player);
if (data.getAdditionalClaims() - Math.max(0, data.usedClaimBlocks() - data.getClaimBlocks()) < blocks) {
message.accept(PermHelper.simpleColoredText(ConfigHandler.langManager.get("sellFail"), ChatFormatting.DARK_RED));
return false;
}
StoredBalance bal = BalanceHandler.INSTANCE.getUser(player.getUUID());
BigDecimal price = BigDecimal.valueOf(blocks * value);
bal.setBalance(bal.getBalance().add(price));
BalanceHandler.INSTANCE.updateUser(bal);
data.setAdditionalClaims(data.getAdditionalClaims() - blocks);
message.accept(PermHelper.simpleColoredText(String.format(ConfigHandler.langManager.get("sellSuccess"), blocks, price), ChatFormatting.GOLD));
return true;
}
int amount = Math.max(0, IntegerArgumentType.getInteger(context, "amount"));
PlayerClaimData data = PlayerClaimData.get(context.getSource().getPlayerOrException());
if (data.getAdditionalClaims() - Math.max(0, data.usedClaimBlocks() - data.getClaimBlocks()) < amount) {
context.getSource().sendSuccess(PermHelper.simpleColoredText(ConfigHandler.langManager.get("sellFail"), ChatFormatting.DARK_RED), false);
return 0;
}
StoredBalance bal = BalanceHandler.INSTANCE.getUser(context.getSource().getPlayerOrException().getUUID());
BigDecimal price = BigDecimal.valueOf(amount * ConfigHandler.config.sellPrice);
bal.setBalance(bal.getBalance().add(price));
BalanceHandler.INSTANCE.updateUser(bal);
data.setAdditionalClaims(data.getAdditionalClaims() - amount);
context.getSource().sendSuccess(PermHelper.simpleColoredText(String.format(ConfigHandler.langManager.get("sellSuccess"), amount, price), ChatFormatting.GOLD), false);
return Command.SINGLE_SUCCESS;
message.accept(PermHelper.simpleColoredText(ConfigHandler.langManager.get("currencyMissing"), ChatFormatting.DARK_RED));
return false;
}
public static int buyClaimBlocks(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
if (!Flan.gunpowder) {
context.getSource().sendSuccess(PermHelper.simpleColoredText(ConfigHandler.langManager.get("currencyMissing"), ChatFormatting.DARK_RED), false);
return 0;
public static boolean buyClaimBlocks(ServerPlayer player, int blocks, float value, Consumer<Component> message) {
if (value == -1) {
message.accept(PermHelper.simpleColoredText(ConfigHandler.langManager.get("buyDisabled"), ChatFormatting.DARK_RED));
return false;
}
if (ConfigHandler.config.buyPrice == -1) {
context.getSource().sendSuccess(PermHelper.simpleColoredText(ConfigHandler.langManager.get("buyDisabled"), ChatFormatting.DARK_RED), false);
return 0;
if (Flan.gunpowder) {
StoredBalance bal = BalanceHandler.INSTANCE.getUser(player.getUUID());
BigDecimal price = BigDecimal.valueOf(Math.max(0, blocks * value));
if (bal.getBalance().compareTo(price) >= 0) {
PlayerClaimData data = PlayerClaimData.get(player);
data.setAdditionalClaims(data.getAdditionalClaims() + blocks);
bal.setBalance(bal.getBalance().subtract(price));
BalanceHandler.INSTANCE.updateUser(bal);
message.accept(PermHelper.simpleColoredText(String.format(ConfigHandler.langManager.get("buySuccess"), blocks, price), ChatFormatting.GOLD));
return true;
}
message.accept(PermHelper.simpleColoredText(ConfigHandler.langManager.get("buyFail"), ChatFormatting.DARK_RED));
return false;
}
StoredBalance bal = BalanceHandler.INSTANCE.getUser(context.getSource().getPlayerOrException().getUUID());
int amount = Math.max(0, IntegerArgumentType.getInteger(context, "amount"));
BigDecimal price = BigDecimal.valueOf(amount * ConfigHandler.config.buyPrice);
if (bal.getBalance().compareTo(price) >= 0) {
PlayerClaimData data = PlayerClaimData.get(context.getSource().getPlayerOrException());
data.setAdditionalClaims(data.getAdditionalClaims() + amount);
bal.setBalance(bal.getBalance().subtract(price));
BalanceHandler.INSTANCE.updateUser(bal);
context.getSource().sendSuccess(PermHelper.simpleColoredText(String.format(ConfigHandler.langManager.get("buySuccess"), amount, price), ChatFormatting.GOLD), false);
return Command.SINGLE_SUCCESS;
}
context.getSource().sendSuccess(PermHelper.simpleColoredText(ConfigHandler.langManager.get("buyFail"), ChatFormatting.DARK_RED), false);
return 0;
message.accept(PermHelper.simpleColoredText(ConfigHandler.langManager.get("currencyMissing"), ChatFormatting.DARK_RED));
return false;
}
}

View File

@ -1,9 +1,5 @@
package io.github.flemmli97.flan.integration.currency.forge;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dicemc.money.MoneyMod;
import dicemc.money.storage.MoneyWSD;
import io.github.flemmli97.flan.Flan;
@ -11,56 +7,56 @@ import io.github.flemmli97.flan.claim.PermHelper;
import io.github.flemmli97.flan.config.ConfigHandler;
import io.github.flemmli97.flan.player.PlayerClaimData;
import net.minecraft.ChatFormatting;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import java.util.UUID;
import java.util.function.Consumer;
public class CommandCurrencyImpl {
public static int sellClaimBlocks(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
if (!Flan.diceMCMoneySign) {
context.getSource().sendSuccess(PermHelper.simpleColoredText(ConfigHandler.langManager.get("currencyMissing"), ChatFormatting.DARK_RED), false);
return 0;
public static boolean sellClaimBlocks(ServerPlayer player, int blocks, float value, Consumer<Component> message) {
if (value == -1) {
message.accept(PermHelper.simpleColoredText(ConfigHandler.langManager.get("sellDisabled"), ChatFormatting.DARK_RED));
return false;
}
if (ConfigHandler.config.sellPrice == -1) {
context.getSource().sendSuccess(PermHelper.simpleColoredText(ConfigHandler.langManager.get("sellDisabled"), ChatFormatting.DARK_RED), false);
return 0;
if (Flan.diceMCMoneySign) {
PlayerClaimData data = PlayerClaimData.get(player);
if (data.getAdditionalClaims() - Math.max(0, data.usedClaimBlocks() - data.getClaimBlocks()) < blocks) {
message.accept(PermHelper.simpleColoredText(ConfigHandler.langManager.get("sellFail"), ChatFormatting.DARK_RED));
return false;
}
double price = blocks * value;
MoneyWSD.get(player.getLevel()).changeBalance(MoneyMod.AcctTypes.PLAYER.key, player.getUUID(), price);
data.setAdditionalClaims(data.getAdditionalClaims() - blocks);
message.accept(PermHelper.simpleColoredText(String.format(ConfigHandler.langManager.get("sellSuccess"), blocks, price), ChatFormatting.GOLD));
return true;
}
int amount = Math.max(0, IntegerArgumentType.getInteger(context, "amount"));
PlayerClaimData data = PlayerClaimData.get(context.getSource().getPlayerOrException());
if (data.getAdditionalClaims() - Math.max(0, data.usedClaimBlocks() - data.getClaimBlocks()) < amount) {
context.getSource().sendSuccess(PermHelper.simpleColoredText(ConfigHandler.langManager.get("sellFail"), ChatFormatting.DARK_RED), false);
return 0;
}
double price = amount * ConfigHandler.config.sellPrice;
MoneyWSD.get(context.getSource().getLevel()).changeBalance(MoneyMod.AcctTypes.PLAYER.key, context.getSource().getPlayerOrException().getUUID(), price);
data.setAdditionalClaims(data.getAdditionalClaims() - amount);
context.getSource().sendSuccess(PermHelper.simpleColoredText(String.format(ConfigHandler.langManager.get("sellSuccess"), amount, price), ChatFormatting.GOLD), false);
return Command.SINGLE_SUCCESS;
message.accept(PermHelper.simpleColoredText(ConfigHandler.langManager.get("currencyMissing"), ChatFormatting.DARK_RED));
return false;
}
public static int buyClaimBlocks(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
if (!Flan.diceMCMoneySign) {
context.getSource().sendSuccess(PermHelper.simpleColoredText(ConfigHandler.langManager.get("currencyMissing"), ChatFormatting.DARK_RED), false);
return 0;
public static boolean buyClaimBlocks(ServerPlayer player, int blocks, float value, Consumer<Component> message) {
if (value == -1) {
message.accept(PermHelper.simpleColoredText(ConfigHandler.langManager.get("buyDisabled"), ChatFormatting.DARK_RED));
return false;
}
if (ConfigHandler.config.buyPrice == -1) {
context.getSource().sendSuccess(PermHelper.simpleColoredText(ConfigHandler.langManager.get("buyDisabled"), ChatFormatting.DARK_RED), false);
return 0;
if (Flan.diceMCMoneySign) {
UUID uuid = player.getUUID();
MoneyWSD manager = MoneyWSD.get(player.getLevel());
double bal = manager.getBalance(MoneyMod.AcctTypes.PLAYER.key, uuid);
double price = blocks * value;
if (bal >= price) {
PlayerClaimData data = PlayerClaimData.get(player);
data.setAdditionalClaims(data.getAdditionalClaims() + blocks);
manager.changeBalance(MoneyMod.AcctTypes.PLAYER.key, uuid, -price);
message.accept(PermHelper.simpleColoredText(String.format(ConfigHandler.langManager.get("buySuccess"), blocks, price), ChatFormatting.GOLD));
return true;
}
message.accept(PermHelper.simpleColoredText(ConfigHandler.langManager.get("buyFail"), ChatFormatting.DARK_RED));
return false;
}
UUID uuid = context.getSource().getPlayerOrException().getUUID();
MoneyWSD manager = MoneyWSD.get(context.getSource().getLevel());
double bal = manager.getBalance(MoneyMod.AcctTypes.PLAYER.key, uuid);
int amount = Math.max(0, IntegerArgumentType.getInteger(context, "amount"));
double price = amount * ConfigHandler.config.buyPrice;
if (bal >= price) {
PlayerClaimData data = PlayerClaimData.get(context.getSource().getPlayerOrException());
data.setAdditionalClaims(data.getAdditionalClaims() + amount);
manager.changeBalance(MoneyMod.AcctTypes.PLAYER.key, uuid, -price);
context.getSource().sendSuccess(PermHelper.simpleColoredText(String.format(ConfigHandler.langManager.get("buySuccess"), amount, price), ChatFormatting.GOLD), false);
return Command.SINGLE_SUCCESS;
}
context.getSource().sendSuccess(PermHelper.simpleColoredText(ConfigHandler.langManager.get("buyFail"), ChatFormatting.DARK_RED), false);
return 0;
message.accept(PermHelper.simpleColoredText(ConfigHandler.langManager.get("currencyMissing"), ChatFormatting.DARK_RED));
return false;
}
}