optimize import and code reformat

This commit is contained in:
unurled 2024-02-27 21:00:24 +01:00
parent 36f8e3f6f0
commit 64ef73f7e0
Signed by: unurled
GPG key ID: FDBC9CBE1F82423F
15 changed files with 379 additions and 371 deletions

View file

@ -4,49 +4,13 @@ import org.bukkit.NamespacedKey;
import org.bukkit.persistence.PersistentDataType;
public enum Attribute {
HEALTH(
"HEALTH",
"Health",
100,
100000,
0,
new NamespacedKey("sr", "health")),
STRENGTH(
"STRENGTH",
"Strength",
10,
10000,
0,
new NamespacedKey("sr", "strength")),
DEFENSE(
"DEFENSE",
"Defense",
10,
10000,
0,
new NamespacedKey("sr", "defense")),
AGILITY(
"AGILITY",
"Agility",
100,
10000,
0,
new NamespacedKey("sr", "agility")),
LUCK(
"LUCK",
"Luck",
100,
99999,
-99999,
new NamespacedKey("sr", "luck")),
HEALTH("HEALTH", "Health", 100, 100000, 0, new NamespacedKey("sr", "health")),
STRENGTH("STRENGTH", "Strength", 10, 10000, 0, new NamespacedKey("sr", "strength")),
DEFENSE("DEFENSE", "Defense", 10, 10000, 0, new NamespacedKey("sr", "defense")),
AGILITY("AGILITY", "Agility", 100, 10000, 0, new NamespacedKey("sr", "agility")),
LUCK("LUCK", "Luck", 100, 99999, -99999, new NamespacedKey("sr", "luck")),
MANA("MANA", "Mana", 100, 10000, 0, new NamespacedKey("sr", "mana")),
CHARISMA(
"CHARISMA",
"Charisma",
0,
10000,
-10000,
new NamespacedKey("sr", "charisma")),
CHARISMA("CHARISMA", "Charisma", 0, 10000, -10000, new NamespacedKey("sr", "charisma")),
;
private final String name;
@ -57,12 +21,7 @@ public enum Attribute {
private final NamespacedKey key;
Attribute(
String ID,
String name,
int defaultValue,
int maxValue,
int minValue,
NamespacedKey key) {
String ID, String name, int defaultValue, int maxValue, int minValue, NamespacedKey key) {
this.ID = ID;
this.name = name;
this.defaultValue = defaultValue;

View file

@ -34,7 +34,8 @@ public class CombatManager extends Manager {
public void onDamage(EntityDamageByEntityEvent e) {
e.setDamage(0.0);
if (!(e.getDamager() instanceof LivingEntity damager) || !(e.getEntity() instanceof LivingEntity entity)) {
if (!(e.getDamager() instanceof LivingEntity damager)
|| !(e.getEntity() instanceof LivingEntity entity)) {
return;
}
PlayerManager pm = (PlayerManager) PlayerManager.getInstance(PlayerManager.class);

View file

@ -1,5 +1,8 @@
package me.unurled.sacredrealms.sr.components.item;
import static me.unurled.sacredrealms.sr.utils.Component.comp;
import static me.unurled.sacredrealms.sr.utils.Items.lore;
import java.util.HashMap;
import java.util.List;
import me.unurled.sacredrealms.sr.components.attributes.Attribute;
@ -7,6 +10,10 @@ import me.unurled.sacredrealms.sr.components.item.abilities.Ability;
import me.unurled.sacredrealms.sr.components.item.enchantments.Enchantment;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull;
/* <b><color of the rarity>rarity
* <dark_gray>Description: blablabla
@ -31,37 +38,45 @@ import org.bukkit.inventory.ItemStack;
* */
public class Item {
private final String ID;
@NotNull private final String ID;
private String name;
@NotNull private String name;
private Material material;
@NotNull private Material material;
private String description;
@NotNull private String description;
private Rarity rarity;
@NotNull private Integer customModelData;
private HashMap<Attribute, Double> attributes;
@NotNull private Rarity rarity;
private HashMap<Enchantment, Integer> enchantments;
@NotNull private HashMap<Attribute, Double> attributes;
private List<Ability> abilities;
@NotNull private HashMap<Enchantment, Integer> enchantments;
public Item(String ID, String name, Material material) {
@NotNull private List<Ability> abilities;
public Item(@NotNull String ID, @NotNull String name, @NotNull Material material) {
this.ID = ID;
this.name = name;
this.material = material;
this.description = "";
this.rarity = Rarity.COMMON;
this.customModelData = 0;
this.abilities = List.of();
this.attributes = new HashMap<>();
this.enchantments = new HashMap<>();
}
public String getID() {
public @NotNull String getID() {
return ID;
}
public String getName() {
public @NotNull String getName() {
return name;
}
public void setName(String name) {
public void setName(@NotNull String name) {
this.name = name;
}
@ -69,7 +84,7 @@ public class Item {
return material;
}
public void setMaterial(Material material) {
public void setMaterial(@NotNull Material material) {
this.material = material;
}
@ -93,14 +108,14 @@ public class Item {
return attributes;
}
public Double getAttribute(Attribute attribute) {
return attributes.get(attribute);
}
public void setAttributes(HashMap<Attribute, Double> attributes) {
this.attributes = attributes;
}
public Double getAttribute(Attribute attribute) {
return attributes.get(attribute);
}
public void addAttribute(Attribute attribute, double value) {
attributes.put(attribute, value);
}
@ -141,9 +156,57 @@ public class Item {
abilities.remove(ability);
}
public Integer getCustomModelData() {
return customModelData;
}
public void setCustomModelData(Integer customModelData) {
this.customModelData = customModelData;
}
public ItemStack toItemStack() {
ItemStack item = new ItemStack(material);
// set item meta
ItemMeta meta = item.getItemMeta();
if (meta == null) {
return null;
}
PersistentDataContainer p = meta.getPersistentDataContainer();
p.set(ItemManager.ID, PersistentDataType.STRING, ID);
meta.displayName(comp(name).color(rarity.getColor()));
if (customModelData != 0) {
meta.setCustomModelData(customModelData);
}
lore(item);
item.setItemMeta(meta);
return item;
}
@Override
public String toString() {
return "{"
+ "ID: "
+ ID
+ ", name: "
+ name
+ ", material: "
+ material
+ ", description: "
+ description
+ ", customModelData: "
+ customModelData
+ ", rarity: "
+ rarity
+ ", attributes: "
+ attributes
+ ", enchantments: "
+ enchantments
+ ", abilities: "
+ abilities
+ "}";
}
}

View file

@ -13,7 +13,10 @@ public enum Rarity {
SPECIAL("<red>Special", "SPECIAL", 7, NamedTextColor.RED),
UNIQUE("<yellow>Unique", "UNIQUE", 8, NamedTextColor.YELLOW),
ADMIN(
"<color:#800000><obf>aa</obf><dark_red>Admin</dark_red><obf>aa</obf></color>", "ADMIN", 100, NamedTextColor.DARK_RED);
"<color:#800000><obf>aa</obf><dark_red>Admin</dark_red><obf>aa</obf></color>",
"ADMIN",
100,
NamedTextColor.DARK_RED);
private final String name;
private final String ID;

View file

@ -1,9 +1,6 @@
package me.unurled.sacredrealms.sr.components.item.abilities;
/**
* Represents an ability that an item can have.
* TODO: Implement this class
*/
/** Represents an ability that an item can have. TODO: Implement this class */
public class Ability {
private final String name;
@ -12,7 +9,8 @@ public class Ability {
private final Integer manaCost;
private final Integer damage;
public Ability(String name, String description, Integer cooldown, Integer manaCost, Integer damage) {
public Ability(
String name, String description, Integer cooldown, Integer manaCost, Integer damage) {
this.name = name;
this.description = description;
this.cooldown = cooldown;

View file

@ -19,17 +19,10 @@ import org.jetbrains.annotations.Nullable;
public class SRPlayer {
@Expose
private final UUID uuid;
@Expose
private Inventory inventory;
@Expose
private final HashMap<Attribute, Double> attributes;
@Expose
private final List<PotionEffect> potionEffects;
@Expose private final UUID uuid;
@Expose private final HashMap<Attribute, Double> attributes;
@Expose private final List<PotionEffect> potionEffects;
@Expose private Inventory inventory;
public SRPlayer(@NotNull Player player) {
this.uuid = player.getUniqueId();
@ -43,15 +36,15 @@ public class SRPlayer {
return uuid;
}
public void setInventory(Inventory inventory) {
this.inventory = inventory;
}
@NotNull
public Inventory getInventory() {
return inventory;
}
public void setInventory(Inventory inventory) {
this.inventory = inventory;
}
@NotNull
public HashMap<Attribute, Double> getAttributes() {
return attributes;
@ -80,7 +73,8 @@ public class SRPlayer {
if (p == null) {
return null;
}
Object o = p.getInventory()
Object o =
p.getInventory()
.getItemInMainHand()
.getItemMeta()
.getPersistentDataContainer()
@ -97,23 +91,20 @@ public class SRPlayer {
if (p == null) {
return;
}
if (checkIfValueIsOutOfBoundsForAttribute(attribute, value, p))
return;
if (checkIfValueIsOutOfBoundsForAttribute(attribute, value, p)) return;
ItemStack i = p.getInventory().getItemInMainHand();
ItemMeta im = i.getItemMeta();
im.getPersistentDataContainer()
.set(attribute.getKey(), PersistentDataType.DOUBLE, value);
im.getPersistentDataContainer().set(attribute.getKey(), PersistentDataType.DOUBLE, value);
im.lore();
}
private boolean checkIfValueIsOutOfBoundsForAttribute(@NotNull Attribute attribute,
@NotNull Double value,
Player p) {
private boolean checkIfValueIsOutOfBoundsForAttribute(
@NotNull Attribute attribute, @NotNull Double value, Player p) {
if (!attribute.getType().getComplexType().isInstance(value)) {
return true;
}
if (attribute.getType().equals(PersistentDataType.DOUBLE)) {
double d = (double) value;
double d = value;
if (d < attribute.getMinValue() || d > attribute.getMaxValue()) {
warn(
"(Player "
@ -157,13 +148,13 @@ public class SRPlayer {
potionEffects.clear();
}
public void setPotionEffects(@NotNull List<PotionEffect> effects) {
potionEffects.clear();
potionEffects.addAll(effects);
}
@NotNull
public List<PotionEffect> getPotionEffects() {
return potionEffects;
}
public void setPotionEffects(@NotNull List<PotionEffect> effects) {
potionEffects.clear();
potionEffects.addAll(effects);
}
}

View file

@ -5,8 +5,6 @@ import static me.unurled.sacredrealms.sr.utils.Logger.error;
import java.io.File;
import java.io.IOException;
import me.unurled.sacredrealms.sr.SR;
import me.unurled.sacredrealms.sr.data.DataHandler;
import me.unurled.sacredrealms.sr.data.Redis;
import me.unurled.sacredrealms.sr.managers.Manager;
import org.bukkit.configuration.file.FileConfiguration;

View file

@ -1,7 +1,5 @@
package me.unurled.sacredrealms.sr.data.gson;
import static me.unurled.sacredrealms.sr.utils.Logger.log;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
@ -9,7 +7,6 @@ import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import java.lang.reflect.Type;
import java.util.Arrays;
import org.bukkit.Bukkit;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
@ -18,8 +15,9 @@ import org.bukkit.inventory.ItemStack;
public class InventoryDeserializer implements JsonDeserializer<Inventory> {
@Override
public Inventory deserialize(JsonElement jsonElement, Type type,
JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
public Inventory deserialize(
JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext)
throws JsonParseException {
Inventory inv = Bukkit.createInventory(null, InventoryType.PLAYER);
// parse from a string, to a list of string
String[] contents = jsonElement.getAsString().split(";");
@ -33,8 +31,7 @@ public class InventoryDeserializer implements JsonDeserializer<Inventory> {
inv.setItem(i, null);
} else {
ItemStack item = gson.fromJson(contents[i], ItemStack.class);
if (item != null)
inv.setItem(i, item);
if (item != null) inv.setItem(i, item);
}
}
return inv;

View file

@ -1,14 +1,11 @@
package me.unurled.sacredrealms.sr.data.gson;
import static me.unurled.sacredrealms.sr.utils.Logger.error;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import org.bukkit.inventory.Inventory;
@ -16,10 +13,12 @@ import org.bukkit.inventory.Inventory;
public class InventorySerializer implements JsonSerializer<Inventory> {
@Override
public JsonElement serialize(Inventory itemStacks, Type type,
JsonSerializationContext jsonSerializationContext) {
List<String> collect = Arrays.stream(itemStacks.getContents())
.map(item -> item == null ? "null" : Arrays.toString(item.serializeAsBytes())).collect(Collectors.toList());
public JsonElement serialize(
Inventory itemStacks, Type type, JsonSerializationContext jsonSerializationContext) {
List<String> collect =
Arrays.stream(itemStacks.getContents())
.map(item -> item == null ? "null" : Arrays.toString(item.serializeAsBytes()))
.collect(Collectors.toList());
String s = String.join(";", collect);
return new JsonPrimitive(s);

View file

@ -1,21 +1,19 @@
package me.unurled.sacredrealms.sr.data.gson;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import java.lang.reflect.Type;
import java.util.Map;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
public class ItemStackDeserializer implements JsonDeserializer<ItemStack> {
@Override
public ItemStack deserialize(JsonElement jsonElement, Type type,
JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
public ItemStack deserialize(
JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext)
throws JsonParseException {
Gson gson = new Gson();
byte[] mm = gson.fromJson(jsonElement, byte[].class);
if (mm != null) {

View file

@ -15,15 +15,16 @@ import org.bukkit.potion.PotionEffectType;
public class PotionEffectDeserializer implements JsonDeserializer<PotionEffect> {
@Override
public PotionEffect deserialize(JsonElement jsonElement, Type type,
JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
public PotionEffect deserialize(
JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext)
throws JsonParseException {
try {
String pot = jsonElement.getAsString();
pot = pot.replace("{", "").replace("}", "");
String[] parts = pot.split(",");
// find the potion effect type from the string part[0] is a key
PotionEffectType pet = Registry.POTION_EFFECT_TYPE.get(new NamespacedKey("minecraft",
parts[0]));
PotionEffectType pet =
Registry.POTION_EFFECT_TYPE.get(new NamespacedKey("minecraft", parts[0]));
int duration = Integer.parseInt(parts[1]);
int amplifier = Integer.parseInt(parts[2]);
boolean particles = Boolean.parseBoolean(parts[3]);

View file

@ -1,6 +1,5 @@
package me.unurled.sacredrealms.sr.data.gson;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
@ -11,17 +10,16 @@ import org.bukkit.potion.PotionEffect;
public class PotionEffectSerializer implements JsonSerializer<PotionEffect> {
@Override
public JsonElement serialize(PotionEffect potionEffect, Type type,
JsonSerializationContext jsonSerializationContext) {
StringBuilder sb = new StringBuilder();
sb.append("{");
sb.append(potionEffect.getType().key());
sb.append(",");
sb.append(potionEffect.getDuration());
sb.append(",");
sb.append(potionEffect.getAmplifier());
sb.append(",").append(potionEffect.hasParticles());
sb.append("}");
return new JsonPrimitive(sb.toString());
public JsonElement serialize(
PotionEffect potionEffect, Type type, JsonSerializationContext jsonSerializationContext) {
String sb = "{"
+ potionEffect.getType().key()
+ ","
+ potionEffect.getDuration()
+ ","
+ potionEffect.getAmplifier()
+ "," + potionEffect.hasParticles()
+ "}";
return new JsonPrimitive(sb);
}
}

View file

@ -53,31 +53,20 @@ public class Items {
return 0.0;
}
/** <b><color of the rarity>rarity
* <dark_gray>Description: blablabla
* <dark_gray>blabbla
* <dark_gray>blabla
* <gray>Attributes:
* <gray> Health: <green>+100
* <gray> Strength: <red>+10
* <gray> Mana: <dark_aqua>+10
* <gray>Abilities:
* <gray> Ability 1,<yellow> On Rightclick unleash a powerful attack
* <dark_gray>cooldown: 10s,
* <dark_gray>damage: 100,
* <dark_gray>mana cost: <dark_aqua>50
* <gray> Ability 2,<yellow> On Rightclick unleash a powerful attack
* <dark_gray>cooldown: 10s,
* <dark_gray>damage: 100,
* <dark_gray>mana cost: <dark_aqua>50
* <dark_purple>Enchantments:
* <blue> Enchantment <if max level gold>1
* <blue> Enchantment <if max level gold>2
/**
* <b><color of the rarity>rarity <dark_gray>Description: blablabla <dark_gray>blabbla
* <dark_gray>blabla <gray>Attributes: <gray> Health: <green>+100 <gray> Strength: <red>+10
* <gray> Mana: <dark_aqua>+10 <gray>Abilities: <gray> Ability 1,<yellow> On Rightclick unleash
* a powerful attack <dark_gray>cooldown: 10s, <dark_gray>damage: 100, <dark_gray>mana cost:
* <dark_aqua>50 <gray> Ability 2,<yellow> On Rightclick unleash a powerful attack
* <dark_gray>cooldown: 10s, <dark_gray>damage: 100, <dark_gray>mana cost: <dark_aqua>50
* <dark_purple>Enchantments: <blue> Enchantment <if max level gold>1 <blue> Enchantment <if max
* level gold>2
*
* @param item the item
* @param stack the itemstack
* @return the itemstack with lore
* */
*/
public static ItemStack lore(ItemStack stack) {
ItemManager im = (ItemManager) ItemManager.getInstance(ItemManager.class);
@ -94,12 +83,23 @@ public class Items {
lore.add(comp("<dark_gray>" + item.getDescription()));
if (!item.getAttributes().isEmpty()) {
lore.add(comp("<gray>Attributes:"));
item.getAttributes().forEach((k, v) -> lore.add(comp(" <gray>➤ " + k.getName() + ": " + (v < 0 ? "<red>" : "<green>") +
"+" + v)));
item.getAttributes()
.forEach(
(k, v) ->
lore.add(
comp(
" <gray>➤ "
+ k.getName()
+ ": "
+ (v < 0 ? "<red>" : "<green>")
+ "+"
+ v)));
}
if (!item.getAbilities().isEmpty()) {
lore.add(comp("<gray>Abilities:"));
item.getAbilities().forEach(a -> {
item.getAbilities()
.forEach(
a -> {
lore.add(comp(" <gray>➤ " + a.getName() + ",<yellow> " + a.getDescription()));
lore.add(comp(" <dark_gray>cooldown: " + a.getCooldown() + "s,"));
lore.add(comp(" <dark_gray>damage: " + a.getDamage() + ","));
@ -108,8 +108,10 @@ public class Items {
}
if (!item.getEnchantments().isEmpty()) {
lore.add(comp("<dark_purple>Enchantments:"));
item.getEnchantments().forEach((k, v) -> lore.add(comp(" <blue>➤ " + k.getName() + " <if "
+ "max level gold>" + v)));
item.getEnchantments()
.forEach(
(k, v) ->
lore.add(comp(" <blue>➤ " + k.getName() + " <if " + "max level gold>" + v)));
}
stack.lore(lore);