Skip to content

Commit

Permalink
Home, Bank
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCurle committed Feb 8, 2025
1 parent 7f56f77 commit f971766
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 27 deletions.
14 changes: 14 additions & 0 deletions src/main/java/uk/gemwire/bareessentials/BareEssentials.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@
*/
package uk.gemwire.bareessentials;

import com.mojang.authlib.GameProfile;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.numbers.FixedFormat;
import net.minecraft.network.chat.numbers.NumberFormat;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.GameRules;
import net.minecraft.world.scores.Objective;
import net.minecraft.world.scores.ScoreHolder;
import net.minecraft.world.scores.criteria.ObjectiveCriteria;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
Expand Down Expand Up @@ -119,6 +126,9 @@ public class BareEssentials {

public static GameRules.Key<GameRules.BooleanValue> OP_OVERRIDES_COOLDOWN = GameRules.register("be.opOverridesCooldowns", GameRules.Category.PLAYER, GameRules.BooleanValue.create(false));

public static ObjectiveCriteria BANK_ACCOUNT_VALUE = ObjectiveCriteria.registerCustom("bank_value");
public static Objective BANK_ACCOUNT_SORTED_OBJECTIVE;

public static Logger LOGGER = LogManager.getLogger(BareEssentials.class);


Expand All @@ -132,10 +142,14 @@ public BareEssentials() {
static class Events {
@SubscribeEvent
public static void started(ServerStartedEvent e) {

e.getServer().getScoreboard().addObjective("be_banks", BANK_ACCOUNT_VALUE, Component.literal("Bank Accounts"), ObjectiveCriteria.RenderType.INTEGER, true, null);

// Load bank details into the static map.
Bank accts = Bank.getOrCreate(e.getServer().overworld());
LOGGER.info("Loaded " + accts.accounts.size() + " bank accounts.");


Homes homes = Homes.getOrCreate(e.getServer().overworld());
LOGGER.info("Loaded " + homes.homes.size() + " user homes.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public static void registerCommands(RegisterCommandsEvent event) {
))
.then(Commands.literal("get")
.requires(hasPermissionNode(PermissionNodes.HOME, PermissionNodes.HOME_GET))
.executes(CmdHomes::executeGet)
.executes(CmdHomes.Get::execute)
)
);

Expand Down
83 changes: 75 additions & 8 deletions src/main/java/uk/gemwire/bareessentials/commands/CmdBank.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,18 @@
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.locale.Language;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.Packet;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import uk.gemwire.bareessentials.BareEssentials;
import uk.gemwire.bareessentials.data.Bank;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class CmdBank {

public static String getCurrencySymbol(ServerLevel level) {
Expand Down Expand Up @@ -64,10 +71,10 @@ public static int execute(CommandContext<CommandSourceStack> cmd, ServerPlayer p

if (accts.hasUser(player)) {
cmd.getSource().getPlayer().sendSystemMessage(Component.translatable(Language.getInstance().getOrDefault(
"bareessentials.balance"), player.getDisplayName().getString(), getCurrencySymbol(player.serverLevel()), accts.getUserBalance(player)));
"bareessentials.bank.balance"), player.getDisplayName().getString(), getCurrencySymbol(player.serverLevel()), accts.getUserBalance(player)));
} else {
cmd.getSource().getPlayer().sendSystemMessage(Component.translatable(Language.getInstance().getOrDefault(
"bareessentials.balance.unable"), player.getDisplayName().getString()));
"bareessentials.bank.unable"), player.getDisplayName().getString()));
}

return Command.SINGLE_SUCCESS;
Expand All @@ -92,10 +99,10 @@ public static int execute(CommandContext<CommandSourceStack> cmd, ServerPlayer p
accts.setUserBalance(player, accts.getUserBalance(player) + (long) amt);

cmd.getSource().getPlayer().sendSystemMessage(Component.translatable(Language.getInstance().getOrDefault(
"bareessentials.balance.give"), getCurrencySymbol(player.serverLevel()), amt, player.getDisplayName().getString(), getCurrencySymbol(player.serverLevel()), accts.getUserBalance(player)));
"bareessentials.bank.give"), getCurrencySymbol(player.serverLevel()), amt, player.getDisplayName().getString(), getCurrencySymbol(player.serverLevel()), accts.getUserBalance(player)));
} else {
cmd.getSource().getPlayer().sendSystemMessage(Component.translatable(Language.getInstance().getOrDefault(
"bareessentials.balance.unable"), player.getDisplayName().getString()));
"bareessentials.bank.unable"), player.getDisplayName().getString()));
}

return Command.SINGLE_SUCCESS;
Expand All @@ -121,10 +128,10 @@ public static int execute(CommandContext<CommandSourceStack> cmd, ServerPlayer p
accts.setUserBalance(player, amt);

cmd.getSource().getPlayer().sendSystemMessage(Component.translatable(Language.getInstance().getOrDefault(
"bareessentials.balance.set"), player.getDisplayName().getString(), getCurrencySymbol(player.serverLevel()), accts.getUserBalance(player)));
"bareessentials.bank.set"), player.getDisplayName().getString(), getCurrencySymbol(player.serverLevel()), accts.getUserBalance(player)));
} else {
cmd.getSource().getPlayer().sendSystemMessage(Component.translatable(Language.getInstance().getOrDefault(
"bareessentials.balance.unable"), player.getDisplayName().getString()));
"bareessentials.bank.unable"), player.getDisplayName().getString()));
}

return Command.SINGLE_SUCCESS;
Expand All @@ -151,13 +158,73 @@ public static int execute(CommandContext<CommandSourceStack> cmd, ServerPlayer p
accts.setUserBalance(player, balance - (balance - (long) amt <= 0 ? amt = (int) balance : (long) amt));
// Don't subtract more than they have; cap it at limiting to their balance.
cmd.getSource().getPlayer().sendSystemMessage(Component.translatable(Language.getInstance().getOrDefault(
"bareessentials.balance.remove"), getCurrencySymbol(player.serverLevel()), amt, player.getDisplayName().getString(), getCurrencySymbol(player.serverLevel()), accts.getUserBalance(player)));
"bareessentials.bank.remove"), getCurrencySymbol(player.serverLevel()), amt, player.getDisplayName().getString(), getCurrencySymbol(player.serverLevel()), accts.getUserBalance(player)));
} else {
cmd.getSource().getPlayer().sendSystemMessage(Component.translatable(Language.getInstance().getOrDefault(
"bareessentials.bank.unable"), player.getDisplayName().getString()));
}

return Command.SINGLE_SUCCESS;
}
}

public static class Clear {
public static int executeOnSelf(CommandContext<CommandSourceStack> cmd) {
return execute(cmd, cmd.getSource().getPlayer());
}

public static int executeOnOther(CommandContext<CommandSourceStack> cmd) throws CommandSyntaxException {
return execute(cmd, EntityArgument.getPlayer(cmd, "user"));
}


public static int execute(CommandContext<CommandSourceStack> cmd, ServerPlayer player) {
Bank accts = Bank.getOrCreate(cmd.getSource().getLevel());

if (accts.hasUser(player)) {
accts.setUserBalance(player, 0);
cmd.getSource().getPlayer().sendSystemMessage(Component.translatable(Language.getInstance().getOrDefault(
"bareessentials.bank.clear"), player.getDisplayName().getString(), getCurrencySymbol(player.serverLevel())));
} else {
cmd.getSource().getPlayer().sendSystemMessage(Component.translatable(Language.getInstance().getOrDefault(
"bareessentials.balance.unable"), player.getDisplayName().getString()));
"bareessentials.bank.unable"), player.getDisplayName().getString()));
}

return Command.SINGLE_SUCCESS;
}
}

public static class Top {
private static List<Map.Entry<UUID, Long>> sortedBanks;

public static void calculateTopBanks(MinecraftServer server) {
var accs = Bank.getOrCreate(server.overworld()).accounts;

sortedBanks = accs.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).toList();

}

public static void displayTopBanks(ServerPlayer player) {
var packets = player.getServer().getScoreboard().getStartTrackingPackets(BareEssentials.BANK_ACCOUNT_SORTED_OBJECTIVE);
for (Packet<?> packet : packets) {
player.connection.send(packet);
}
}

public static int execute(CommandContext<CommandSourceStack> cmd) {
displayTopBanks(cmd.getSource().getPlayer());
return Command.SINGLE_SUCCESS;
}
}
}











19 changes: 17 additions & 2 deletions src/main/java/uk/gemwire/bareessentials/commands/CmdHomes.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.phys.Vec3;
import uk.gemwire.bareessentials.data.Bank;
import uk.gemwire.bareessentials.data.Homes;

Expand Down Expand Up @@ -88,7 +89,7 @@ public static int execute(CommandContext<CommandSourceStack> cmd) {
home.setUserHome(player, BlockPos.containing(player.getPosition(0)));

cmd.getSource().getPlayer().sendSystemMessage(Component.translatable(Language.getInstance().getOrDefault(
"bareessentials.sethome")));
"bareessentials.home.set")));

return Command.SINGLE_SUCCESS;
}
Expand All @@ -109,7 +110,7 @@ public static int execute(CommandContext<CommandSourceStack> cmd) {
home.setUserHome(player, BlockPos.containing(player.getPosition(0)));

cmd.getSource().getPlayer().sendSystemMessage(Component.translatable(Language.getInstance().getOrDefault(
"bareessentials.sethome.overwrite")));
"bareessentials.home.set.overwrite")));

return Command.SINGLE_SUCCESS;
}
Expand All @@ -127,4 +128,18 @@ public static int execute(CommandContext<CommandSourceStack> cmd) {
return Command.SINGLE_SUCCESS;
}
}

public class Get {
public static int execute(CommandContext<CommandSourceStack> cmd) {
ServerPlayer player = cmd.getSource().getPlayer();
Homes home = Homes.getOrCreate(cmd.getSource().getLevel());
BlockPos position = home.getUserHome(cmd.getSource().getPlayer());

if (position == null)
player.sendSystemMessage(Component.translatable("bareessentials.home.nohome"));
else
player.sendSystemMessage(Component.translatable("bareessentials.home.location", position.getX(), position.getY(), position.getZ()));
return Command.SINGLE_SUCCESS;
}
}
}
24 changes: 22 additions & 2 deletions src/main/java/uk/gemwire/bareessentials/data/Bank.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package uk.gemwire.bareessentials.data;

import com.mojang.authlib.GameProfile;
import net.minecraft.core.HolderLookup;
import net.minecraft.locale.Language;
import net.minecraft.nbt.CompoundTag;
Expand All @@ -32,6 +33,8 @@
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.saveddata.SavedData;
import net.minecraft.world.scores.ScoreHolder;
import net.neoforged.neoforge.server.ServerLifecycleHooks;
import org.jetbrains.annotations.NotNull;
import uk.gemwire.bareessentials.BareEssentials;
import uk.gemwire.bareessentials.commands.CmdBank;
Expand All @@ -40,6 +43,7 @@
import java.util.Map;
import java.util.UUID;

import static uk.gemwire.bareessentials.BareEssentials.BANK_ACCOUNT_SORTED_OBJECTIVE;
import static uk.gemwire.bareessentials.BareEssentials.DAILY_INCOME;
import static uk.gemwire.bareessentials.BareEssentials.STARTING_BALANCE;

Expand All @@ -59,6 +63,16 @@ public Bank() {
private static final SavedData.Factory<Bank> bankFactory
= new SavedData.Factory<>(Bank::new, Bank::load, null);

@Override
public void setDirty() {
super.setDirty();

MinecraftServer server = ServerLifecycleHooks.getCurrentServer();
for (var acc : accounts.entrySet()) {
setPlayerBankScore(acc.getKey(), server);
}
}

@Override
public @NotNull CompoundTag save(final @NotNull CompoundTag pCompoundTag, final @NotNull HolderLookup.Provider provider) {
CompoundTag tag = new CompoundTag();
Expand All @@ -70,7 +84,7 @@ public Bank() {
return pCompoundTag;
}

public static Bank load(CompoundTag tag) {
public static Bank load(CompoundTag tag, final HolderLookup.Provider prov) {
CompoundTag accts = tag.getCompound("accounts");
Map<UUID, Long> accounts = new HashMap<>();
for (String key : accts.getAllKeys())
Expand All @@ -84,7 +98,7 @@ public static Bank getOrCreate(ServerLevel level) {
}

public long getUserBalance(ServerPlayer p) {
if (!hasUser(p)) { accounts.put(p.getUUID(), (long) p.getServer().getGameRules().getInt(STARTING_BALANCE)); return accounts.get(p.getUUID()); }
if (!hasUser(p)) { accounts.put(p.getUUID(), (long) p.getServer().getGameRules().getInt(STARTING_BALANCE)); setDirty(); return accounts.get(p.getUUID()); }
for (var acc : accounts.entrySet()) {
if (acc.getKey().equals(p.getUUID())) {
return acc.getValue();
Expand Down Expand Up @@ -131,7 +145,13 @@ public void updateBalances(MinecraftServer s) {
BareEssentials.LOGGER.info("Granting the " + s.getGameRules().getInt(DAILY_INCOME) + " daily income to all players.");
for (var acct : accounts.entrySet()) {
acct.setValue(acct.getValue() + s.getGameRules().getInt(DAILY_INCOME));
setDirty();
}
}

public void setPlayerBankScore(UUID player, MinecraftServer server) {
GameProfile profile = server.getProfileCache().get(player).get();
server.getScoreboard().getOrCreatePlayerScore(ScoreHolder.fromGameProfile(profile), BANK_ACCOUNT_SORTED_OBJECTIVE).set(Math.toIntExact(accounts.get(player)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public Cooldowns() {
return new CompoundTag();
}

public static Cooldowns load(CompoundTag tag) {
public static Cooldowns load(CompoundTag tag, final HolderLookup.Provider prov) {
// Do not save or load cooldowns, they only exist temporarily.
return new Cooldowns();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/uk/gemwire/bareessentials/data/Homes.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public Homes() {
return pCompoundTag;
}

public static Homes load(CompoundTag tag) {
public static Homes load(CompoundTag tag, final HolderLookup.Provider prov) {
CompoundTag accts = tag.getCompound("homes");
Map<UUID, BlockPos> homes = new HashMap<>();
for (String key : accts.getAllKeys()) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/META-INF/accesstransformer.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ public net.minecraft.server.commands.TeleportCommand teleportToPos(Lnet/minecraf

public net.minecraft.server.commands.TeleportCommand$LookAtPosition
public net.minecraft.server.commands.TeleportCommand$LookAtEntity

public net.minecraft.world.scores.criteria.ObjectiveCriteria registerCustom(Ljava/lang/String;)Lnet/minecraft/world/scores/criteria/ObjectiveCriteria; # registerCustom
26 changes: 14 additions & 12 deletions src/main/resources/assets/bareessentials/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,28 @@
"bareessentials.godmode.enabled": "God mode enabled for %s!",
"bareessentials.godmode.disabled": "God mode disabled for %s!",

"bareessentials.balance": "Player %s's balance is %s%s.",
"bareessentials.balance.give" : "Added %s%s to player %s's balance. New total: %s%s.",
"bareessentials.balance.set": "Set player %s's balance to %s%s.",
"bareessentials.balance.remove": "Subtracted %s%s from the balance of player %s. New total: %s%s.",
"bareessentials.balance.unable": "Unable to view or modify player %s's balance. Something is wrong.",
"bareessentials.balance.insufficient": "You do not have enough money to do that! You need at least %s%s.",
"bareessentials.bank.balance": "Player %s's balance is %s%s.",
"bareessentials.bank.give" : "Added %s%s to player %s's balance. New total: %s%s.",
"bareessentials.bank.set": "Set player %s's balance to %s%s.",
"bareessentials.bank.remove": "Subtracted %s%s from the balance of player %s. New total: %s%s.",
"bareessentials.bank.unable": "Unable to view or modify player %s's balance. Something is wrong.",
"bareessentials.bank.insufficient": "You do not have enough money to do that! You need at least %s%s.",
"bareessentials.bank.clear": "Set player %s's balance to %s0.",

"bareessentials.heal": "Healed %s.",
"bareessentials.feed": "Fed %s.",

"bareessentials.home.wrongdimension": "You can only use /home or /sethome in the Overworld!",
"bareessentials.home.wrongdimension": "You can only use /home or /home set in the Overworld!",
"bareessentials.home.teleporting": "Teleporting you to your home..",
"bareessentials.home.nohome": "You do not have a home to teleport to! Use /sethome somewhere safe.",
"bareessentials.home.confirmoverwrite": "You already have a home set. Use /sethome overwrite to confirm that you wish to replace this home with the new one.",
"bareessentials.home.nohome": "You do not have a home to teleport to! Use /home set somewhere safe.",
"bareessentials.home.confirmoverwrite": "You already have a home set. Use /home set overwrite to confirm that you wish to replace this home with the new one.",
"bareessentials.home.deleted": "Your home has been deleted.",
"bareessentials.home.location": "Your home is at [X: %s, Y: %s, Z: %s].",
"bareessentials.home.set": "Setting your home!",
"bareessentials.home.set.overwrite": "Setting your home! (Overwriting previous home)",

"bareessentials.cooldown.active": "You can't use that command for another %s seconds.",

"bareessentials.sethome": "Setting your home!",
"bareessentials.sethome.overwrite": "Setting your home! (Overwriting previous home)",
"bareessentials.cooldown.active": "You can't use that command for another %s seconds.",


"bareessentials.targetyou": "you",
Expand Down

0 comments on commit f971766

Please sign in to comment.