Skip to content

Commit

Permalink
Experience Change
Browse files Browse the repository at this point in the history
-Removed onExperiencePickup
+Added onExperienceProcessed: instead of all experience counting doubly for vanilla and playerex levelling, experience gained is now split, and hence consumed.
+Added config option to set the percentage of experience that goes towards levelling playerex levels INSTEAD of vanilla levels
+Added command to level up the player's playerex level (/playerex level-up <player> <amount>)
*Changed reset command to (/playerex reset <player>)

This change results in the player gaining both vanilla and playerex levels slower than previously. This is perhaps not an ideal solution to xp duping, but it is the best I can come up with for now.
  • Loading branch information
CleverNucleus committed May 4, 2021
1 parent 97a40d1 commit ba5177c
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public static void sync(PlayerEntity par0) {
@SubscribeEvent
public static void serverLoad(final FMLServerStartingEvent par0) {
ResetCommand.register(par0.getServer().getCommandManager().getDispatcher());
LevelCommand.register(par0.getServer().getCommandManager().getDispatcher());
}

/**
Expand Down Expand Up @@ -239,30 +240,34 @@ public static void onPlayerTick(final net.minecraftforge.event.TickEvent.PlayerT
}
}
});
}
}

/**
* Event fired when xp is picked up.
* Event fired when xp is added.
* @param par0
*/
@SubscribeEvent
public static void onExperiencePickup(final net.minecraftforge.event.entity.player.PlayerXpEvent.PickupXp par0) {
public static void onExperienceProcessed(final net.minecraftforge.event.entity.player.PlayerXpEvent.XpChange par0) {
PlayerEntity var0 = par0.getPlayer();

if(var0.world.isRemote) return;

int var1 = par0.getOrb().getXpValue();
int var1 = par0.getAmount();
int var2 = Math.round((float)var1 * CommonConfig.COMMON.experienceSplit.get().floatValue() / 100F);
int var3 = Math.round((float)var1 * (100F - CommonConfig.COMMON.experienceSplit.get().floatValue()) / 100F);

ExAPI.playerAttributes(var0).ifPresent(var -> {
var.add(var0, PlayerAttributes.EXPERIENCE, var1);
var.add(var0, PlayerAttributes.EXPERIENCE, var2);

float var2 = (float)var.expCoeff(var0);
float var4 = (float)var.expCoeff(var0);

if(var2 > 0.95F) {
if(var4 > 0.95F) {
var.add(var0, PlayerAttributes.LEVEL, 1);
var.forceSet(var0, PlayerAttributes.EXPERIENCE, 0F);
}
});

par0.setAmount(var3);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.github.clevernucleus.playerex.init;

import com.github.clevernucleus.playerex.api.ExAPI;
import com.github.clevernucleus.playerex.api.attribute.PlayerAttributes;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;

import net.minecraft.command.CommandSource;
import net.minecraft.command.Commands;
import net.minecraft.command.ISuggestionProvider;
import net.minecraft.command.arguments.EntityArgument;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.management.PlayerList;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TextFormatting;

public class LevelCommand {

/** Error procedure. */
private static boolean error(final CommandContext<CommandSource> par0, final PlayerEntity par1) {
if(par1.world.isRemote) return true;

PlayerList var0 = par0.getSource().getServer().getPlayerList();

if(!var0.getPlayers().contains(par1)) {
par0.getSource().sendFeedback(new StringTextComponent(TextFormatting.RED + par1.getDisplayName().toString() + TextFormatting.GRAY + " does not exist!"), true);

return true;
}

return false;
}

private static int level(CommandContext<CommandSource> par0, PlayerEntity par1, int par2) throws CommandSyntaxException {
if(error(par0, par1)) return 1;

ExAPI.playerAttributes(par1).ifPresent(var -> {
var.add(par1, PlayerAttributes.LEVEL, par2);
});
EventHandler.update(par1);
EventHandler.sync(par1);

return 1;
}

/**
* Register the command(s) to the game.
* @param par0
*/
public static void register(CommandDispatcher<CommandSource> par0) {
par0.register(Commands.literal(ExAPI.MODID).requires(var -> var.hasPermissionLevel(2)).then(Commands.literal("level-add").then(Commands.argument("player", EntityArgument.player()).suggests((var0, var1) -> {
PlayerList var2 = var0.getSource().getServer().getPlayerList();

return ISuggestionProvider.suggest(var2.getPlayers().stream().map(var3 -> var3.getGameProfile().getName()), var1);
}).then(Commands.argument("amount", IntegerArgumentType.integer()).executes(var -> {
return level(var, EntityArgument.getPlayer(var, "player"), IntegerArgumentType.getInteger(var, "amount"));
})))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ private static boolean error(final CommandContext<CommandSource> par0, final Pla
return false;
}

/** A partially formed entitled command. */
private static int run(CommandContext<CommandSource> par0) throws CommandSyntaxException {
par0.getSource().sendFeedback(new StringTextComponent(TextFormatting.RED + "/playerex player reset"), true);

return 1;
}

private static int reset(CommandContext<CommandSource> par0, PlayerEntity par1) throws CommandSyntaxException {
if(error(par0, par1)) return 1;

Expand All @@ -53,12 +46,12 @@ private static int reset(CommandContext<CommandSource> par0, PlayerEntity par1)
* @param par0
*/
public static void register(CommandDispatcher<CommandSource> par0) {
par0.register(Commands.literal(ExAPI.MODID)
.requires(var -> var.hasPermissionLevel(2))
.executes(ResetCommand::run).then(Commands.argument("player", EntityArgument.player()).suggests((var0, var1) -> {
PlayerList var2 = var0.getSource().getServer().getPlayerList();
return ISuggestionProvider.suggest(var2.getPlayers().stream().map(var3 -> var3.getGameProfile().getName()), var1);
}).then(Commands.literal("reset").executes(var -> reset(var, EntityArgument.getPlayer(var, "player"))))));
par0.register(Commands.literal(ExAPI.MODID).requires(var -> var.hasPermissionLevel(2)).then(Commands.literal("reset").then(Commands.argument("player", EntityArgument.player()).suggests((var0, var1) -> {
PlayerList var2 = var0.getSource().getServer().getPlayerList();

return ISuggestionProvider.suggest(var2.getPlayers().stream().map(var3 -> var3.getGameProfile().getName()), var1);
}).executes(var -> {
return reset(var, EntityArgument.getPlayer(var, "player"));
}))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static class Common {
/** Starting attribute. */
public final IntValue constitution, strength, dexterity, intelligence, luckiness;
/** expCoeff parameters. */
public final DoubleValue offset, scale;
public final DoubleValue offset, scale, experienceSplit;
/** reset attributes on death */
public final BooleanValue resetOnDeath;

Expand Down Expand Up @@ -96,6 +96,11 @@ public Common(ForgeConfigSpec.Builder par0) {
.worldRestart()
.define("resetOnDeath", false);

par0.pop();
par0.push("experienceSplit");

this.experienceSplit = par0.comment("The percentage of experience that contributes to PlayerEx levels.").translation(ExAPI.MODID + ".config.common.experiencesplit").defineInRange("experienceSplit", 50D, 1D, 99D);

par0.pop();
par0.push("expcoeff");

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/playerex/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"playerex.config.common.dexterity": "Dexterity",
"playerex.config.common.intelligence": "Intelligence",
"playerex.config.common.luckiness": "Luckiness",
"playerex.config.common.experiencesplit": "The percentage of experience that contributes to PlayerEx levels.",
"playerex.config.common.offset": "Exp Coeff Offset",
"playerex.config.common.scale": "Exp Coeff Scaler",
"key.playerex.hud": "Additional HUD",
Expand Down

0 comments on commit ba5177c

Please sign in to comment.