!small refactor

added force class command
This commit is contained in:
ASangarin 2020-10-11 16:49:30 +02:00
parent 0bf926eba3
commit 610045c319
8 changed files with 100 additions and 45 deletions

View File

@ -13,6 +13,7 @@ import com.google.gson.JsonObject;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.attribute.PlayerAttribute;
import net.Indyuce.mmocore.api.skill.Skill;
import net.Indyuce.mmocore.manager.data.PlayerDataManager.DefaultPlayerData;
public class SavedClassInformation {
private final int level, experience, skillPoints, attributePoints, attributeReallocationPoints;
@ -53,23 +54,26 @@ public class SavedClassInformation {
}
public SavedClassInformation(PlayerData player) {
level = player.getLevel();
skillPoints = player.getSkillPoints();
experience = player.getExperience();
skills = player.mapSkillLevels();
attributes = player.getAttributes().mapPoints();
attributePoints = player.getAttributePoints();
attributeReallocationPoints = player.getAttributeReallocationPoints();
this(player.getLevel(), player.getExperience(), player.getSkillPoints(), player.getAttributePoints(),
player.getAttributeReallocationPoints(), player.mapSkillLevels(), player.getAttributes().mapPoints());
}
public SavedClassInformation(DefaultPlayerData data) {
this(data.getLevel(), 0, data.getSkillPoints(), data.getAttributePoints(), data.getAttrReallocPoints());
}
public SavedClassInformation(int level, int experience, int skillPoints, int attributePoints, int attributeReallocationPoints) {
this.level = level;
this.experience = experience;
this.skillPoints = skillPoints;
this.attributePoints = attributePoints;
this.attributeReallocationPoints = attributeReallocationPoints;
attributes = new HashMap<>();
skills = new HashMap<>();
this(level, experience, skillPoints, attributePoints, attributeReallocationPoints, new HashMap<>(), new HashMap<>());
}
private SavedClassInformation(int l, int e, int sp, int ap, int arp, Map<String, Integer> a, Map<String, Integer> s) {
this.level = l;
this.experience = e;
this.skillPoints = sp;
this.attributePoints = ap;
this.attributeReallocationPoints = arp;
this.attributes = a;
this.skills = s;
}
public int getLevel() {

View File

@ -13,6 +13,7 @@ public class AdminCommandTreeNode extends CommandTreeNode {
addChild(new ResetCommandTreeNode(this));
addChild(new InfoCommandTreeNode(this));
addChild(new ClassCommandTreeNode(this));
addChild(new ForceClassCommandTreeNode(this));
addChild(new ExperienceCommandTreeNode(this));
addChild(new LevelCommandTreeNode(this));

View File

@ -2,12 +2,15 @@ package net.Indyuce.mmocore.command.rpg.admin;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Sound;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.event.PlayerChangeClassEvent;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.profess.PlayerClass;
import net.Indyuce.mmocore.api.player.profess.SavedClassInformation;
import net.mmogroup.mmolib.command.api.CommandTreeNode;
import net.mmogroup.mmolib.command.api.Parameter;
@ -16,8 +19,8 @@ public class ClassCommandTreeNode extends CommandTreeNode {
super(parent, "class");
addParameter(Parameter.PLAYER);
addParameter(
new Parameter("<class>", (explorer, list) -> MMOCore.plugin.classManager.getAll().forEach(profess -> list.add(profess.getId()))));
addParameter(new Parameter("<class>", (explorer, list) -> MMOCore.plugin.classManager.getAll()
.forEach(profess -> list.add(profess.getId()))));
}
@Override
@ -40,9 +43,24 @@ public class ClassCommandTreeNode extends CommandTreeNode {
PlayerClass profess = MMOCore.plugin.classManager.get(format);
PlayerData data = PlayerData.get(player);
data.setClass(profess);
sender.sendMessage(
ChatColor.GOLD + player.getName() + ChatColor.YELLOW + " is now a " + ChatColor.GOLD + profess.getName() + ChatColor.YELLOW + ".");
PlayerChangeClassEvent called = new PlayerChangeClassEvent(data, profess);
Bukkit.getPluginManager().callEvent(called);
if (called.isCancelled())
return CommandResult.SUCCESS;
data.giveClassPoints(-1);
(data.hasSavedClass(profess) ? data.getClassInfo(profess) : new SavedClassInformation(
MMOCore.plugin.dataProvider.getDataManager().getDefaultData())).load(profess, data);
while (data.hasSkillBound(0))
data.unbindSkill(0);
if (data.isOnline()) {
MMOCore.plugin.configManager.getSimpleMessage("class-select", "class", profess.getName())
.send(data.getPlayer());
data.getPlayer().playSound(data.getPlayer().getLocation(), Sound.UI_TOAST_CHALLENGE_COMPLETE, 1, 1);
}
sender.sendMessage(ChatColor.GOLD + player.getName() + ChatColor.YELLOW + " is now a " + ChatColor.GOLD
+ profess.getName() + ChatColor.YELLOW + ".");
return CommandResult.SUCCESS;
}
}

View File

@ -0,0 +1,48 @@
package net.Indyuce.mmocore.command.rpg.admin;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.profess.PlayerClass;
import net.mmogroup.mmolib.command.api.CommandTreeNode;
import net.mmogroup.mmolib.command.api.Parameter;
public class ForceClassCommandTreeNode extends CommandTreeNode {
public ForceClassCommandTreeNode(CommandTreeNode parent) {
super(parent, "force-class");
addParameter(Parameter.PLAYER);
addParameter(
new Parameter("<class>", (explorer, list) -> MMOCore.plugin.classManager.getAll().forEach(profess -> list.add(profess.getId()))));
}
@Override
public CommandResult execute(CommandSender sender, String[] args) {
if (args.length < 4)
return CommandResult.THROW_USAGE;
Player player = Bukkit.getPlayer(args[2]);
if (player == null) {
sender.sendMessage(ChatColor.RED + "Could not find the player called " + args[2] + ".");
return CommandResult.FAILURE;
}
String format = args[3].toUpperCase().replace("-", "_");
if (!MMOCore.plugin.classManager.has(format)) {
sender.sendMessage(ChatColor.RED + "Could not find class " + format + ".");
return CommandResult.FAILURE;
}
PlayerClass profess = MMOCore.plugin.classManager.get(format);
PlayerData data = PlayerData.get(player);
data.setClass(profess);
sender.sendMessage(
ChatColor.GOLD + player.getName() + ChatColor.YELLOW + " is now a " + ChatColor.GOLD + profess.getName() + ChatColor.YELLOW + ".");
return CommandResult.SUCCESS;
}
}

View File

@ -92,7 +92,9 @@ public class ClassConfirmation extends EditableInventory {
@Override
public ItemStack display(GeneratedInventory inv, int n) {
return inv.getPlayerData().hasSavedClass(((ClassConfirmationInventory) inv).profess) ? unlocked.display(inv, n) : locked.display(inv, n);
return inv.getPlayerData().hasSavedClass(((ClassConfirmationInventory) inv).profess)
? unlocked.display(inv, n)
: locked.display(inv, n);
}
@Override
@ -105,7 +107,8 @@ public class ClassConfirmation extends EditableInventory {
private final PlayerClass profess;
private final PluginInventory last;
public ClassConfirmationInventory(PlayerData playerData, EditableInventory editable, PlayerClass profess, PluginInventory last) {
public ClassConfirmationInventory(PlayerData playerData, EditableInventory editable, PlayerClass profess,
PluginInventory last) {
super(playerData, editable);
this.profess = profess;
@ -128,7 +131,8 @@ public class ClassConfirmation extends EditableInventory {
return;
playerData.giveClassPoints(-1);
(playerData.hasSavedClass(profess) ? playerData.getClassInfo(profess) : new SavedClassInformation(1, 0, 0, 0, 0)).load(profess, playerData);
(playerData.hasSavedClass(profess) ? playerData.getClassInfo(profess) : new SavedClassInformation(
MMOCore.plugin.dataProvider.getDataManager().getDefaultData())).load(profess, playerData);
while (playerData.hasSkillBound(0))
playerData.unbindSkill(0);
MMOCore.plugin.configManager.getSimpleMessage("class-select", "class", profess.getName()).send(player);

View File

@ -99,25 +99,5 @@ public abstract class PlayerDataManager {
attributePoints = 0;
attrReallocPoints = 0;
}
public int getLevel() {
return level;
}
public int getSkillPoints() {
return skillPoints;
}
public int getClassPoints() {
return classPoints;
}
public int getAttributeReallocationPoints() {
return attrReallocPoints;
}
public int getAttributePoints() {
return attributePoints;
}
}
}

View File

@ -43,7 +43,7 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
data.setClassPoints(getDefaultData().getClassPoints());
data.setSkillPoints(getDefaultData().getSkillPoints());
data.setAttributePoints(getDefaultData().getAttributePoints());
data.setAttributeReallocationPoints(getDefaultData().getAttributeReallocationPoints());
data.setAttributeReallocationPoints(getDefaultData().getAttrReallocPoints());
data.setExperience(0);
data.setMana(data.getStats().getStat(StatType.MAX_MANA));
data.setStamina(data.getStats().getStat(StatType.MAX_STAMINA));

View File

@ -27,7 +27,7 @@ public class YAMLPlayerDataManager extends PlayerDataManager {
data.setClassPoints(config.getInt("class-points", getDefaultData().getClassPoints()));
data.setSkillPoints(config.getInt("skill-points", getDefaultData().getSkillPoints()));
data.setAttributePoints(config.getInt("attribute-points", getDefaultData().getAttributePoints()));
data.setAttributeReallocationPoints(config.getInt("attribute-realloc-points", getDefaultData().getAttributeReallocationPoints()));
data.setAttributeReallocationPoints(config.getInt("attribute-realloc-points", getDefaultData().getAttrReallocPoints()));
data.setLevel(config.getInt("level", getDefaultData().getLevel()));
data.setExperience(config.getInt("experience"));
if (config.contains("class"))