added api for conditional stats

This commit is contained in:
Indyuce 2019-08-29 14:52:43 +02:00
parent c6545f5e95
commit c41d400ffd
8 changed files with 132 additions and 72 deletions

View File

@ -1,18 +1,15 @@
package net.Indyuce.mmoitems.api.player;
import java.text.DecimalFormat;
import java.util.regex.Pattern;
import org.bukkit.ChatColor;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import com.google.gson.JsonParser;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.Message;
import net.Indyuce.mmoitems.api.item.NBTItem;
import net.Indyuce.mmoitems.stat.data.AbilityData;
import net.Indyuce.mmoitems.stat.type.Conditional;
public abstract class RPGPlayer {
private final PlayerData playerData;
@ -56,60 +53,11 @@ public abstract class RPGPlayer {
setStamina(getStamina() + value);
}
public boolean canUse(NBTItem item, boolean msg) {
public boolean canUse(NBTItem item, boolean message) {
// item permission
String perm = item.getString("MMOITEMS_PERMISSION");
if (!perm.equals("") && !player.hasPermission("mmoitems.bypass.item") && MMOItems.plugin.getConfig().getBoolean("permissions.items")) {
String[] split = perm.split("\\|");
for (String s : split)
if (!player.hasPermission(s)) {
if (msg) {
Message.NOT_ENOUGH_PERMS.format(ChatColor.RED).send(player, "cant-use-item");
player.playSound(player.getLocation(), Sound.ENTITY_VILLAGER_NO, 1, 1.5f);
}
for (Conditional condition : MMOItems.plugin.getStats().getConditionals())
if (!condition.canUse(this, item, message))
return false;
}
}
if (item.hasTag("MMOITEMS_SOULBOUND") && !item.getString("MMOITEMS_SOULBOUND").contains(player.getUniqueId().toString())) {
if (msg) {
int level = new JsonParser().parse(item.getString("MMOITEMS_SOULBOUND")).getAsJsonObject().get("Level").getAsInt();
Message.SOULBOUND_RESTRICTION.format(ChatColor.RED).send(player, "cant-use-item");
player.playSound(player.getLocation(), Sound.ENTITY_VILLAGER_NO, 1, 1.5f);
player.damage(MMOItems.plugin.getLanguage().soulboundBaseDamage + level * MMOItems.plugin.getLanguage().soulboundPerLvlDamage);
}
return false;
}
// durability
if (item.hasTag("MMOITEMS_DURABILITY") && item.getDouble("MMOITEMS_DURABILITY") < 1) {
if (msg) {
Message.ZERO_DURABILITY.format(ChatColor.RED).send(player, "cant-use-item");
player.playSound(player.getLocation(), Sound.ENTITY_VILLAGER_NO, 1, 1.5f);
}
return false;
}
// required class
String requiredClass = item.getString("MMOITEMS_REQUIRED_CLASS");
if (!requiredClass.equals("") && !hasRightClass(requiredClass) && !player.hasPermission("mmoitems.bypass.class")) {
if (msg) {
Message.WRONG_CLASS.format(ChatColor.RED).send(player, "cant-use-item");
player.playSound(player.getLocation(), Sound.ENTITY_VILLAGER_NO, 1, 1.5f);
}
return false;
}
// required level
int level = item.getInteger("MMOITEMS_REQUIRED_LEVEL");
if (getLevel() < level && !player.hasPermission("mmoitems.bypass.level")) {
if (msg) {
Message.NOT_ENOUGH_LEVELS.format(ChatColor.RED).send(player, "cant-use-item");
player.playSound(player.getLocation(), Sound.ENTITY_VILLAGER_NO, 1, 1.5f);
}
return false;
}
return true;
}
@ -146,13 +94,4 @@ public abstract class RPGPlayer {
return true;
}
private boolean hasRightClass(String requiredClass) {
String name = ChatColor.stripColor(getClassName());
for (String found : requiredClass.split(Pattern.quote(", ")))
if (found.equalsIgnoreCase(name))
return true;
return false;
}
}

View File

@ -12,6 +12,7 @@ import java.util.logging.Level;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.Type;
import net.Indyuce.mmoitems.stat.type.AttributeStat;
import net.Indyuce.mmoitems.stat.type.Conditional;
import net.Indyuce.mmoitems.stat.type.DoubleStat;
import net.Indyuce.mmoitems.stat.type.ItemStat;
@ -19,6 +20,7 @@ public class StatManager {
private final Map<String, ItemStat> stats = new LinkedHashMap<>();
private final Set<DoubleStat> gem = new HashSet<>();
private final Set<AttributeStat> attribute = new HashSet<>();
private final Set<Conditional> conditionals = new HashSet<>();
/*
* load default stats using java reflection, get all public static final
@ -49,6 +51,10 @@ public class StatManager {
return gem;
}
public Set<Conditional> getConditionals() {
return conditionals;
}
/*
* the extra checks in that method to register stats even after the plugin
* has successfully enabled otherwise the other sets would not be updated.
@ -66,6 +72,9 @@ public class StatManager {
if (stat instanceof AttributeStat)
attribute.add((AttributeStat) stat);
if (stat instanceof Conditional)
conditionals.add((Conditional) stat);
/*
* cache stat for every type which may have this stat. really important
* otherwise the stat will NOT be used anywhere in the plugin.

View File

@ -2,17 +2,23 @@ package net.Indyuce.mmoitems.stat;
import java.util.logging.Level;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.inventory.ItemStack;
import net.Indyuce.mmoitems.api.Message;
import net.Indyuce.mmoitems.api.interaction.util.DurabilityItem;
import net.Indyuce.mmoitems.api.interaction.util.DurabilityState;
import net.Indyuce.mmoitems.api.item.NBTItem;
import net.Indyuce.mmoitems.api.item.build.MMOItemBuilder;
import net.Indyuce.mmoitems.api.player.RPGPlayer;
import net.Indyuce.mmoitems.stat.data.StatData;
import net.Indyuce.mmoitems.stat.type.Conditional;
import net.Indyuce.mmoitems.stat.type.DoubleStat;
import net.Indyuce.mmoitems.version.nms.ItemTag;
public class Max_Custom_Durability extends DoubleStat {
public class Max_Custom_Durability extends DoubleStat implements Conditional {
public Max_Custom_Durability() {
super(new ItemStack(Material.SHEARS), "Max Custom Durability", new String[] { "The amount of uses before your", "item becomes unusable/breaks." }, "max-durability", new String[] { "all" });
}
@ -33,4 +39,16 @@ public class Max_Custom_Durability extends DoubleStat {
}
return true;
}
@Override
public boolean canUse(RPGPlayer player, NBTItem item, boolean message) {
if (item.hasTag("MMOITEMS_DURABILITY") && item.getDouble("MMOITEMS_DURABILITY") < 1) {
if (message) {
Message.ZERO_DURABILITY.format(ChatColor.RED).send(player.getPlayer(), "cant-use-item");
player.getPlayer().playSound(player.getPlayer().getLocation(), Sound.ENTITY_VILLAGER_NO, 1, 1.5f);
}
return false;
}
return true;
}
}

View File

@ -5,6 +5,7 @@ import java.util.Arrays;
import java.util.List;
import org.bukkit.ChatColor;
import org.bukkit.Sound;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.event.inventory.InventoryAction;
@ -13,19 +14,22 @@ import org.bukkit.inventory.ItemStack;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.ConfigFile;
import net.Indyuce.mmoitems.api.Message;
import net.Indyuce.mmoitems.api.edition.StatEdition;
import net.Indyuce.mmoitems.api.item.MMOItem;
import net.Indyuce.mmoitems.api.item.NBTItem;
import net.Indyuce.mmoitems.api.item.build.MMOItemBuilder;
import net.Indyuce.mmoitems.api.player.RPGPlayer;
import net.Indyuce.mmoitems.api.util.AltChar;
import net.Indyuce.mmoitems.gui.edition.EditionInventory;
import net.Indyuce.mmoitems.stat.data.StatData;
import net.Indyuce.mmoitems.stat.data.StringListData;
import net.Indyuce.mmoitems.stat.type.Conditional;
import net.Indyuce.mmoitems.stat.type.ItemStat;
import net.Indyuce.mmoitems.version.VersionMaterial;
import net.Indyuce.mmoitems.version.nms.ItemTag;
public class Permission extends ItemStat {
public class Permission extends ItemStat implements Conditional {
public Permission() {
super(new ItemStack(VersionMaterial.OAK_SIGN.toMaterial()), "Permission", new String[] { "The permission needed to use this item." }, "permission", new String[] { "all" });
}
@ -99,4 +103,21 @@ public class Permission extends ItemStat {
if (item.hasTag(getNBTPath()))
mmoitem.setData(this, new StringListData(Arrays.asList(item.getString(getNBTPath()).split("\\|"))));
}
@Override
public boolean canUse(RPGPlayer player, NBTItem item, boolean message) {
String perm = item.getString("MMOITEMS_PERMISSION");
if (!perm.equals("") && !player.getPlayer().hasPermission("mmoitems.bypass.item") && MMOItems.plugin.getConfig().getBoolean("permissions.items")) {
String[] split = perm.split("\\|");
for (String s : split)
if (!player.getPlayer().hasPermission(s)) {
if (message) {
Message.NOT_ENOUGH_PERMS.format(ChatColor.RED).send(player.getPlayer(), "cant-use-item");
player.getPlayer().playSound(player.getPlayer().getLocation(), Sound.ENTITY_VILLAGER_NO, 1, 1.5f);
}
return false;
}
}
return true;
}
}

View File

@ -5,6 +5,7 @@ import java.util.List;
import java.util.regex.Pattern;
import org.bukkit.ChatColor;
import org.bukkit.Sound;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.event.inventory.InventoryAction;
@ -13,20 +14,23 @@ import org.bukkit.inventory.ItemStack;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.ConfigFile;
import net.Indyuce.mmoitems.api.Message;
import net.Indyuce.mmoitems.api.edition.StatEdition;
import net.Indyuce.mmoitems.api.item.MMOItem;
import net.Indyuce.mmoitems.api.item.NBTItem;
import net.Indyuce.mmoitems.api.item.build.MMOItemBuilder;
import net.Indyuce.mmoitems.api.player.RPGPlayer;
import net.Indyuce.mmoitems.api.util.AltChar;
import net.Indyuce.mmoitems.gui.edition.EditionInventory;
import net.Indyuce.mmoitems.stat.data.StatData;
import net.Indyuce.mmoitems.stat.data.StringListData;
import net.Indyuce.mmoitems.stat.type.Conditional;
import net.Indyuce.mmoitems.stat.type.ItemStat;
import net.Indyuce.mmoitems.stat.type.StringStat;
import net.Indyuce.mmoitems.version.VersionMaterial;
import net.Indyuce.mmoitems.version.nms.ItemTag;
public class Required_Class extends StringStat {
public class Required_Class extends StringStat implements Conditional {
public Required_Class() {
super(new ItemStack(VersionMaterial.WRITABLE_BOOK.toMaterial()), "Required Class", new String[] { "The class you need to", "profress to use your item." }, "required-class", new String[] { "all" });
}
@ -98,4 +102,27 @@ public class Required_Class extends StringStat {
item.addItemTag(new ItemTag("MMOITEMS_REQUIRED_CLASS", joined));
return true;
}
@Override
public boolean canUse(RPGPlayer player, NBTItem item, boolean message) {
String requiredClass = item.getString("MMOITEMS_REQUIRED_CLASS");
if (!requiredClass.equals("") && !hasRightClass(player, requiredClass) && !player.getPlayer().hasPermission("mmoitems.bypass.class")) {
if (message) {
Message.WRONG_CLASS.format(ChatColor.RED).send(player.getPlayer(), "cant-use-item");
player.getPlayer().playSound(player.getPlayer().getLocation(), Sound.ENTITY_VILLAGER_NO, 1, 1.5f);
}
return false;
}
return true;
}
private boolean hasRightClass(RPGPlayer player, String requiredClass) {
String name = ChatColor.stripColor(player.getClassName());
for (String found : requiredClass.split(Pattern.quote(", ")))
if (found.equalsIgnoreCase(name))
return true;
return false;
}
}

View File

@ -1,14 +1,20 @@
package net.Indyuce.mmoitems.stat;
import org.bukkit.ChatColor;
import org.bukkit.Sound;
import org.bukkit.inventory.ItemStack;
import net.Indyuce.mmoitems.api.Message;
import net.Indyuce.mmoitems.api.item.NBTItem;
import net.Indyuce.mmoitems.api.item.build.MMOItemBuilder;
import net.Indyuce.mmoitems.api.player.RPGPlayer;
import net.Indyuce.mmoitems.stat.data.StatData;
import net.Indyuce.mmoitems.stat.type.Conditional;
import net.Indyuce.mmoitems.stat.type.DoubleStat;
import net.Indyuce.mmoitems.version.VersionMaterial;
import net.Indyuce.mmoitems.version.nms.ItemTag;
public class Required_Level extends DoubleStat {
public class Required_Level extends DoubleStat implements Conditional {
public Required_Level() {
super(new ItemStack(VersionMaterial.EXPERIENCE_BOTTLE.toMaterial()), "Required Level", new String[] { "The level your item needs", "in order to be used." }, "required-level", new String[] { "all" });
}
@ -21,4 +27,17 @@ public class Required_Level extends DoubleStat {
item.getLore().insert("required-level", format(lvl, "#", "" + lvl));
return true;
}
@Override
public boolean canUse(RPGPlayer player, NBTItem item, boolean message) {
int level = item.getInteger("MMOITEMS_REQUIRED_LEVEL");
if (player.getLevel() < level && !player.getPlayer().hasPermission("mmoitems.bypass.level")) {
if (message) {
Message.NOT_ENOUGH_LEVELS.format(ChatColor.RED).send(player.getPlayer(), "cant-use-item");
player.getPlayer().playSound(player.getPlayer().getLocation(), Sound.ENTITY_VILLAGER_NO, 1, 1.5f);
}
return false;
}
return true;
}
}

View File

@ -3,24 +3,29 @@ package net.Indyuce.mmoitems.stat;
import java.util.UUID;
import java.util.regex.Pattern;
import org.bukkit.ChatColor;
import org.bukkit.Sound;
import org.bukkit.inventory.ItemStack;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.MMOUtils;
import net.Indyuce.mmoitems.api.Message;
import net.Indyuce.mmoitems.api.item.MMOItem;
import net.Indyuce.mmoitems.api.item.NBTItem;
import net.Indyuce.mmoitems.api.item.build.MMOItemBuilder;
import net.Indyuce.mmoitems.api.player.RPGPlayer;
import net.Indyuce.mmoitems.stat.data.StatData;
import net.Indyuce.mmoitems.stat.type.Conditional;
import net.Indyuce.mmoitems.stat.type.InternalStat;
import net.Indyuce.mmoitems.stat.type.ItemStat;
import net.Indyuce.mmoitems.version.VersionMaterial;
import net.Indyuce.mmoitems.version.nms.ItemTag;
public class Soulbound extends InternalStat {
public class Soulbound extends InternalStat implements Conditional {
public Soulbound() {
super(new ItemStack(VersionMaterial.ENDER_EYE.toMaterial()), "Soulbound", new String[0], "soulbound", new String[] { "all" });
}
@ -86,4 +91,18 @@ public class Soulbound extends InternalStat {
return object;
}
}
@Override
public boolean canUse(RPGPlayer player, NBTItem item, boolean message) {
if (item.hasTag("MMOITEMS_SOULBOUND") && !item.getString("MMOITEMS_SOULBOUND").contains(player.getPlayer().getUniqueId().toString())) {
if (message) {
int level = new JsonParser().parse(item.getString("MMOITEMS_SOULBOUND")).getAsJsonObject().get("Level").getAsInt();
Message.SOULBOUND_RESTRICTION.format(ChatColor.RED).send(player.getPlayer(), "cant-use-item");
player.getPlayer().playSound(player.getPlayer().getLocation(), Sound.ENTITY_VILLAGER_NO, 1, 1.5f);
player.getPlayer().damage(MMOItems.plugin.getLanguage().soulboundBaseDamage + level * MMOItems.plugin.getLanguage().soulboundPerLvlDamage);
}
return false;
}
return true;
}
}

View File

@ -0,0 +1,8 @@
package net.Indyuce.mmoitems.stat.type;
import net.Indyuce.mmoitems.api.item.NBTItem;
import net.Indyuce.mmoitems.api.player.RPGPlayer;
public interface Conditional {
public boolean canUse(RPGPlayer player, NBTItem item, boolean message);
}