code refactoring

This commit is contained in:
unurled 2024-03-19 21:16:53 +01:00
parent ffbdf2e367
commit bcb1fa2e56
Signed by: unurled
GPG key ID: FDBC9CBE1F82423F
5 changed files with 45 additions and 28 deletions

View file

@ -42,7 +42,7 @@ public class EntityTypeCommand implements TabExecutor {
} }
if (args.length < 3) { if (args.length < 3) {
EntityManager em = Manager.getInstance(EntityManager.class); EntityManager em = Manager.getInstance(EntityManager.class);
if (!em.getTypes().stream().map(SREntityType::getID).toList().contains(args[1])) { if (!em.getTypes().stream().map(SREntityType::getId).toList().contains(args[1])) {
sender.sendMessage("Invalid entity type."); sender.sendMessage("Invalid entity type.");
return true; return true;
} }
@ -181,7 +181,7 @@ public class EntityTypeCommand implements TabExecutor {
} }
if (args.length == 2) { if (args.length == 2) {
return Manager.getInstance(EntityManager.class).getTypes().stream() return Manager.getInstance(EntityManager.class).getTypes().stream()
.map(SREntityType::getID) .map(SREntityType::getId)
.toList(); .toList();
} }
if (args.length == 3 && (args[0].equalsIgnoreCase("edit"))) { if (args.length == 3 && (args[0].equalsIgnoreCase("edit"))) {

View file

@ -34,11 +34,10 @@ public class ClientBuildManager extends Manager {
private final List<ClientBuild> builds = new ArrayList<>(); private final List<ClientBuild> builds = new ArrayList<>();
/** a map of players that have ClientBuild displayed */ /** a map of players that have ClientBuild displayed */
private final HashMap<Player, List<String>> playerBlocks = new HashMap<>(); private final Map<Player, List<String>> playerBlocks = new HashMap<>();
private final HashMap<UUID, HashMap<Location, BlockData>> temporaryBlocks = new HashMap<>(); private final Map<UUID, Map<Location, BlockData>> temporaryBlocks = new HashMap<>();
private final HashMap<UUID, HashMap<Location, BlockData>> temporaryPreviousBlocks = private final Map<UUID, Map<Location, BlockData>> temporaryPreviousBlocks = new HashMap<>();
new HashMap<>();
/** Load the manager */ /** Load the manager */
@Override @Override
@ -212,7 +211,7 @@ public class ClientBuildManager extends Manager {
return temporaryBlocks.get(p.getUniqueId()); return temporaryBlocks.get(p.getUniqueId());
} }
public void setTemporaryBlocks(@NotNull Player p, HashMap<Location, BlockData> blocks) { public void setTemporaryBlocks(@NotNull Player p, Map<Location, BlockData> blocks) {
temporaryBlocks.put(p.getUniqueId(), blocks); temporaryBlocks.put(p.getUniqueId(), blocks);
} }
@ -228,7 +227,7 @@ public class ClientBuildManager extends Manager {
return temporaryPreviousBlocks.get(p.getUniqueId()); return temporaryPreviousBlocks.get(p.getUniqueId());
} }
public void setTemporaryPreviousBlocks(@NotNull Player p, HashMap<Location, BlockData> blocks) { public void setTemporaryPreviousBlocks(@NotNull Player p, Map<Location, BlockData> blocks) {
temporaryPreviousBlocks.put(p.getUniqueId(), blocks); temporaryPreviousBlocks.put(p.getUniqueId(), blocks);
} }

View file

@ -86,7 +86,7 @@ public class EntityManager extends Manager {
.registerTypeAdapter(ItemStack.class, new ItemStackSerializer()) .registerTypeAdapter(ItemStack.class, new ItemStackSerializer())
.excludeFieldsWithoutExposeAnnotation() .excludeFieldsWithoutExposeAnnotation()
.create(); .create();
dh.set("sr.entities." + type.getID(), gson.toJson(type)); dh.set("sr.entities." + type.getId(), gson.toJson(type));
} }
@Nullable @Nullable
@ -114,9 +114,9 @@ public class EntityManager extends Manager {
return types; return types;
} }
public SREntityType getType(@NotNull String ID) { public SREntityType getType(@NotNull String id) {
for (SREntityType type : types) { for (SREntityType type : types) {
if (type.getID().equals(ID)) return type; if (type.getId().equals(id)) return type;
} }
return null; return null;
} }

View file

@ -2,8 +2,10 @@ package me.unurled.sacredrealms.sr.components.entity;
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 me.unurled.sacredrealms.sr.components.attributes.Attribute; import me.unurled.sacredrealms.sr.components.attributes.Attribute;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -11,23 +13,23 @@ import org.bukkit.potion.PotionEffect;
public class SREntityType { public class SREntityType {
@Expose private final String ID; @Expose private final String id;
@Expose private String name; @Expose private String name;
@Expose private Integer level = 1; @Expose private Integer level = 1;
@Expose private Long experience = 0L; @Expose private Long experience = 0L;
@Expose private HashMap<Attribute, Double> attributes = new HashMap<>(); @Expose private Map<Attribute, Double> attributes = new EnumMap<>(Attribute.class);
@Expose private List<ItemStack> armor = new ArrayList<>(); @Expose private List<ItemStack> armor = new ArrayList<>();
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 HashMap<ItemStack, Double> lootTable = new HashMap<>(); @Expose private Map<ItemStack, Double> lootTable = new HashMap<>();
@Expose private EntityType type; @Expose private EntityType type;
@Expose private ItemStack item; @Expose private ItemStack item;
@Expose private ItemStack handItem; @Expose private ItemStack handItem;
@Expose private ItemStack secondHandItem; @Expose private ItemStack secondHandItem;
public SREntityType(String name, String ID) { public SREntityType(String name, String id) {
this.name = name; this.name = name;
this.ID = ID; this.id = id;
} }
public String getName() { public String getName() {
@ -38,14 +40,15 @@ public class SREntityType {
this.name = name; this.name = name;
} }
public String getID() { public String getId() {
return ID; return id;
} }
public int getLevel() { public int getLevel() {
return level; return level;
} }
@SuppressWarnings("unused")
public void setLevel(int level) { public void setLevel(int level) {
this.level = level; this.level = level;
} }
@ -54,42 +57,50 @@ public class SREntityType {
return experience; return experience;
} }
@SuppressWarnings("unused")
public void setExperience(Long experience) { public void setExperience(Long experience) {
this.experience = experience; this.experience = experience;
} }
public HashMap<Attribute, Double> getAttributes() { public Map<Attribute, Double> getAttributes() {
return attributes; return attributes;
} }
public void setAttributes(HashMap<Attribute, Double> attributes) { public void setAttributes(Map<Attribute, Double> attributes) {
this.attributes = attributes; this.attributes = attributes;
} }
public HashMap<Attribute, HashMap<ItemStack, Double>> getItemAttributes() { @SuppressWarnings("unused")
public Map<Attribute, Map<ItemStack, Double>> getItemAttributes() {
return itemAttributes; return itemAttributes;
} }
public void setItemAttributes(HashMap<Attribute, HashMap<ItemStack, Double>> itemAttributes) { @SuppressWarnings("unused")
public void setItemAttributes(Map<Attribute, Map<ItemStack, Double>> itemAttributes) {
this.itemAttributes = itemAttributes; this.itemAttributes = itemAttributes;
} }
@SuppressWarnings("unused")
public List<PotionEffect> getPotionEffects() { public List<PotionEffect> getPotionEffects() {
return potionEffects; return potionEffects;
} }
@SuppressWarnings("unused")
public void setPotionEffects(List<PotionEffect> potionEffects) { public void setPotionEffects(List<PotionEffect> potionEffects) {
this.potionEffects = potionEffects; this.potionEffects = potionEffects;
} }
public HashMap<ItemStack, Double> getLootTable() { @SuppressWarnings("unused")
public Map<ItemStack, Double> getLootTable() {
return lootTable; return lootTable;
} }
public void setLootTable(HashMap<ItemStack, Double> lootTable) { @SuppressWarnings("unused")
public void setLootTable(Map<ItemStack, Double> lootTable) {
this.lootTable = lootTable; this.lootTable = lootTable;
} }
@SuppressWarnings("unused")
public void setItemAttributes(Attribute attribute, ItemStack item, Double value) { public void setItemAttributes(Attribute attribute, ItemStack item, Double value) {
if (itemAttributes.containsKey(attribute)) { if (itemAttributes.containsKey(attribute)) {
itemAttributes.get(attribute).put(item, value); itemAttributes.get(attribute).put(item, value);
@ -100,12 +111,14 @@ public class SREntityType {
} }
} }
@SuppressWarnings("unused")
public void removeItemAttributes(Attribute attribute, ItemStack item) { public void removeItemAttributes(Attribute attribute, ItemStack item) {
if (itemAttributes.containsKey(attribute)) { if (itemAttributes.containsKey(attribute)) {
itemAttributes.get(attribute).remove(item); itemAttributes.get(attribute).remove(item);
} }
} }
@SuppressWarnings("unused")
public Double getItemAttributes(Attribute attribute, ItemStack item) { public Double getItemAttributes(Attribute attribute, ItemStack item) {
if (itemAttributes.containsKey(attribute)) { if (itemAttributes.containsKey(attribute)) {
return itemAttributes.get(attribute).get(item); return itemAttributes.get(attribute).get(item);
@ -113,10 +126,12 @@ public class SREntityType {
return null; return null;
} }
public HashMap<ItemStack, Double> getItemAttributes(Attribute attribute) { @SuppressWarnings("unused")
public Map<ItemStack, Double> getItemAttributes(Attribute attribute) {
return itemAttributes.getOrDefault(attribute, new HashMap<>()); return itemAttributes.getOrDefault(attribute, new HashMap<>());
} }
@SuppressWarnings("unused")
public Double getTotalItemAttribute(Attribute attribute) { public Double getTotalItemAttribute(Attribute attribute) {
if (itemAttributes == null) itemAttributes = new HashMap<>(); if (itemAttributes == null) itemAttributes = new HashMap<>();
return itemAttributes.getOrDefault(attribute, new HashMap<>()).values().stream() return itemAttributes.getOrDefault(attribute, new HashMap<>()).values().stream()
@ -143,6 +158,7 @@ public class SREntityType {
return armor; return armor;
} }
@SuppressWarnings("unused")
public void setArmor(List<ItemStack> armor) { public void setArmor(List<ItemStack> armor) {
this.armor = armor; this.armor = armor;
} }
@ -151,6 +167,7 @@ public class SREntityType {
return handItem; return handItem;
} }
@SuppressWarnings("unused")
public void setHandItem(ItemStack handItem) { public void setHandItem(ItemStack handItem) {
this.handItem = handItem; this.handItem = handItem;
} }
@ -159,6 +176,7 @@ public class SREntityType {
return secondHandItem; return secondHandItem;
} }
@SuppressWarnings("unused")
public void setSecondHandItem(ItemStack secondHandItem) { public void setSecondHandItem(ItemStack secondHandItem) {
this.secondHandItem = secondHandItem; this.secondHandItem = secondHandItem;
} }

View file

@ -1,6 +1,6 @@
package me.unurled.sacredrealms.sr.gui.entitytype; package me.unurled.sacredrealms.sr.gui.entitytype;
import java.util.HashMap; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import me.unurled.sacredrealms.sr.components.attributes.Attribute; import me.unurled.sacredrealms.sr.components.attributes.Attribute;
import me.unurled.sacredrealms.sr.components.entity.SREntityType; import me.unurled.sacredrealms.sr.components.entity.SREntityType;
@ -18,7 +18,7 @@ import xyz.xenondevs.invui.window.Window;
public class EntityStatsItem extends AbstractItem { public class EntityStatsItem extends AbstractItem {
private final HashMap<Attribute, Double> attributes; private final Map<Attribute, Double> attributes;
public EntityStatsItem(SREntityType type) { public EntityStatsItem(SREntityType type) {
attributes = type.getAttributes(); attributes = type.getAttributes();