entity type gui + command
All checks were successful
Build / build (push) Successful in 1m41s

This commit is contained in:
unurled 2024-03-14 21:28:49 +01:00
parent f387c63183
commit 627e533e9c
Signed by: unurled
GPG key ID: FDBC9CBE1F82423F
18 changed files with 1015 additions and 0 deletions

View file

@ -1,13 +1,26 @@
package me.unurled.sacredrealms.sr.components.entity;
import static me.unurled.sacredrealms.sr.utils.Logger.error;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import me.unurled.sacredrealms.sr.data.DataHandler;
import me.unurled.sacredrealms.sr.data.DataManager;
import me.unurled.sacredrealms.sr.data.gson.ItemStackDeserializer;
import me.unurled.sacredrealms.sr.data.gson.ItemStackSerializer;
import me.unurled.sacredrealms.sr.data.gson.PotionEffectDeserializer;
import me.unurled.sacredrealms.sr.managers.Manager;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Mob;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.entity.EntitySpawnEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -15,11 +28,67 @@ public class EntityManager extends Manager {
@NotNull private final HashMap<UUID, SREntity> entities;
private final List<SREntityType> types;
public EntityManager() {
super();
this.types = new ArrayList<>();
entities = new HashMap<>();
}
/** Load the data */
@Override
public void loadData() {
// loads all types
DataManager dm = DataManager.getInstance(DataManager.class);
if (dm == null) {
error("DataManager is null, Can't load entity data.");
return;
}
DataHandler dh = dm.getDataHandler();
if (dh == null) {
error("DataHandler is null, Can't load entity data.");
return;
}
Gson gson =
new GsonBuilder()
.registerTypeAdapter(PotionEffect.class, new PotionEffectDeserializer())
.registerTypeAdapter(ItemStack.class, new ItemStackDeserializer())
.excludeFieldsWithoutExposeAnnotation()
.create();
dh.getKeysAll("sr.entities")
.forEach(
key -> {
SREntityType type = gson.fromJson(dh.get(key), SREntityType.class);
if (type != null) {
types.add(type);
}
});
}
public void saveType(@NotNull SREntityType type) {
// save type
DataManager dm = DataManager.getInstance(DataManager.class);
if (dm == null) {
error("DataManager is null, Can't save entity data.");
return;
}
DataHandler dh = dm.getDataHandler();
if (dh == null) {
error("DataHandler is null, Can't save entity data.");
return;
}
Gson gson =
new GsonBuilder()
.registerTypeAdapter(PotionEffect.class, new PotionEffectDeserializer())
.registerTypeAdapter(ItemStack.class, new ItemStackSerializer())
.excludeFieldsWithoutExposeAnnotation()
.create();
dh.set("sr.entities." + type.getID(), gson.toJson(type));
}
@Nullable
public SREntity getEntity(@NotNull UUID uuid) {
return entities.get(uuid);
@ -37,6 +106,21 @@ public class EntityManager extends Manager {
return entities.containsKey(uuid);
}
public void addEntityType(@NotNull SREntityType type) {
types.add(type);
}
public List<SREntityType> getTypes() {
return types;
}
public SREntityType getType(@NotNull String ID) {
for (SREntityType type : types) {
if (type.getID().equals(ID)) return type;
}
return null;
}
@EventHandler
public void onEntitySpawn(@NotNull EntitySpawnEvent e) {
if (e.getEntity() instanceof Player) return;

View file

@ -0,0 +1,165 @@
package me.unurled.sacredrealms.sr.components.entity;
import com.google.gson.annotations.Expose;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import me.unurled.sacredrealms.sr.components.attributes.Attribute;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
public class SREntityType {
@Expose private final String ID;
@Expose private String name;
@Expose private Integer level = 1;
@Expose private Long experience = 0L;
@Expose private HashMap<Attribute, Double> attributes = new HashMap<>();
@Expose private List<ItemStack> armor = new ArrayList<>();
private HashMap<Attribute, HashMap<ItemStack, Double>> itemAttributes = new HashMap<>();
@Expose private List<PotionEffect> potionEffects = new ArrayList<>();
@Expose private HashMap<ItemStack, Double> lootTable = new HashMap<>();
@Expose private EntityType type;
@Expose private ItemStack item;
@Expose private ItemStack handItem;
@Expose private ItemStack secondHandItem;
public SREntityType(String name, String ID) {
this.name = name;
this.ID = ID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getID() {
return ID;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public Long getExperience() {
return experience;
}
public void setExperience(Long experience) {
this.experience = experience;
}
public HashMap<Attribute, Double> getAttributes() {
return attributes;
}
public void setAttributes(HashMap<Attribute, Double> attributes) {
this.attributes = attributes;
}
public HashMap<Attribute, HashMap<ItemStack, Double>> getItemAttributes() {
return itemAttributes;
}
public void setItemAttributes(HashMap<Attribute, HashMap<ItemStack, Double>> itemAttributes) {
this.itemAttributes = itemAttributes;
}
public List<PotionEffect> getPotionEffects() {
return potionEffects;
}
public void setPotionEffects(List<PotionEffect> potionEffects) {
this.potionEffects = potionEffects;
}
public HashMap<ItemStack, Double> getLootTable() {
return lootTable;
}
public void setLootTable(HashMap<ItemStack, Double> lootTable) {
this.lootTable = lootTable;
}
public void setItemAttributes(Attribute attribute, ItemStack item, Double value) {
if (itemAttributes.containsKey(attribute)) {
itemAttributes.get(attribute).put(item, value);
} else {
HashMap<ItemStack, Double> map = new HashMap<>();
map.put(item, value);
itemAttributes.put(attribute, map);
}
}
public void removeItemAttributes(Attribute attribute, ItemStack item) {
if (itemAttributes.containsKey(attribute)) {
itemAttributes.get(attribute).remove(item);
}
}
public Double getItemAttributes(Attribute attribute, ItemStack item) {
if (itemAttributes.containsKey(attribute)) {
return itemAttributes.get(attribute).get(item);
}
return null;
}
public HashMap<ItemStack, Double> getItemAttributes(Attribute attribute) {
return itemAttributes.getOrDefault(attribute, new HashMap<>());
}
public Double getTotalItemAttribute(Attribute attribute) {
if (itemAttributes == null) itemAttributes = new HashMap<>();
return itemAttributes.getOrDefault(attribute, new HashMap<>()).values().stream()
.reduce(0.0, Double::sum);
}
public EntityType getType() {
return type;
}
public void setType(EntityType type) {
this.type = type;
}
public ItemStack getItem() {
return item;
}
public void setItem(ItemStack item) {
this.item = item;
}
public List<ItemStack> getArmor() {
return armor;
}
public void setArmor(List<ItemStack> armor) {
this.armor = armor;
}
public ItemStack getHandItem() {
return handItem;
}
public void setHandItem(ItemStack handItem) {
this.handItem = handItem;
}
public ItemStack getSecondHandItem() {
return secondHandItem;
}
public void setSecondHandItem(ItemStack secondHandItem) {
this.secondHandItem = secondHandItem;
}
}