Skip to content

Commit

Permalink
Merge pull request #845 from TonytheMacaroni/main
Browse files Browse the repository at this point in the history
Fix modifying global variables without a player
  • Loading branch information
Chronoken authored Feb 5, 2024
2 parents 8371e23 + 96319c6 commit d44b433
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import com.nisovin.magicspells.spells.TargetedEntitySpell;
import com.nisovin.magicspells.events.SpellbookReloadEvent;
import com.nisovin.magicspells.spells.TargetedLocationSpell;
import com.nisovin.magicspells.variables.variabletypes.GlobalVariable;
import com.nisovin.magicspells.variables.variabletypes.GlobalStringVariable;

@CommandAlias("ms|magicspells")
public class MagicCommand extends BaseCommand {
Expand Down Expand Up @@ -503,18 +505,21 @@ public class VariableCommands extends BaseCommand {
public void onShowVariable(CommandIssuer issuer, String[] args) {
if (!MagicSpells.isLoaded()) return;
if (noPermission(issuer.getIssuer(), Perm.COMMAND_VARIABLE_SHOW)) return;
Variable variable;
Player player;
if (args.length == 0) throw new InvalidCommandArgument();

variable = MagicSpells.getVariableManager().getVariable(args[0]);
Variable variable = MagicSpells.getVariableManager().getVariable(args[0]);
if (variable == null) throw new ConditionFailedException("No matching variable found: '" + args[0] + "'");

if (args.length == 1) player = getPlayerFromIssuer(issuer);
else player = ACFBukkitUtil.findPlayerSmart(issuer, args[1]);
if (player == null) return;
String name = null;
if (!(variable instanceof GlobalVariable || variable instanceof GlobalStringVariable)) {
Player player = args.length == 1 ? getPlayerFromIssuer(issuer) : ACFBukkitUtil.findPlayerSmart(issuer, args[1]);
if (player == null) return;

name = player.getName();
}

issuer.sendMessage(MagicSpells.getTextColor() + TxtUtil.getPossessiveName(player.getName()) + " variable value for " + args[0] + " is: " + variable.getStringValue(player));
String message = name == null ? "Variable" : TxtUtil.getPossessiveName(name) + " variable";
issuer.sendMessage(MagicSpells.getTextColor() + message + " value for " + args[0] + " is: " + variable.getStringValue(name));
}

@Subcommand("modify")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,15 @@ public void reset(String variable, Player player) {

public void reset(Variable variable, Player player) {
if (variable == null) return;
variable.reset(player);
updateBossBar(variable, player != null ? player.getName() : "");
updateExpBar(variable, player != null ? player.getName() : "");

String name = player != null ? player.getName() : "";
variable.reset(name);
updateBossBar(variable, name);
updateExpBar(variable, name);

if (!variable.isPermanent()) return;
if (variable instanceof PlayerVariable) dirtyPlayerVars.add(player != null ? player.getName() : "");

if (variable instanceof PlayerVariable) dirtyPlayerVars.add(name);
else if (variable instanceof GlobalVariable) dirtyGlobalVars = true;
else if (variable instanceof GlobalStringVariable) dirtyGlobalVars = true;
}
Expand Down Expand Up @@ -598,42 +602,46 @@ public String processVariableMods(String var, VariableMod mod, Player playerToMo
}

public String processVariableMods(String var, VariableMod mod, Player playerToMod, SpellData data) {
if (mod == null) return 0 + "";
if (playerToMod == null) return 0 + "";
if (mod == null) return "0";

Variable variable = getVariable(var);
if (variable == null) return 0 + "";
if (variable == null) return "0";

return processVariableMods(variable, mod, playerToMod, data);
}

public String processVariableMods(Variable variable, VariableMod mod, Player playerToMod, SpellData data) {
VariableMod.Operation op = mod.getOperation();

if (playerToMod == null && !(variable instanceof GlobalVariable || variable instanceof GlobalStringVariable))
return "0";

String playerToModName = playerToMod == null ? null : playerToMod.getName();

if (variable instanceof PlayerStringVariable || variable instanceof GlobalStringVariable) {
switch (op) {
case SET -> {
String value = mod.getStringValue(data);

if (value.equals(variable.getDefaultStringValue())) reset(variable, playerToMod);
else set(variable, playerToMod.getName(), value);
else set(variable, playerToModName, value);

return value;
}
case ADD -> {
String value = variable.getStringValue(playerToMod) + mod.getStringValue(data);
String value = variable.getStringValue(playerToModName) + mod.getStringValue(data);

if (value.equals(variable.getDefaultStringValue())) reset(variable, playerToMod);
else set(variable, playerToMod.getName(), value);
else set(variable, playerToModName, value);

return value;
}
case MULTIPLY -> {
int count = (int) mod.getValue(data);
String value = variable.getStringValue(playerToMod).repeat(count);
String value = variable.getStringValue(playerToModName).repeat(count);

if (value.equals(variable.getDefaultStringValue())) reset(variable, playerToMod);
else set(variable, playerToMod.getName(), value);
else set(variable, playerToModName, value);

return value;
}
Expand All @@ -645,7 +653,7 @@ public String processVariableMods(Variable variable, VariableMod mod, Player pla
if (value == variable.getDefaultValue() && !(variable instanceof MetaVariable)) {
reset(variable, playerToMod);
} else {
set(variable, playerToMod.getName(), value);
set(variable, playerToModName, value);
}

return Double.toString(value);
Expand Down

0 comments on commit d44b433

Please sign in to comment.