Skip to content

Commit

Permalink
Merge pull request #14 from skycatminepokie/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
skycatminepokie authored Nov 29, 2023
2 parents 8b5ff9a + 64a89e2 commit de20bdd
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 94 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:
env:
MINECRAFT_VERSION: 1.20.1
JAVA_VERSION: 17
VERSION: 2.0.2+1.20.1
VERSION: 2.0.3+1.20.1
VERSION_TYPE: alpha

permissions:
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
- Fix leaderboard being backward
- Added exp4j for custom border algorithms
- Added `/wbshop econ borderFunction <function>` for setting the function that determines the width of the border
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Worldborder Shop
Fight the world border for every block
### A rewrite is in progress...
Until then, enjoy the [old version](https://github.com/skycatminepokie/wbshop-mod)!

### Probably important legal info
wbshop is licensed under the MIT license.
exp4j is embedded, which is license under Apache License 2.0. No modifications were made.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ repositories {

dependencies {
implementation 'org.projectlombok:lombok:1.18.26'
include(implementation 'net.objecthunter:exp4j:0.4.8')

// To change the versions see the gradle.properties file
minecraft "com.mojang:minecraft:${project.minecraft_version}"
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ yarn_mappings=1.20.1+build.10
loader_version=0.14.22

# Mod Properties
mod_version=2.0.2+1.20.1
mod_version=2.0.3+1.20.1
maven_group=com.skycat
archives_base_name=wbshop

Expand Down
8 changes: 3 additions & 5 deletions src/main/java/com/skycat/wbshop/WBShop.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.function.Function;

public class WBShop implements ModInitializer, ServerWorldEvents.Load, ServerWorldEvents.Unload, ServerLivingEntityEvents.AfterDeath {
public static final String MOD_ID = "wbshop";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
private static @Nullable MinecraftServer server = null;
private static Economy economy = null;
private static final CommandHandler COMMAND_HANDLER = new CommandHandler();
public static Function<Long, Double> borderFunction = Math::sqrt;

/**
* Null when world is remote or not loaded.
Expand All @@ -44,7 +41,7 @@ public static Economy getEconomy() {

public static void updateBorder() {
if (getServer() != null && getEconomy() != null && getServer().getOverworld() != null) {
getServer().getOverworld().getWorldBorder().setSize(Math.max(Math.ceil(borderFunction.apply(economy.getTotalPoints())), 5));
getServer().getOverworld().getWorldBorder().setSize(getEconomy().evaluateBorderSize(economy.getTotalPoints()));
} else {
Utils.log("Attempted to update border while something was null - this shouldn't happen. Hopefully nothing goes wrong, but please report this.");
}
Expand All @@ -58,7 +55,7 @@ public void afterDeath(LivingEntity entity, DamageSource damageSource) {
Account account = econ.getOrCreateAccount(player);
long pointsLost = (long) Math.ceil(account.balance() * 0.1);
account.removeBalance(pointsLost);
player.sendMessage(Text.of("You died and lost " + pointsLost + " points.")); // TODO: Plurality
player.sendMessage(Text.of("You died and lost " + pointsLost + " points."));
}
}
}
Expand All @@ -68,6 +65,7 @@ public void onInitialize() {
ServerWorldEvents.LOAD.register(this);
CommandRegistrationCallback.EVENT.register(COMMAND_HANDLER);
ServerLivingEntityEvents.AFTER_DEATH.register(this);
updateBorder();
// CommonEconomy.register(EconomyProvider) // Hmm. How is this gonna work?
}

Expand Down
49 changes: 37 additions & 12 deletions src/main/java/com/skycat/wbshop/command/CommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.LongArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.skycat.wbshop.WBShop;
Expand Down Expand Up @@ -32,47 +33,53 @@ public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandR
var root = dispatcher.getRoot();
var wbshop = literal("wbshop")
.executes(this::wbshop)
.build(); // TODO: Gui
.build();
var econ = literal("econ")
.requires(Permissions.require("wbshop.econ", 4))
.build(); // TODO
.build();
var econGet = literal("get")
.build(); // TODO
.build();
var econGetPlayers = argument("players", GameProfileArgumentType.gameProfile())
.executes(this::econGet)
.build(); // TODO
.build();
var econAdd = literal("add")
.build(); // TODO
.build();
var econAddPlayers = argument("players", GameProfileArgumentType.gameProfile())
.build(); // TODO
.build();
var econAddPlayersPoints = argument("points", LongArgumentType.longArg(1))
.executes(this::econAdd)
.build();
var econRemove = literal("remove")
.build(); // TODO
.build();
var econRemovePlayers = argument("players", GameProfileArgumentType.gameProfile())
.build(); // TODO
.build();
var econRemovePlayersPoints = argument("points", LongArgumentType.longArg(1))
.executes(this::econRemove)
.build(); // TODO
.build();
var econTotal = literal("total")
.build(); // TODO
.executes(this::econTotal)
.build();
var econBorderFunction = literal("borderFunction")
.build();
var econBorderFunctionFunction = argument("function", StringArgumentType.string())
.executes(this::setBorderFunction)
.build();
var bal = literal("bal")
.executes(this::bal)
.build();
var donate = literal("donate")
.executes(this::donate)
.build();
var withdraw = literal("withdraw")
.build(); // TODO
.build();
var withdrawPoints = argument("points", LongArgumentType.longArg(1))
.executes(this::withdraw)
.build();
var withdrawAll = literal("all")
.executes(this::withdrawAll)
.build();
var pay = literal("pay")
.build(); // TODO
.build();

// Building tree
root.addChild(wbshop);
Expand All @@ -86,6 +93,8 @@ public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandR
econRemove.addChild(econRemovePlayers);
econRemovePlayers.addChild(econRemovePlayersPoints);
econ.addChild(econTotal);
econ.addChild(econBorderFunction);
econBorderFunction.addChild(econBorderFunctionFunction);
root.addChild(bal);
root.addChild(donate);
root.addChild(withdraw);
Expand All @@ -94,6 +103,22 @@ public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandR

}

private int econTotal(CommandContext<ServerCommandSource> context) {
long total = WBShop.getEconomy().getTotalPoints();
context.getSource().sendFeedback(()-> Text.of("There are a total of " + total + " points in player's accounts."), false);
return Command.SINGLE_SUCCESS;
}

private int setBorderFunction(CommandContext<ServerCommandSource> context) {
String functionString = StringArgumentType.getString(context, "function");
if (WBShop.getEconomy().setBorderFunction(functionString)) {
context.getSource().sendFeedback(() -> Text.of("Successfully updated border function!"), true);
return Command.SINGLE_SUCCESS;
}
context.getSource().sendError(Text.of("Failed to update border function."));
return -1;
}

private int wbshop(CommandContext<ServerCommandSource> context) {
if (context.getSource().getEntity() instanceof ServerPlayerEntity player) {
new OverviewGui(player).open();
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/skycat/wbshop/econ/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import com.skycat.wbshop.WBShop;
import com.skycat.wbshop.util.LogLevel;
import com.skycat.wbshop.util.Utils;
import eu.pb4.common.economy.api.EconomyAccount;
import eu.pb4.common.economy.api.EconomyCurrency;
Expand Down Expand Up @@ -69,7 +70,7 @@ public long donateItems(ItemStack stack) {
long value = WBShop.getEconomy().pointValueOf(stack);
donatedItemCounts.put(stack.getItem(), current + stack.getCount());
addBalance(value);
totalItemsDonated += stack.getCount(); // TODO: Better way - make a wrapper on hashmap
totalItemsDonated += stack.getCount();
return value;
}

Expand Down Expand Up @@ -126,7 +127,6 @@ public Identifier id() { // I believe this is the type of account this is
* @return {@code false} if the account does not have enough points, {@code true} on success.
*/
public boolean withdraw(long amount, ServerPlayerEntity player) {
// TODO
if (amount > balance) return false;
ItemStack voucher = Economy.makeVoucher(amount);

Expand Down Expand Up @@ -155,7 +155,7 @@ public void setBalance(long value) throws IllegalArgumentException {
if (value >= 0) {
balance = value;
} else {
WBShop.LOGGER.error("Something tried to set the value of account " + id() + " to " + value + ". Negatives are not allowed, defaulting to 0.");
Utils.log("Something tried to set the value of account " + id() + " to " + value + ". Negatives are not allowed, defaulting to 0.", LogLevel.WARN);
balance = 0;
}
WBShop.getEconomy().markDirty();
Expand Down
Loading

0 comments on commit de20bdd

Please sign in to comment.