Initial commit
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
package com.gmail.sneakdevs.diamondeconomy;
|
||||
|
||||
import com.gmail.sneakdevs.diamondeconomy.command.DiamondEconomyCommands;
|
||||
import com.gmail.sneakdevs.diamondeconomy.config.DiamondEconomyConfig;
|
||||
import com.gmail.sneakdevs.diamondeconomy.sql.SQLiteDatabaseManager;
|
||||
import eu.pb4.placeholders.api.PlaceholderResult;
|
||||
import eu.pb4.placeholders.api.Placeholders;
|
||||
import me.shedaniel.autoconfig.AutoConfig;
|
||||
import me.shedaniel.autoconfig.serializer.JanksonConfigSerializer;
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
|
||||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.world.level.storage.LevelResource;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class DiamondEconomy implements ModInitializer {
|
||||
public static final String MODID = "diamondeconomy";
|
||||
public static ArrayList<String> tableRegistry = new ArrayList<>();
|
||||
|
||||
public static void initServer(MinecraftServer server) {
|
||||
DiamondUtils.registerTable("CREATE TABLE IF NOT EXISTS diamonds (uuid text PRIMARY KEY, name text NOT NULL, money integer DEFAULT 0);");
|
||||
SQLiteDatabaseManager.createNewDatabase((DiamondEconomyConfig.getInstance().fileLocation != null) ? (new File(DiamondEconomyConfig.getInstance().fileLocation)) : server.getWorldPath(LevelResource.ROOT).resolve(DiamondEconomy.MODID + ".sqlite").toFile());
|
||||
}
|
||||
|
||||
public static void registerPlaceholders() {
|
||||
Placeholders.register(new ResourceLocation(MODID, "rank_from_player"), (ctx, arg) -> {
|
||||
if (ctx.hasPlayer()) {
|
||||
return PlaceholderResult.value(Component.literal(DiamondUtils.getDatabaseManager().playerRank(ctx.player().getStringUUID()) + ""));
|
||||
} else {
|
||||
return PlaceholderResult.invalid();
|
||||
}
|
||||
});
|
||||
Placeholders.register(new ResourceLocation(MODID, "rank_from_string_uuid"), (ctx, arg) -> {
|
||||
if (arg != null) {
|
||||
return PlaceholderResult.value(Component.literal(DiamondUtils.getDatabaseManager().playerRank(arg) + ""));
|
||||
} else {
|
||||
return PlaceholderResult.invalid();
|
||||
}
|
||||
});
|
||||
Placeholders.register(new ResourceLocation(MODID, "balance_from_player"), (ctx, arg) -> {
|
||||
if (ctx.hasPlayer()) {
|
||||
return PlaceholderResult.value(Component.literal(DiamondUtils.getDatabaseManager().getBalanceFromUUID(ctx.player().getStringUUID()) + ""));
|
||||
} else {
|
||||
return PlaceholderResult.invalid();
|
||||
}
|
||||
});
|
||||
Placeholders.register(new ResourceLocation(MODID, "balance_from_string_uuid"), (ctx, arg) -> {
|
||||
if (arg != null) {
|
||||
return PlaceholderResult.value(Component.literal(DiamondUtils.getDatabaseManager().getBalanceFromUUID(arg) + ""));
|
||||
} else {
|
||||
return PlaceholderResult.invalid();
|
||||
}
|
||||
});
|
||||
Placeholders.register(new ResourceLocation(MODID, "balance_from_name"), (ctx, arg) -> {
|
||||
if (arg != null) {
|
||||
return PlaceholderResult.value(Component.literal(DiamondUtils.getDatabaseManager().getBalanceFromName(arg) + ""));
|
||||
} else {
|
||||
return PlaceholderResult.invalid();
|
||||
}
|
||||
});
|
||||
Placeholders.register(new ResourceLocation(MODID, "player_from_rank"), (ctx, arg) -> {
|
||||
if (arg != null) {
|
||||
return PlaceholderResult.value(Component.literal(DiamondUtils.getDatabaseManager().rank(Integer.parseInt(arg))));
|
||||
} else {
|
||||
return PlaceholderResult.invalid();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onInitialize() {
|
||||
AutoConfig.register(DiamondEconomyConfig.class, JanksonConfigSerializer::new);
|
||||
registerPlaceholders();
|
||||
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> DiamondEconomyCommands.register(dispatcher));
|
||||
ServerLifecycleEvents.SERVER_STARTING.register(DiamondEconomy::initServer);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.gmail.sneakdevs.diamondeconomy;
|
||||
|
||||
import com.gmail.sneakdevs.diamondeconomy.config.DiamondEconomyConfig;
|
||||
import com.gmail.sneakdevs.diamondeconomy.sql.DatabaseManager;
|
||||
import com.gmail.sneakdevs.diamondeconomy.sql.SQLiteDatabaseManager;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.world.entity.item.ItemEntity;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
|
||||
public class DiamondUtils {
|
||||
public static void registerTable(String query){
|
||||
DiamondEconomy.tableRegistry.add(query);
|
||||
}
|
||||
|
||||
public static DatabaseManager getDatabaseManager() {
|
||||
return new SQLiteDatabaseManager();
|
||||
}
|
||||
|
||||
public static int dropItem(int amount, ServerPlayer player) {
|
||||
|
||||
|
||||
if (DiamondEconomyConfig.getInstance().greedyWithdraw) {
|
||||
|
||||
for (int i = DiamondEconomyConfig.getCurrencyValues().length - 1; i >= 0 && amount > 0; i--) {
|
||||
|
||||
int val = DiamondEconomyConfig.getCurrencyValues()[i];
|
||||
int currSize = DiamondEconomyConfig.getCurrency(i).getMaxStackSize();
|
||||
Item curr = DiamondEconomyConfig.getCurrency(i);
|
||||
|
||||
while (amount >= val * currSize) {
|
||||
ItemEntity itemEntity = player.drop(new ItemStack(curr, currSize), true);
|
||||
itemEntity.setNoPickUpDelay();
|
||||
amount -= val * currSize;
|
||||
}
|
||||
|
||||
if (amount >= val) {
|
||||
ItemEntity itemEntity = player.drop(new ItemStack(curr, amount / val), true);
|
||||
itemEntity.setNoPickUpDelay();
|
||||
amount -= amount / val * val;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
int val = DiamondEconomyConfig.getCurrencyValues()[0];
|
||||
int currSize = DiamondEconomyConfig.getCurrency(0).getMaxStackSize();
|
||||
Item curr = DiamondEconomyConfig.getCurrency(0);
|
||||
|
||||
while (amount >= val * currSize) {
|
||||
ItemEntity itemEntity = player.drop(new ItemStack(curr, currSize), true);
|
||||
itemEntity.setNoPickUpDelay();
|
||||
amount -= val * currSize;
|
||||
}
|
||||
|
||||
if (amount >= val) {
|
||||
ItemEntity itemEntity = player.drop(new ItemStack(curr, amount / val), true);
|
||||
itemEntity.setNoPickUpDelay();
|
||||
amount -= amount / val * val;
|
||||
}
|
||||
}
|
||||
|
||||
DatabaseManager dm = getDatabaseManager();
|
||||
dm.changeBalance(player.getStringUUID(), amount);
|
||||
|
||||
return amount;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.gmail.sneakdevs.diamondeconomy.command;
|
||||
|
||||
import com.gmail.sneakdevs.diamondeconomy.DiamondUtils;
|
||||
import com.gmail.sneakdevs.diamondeconomy.config.DiamondEconomyConfig;
|
||||
import com.gmail.sneakdevs.diamondeconomy.sql.DatabaseManager;
|
||||
import com.mojang.brigadier.arguments.StringArgumentType;
|
||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||
import com.mojang.brigadier.context.CommandContext;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.commands.Commands;
|
||||
import net.minecraft.commands.arguments.EntityArgument;
|
||||
import net.minecraft.network.chat.Component;
|
||||
|
||||
public class BalanceCommand {
|
||||
public static LiteralArgumentBuilder<CommandSourceStack> buildCommand(){
|
||||
return Commands.literal(DiamondEconomyConfig.getInstance().balanceCommandName)
|
||||
.then(
|
||||
Commands.argument("playerName", StringArgumentType.string())
|
||||
.executes(e -> {
|
||||
String string = StringArgumentType.getString(e, "playerName");
|
||||
return balanceCommand(e, string);
|
||||
})
|
||||
)
|
||||
.then(
|
||||
Commands.argument("player", EntityArgument.player())
|
||||
.executes(e -> {
|
||||
String player = EntityArgument.getPlayer(e, "player").getName().getString();
|
||||
return balanceCommand(e, player);
|
||||
})
|
||||
)
|
||||
.executes(e -> balanceCommand(e, e.getSource().getPlayerOrException().getName().getString()));
|
||||
}
|
||||
|
||||
public static int balanceCommand(CommandContext<CommandSourceStack> ctx, String player) {
|
||||
DatabaseManager dm = DiamondUtils.getDatabaseManager();
|
||||
int bal = dm.getBalanceFromName(player);
|
||||
ctx.getSource().sendSuccess(() -> Component.literal((bal > -1) ? (player + " has $" + bal) : ("No account was found for player with the name \"" + player + "\"")), false);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.gmail.sneakdevs.diamondeconomy.command;
|
||||
|
||||
import com.gmail.sneakdevs.diamondeconomy.DiamondUtils;
|
||||
import com.gmail.sneakdevs.diamondeconomy.config.DiamondEconomyConfig;
|
||||
import com.gmail.sneakdevs.diamondeconomy.sql.DatabaseManager;
|
||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||
import com.mojang.brigadier.context.CommandContext;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.commands.Commands;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.Items;
|
||||
|
||||
public class DepositCommand {
|
||||
public static LiteralArgumentBuilder<CommandSourceStack> buildCommand(){
|
||||
return Commands.literal(DiamondEconomyConfig.getInstance().depositCommandName)
|
||||
.executes(DepositCommand::depositCommand);
|
||||
}
|
||||
|
||||
public static int depositCommand(CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {
|
||||
ServerPlayer player = ctx.getSource().getPlayerOrException();
|
||||
DatabaseManager dm = DiamondUtils.getDatabaseManager();
|
||||
int currencyCount = 0;
|
||||
for (int i = DiamondEconomyConfig.getCurrencyValues().length - 1; i >= 0; i--) {
|
||||
for (int j = 0; j < player.getInventory().getContainerSize(); j++) {
|
||||
if (player.getInventory().getItem(j).getItem().equals(DiamondEconomyConfig.getCurrency(i))) {
|
||||
currencyCount += player.getInventory().getItem(j).getCount() * DiamondEconomyConfig.getCurrencyValues()[i];
|
||||
player.getInventory().setItem(j, new ItemStack(Items.AIR));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (dm.changeBalance(player.getStringUUID(), currencyCount)) {
|
||||
String output = "Added $" + currencyCount + " to your account";
|
||||
ctx.getSource().sendSuccess(() -> Component.literal(output), false);
|
||||
} else {
|
||||
DiamondUtils.dropItem(currencyCount, player);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.gmail.sneakdevs.diamondeconomy.command;
|
||||
|
||||
import com.gmail.sneakdevs.diamondeconomy.config.DiamondEconomyConfig;
|
||||
import com.mojang.brigadier.CommandDispatcher;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.commands.Commands;
|
||||
|
||||
public class DiamondEconomyCommands {
|
||||
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
|
||||
if (DiamondEconomyConfig.getInstance().commandName == null) {
|
||||
if (DiamondEconomyConfig.getInstance().modifyCommandName != null) {
|
||||
dispatcher.register(ModifyCommand.buildCommand());
|
||||
}
|
||||
if (DiamondEconomyConfig.getInstance().balanceCommandName != null) {
|
||||
dispatcher.register(BalanceCommand.buildCommand());
|
||||
}
|
||||
if (DiamondEconomyConfig.getInstance().topCommandName != null) {
|
||||
dispatcher.register(TopCommand.buildCommand());
|
||||
}
|
||||
if (DiamondEconomyConfig.getInstance().depositCommandName != null) {
|
||||
dispatcher.register(DepositCommand.buildCommand());
|
||||
}
|
||||
if (DiamondEconomyConfig.getInstance().sendCommandName != null) {
|
||||
dispatcher.register(SendCommand.buildCommand());
|
||||
}
|
||||
if (DiamondEconomyConfig.getInstance().setCommandName != null) {
|
||||
dispatcher.register(SetCommand.buildCommand());
|
||||
}
|
||||
if (DiamondEconomyConfig.getInstance().withdrawCommandName != null) {
|
||||
dispatcher.register(WithdrawCommand.buildCommand());
|
||||
}
|
||||
} else {
|
||||
if (DiamondEconomyConfig.getInstance().modifyCommandName != null) {
|
||||
dispatcher.register(Commands.literal(DiamondEconomyConfig.getInstance().commandName).then(ModifyCommand.buildCommand()));
|
||||
}
|
||||
if (DiamondEconomyConfig.getInstance().balanceCommandName != null) {
|
||||
dispatcher.register(Commands.literal(DiamondEconomyConfig.getInstance().commandName).then(BalanceCommand.buildCommand()));
|
||||
}
|
||||
if (DiamondEconomyConfig.getInstance().topCommandName != null) {
|
||||
dispatcher.register(Commands.literal(DiamondEconomyConfig.getInstance().commandName).then(TopCommand.buildCommand()));
|
||||
}
|
||||
if (DiamondEconomyConfig.getInstance().depositCommandName != null) {
|
||||
dispatcher.register(Commands.literal(DiamondEconomyConfig.getInstance().commandName).then(DepositCommand.buildCommand()));
|
||||
}
|
||||
if (DiamondEconomyConfig.getInstance().sendCommandName != null) {
|
||||
dispatcher.register(Commands.literal(DiamondEconomyConfig.getInstance().commandName).then(SendCommand.buildCommand()));
|
||||
}
|
||||
if (DiamondEconomyConfig.getInstance().setCommandName != null) {
|
||||
dispatcher.register(Commands.literal(DiamondEconomyConfig.getInstance().commandName).then(SetCommand.buildCommand()));
|
||||
}
|
||||
if (DiamondEconomyConfig.getInstance().withdrawCommandName != null) {
|
||||
dispatcher.register(Commands.literal(DiamondEconomyConfig.getInstance().commandName).then(WithdrawCommand.buildCommand()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.gmail.sneakdevs.diamondeconomy.command;
|
||||
|
||||
import com.gmail.sneakdevs.diamondeconomy.DiamondUtils;
|
||||
import com.gmail.sneakdevs.diamondeconomy.config.DiamondEconomyConfig;
|
||||
import com.mojang.brigadier.arguments.BoolArgumentType;
|
||||
import com.mojang.brigadier.arguments.IntegerArgumentType;
|
||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||
import com.mojang.brigadier.context.CommandContext;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.commands.Commands;
|
||||
import net.minecraft.commands.arguments.EntityArgument;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public class ModifyCommand {
|
||||
public static LiteralArgumentBuilder<CommandSourceStack> buildCommand(){
|
||||
return Commands.literal(DiamondEconomyConfig.getInstance().modifyCommandName)
|
||||
.requires((permission) -> permission.hasPermission(DiamondEconomyConfig.getInstance().opCommandsPermissionLevel))
|
||||
.then(
|
||||
Commands.argument("players", EntityArgument.players())
|
||||
.then(
|
||||
Commands.argument("amount", IntegerArgumentType.integer())
|
||||
.executes(e -> {
|
||||
int amount = IntegerArgumentType.getInteger(e, "amount");
|
||||
return modifyCommand(e, EntityArgument.getPlayers(e, "players").stream().toList(), amount);
|
||||
}))
|
||||
)
|
||||
.then(
|
||||
Commands.argument("amount", IntegerArgumentType.integer())
|
||||
.then(
|
||||
Commands.argument("shouldModifyAll", BoolArgumentType.bool())
|
||||
.executes(e -> {
|
||||
int amount = IntegerArgumentType.getInteger(e, "amount");
|
||||
boolean shouldModifyAll = BoolArgumentType.getBool(e, "shouldModifyAll");
|
||||
return modifyCommand(e, amount, shouldModifyAll);
|
||||
})
|
||||
)
|
||||
.executes(e -> {
|
||||
int amount = IntegerArgumentType.getInteger(e, "amount");
|
||||
return modifyCommand(e, amount, false);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
public static int modifyCommand(CommandContext<CommandSourceStack> ctx, Collection<ServerPlayer> players, int amount) {
|
||||
players.forEach(player -> ctx.getSource().sendSuccess(() -> Component.literal((DiamondUtils.getDatabaseManager().changeBalance(player.getStringUUID(), amount)) ? ("Modified " + players.size() + " players money by $" + amount) : ("That would go out of the valid money range for " + player.getName().getString())), true));
|
||||
return players.size();
|
||||
}
|
||||
|
||||
public static int modifyCommand(CommandContext<CommandSourceStack> ctx, int amount, boolean shouldModifyAll) throws CommandSyntaxException {
|
||||
if (shouldModifyAll) {
|
||||
DiamondUtils.getDatabaseManager().changeAllBalance(amount);
|
||||
ctx.getSource().sendSuccess(() -> Component.literal(("Modified everyones account by $" + amount)), true);
|
||||
} else {
|
||||
String output = (DiamondUtils.getDatabaseManager().changeBalance(ctx.getSource().getPlayerOrException().getStringUUID(), amount)) ? ("Modified your money by $" + amount) : ("That would go out of your valid money range");
|
||||
ctx.getSource().sendSuccess(() -> Component.literal(output), true);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.gmail.sneakdevs.diamondeconomy.command;
|
||||
|
||||
import com.gmail.sneakdevs.diamondeconomy.DiamondUtils;
|
||||
import com.gmail.sneakdevs.diamondeconomy.config.DiamondEconomyConfig;
|
||||
import com.gmail.sneakdevs.diamondeconomy.sql.DatabaseManager;
|
||||
import com.mojang.brigadier.arguments.IntegerArgumentType;
|
||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||
import com.mojang.brigadier.context.CommandContext;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.commands.Commands;
|
||||
import net.minecraft.commands.arguments.EntityArgument;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
|
||||
public class SendCommand {
|
||||
public static LiteralArgumentBuilder<CommandSourceStack> buildCommand(){
|
||||
return Commands.literal(DiamondEconomyConfig.getInstance().sendCommandName)
|
||||
.then(
|
||||
Commands.argument("player", EntityArgument.player())
|
||||
.then(
|
||||
Commands.argument("amount", IntegerArgumentType.integer(1))
|
||||
.executes(e -> {
|
||||
ServerPlayer player = EntityArgument.getPlayer(e, "player");
|
||||
int amount = IntegerArgumentType.getInteger(e, "amount");
|
||||
return sendCommand(e, player, e.getSource().getPlayerOrException(), amount);
|
||||
})));
|
||||
}
|
||||
|
||||
public static int sendCommand(CommandContext<CommandSourceStack> ctx, ServerPlayer player, ServerPlayer player1, int amount) throws CommandSyntaxException {
|
||||
DatabaseManager dm = DiamondUtils.getDatabaseManager();
|
||||
long newValue = dm.getBalanceFromUUID(player.getStringUUID()) + amount;
|
||||
if (newValue < Integer.MAX_VALUE && dm.changeBalance(player1.getStringUUID(), -amount)) {
|
||||
dm.changeBalance(player.getStringUUID(), amount);
|
||||
player.displayClientMessage(Component.literal("You received $" + amount + " from " + player1.getName().getString()), false);
|
||||
ctx.getSource().sendSuccess(() -> Component.literal("Sent $" + amount + " to " + player.getName().getString()), false);
|
||||
} else {
|
||||
ctx.getSource().sendSuccess(() -> Component.literal("Failed because that would go over the max value"), false);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.gmail.sneakdevs.diamondeconomy.command;
|
||||
|
||||
import com.gmail.sneakdevs.diamondeconomy.DiamondUtils;
|
||||
import com.gmail.sneakdevs.diamondeconomy.config.DiamondEconomyConfig;
|
||||
import com.gmail.sneakdevs.diamondeconomy.sql.DatabaseManager;
|
||||
import com.mojang.brigadier.arguments.BoolArgumentType;
|
||||
import com.mojang.brigadier.arguments.IntegerArgumentType;
|
||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||
import com.mojang.brigadier.context.CommandContext;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.commands.Commands;
|
||||
import net.minecraft.commands.arguments.EntityArgument;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public class SetCommand {
|
||||
public static LiteralArgumentBuilder<CommandSourceStack> buildCommand(){
|
||||
return Commands.literal(DiamondEconomyConfig.getInstance().setCommandName)
|
||||
.requires((permission) -> permission.hasPermission(DiamondEconomyConfig.getInstance().opCommandsPermissionLevel))
|
||||
.then(
|
||||
Commands.argument("players", EntityArgument.players())
|
||||
.then(
|
||||
Commands.argument("amount", IntegerArgumentType.integer(0))
|
||||
.executes(e -> {
|
||||
int amount = IntegerArgumentType.getInteger(e, "amount");
|
||||
return setCommand(e, EntityArgument.getPlayers(e, "players").stream().toList(), amount);
|
||||
}))
|
||||
)
|
||||
.then(
|
||||
Commands.argument("amount", IntegerArgumentType.integer(0))
|
||||
.then(
|
||||
Commands.argument("shouldModifyAll", BoolArgumentType.bool())
|
||||
.executes(e -> {
|
||||
int amount = IntegerArgumentType.getInteger(e, "amount");
|
||||
boolean shouldModifyAll = BoolArgumentType.getBool(e, "shouldModifyAll");
|
||||
return setCommand(e, amount, shouldModifyAll);
|
||||
})
|
||||
)
|
||||
.executes(e -> {
|
||||
int amount = IntegerArgumentType.getInteger(e, "amount");
|
||||
return setCommand(e, amount, false);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
public static int setCommand(CommandContext<CommandSourceStack> ctx, Collection<ServerPlayer> players, int amount) {
|
||||
DatabaseManager dm = DiamondUtils.getDatabaseManager();
|
||||
players.forEach(player -> dm.setBalance(player.getStringUUID(), amount));
|
||||
ctx.getSource().sendSuccess(() -> Component.literal("Updated balance of " + players.size() + " players to " + amount), true);
|
||||
return players.size();
|
||||
}
|
||||
|
||||
public static int setCommand(CommandContext<CommandSourceStack> ctx, int amount, boolean shouldModifyAll) throws CommandSyntaxException {
|
||||
if (shouldModifyAll) {
|
||||
DiamondUtils.getDatabaseManager().setAllBalance(amount);
|
||||
ctx.getSource().sendSuccess(() -> Component.literal("All accounts balance to " + amount), true);
|
||||
} else {
|
||||
DiamondUtils.getDatabaseManager().setBalance(ctx.getSource().getPlayerOrException().getStringUUID(), amount);
|
||||
ctx.getSource().sendSuccess(() -> Component.literal("Updated your balance to " + amount), true);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.gmail.sneakdevs.diamondeconomy.command;
|
||||
|
||||
import com.gmail.sneakdevs.diamondeconomy.DiamondUtils;
|
||||
import com.gmail.sneakdevs.diamondeconomy.config.DiamondEconomyConfig;
|
||||
import com.gmail.sneakdevs.diamondeconomy.sql.DatabaseManager;
|
||||
import com.mojang.brigadier.arguments.IntegerArgumentType;
|
||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||
import com.mojang.brigadier.context.CommandContext;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.commands.Commands;
|
||||
import net.minecraft.network.chat.Component;
|
||||
|
||||
public class TopCommand {
|
||||
public static LiteralArgumentBuilder<CommandSourceStack> buildCommand(){
|
||||
return Commands.literal(DiamondEconomyConfig.getInstance().topCommandName)
|
||||
.then(
|
||||
Commands.argument("page", IntegerArgumentType.integer(1))
|
||||
.executes(e -> {
|
||||
int page = IntegerArgumentType.getInteger(e, "page");
|
||||
return topCommand(e, page);
|
||||
})
|
||||
)
|
||||
.executes(e -> topCommand(e, 1));
|
||||
}
|
||||
|
||||
public static int topCommand(CommandContext<CommandSourceStack> ctx, int page) throws CommandSyntaxException {
|
||||
DatabaseManager dm = DiamondUtils.getDatabaseManager();
|
||||
String output = dm.top(ctx.getSource().getPlayerOrException().getStringUUID(), page);
|
||||
ctx.getSource().sendSuccess(() -> Component.literal(output), false);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.gmail.sneakdevs.diamondeconomy.command;
|
||||
|
||||
import com.gmail.sneakdevs.diamondeconomy.DiamondUtils;
|
||||
import com.gmail.sneakdevs.diamondeconomy.config.DiamondEconomyConfig;
|
||||
import com.gmail.sneakdevs.diamondeconomy.sql.DatabaseManager;
|
||||
import com.mojang.brigadier.arguments.IntegerArgumentType;
|
||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||
import com.mojang.brigadier.context.CommandContext;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.commands.Commands;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
|
||||
public class WithdrawCommand {
|
||||
public static LiteralArgumentBuilder<CommandSourceStack> buildCommand(){
|
||||
return Commands.literal(DiamondEconomyConfig.getInstance().withdrawCommandName)
|
||||
.then(
|
||||
Commands.argument("amount", IntegerArgumentType.integer(1))
|
||||
.executes(e -> {
|
||||
int amount = IntegerArgumentType.getInteger(e, "amount");
|
||||
return withdrawCommand(e, amount);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
public static int withdrawCommand(CommandContext<CommandSourceStack> ctx, int amount) throws CommandSyntaxException {
|
||||
ServerPlayer player = ctx.getSource().getPlayerOrException();
|
||||
DatabaseManager dm = DiamondUtils.getDatabaseManager();
|
||||
if (dm.changeBalance(player.getStringUUID(), -amount)) {
|
||||
ctx.getSource().sendSuccess(() -> Component.literal("Withdrew $" + (amount - DiamondUtils.dropItem(amount, player))), false);
|
||||
} else {
|
||||
ctx.getSource().sendSuccess(() -> Component.literal("You have less than $" + amount), false);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.gmail.sneakdevs.diamondeconomy.config;
|
||||
|
||||
import com.gmail.sneakdevs.diamondeconomy.DiamondEconomy;
|
||||
import me.shedaniel.autoconfig.AutoConfig;
|
||||
import me.shedaniel.autoconfig.ConfigData;
|
||||
import me.shedaniel.autoconfig.annotation.Config;
|
||||
import me.shedaniel.cloth.clothconfig.shadowed.blue.endless.jankson.Comment;
|
||||
import net.minecraft.core.RegistryCodecs;
|
||||
import net.minecraft.core.registries.BuiltInRegistries;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.world.item.Item;
|
||||
|
||||
@Config(name = DiamondEconomy.MODID)
|
||||
public class DiamondEconomyConfig implements ConfigData {
|
||||
|
||||
@Comment("List of items used as currency")
|
||||
public String[] currencies = {"minecraft:diamond","minecraft:diamond_block"};
|
||||
|
||||
@Comment("Values of each currency in the same order, decimals not allowed (must be in ascending order unless greedyWithdraw is disabled)")
|
||||
public int[] currencyValues = {1,9};
|
||||
|
||||
@Comment("Where the diamondeconomy.sqlite file is located (ex: \"C:/Users/example/Desktop/server/world/diamondeconomy.sqlite\")")
|
||||
public String fileLocation = null;
|
||||
|
||||
@Comment("Name of the base command (null to disable base command)")
|
||||
public String commandName = "diamonds";
|
||||
|
||||
@Comment("Names of the subcommands (null to disable command)")
|
||||
public String topCommandName = "top";
|
||||
public String balanceCommandName = "balance";
|
||||
public String depositCommandName = "deposit";
|
||||
public String sendCommandName = "send";
|
||||
public String withdrawCommandName = "withdraw";
|
||||
|
||||
@Comment("Names of the admin subcommands (null to disable command)")
|
||||
public String setCommandName = "set";
|
||||
public String modifyCommandName = "modify";
|
||||
|
||||
@Comment("Try to withdraw items using the most high value items possible (ex. diamond blocks then diamonds) \n If disabled withdraw will give player the first item in the list")
|
||||
public boolean greedyWithdraw = true;
|
||||
|
||||
@Comment("Money the player starts with when they first join the server")
|
||||
public int startingMoney = 0;
|
||||
|
||||
@Comment("How often to add money to each player, in seconds (0 to disable)")
|
||||
public int moneyAddTimer = 0;
|
||||
|
||||
@Comment("Amount of money to add each cycle")
|
||||
public int moneyAddAmount = 0;
|
||||
|
||||
@Comment("Permission level (1-4) of the op commands in diamond economy. Set to 2 to allow command blocks to use these commands.")
|
||||
public int opCommandsPermissionLevel = 4;
|
||||
|
||||
public static Item getCurrency(int num) {
|
||||
return BuiltInRegistries.ITEM.get(ResourceLocation.tryParse(DiamondEconomyConfig.getInstance().currencies[num]));
|
||||
}
|
||||
|
||||
public static String getCurrencyName(int num) {
|
||||
return BuiltInRegistries.ITEM.get(ResourceLocation.tryParse(DiamondEconomyConfig.getInstance().currencies[num])).getDescription().getString();
|
||||
}
|
||||
|
||||
public static int[] getCurrencyValues() {
|
||||
return DiamondEconomyConfig.getInstance().currencyValues;
|
||||
}
|
||||
|
||||
public static DiamondEconomyConfig getInstance() {
|
||||
return AutoConfig.getConfigHolder(DiamondEconomyConfig.class).getConfig();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.gmail.sneakdevs.diamondeconomy.mixin;
|
||||
|
||||
import com.gmail.sneakdevs.diamondeconomy.DiamondUtils;
|
||||
import com.gmail.sneakdevs.diamondeconomy.sql.DatabaseManager;
|
||||
import net.minecraft.network.Connection;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.server.players.PlayerList;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
@Mixin(PlayerList.class)
|
||||
public class PlayerListMixin_DiamondEconomy {
|
||||
@Inject(method = "placeNewPlayer", at = @At("TAIL"))
|
||||
private void diamondeconomy_onPlayerConnectMixin(Connection connection, ServerPlayer serverPlayer, CallbackInfo ci) {
|
||||
DatabaseManager dm = DiamondUtils.getDatabaseManager();
|
||||
String uuid = serverPlayer.getStringUUID();
|
||||
String name = serverPlayer.getName().getString();
|
||||
dm.addPlayer(uuid, name);
|
||||
dm.setName(uuid, name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.gmail.sneakdevs.diamondeconomy.mixin;
|
||||
|
||||
import com.gmail.sneakdevs.diamondeconomy.DiamondUtils;
|
||||
import com.gmail.sneakdevs.diamondeconomy.config.DiamondEconomyConfig;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.stats.Stats;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
@Mixin(ServerPlayer.class)
|
||||
public class ServerPlayerMixin {
|
||||
@Inject(method = "tick", at = @At("TAIL"))
|
||||
private void diamondeconomy_tickMixin(CallbackInfo ci) {
|
||||
if (DiamondEconomyConfig.getInstance().moneyAddAmount != 0 && DiamondEconomyConfig.getInstance().moneyAddTimer > 0) {
|
||||
int playTime = ((ServerPlayer)(Object)this).getStats().getValue(Stats.CUSTOM.get(Stats.PLAY_TIME));
|
||||
if (playTime > 0 && playTime % (DiamondEconomyConfig.getInstance().moneyAddTimer * 20) == 0) {
|
||||
DiamondUtils.getDatabaseManager().changeBalance(((ServerPlayer)(Object)this).getStringUUID(), DiamondEconomyConfig.getInstance().moneyAddAmount);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.gmail.sneakdevs.diamondeconomy.sql;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public interface DatabaseManager {
|
||||
static void createNewDatabase(File file) {}
|
||||
|
||||
void addPlayer(String uuid, String name);
|
||||
void updateName(String uuid, String name);
|
||||
void setName(String uuid, String name);
|
||||
String getNameFromUUID(String uuid);
|
||||
|
||||
int getBalanceFromUUID(String uuid);
|
||||
int getBalanceFromName(String name);
|
||||
boolean setBalance(String uuid, int money);
|
||||
void setAllBalance(int money);
|
||||
boolean changeBalance(String uuid, int money);
|
||||
void changeAllBalance(int money);
|
||||
|
||||
String top(String uuid, int topAmount);
|
||||
String rank(int rank);
|
||||
int playerRank(String uuid);
|
||||
}
|
||||
@@ -0,0 +1,234 @@
|
||||
package com.gmail.sneakdevs.diamondeconomy.sql;
|
||||
|
||||
import com.gmail.sneakdevs.diamondeconomy.DiamondEconomy;
|
||||
import com.gmail.sneakdevs.diamondeconomy.config.DiamondEconomyConfig;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.*;
|
||||
|
||||
public class SQLiteDatabaseManager implements DatabaseManager {
|
||||
public static String url;
|
||||
|
||||
public static void createNewDatabase(File file) {
|
||||
url = "jdbc:sqlite:" + file.getPath().replace('\\', '/');
|
||||
|
||||
Connection conn = null;
|
||||
try {
|
||||
conn = DriverManager.getConnection(url);
|
||||
} catch (SQLException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
|
||||
createNewTable();
|
||||
}
|
||||
|
||||
public Connection connect() {
|
||||
Connection conn = null;
|
||||
try {
|
||||
conn = DriverManager.getConnection(url);
|
||||
} catch (SQLException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
return conn;
|
||||
}
|
||||
|
||||
private static void createNewTable() {
|
||||
try (Connection conn = DriverManager.getConnection(url); Statement stmt = conn.createStatement()) {
|
||||
for (String query : DiamondEconomy.tableRegistry) {
|
||||
stmt.execute(query);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void addPlayer(String uuid, String name) {
|
||||
String sql = "INSERT INTO diamonds(uuid,name,money) VALUES(?,?,?)";
|
||||
|
||||
try (Connection conn = this.connect(); PreparedStatement pstmt = conn.prepareStatement(sql)){
|
||||
pstmt.setString(1, uuid);
|
||||
pstmt.setString(2, name);
|
||||
pstmt.setInt(3, DiamondEconomyConfig.getInstance().startingMoney);
|
||||
pstmt.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
updateName(uuid, name);
|
||||
}
|
||||
}
|
||||
|
||||
public void updateName(String uuid, String name) {
|
||||
String sql = "UPDATE diamonds SET name = ? WHERE uuid = ?";
|
||||
|
||||
try (Connection conn = this.connect(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
|
||||
pstmt.setString(1, name);
|
||||
pstmt.setString(2, uuid);
|
||||
pstmt.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void setName(String uuid, String name) {
|
||||
String sql = "UPDATE diamonds SET name = ? WHERE uuid != ? AND name = ?";
|
||||
|
||||
try (Connection conn = this.connect(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
|
||||
pstmt.setString(1, "a");
|
||||
pstmt.setString(2, uuid);
|
||||
pstmt.setString(3, name);
|
||||
pstmt.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public int getBalanceFromUUID(String uuid){
|
||||
String sql = "SELECT uuid, money FROM diamonds WHERE uuid = '" + uuid + "'";
|
||||
|
||||
try (Connection conn = this.connect(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql)){
|
||||
rs.next();
|
||||
return rs.getInt("money");
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public String getNameFromUUID(String uuid){
|
||||
String sql = "SELECT uuid, name FROM diamonds WHERE uuid = '" + uuid + "'";
|
||||
|
||||
try (Connection conn = this.connect(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql)){
|
||||
rs.next();
|
||||
return rs.getString("name");
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public int getBalanceFromName(String name){
|
||||
String sql = "SELECT name, money FROM diamonds WHERE name = '" + name + "'";
|
||||
|
||||
try (Connection conn = this.connect(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql)){
|
||||
rs.next();
|
||||
return rs.getInt("money");
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public boolean setBalance(String uuid, int money) {
|
||||
String sql = "UPDATE diamonds SET money = ? WHERE uuid = ?";
|
||||
|
||||
try (Connection conn = this.connect(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
|
||||
if (money >= 0 && money < Integer.MAX_VALUE) {
|
||||
pstmt.setInt(1, money);
|
||||
pstmt.setString(2, uuid);
|
||||
pstmt.executeUpdate();
|
||||
return true;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void setAllBalance(int money) {
|
||||
String sql = "UPDATE diamonds SET money = ?";
|
||||
|
||||
try (Connection conn = this.connect(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
|
||||
if (money >= 0 && money < Integer.MAX_VALUE) {
|
||||
pstmt.setInt(1, money);
|
||||
pstmt.executeUpdate();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean changeBalance(String uuid, int money) {
|
||||
String sql = "UPDATE diamonds SET money = ? WHERE uuid = ?";
|
||||
|
||||
try (Connection conn = this.connect(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
|
||||
int bal = getBalanceFromUUID(uuid);
|
||||
if (bal + money >= 0 && bal + money < Integer.MAX_VALUE) {
|
||||
pstmt.setInt(1, bal + money);
|
||||
pstmt.setString(2, uuid);
|
||||
pstmt.executeUpdate();
|
||||
return true;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void changeAllBalance(int money) {
|
||||
String sql = "UPDATE diamonds SET money = money + " + money + " WHERE " + Integer.MAX_VALUE + " > money + " + money + " AND 0 <= money + " + money;
|
||||
|
||||
try (Connection conn = this.connect(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
|
||||
pstmt.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public String top(String uuid, int page){
|
||||
String sql = "SELECT uuid, name, money FROM diamonds ORDER BY money DESC";
|
||||
String rankings = "";
|
||||
int i = 0;
|
||||
int playerRank = 0;
|
||||
int repeats = 0;
|
||||
|
||||
try (Connection conn = this.connect(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql)){
|
||||
while (rs.next() && (repeats < 10 || playerRank == 0)) {
|
||||
if (repeats / 10 + 1 == page) {
|
||||
rankings = rankings.concat(rs.getRow() + ") " + rs.getString("name") + ": $" + rs.getInt("money") + "\n");
|
||||
i++;
|
||||
}
|
||||
repeats++;
|
||||
if (uuid.equals(rs.getString("uuid"))) {
|
||||
playerRank = repeats;
|
||||
}
|
||||
}
|
||||
if (i < 10) {
|
||||
rankings = rankings.concat("---End--- \n");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return rankings.concat("Your rank is: " + playerRank);
|
||||
}
|
||||
|
||||
public String rank(int rank){
|
||||
int repeats = 0;
|
||||
String sql = "SELECT name FROM diamonds ORDER BY money DESC";
|
||||
try (Connection conn = this.connect(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql)){
|
||||
while (rs.next() ) {
|
||||
repeats++;
|
||||
if (repeats == rank) {
|
||||
return rs.getString("name");
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "No Player";
|
||||
}
|
||||
|
||||
public int playerRank(String uuid){
|
||||
String sql = "SELECT uuid FROM diamonds ORDER BY money DESC";
|
||||
int repeats = 1;
|
||||
|
||||
try (Connection conn = this.connect(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql)){
|
||||
rs.next();
|
||||
while (!rs.getString("uuid").equals(uuid)) {
|
||||
rs.next();
|
||||
repeats++;
|
||||
}
|
||||
return repeats;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user