code refactoring

This commit is contained in:
unurled 2024-03-19 21:27:14 +01:00
parent bcb1fa2e56
commit 2d3ddef181
Signed by: unurled
GPG key ID: FDBC9CBE1F82423F
9 changed files with 62 additions and 62 deletions

View file

@ -302,7 +302,7 @@ public class ItemCommand implements TabExecutor {
return; return;
} }
Item item = new Item(); Item item = new Item();
item.setID(args[1]); item.setId(args[1]);
im.addItem(item); im.addItem(item);
sender.sendMessage( sender.sendMessage(
comp( comp(

View file

@ -133,7 +133,7 @@ public class SREntityType {
@SuppressWarnings("unused") @SuppressWarnings("unused")
public Double getTotalItemAttribute(Attribute attribute) { public Double getTotalItemAttribute(Attribute attribute) {
if (itemAttributes == null) itemAttributes = new HashMap<>(); if (itemAttributes == null) itemAttributes = new EnumMap<>(Attribute.class);
return itemAttributes.getOrDefault(attribute, new HashMap<>()).values().stream() return itemAttributes.getOrDefault(attribute, new HashMap<>()).values().stream()
.reduce(0.0, Double::sum); .reduce(0.0, Double::sum);
} }

View file

@ -45,7 +45,7 @@ import org.jetbrains.annotations.NotNull;
* */ * */
public class Item { public class Item {
@NotNull private String ID; @NotNull private String id;
@NotNull private String name; @NotNull private String name;
@ -59,14 +59,14 @@ public class Item {
@NotNull private ItemType type; @NotNull private ItemType type;
@NotNull private HashMap<Attribute, Double> attributes; @NotNull private Map<Attribute, Double> attributes;
@NotNull private HashMap<Enchantment, Integer> enchantments; @NotNull private Map<Enchantment, Integer> enchantments;
@NotNull private List<Ability> abilities; @NotNull private List<Ability> abilities;
public Item() { public Item() {
this.ID = ""; this.id = "";
this.name = ""; this.name = "";
this.material = Material.AIR; this.material = Material.AIR;
this.description = ""; this.description = "";
@ -78,8 +78,8 @@ public class Item {
this.type = ItemType.MISC; this.type = ItemType.MISC;
} }
public Item(@NotNull String ID, @NotNull String name, @NotNull Material material) { public Item(@NotNull String id, @NotNull String name, @NotNull Material material) {
this.ID = ID; this.id = id;
this.name = name; this.name = name;
this.material = material; this.material = material;
this.description = ""; this.description = "";
@ -91,8 +91,8 @@ public class Item {
this.type = ItemType.MISC; this.type = ItemType.MISC;
} }
public @NotNull String getID() { public @NotNull String getId() {
return ID; return id;
} }
public @NotNull String getName() { public @NotNull String getName() {
@ -103,46 +103,46 @@ public class Item {
this.name = name; this.name = name;
} }
public Material getMaterial() { public void setId(@NotNull String arg) {
return material; this.id = arg;
} }
public void setMaterial(@NotNull Material material) { public void setMaterial(@NotNull Material material) {
this.material = material; this.material = material;
} }
public String getDescription() { public @NotNull Material getMaterial() {
return material;
}
public @NotNull String getDescription() {
return description; return description;
} }
public void setDescription(String description) { public void setDescription(@NotNull String description) {
this.description = description; this.description = description;
} }
public Rarity getRarity() { public @NotNull Rarity getRarity() {
return rarity; return rarity;
} }
public void setRarity(Rarity rarity) { public void setRarity(@NotNull Rarity rarity) {
this.rarity = rarity; this.rarity = rarity;
} }
public ItemType getType() { public @NotNull ItemType getType() {
return type; return type;
} }
public void setType(ItemType type) { public void setType(@NotNull ItemType type) {
this.type = type; this.type = type;
} }
public HashMap<Attribute, Double> getAttributes() { public @NotNull Map<Attribute, Double> getAttributes() {
return attributes; return attributes;
} }
public void setAttributes(HashMap<Attribute, Double> attributes) {
this.attributes = attributes;
}
public Double getAttribute(Attribute attribute) { public Double getAttribute(Attribute attribute) {
return attributes.get(attribute); return attributes.get(attribute);
} }
@ -155,43 +155,54 @@ public class Item {
attributes.remove(attribute); attributes.remove(attribute);
} }
public HashMap<Enchantment, Integer> getEnchantments() { public void setAttributes(@NotNull Map<Attribute, Double> attributes) {
this.attributes = attributes;
}
public @NotNull Map<Enchantment, Integer> getEnchantments() {
return enchantments; return enchantments;
} }
public void setEnchantments(HashMap<Enchantment, Integer> enchantments) { @SuppressWarnings("unused")
public void setEnchantments(@NotNull Map<Enchantment, Integer> enchantments) {
this.enchantments = enchantments; this.enchantments = enchantments;
} }
@SuppressWarnings("unused")
public void addEnchantment(Enchantment enchantment, int level) { public void addEnchantment(Enchantment enchantment, int level) {
enchantments.put(enchantment, level); enchantments.put(enchantment, level);
} }
@SuppressWarnings("unused")
public void removeEnchantment(Enchantment enchantment) { public void removeEnchantment(Enchantment enchantment) {
enchantments.remove(enchantment); enchantments.remove(enchantment);
} }
public List<Ability> getAbilities() { public @NotNull List<Ability> getAbilities() {
return abilities; return abilities;
} }
public void setAbilities(List<Ability> abilities) { @SuppressWarnings("unused")
public void setAbilities(@NotNull List<Ability> abilities) {
this.abilities = abilities; this.abilities = abilities;
} }
@SuppressWarnings("unused")
public void addAbility(Ability ability) { public void addAbility(Ability ability) {
abilities.add(ability); abilities.add(ability);
} }
@SuppressWarnings("unused")
public void removeAbility(Ability ability) { public void removeAbility(Ability ability) {
abilities.remove(ability); abilities.remove(ability);
} }
public Integer getCustomModelData() { @SuppressWarnings("unused")
public @NotNull Integer getCustomModelData() {
return customModelData; return customModelData;
} }
public void setCustomModelData(Integer customModelData) { public void setCustomModelData(@NotNull Integer customModelData) {
this.customModelData = customModelData; this.customModelData = customModelData;
} }
@ -203,7 +214,7 @@ public class Item {
} }
PersistentDataContainer p = meta.getPersistentDataContainer(); PersistentDataContainer p = meta.getPersistentDataContainer();
p.set(ItemManager.ID, PersistentDataType.STRING, ID); p.set(ItemManager.ID, PersistentDataType.STRING, id);
meta.displayName(comp(name).color(rarity.getColor())); meta.displayName(comp(name).color(rarity.getColor()));
@ -221,7 +232,7 @@ public class Item {
public String toString() { public String toString() {
return "{" return "{"
+ "\"id\": \"" + "\"id\": \""
+ ID + id
+ "\", \"name\": \"" + "\", \"name\": \""
+ name + name
+ "\", \"material\": \"" + "\", \"material\": \""
@ -251,7 +262,7 @@ public class Item {
Map<String, Object> map = Map<String, Object> map =
gson.fromJson(item, new TypeToken<Map<String, Object>>() {}.getType()); gson.fromJson(item, new TypeToken<Map<String, Object>>() {}.getType());
this.ID = (String) map.get("id"); this.id = (String) map.get("id");
this.name = (String) map.get("name"); this.name = (String) map.get("name");
this.material = Material.valueOf((String) map.get("material")); this.material = Material.valueOf((String) map.get("material"));
this.description = (String) map.get("description"); this.description = (String) map.get("description");
@ -271,8 +282,4 @@ public class Item {
error("Failed to parse item from string: " + item + "\n" + e.getMessage()); error("Failed to parse item from string: " + item + "\n" + e.getMessage());
} }
} }
public void setID(String arg) {
this.ID = arg;
}
} }

View file

@ -23,12 +23,6 @@ public class ItemManager extends Manager {
items = new HashMap<>(); items = new HashMap<>();
} }
/** Load the manager */
@Override
public void load() {
super.load();
}
/** Save the data */ /** Save the data */
@Override @Override
public void saveData() { public void saveData() {
@ -56,12 +50,12 @@ public class ItemManager extends Manager {
.forEach( .forEach(
key -> { key -> {
Item item = gson.fromJson(dh.get(key), Item.class); Item item = gson.fromJson(dh.get(key), Item.class);
items.put(item.getID(), item); items.put(item.getId(), item);
}); });
} }
public void addItem(Item item) { public void addItem(Item item) {
items.put(item.getID(), item); items.put(item.getId(), item);
} }
public Item getItem(String id) { public Item getItem(String id) {

View file

@ -19,13 +19,13 @@ public enum Rarity {
NamedTextColor.DARK_RED); NamedTextColor.DARK_RED);
private final String name; private final String name;
private final String ID; private final String id;
private final Integer weight; private final Integer weight;
private final TextColor color; private final TextColor color;
Rarity(String name, String ID, Integer weight, TextColor color) { Rarity(String name, String ID, Integer weight, TextColor color) {
this.name = name; this.name = name;
this.ID = ID; this.id = ID;
this.weight = weight; this.weight = weight;
this.color = color; this.color = color;
} }
@ -34,10 +34,11 @@ public enum Rarity {
return name; return name;
} }
public String getID() { public String getId() {
return ID; return id;
} }
@SuppressWarnings("unused")
public Integer getWeight() { public Integer getWeight() {
return weight; return weight;
} }

View file

@ -5,6 +5,7 @@ import static me.unurled.sacredrealms.sr.utils.Logger.warn;
import com.google.gson.annotations.Expose; import com.google.gson.annotations.Expose;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.EnumMap;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -33,8 +34,8 @@ public class SRPlayer {
@Expose private int level = 1; @Expose private int level = 1;
@Expose private Long experience = 0L; @Expose private Long experience = 0L;
private double health = 100; private double health = 100;
@Expose private HashMap<Attribute, Double> attributes = new HashMap<>(); @Expose private Map<Attribute, Double> attributes = new EnumMap<>(Attribute.class);
private HashMap<Attribute, HashMap<ItemStack, Double>> itemAttributes = new HashMap<>(); private Map<Attribute, Map<ItemStack, Double>> itemAttributes = new EnumMap<>(Attribute.class);
@Expose private List<PotionEffect> potionEffects = new ArrayList<>(); @Expose private List<PotionEffect> potionEffects = new ArrayList<>();
@Expose private Inventory inventory = null; @Expose private Inventory inventory = null;
@ -177,18 +178,18 @@ public class SRPlayer {
public void setItemAttributes( public void setItemAttributes(
@NotNull Attribute attribute, @NotNull ItemStack item, @NotNull Double value) { @NotNull Attribute attribute, @NotNull ItemStack item, @NotNull Double value) {
if (itemAttributes == null) itemAttributes = new HashMap<>(); if (itemAttributes == null) itemAttributes = new EnumMap<>(Attribute.class);
if (itemAttributes.containsKey(attribute)) { if (itemAttributes.containsKey(attribute)) {
itemAttributes.get(attribute).put(item, value); itemAttributes.get(attribute).put(item, value);
} else { } else {
HashMap<ItemStack, Double> map = new HashMap<>(); Map<ItemStack, Double> map = new HashMap<>();
map.put(item, value); map.put(item, value);
itemAttributes.put(attribute, map); itemAttributes.put(attribute, map);
} }
} }
public void removeItemAttributes(@NotNull Attribute attribute, @NotNull ItemStack item) { public void removeItemAttributes(@NotNull Attribute attribute, @NotNull ItemStack item) {
if (itemAttributes == null) itemAttributes = new HashMap<>(); if (itemAttributes == null) itemAttributes = new EnumMap<>(Attribute.class);
if (itemAttributes.containsKey(attribute)) { if (itemAttributes.containsKey(attribute)) {
itemAttributes.get(attribute).remove(item); itemAttributes.get(attribute).remove(item);
} }
@ -196,7 +197,7 @@ public class SRPlayer {
@SuppressWarnings("unused") @SuppressWarnings("unused")
public @Nullable Double getItemAttributes(@NotNull Attribute attribute, @NotNull ItemStack item) { public @Nullable Double getItemAttributes(@NotNull Attribute attribute, @NotNull ItemStack item) {
if (itemAttributes == null) itemAttributes = new HashMap<>(); if (itemAttributes == null) itemAttributes = new EnumMap<>(Attribute.class);
if (itemAttributes.containsKey(attribute)) { if (itemAttributes.containsKey(attribute)) {
return itemAttributes.get(attribute).get(item); return itemAttributes.get(attribute).get(item);
} }
@ -205,20 +206,20 @@ public class SRPlayer {
@SuppressWarnings("unused") @SuppressWarnings("unused")
public @NotNull Map<ItemStack, Double> getItemAttributes(@NotNull Attribute attribute) { public @NotNull Map<ItemStack, Double> getItemAttributes(@NotNull Attribute attribute) {
if (itemAttributes == null) itemAttributes = new HashMap<>(); if (itemAttributes == null) itemAttributes = new EnumMap<>(Attribute.class);
return itemAttributes.getOrDefault(attribute, new HashMap<>()); return itemAttributes.getOrDefault(attribute, new HashMap<>());
} }
@SuppressWarnings("unused") @SuppressWarnings("unused")
public @NotNull Map<Attribute, Double> getItemAttributes(Item item) { public @NotNull Map<Attribute, Double> getItemAttributes(Item item) {
if (itemAttributes == null) itemAttributes = new HashMap<>(); if (itemAttributes == null) itemAttributes = new EnumMap<>(Attribute.class);
HashMap<Attribute, Double> map = new HashMap<>(); HashMap<Attribute, Double> map = new HashMap<>();
itemAttributes.forEach((k, v) -> map.put(k, v.getOrDefault(item.toItemStack(), 0d))); itemAttributes.forEach((k, v) -> map.put(k, v.getOrDefault(item.toItemStack(), 0d)));
return map; return map;
} }
public @NotNull Double getTotalItemAttribute(@NotNull Attribute attribute) { public @NotNull Double getTotalItemAttribute(@NotNull Attribute attribute) {
if (itemAttributes == null) itemAttributes = new HashMap<>(); if (itemAttributes == null) itemAttributes = new EnumMap<>(Attribute.class);
return itemAttributes.getOrDefault(attribute, new HashMap<>()).values().stream() return itemAttributes.getOrDefault(attribute, new HashMap<>()).values().stream()
.reduce(0.0, Double::sum); .reduce(0.0, Double::sum);
} }

View file

@ -41,7 +41,7 @@ public class ClientBuildDeserializer implements JsonDeserializer<ClientBuild> {
World world = Bukkit.getWorld(spl[0]); World world = Bukkit.getWorld(spl[0]);
if (world == null) { if (world == null) {
error("World " + spl[0] + " does not exist"); error("World " + spl[0] + " does not exist");
continue; return build;
} }
Location loc; Location loc;
BlockData bData; BlockData bData;

View file

@ -4,8 +4,6 @@ import static me.unurled.sacredrealms.sr.utils.Component.comp;
import me.unurled.sacredrealms.sr.components.player.SRPlayer; import me.unurled.sacredrealms.sr.components.player.SRPlayer;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -21,7 +19,6 @@ public class PlayerLevelUpEvent extends Event {
super(true); super(true);
this.p = p; this.p = p;
this.previousLevel = previousLevel; this.previousLevel = previousLevel;
OfflinePlayer pa = Bukkit.getOfflinePlayer(p.getUuid());
/* /*
* <blue>-------------------------------------------------- * <blue>--------------------------------------------------
* *

View file

@ -73,7 +73,7 @@ public class SRPlayerUtils {
* href="https://github.com/DragonL0508/D-Damage-Indicators/blob/master/src/main/java/me/dragonl/damageIndicators/GlobalListener.java">author</a> * href="https://github.com/DragonL0508/D-Damage-Indicators/blob/master/src/main/java/me/dragonl/damageIndicators/GlobalListener.java">author</a>
*/ */
public static @NotNull TextDisplay spawnIndicator( public static @NotNull TextDisplay spawnIndicator(
@NotNull Entity eventEntity, @NotNull Boolean isHeal, double number) { @NotNull Entity eventEntity, boolean isHeal, double number) {
// vector operation // vector operation
Vector location = eventEntity.getLocation().toVector(); Vector location = eventEntity.getLocation().toVector();
Vector facingVector = eventEntity.getFacing().getDirection(); Vector facingVector = eventEntity.getFacing().getDirection();