From 146a670f383a1dea3366c14288b1370f4d637ab1 Mon Sep 17 00:00:00 2001 From: Flemmli97 Date: Mon, 31 May 2021 14:30:18 +0200 Subject: [PATCH] add gunpowder currency support. disabled by default --- Changelog.txt | 11 ++++ build.gradle | 6 ++ src/main/java/com/flemmli97/flan/Flan.java | 2 + .../flemmli97/flan/commands/CommandClaim.java | 4 ++ .../flan/commands/CommandCurrency.java | 66 +++++++++++++++++++ .../flan/commands/CommandPermission.java | 3 + .../com/flemmli97/flan/config/Config.java | 7 ++ .../com/flemmli97/flan/config/LangConfig.java | 8 +++ 8 files changed, 107 insertions(+) create mode 100644 src/main/java/com/flemmli97/flan/commands/CommandCurrency.java diff --git a/Changelog.txt b/Changelog.txt index 24968eb..20471ff 100644 --- a/Changelog.txt +++ b/Changelog.txt @@ -1,3 +1,14 @@ +Flan 1.3.3 +====================== +- Some blocks permission changed from PROJECTILES to OPENCONTAINER on direct interaction + Affected blocks: campfire, tnt, (chorus fruit), bells + Direct interaction as in right clicking the block. For actual projectile hits this remains unchanged +- Fix several entities not protected on indirect non projectile player attacks (e.g. through player ignited tnt)- + More noticable with other mods #52 +- Add gunpowder currency support. + Enables the purchase and selling of claim blocks. Price configurable. Disabled by default + Only selling and purchase of additional claim blocks possible (not the ones you get with time). + Flan 1.3.2 ====================== - Change gui item text to non italic (there are some very picky people) diff --git a/build.gradle b/build.gradle index 2a6aa33..7dc8155 100644 --- a/build.gradle +++ b/build.gradle @@ -12,10 +12,15 @@ group = project.maven_group repositories { mavenCentral() + jcenter() maven { name = 'Fabric-Permission-API' url 'https://oss.sonatype.org/content/repositories/snapshots' } + maven { + name = "Gunpowder" + url = "https://maven.martmists.com/releases" + } } dependencies { @@ -31,6 +36,7 @@ dependencies { include group: 'org.yaml', name: 'snakeyaml', version: '1.25' modImplementation 'me.lucko:fabric-permissions-api:0.1-SNAPSHOT' + modCompileOnly "io.github.gunpowder:gunpowder-api:${gunpowder_version}+1.16.2" // PSA: Some older mods, compiled on Loom 0.2.1, might have outdated Maven POMs. // You may need to force-disable transitiveness on them. } diff --git a/src/main/java/com/flemmli97/flan/Flan.java b/src/main/java/com/flemmli97/flan/Flan.java index 66a6711..7f6abfc 100644 --- a/src/main/java/com/flemmli97/flan/Flan.java +++ b/src/main/java/com/flemmli97/flan/Flan.java @@ -25,6 +25,7 @@ public class Flan implements ModInitializer { public static final Logger logger = LogManager.getLogger("flan"); public static boolean permissionAPI; + public static boolean gunpowder; @Override public void onInitialize() { @@ -40,6 +41,7 @@ public class Flan implements ModInitializer { CommandRegistrationCallback.EVENT.register(CommandClaim::register); permissionAPI = FabricLoader.getInstance().isModLoaded("fabric-permissions-api-v0"); + gunpowder = FabricLoader.getInstance().isModLoaded("gunpowder-currency"); } public void lockRegistry(MinecraftServer server) { diff --git a/src/main/java/com/flemmli97/flan/commands/CommandClaim.java b/src/main/java/com/flemmli97/flan/commands/CommandClaim.java index b661915..bd5cf5e 100644 --- a/src/main/java/com/flemmli97/flan/commands/CommandClaim.java +++ b/src/main/java/com/flemmli97/flan/commands/CommandClaim.java @@ -71,6 +71,10 @@ public class CommandClaim { .executes(CommandClaim::adminDeleteAll)))) .then(CommandManager.literal("giveClaimBlocks").requires(src -> CommandPermission.perm(src, CommandPermission.cmdAdminGive, true)).then(CommandManager.argument("players", GameProfileArgumentType.gameProfile()) .then(CommandManager.argument("amount", IntegerArgumentType.integer()).executes(CommandClaim::giveClaimBlocks)))) + .then(CommandManager.literal("buyBlocks").requires(src -> CommandPermission.perm(src, CommandPermission.cmdBuy, false)) + .then(CommandManager.argument("amount", IntegerArgumentType.integer()).executes(CommandCurrency::buyClaimBlocks))) + .then(CommandManager.literal("sellBlocks").requires(src -> CommandPermission.perm(src, CommandPermission.cmdSell, false)) + .then(CommandManager.argument("amount", IntegerArgumentType.integer()).executes(CommandCurrency::sellClaimBlocks))) .then(CommandManager.literal("group").requires(src -> CommandPermission.perm(src, CommandPermission.cmdGroup)) .then(CommandManager.literal("add").then(CommandManager.argument("group", StringArgumentType.string()).executes(CommandClaim::addGroup))) .then(CommandManager.literal("remove").then(CommandManager.argument("group", StringArgumentType.string()) diff --git a/src/main/java/com/flemmli97/flan/commands/CommandCurrency.java b/src/main/java/com/flemmli97/flan/commands/CommandCurrency.java new file mode 100644 index 0000000..a339070 --- /dev/null +++ b/src/main/java/com/flemmli97/flan/commands/CommandCurrency.java @@ -0,0 +1,66 @@ +package com.flemmli97.flan.commands; + +import com.flemmli97.flan.Flan; +import com.flemmli97.flan.claim.PermHelper; +import com.flemmli97.flan.config.ConfigHandler; +import com.flemmli97.flan.player.PlayerClaimData; +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.gunpowder.api.GunpowderMod; +import io.github.gunpowder.api.module.currency.dataholders.StoredBalance; +import io.github.gunpowder.api.module.currency.modelhandlers.BalanceHandler; +import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.util.Formatting; + +import java.math.BigDecimal; + +public class CommandCurrency { + + public static int sellClaimBlocks(CommandContext context) throws CommandSyntaxException { + if (!Flan.gunpowder) { + context.getSource().sendFeedback(PermHelper.simpleColoredText(ConfigHandler.lang.gunpowderMissing, Formatting.DARK_RED), false); + return 0; + } + if (ConfigHandler.config.sellPrice == -1) { + context.getSource().sendFeedback(PermHelper.simpleColoredText(ConfigHandler.lang.sellDisabled, Formatting.DARK_RED), false); + return 0; + } + int amount = IntegerArgumentType.getInteger(context, "amount"); + PlayerClaimData data = PlayerClaimData.get(context.getSource().getPlayer()); + if (data.getAdditionalClaims() - data.usedClaimBlocks() + data.getClaimBlocks() < amount) { + context.getSource().sendFeedback(PermHelper.simpleColoredText(ConfigHandler.lang.sellFail, Formatting.DARK_RED), false); + return 0; + } + StoredBalance bal = GunpowderMod.getInstance().getRegistry().getModelHandler(BalanceHandler.class).getUser(context.getSource().getPlayer().getUuid()); + BigDecimal price = BigDecimal.valueOf(amount * ConfigHandler.config.sellPrice); + bal.setBalance(bal.getBalance().add(price)); + data.setAdditionalClaims(data.getAdditionalClaims() - amount); + context.getSource().sendFeedback(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.sellSuccess, amount, price), Formatting.GOLD), false); + return Command.SINGLE_SUCCESS; + } + + public static int buyClaimBlocks(CommandContext context) throws CommandSyntaxException { + if (!Flan.gunpowder) { + context.getSource().sendFeedback(PermHelper.simpleColoredText(ConfigHandler.lang.gunpowderMissing, Formatting.DARK_RED), false); + return 0; + } + if (ConfigHandler.config.buyPrice == -1) { + context.getSource().sendFeedback(PermHelper.simpleColoredText(ConfigHandler.lang.buyDisabled, Formatting.DARK_RED), false); + return 0; + } + StoredBalance bal = GunpowderMod.getInstance().getRegistry().getModelHandler(BalanceHandler.class).getUser(context.getSource().getPlayer().getUuid()); + int amount = IntegerArgumentType.getInteger(context, "amount"); + BigDecimal price = BigDecimal.valueOf(amount * ConfigHandler.config.buyPrice); + if (bal.getBalance().compareTo(price) > 0) { + PlayerClaimData data = PlayerClaimData.get(context.getSource().getPlayer()); + data.setAdditionalClaims(data.getAdditionalClaims() + amount); + bal.setBalance(bal.getBalance().subtract(price)); + context.getSource().sendFeedback(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.buySuccess, amount, price), Formatting.GOLD), false); + return Command.SINGLE_SUCCESS; + } + context.getSource().sendFeedback(PermHelper.simpleColoredText(ConfigHandler.lang.buyFail, Formatting.DARK_RED), false); + return 0; + } +} diff --git a/src/main/java/com/flemmli97/flan/commands/CommandPermission.java b/src/main/java/com/flemmli97/flan/commands/CommandPermission.java index ab53910..bd12649 100644 --- a/src/main/java/com/flemmli97/flan/commands/CommandPermission.java +++ b/src/main/java/com/flemmli97/flan/commands/CommandPermission.java @@ -36,6 +36,9 @@ public class CommandPermission { public static final String cmdGroup = "flan.command.group"; public static final String cmdPermission = "flan.command.permission"; + public static final String cmdSell = "flan.command.buy"; + public static final String cmdBuy = "flan.command.sell"; + public static boolean perm(CommandSource src, String perm) { return perm(src, perm, false); } diff --git a/src/main/java/com/flemmli97/flan/config/Config.java b/src/main/java/com/flemmli97/flan/config/Config.java index 438adeb..0e35cfa 100644 --- a/src/main/java/com/flemmli97/flan/config/Config.java +++ b/src/main/java/com/flemmli97/flan/config/Config.java @@ -46,6 +46,9 @@ public class Config { public int claimDisplayTime = 1000; public int permissionLevel = 2; + public int sellPrice = -1; + public int buyPrice = -1; + public boolean log; public Map> defaultGroups = createHashMap(map -> { @@ -139,6 +142,8 @@ public class Config { }); this.log = ConfigHandler.fromJson(obj, "enableLogs", this.log); this.permissionLevel = ConfigHandler.fromJson(obj, "permissionLevel", this.permissionLevel); + this.sellPrice = ConfigHandler.fromJson(obj, "sellPrice", this.sellPrice); + this.buyPrice = ConfigHandler.fromJson(obj, "buyPrice", this.buyPrice); } catch (IOException e) { e.printStackTrace(); } @@ -182,6 +187,8 @@ public class Config { }); obj.add("globalDefaultPerms", global); obj.addProperty("enableLogs", this.log); + obj.addProperty("sellPrice", this.sellPrice); + obj.addProperty("buyPrice", this.buyPrice); try { FileWriter writer = new FileWriter(this.config); ConfigHandler.GSON.toJson(obj, writer); diff --git a/src/main/java/com/flemmli97/flan/config/LangConfig.java b/src/main/java/com/flemmli97/flan/config/LangConfig.java index 9e14cf6..a55f134 100644 --- a/src/main/java/com/flemmli97/flan/config/LangConfig.java +++ b/src/main/java/com/flemmli97/flan/config/LangConfig.java @@ -108,6 +108,14 @@ public class LangConfig { public String screenPersonalGroups = "Personal-Groups"; public String screenPersonalPermissions = "Personal Permissions for %s"; + public String sellDisabled = "Claimblocks selling is disabled"; + public String buyDisabled = "Claimblocks purchasing is disabled"; + public String sellFail = "Not enough claimblocks to sell"; + public String buyFail = "Not enough money"; + public String sellSuccess = "Sold %1$s claimblocks for %2$s"; + public String buySuccess = "Bought %1$s claimblocks for %2$s"; + public String gunpowderMissing = "Missing gunpowder currency mod"; + public LangConfig(MinecraftServer server) { File configDir = FabricLoader.getInstance().getConfigDir().resolve("flan").toFile(); //server.getSavePath(WorldSavePath.ROOT).resolve("config/claimConfigs").toFile();