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;
}
Item item = new Item();
item.setID(args[1]);
item.setId(args[1]);
im.addItem(item);
sender.sendMessage(
comp(

View file

@ -133,7 +133,7 @@ public class SREntityType {
@SuppressWarnings("unused")
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()
.reduce(0.0, Double::sum);
}

View file

@ -45,7 +45,7 @@ import org.jetbrains.annotations.NotNull;
* */
public class Item {
@NotNull private String ID;
@NotNull private String id;
@NotNull private String name;
@ -59,14 +59,14 @@ public class Item {
@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;
public Item() {
this.ID = "";
this.id = "";
this.name = "";
this.material = Material.AIR;
this.description = "";
@ -78,8 +78,8 @@ public class Item {
this.type = ItemType.MISC;
}
public Item(@NotNull String ID, @NotNull String name, @NotNull Material material) {
this.ID = ID;
public Item(@NotNull String id, @NotNull String name, @NotNull Material material) {
this.id = id;
this.name = name;
this.material = material;
this.description = "";
@ -91,8 +91,8 @@ public class Item {
this.type = ItemType.MISC;
}
public @NotNull String getID() {
return ID;
public @NotNull String getId() {
return id;
}
public @NotNull String getName() {
@ -103,46 +103,46 @@ public class Item {
this.name = name;
}
public Material getMaterial() {
return material;
public void setId(@NotNull String arg) {
this.id = arg;
}
public void setMaterial(@NotNull Material material) {
this.material = material;
}
public String getDescription() {
public @NotNull Material getMaterial() {
return material;
}
public @NotNull String getDescription() {
return description;
}
public void setDescription(String description) {
public void setDescription(@NotNull String description) {
this.description = description;
}
public Rarity getRarity() {
public @NotNull Rarity getRarity() {
return rarity;
}
public void setRarity(Rarity rarity) {
public void setRarity(@NotNull Rarity rarity) {
this.rarity = rarity;
}
public ItemType getType() {
public @NotNull ItemType getType() {
return type;
}
public void setType(ItemType type) {
public void setType(@NotNull ItemType type) {
this.type = type;
}
public HashMap<Attribute, Double> getAttributes() {
public @NotNull Map<Attribute, Double> getAttributes() {
return attributes;
}
public void setAttributes(HashMap<Attribute, Double> attributes) {
this.attributes = attributes;
}
public Double getAttribute(Attribute attribute) {
return attributes.get(attribute);
}
@ -155,43 +155,54 @@ public class Item {
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;
}
public void setEnchantments(HashMap<Enchantment, Integer> enchantments) {
@SuppressWarnings("unused")
public void setEnchantments(@NotNull Map<Enchantment, Integer> enchantments) {
this.enchantments = enchantments;
}
@SuppressWarnings("unused")
public void addEnchantment(Enchantment enchantment, int level) {
enchantments.put(enchantment, level);
}
@SuppressWarnings("unused")
public void removeEnchantment(Enchantment enchantment) {
enchantments.remove(enchantment);
}
public List<Ability> getAbilities() {
public @NotNull List<Ability> getAbilities() {
return abilities;
}
public void setAbilities(List<Ability> abilities) {
@SuppressWarnings("unused")
public void setAbilities(@NotNull List<Ability> abilities) {
this.abilities = abilities;
}
@SuppressWarnings("unused")
public void addAbility(Ability ability) {
abilities.add(ability);
}
@SuppressWarnings("unused")
public void removeAbility(Ability ability) {
abilities.remove(ability);
}
public Integer getCustomModelData() {
@SuppressWarnings("unused")
public @NotNull Integer getCustomModelData() {
return customModelData;
}
public void setCustomModelData(Integer customModelData) {
public void setCustomModelData(@NotNull Integer customModelData) {
this.customModelData = customModelData;
}
@ -203,7 +214,7 @@ public class Item {
}
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()));
@ -221,7 +232,7 @@ public class Item {
public String toString() {
return "{"
+ "\"id\": \""
+ ID
+ id
+ "\", \"name\": \""
+ name
+ "\", \"material\": \""
@ -251,7 +262,7 @@ public class Item {
Map<String, Object> map =
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.material = Material.valueOf((String) map.get("material"));
this.description = (String) map.get("description");
@ -271,8 +282,4 @@ public class Item {
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<>();
}
/** Load the manager */
@Override
public void load() {
super.load();
}
/** Save the data */
@Override
public void saveData() {
@ -56,12 +50,12 @@ public class ItemManager extends Manager {
.forEach(
key -> {
Item item = gson.fromJson(dh.get(key), Item.class);
items.put(item.getID(), item);
items.put(item.getId(), item);
});
}
public void addItem(Item item) {
items.put(item.getID(), item);
items.put(item.getId(), item);
}
public Item getItem(String id) {

View file

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

View file

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

View file

@ -41,7 +41,7 @@ public class ClientBuildDeserializer implements JsonDeserializer<ClientBuild> {
World world = Bukkit.getWorld(spl[0]);
if (world == null) {
error("World " + spl[0] + " does not exist");
continue;
return build;
}
Location loc;
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 net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
@ -21,7 +19,6 @@ public class PlayerLevelUpEvent extends Event {
super(true);
this.p = p;
this.previousLevel = previousLevel;
OfflinePlayer pa = Bukkit.getOfflinePlayer(p.getUuid());
/*
* <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>
*/
public static @NotNull TextDisplay spawnIndicator(
@NotNull Entity eventEntity, @NotNull Boolean isHeal, double number) {
@NotNull Entity eventEntity, boolean isHeal, double number) {
// vector operation
Vector location = eventEntity.getLocation().toVector();
Vector facingVector = eventEntity.getFacing().getDirection();