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

@ -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);
}