Fixed stacks unloading on startup.

This commit is contained in:
Brianna O'Keefe 2019-02-28 10:53:12 -05:00
parent 2c09b3a033
commit 3924d51c5a
4 changed files with 41 additions and 19 deletions

View File

@ -155,16 +155,16 @@ public class UltimateStacker extends JavaPlugin {
Bukkit.getScheduler().runTaskLater(this, () -> { Bukkit.getScheduler().runTaskLater(this, () -> {
if (storage.containsGroup("entities")) { if (storage.containsGroup("entities")) {
for (StorageRow row : storage.getRowsByGroup("entities")) { for (StorageRow row : storage.getRowsByGroup("entities")) {
try {
Entity entity = getEntityByUniqueId(UUID.fromString(row.getKey()));
if (entity == null) continue;
EntityStack stack = new EntityStack( EntityStack stack = new EntityStack(
entity, UUID.fromString(row.getKey()),
row.get("amount").asInt()); row.get("amount").asInt());
this.entityStackManager.addStack(stack); this.entityStackManager.addStack(stack);
} catch (Exception e) {
console.sendMessage("Failed to load entity.");
e.printStackTrace();
}
} }
} }
if (storage.containsGroup("spawners")) { if (storage.containsGroup("spawners")) {
@ -184,11 +184,6 @@ public class UltimateStacker extends JavaPlugin {
} }
} }
for (SpawnerStack stack : spawnerStackManager.getStacks()) {
storage.prepareSaveItem("spawners", new StorageItem("location", Methods.serializeLocation(stack.getLocation())),
new StorageItem("amount", stack.getAmount()));
}
// Save data initially so that if the person reloads again fast they don't lose all their data. // Save data initially so that if the person reloads again fast they don't lose all their data.
this.saveToFile(); this.saveToFile();
if (hologram != null) if (hologram != null)

View File

@ -2,29 +2,45 @@ package com.songoda.ultimatestacker.entity;
import com.songoda.ultimatestacker.UltimateStacker; import com.songoda.ultimatestacker.UltimateStacker;
import com.songoda.ultimatestacker.utils.Methods; import com.songoda.ultimatestacker.utils.Methods;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import java.util.UUID;
public class EntityStack { public class EntityStack {
private Entity entity; private UUID entity;
private int amount; private int amount;
public EntityStack(Entity entity, int amount) { public EntityStack(Entity entity, int amount) {
this.entity = entity; this.entity = entity.getUniqueId();
this.setAmount(amount);
}
public EntityStack(UUID uuid, int amount) {
this.entity = uuid;
this.setAmount(amount); this.setAmount(amount);
} }
public void updateStack() { public void updateStack() {
Entity entity = Bukkit.getEntity(this.entity);
if (entity == null) return;
entity.setCustomNameVisible(true); entity.setCustomNameVisible(true);
entity.setCustomName(Methods.compileEntityName(entity, amount)); entity.setCustomName(Methods.compileEntityName(entity, amount));
} }
public Entity getEntity() { public Entity getEntity() {
Entity entity = Bukkit.getEntity(this.entity);
if (entity == null) {
UltimateStacker.getInstance().getEntityStackManager().removeStack(this.entity);
return null;
}
return entity; return entity;
} }
protected void setEntity(Entity entity) { protected void setEntity(Entity entity) {
this.entity = entity; this.entity = entity.getUniqueId();
} }
public void addAmount(int amount) { public void addAmount(int amount) {
@ -32,13 +48,20 @@ public class EntityStack {
updateStack(); updateStack();
} }
public UUID getEntityUniqueId() {
return entity;
}
public int getAmount() { public int getAmount() {
return amount; return amount;
} }
public void setAmount(int amount) { public void setAmount(int amount) {
if (amount == 1) { if (amount == 1) {
UltimateStacker.getInstance().getEntityStackManager().removeStack(entity); Entity entity = Bukkit.getEntity(this.entity);
if (entity == null) return;
UltimateStacker.getInstance().getEntityStackManager().removeStack(this.entity);
entity.setCustomName(null); entity.setCustomName(null);
entity.setCustomNameVisible(false); entity.setCustomNameVisible(false);
return; return;
@ -51,7 +74,7 @@ public class EntityStack {
@Override @Override
public String toString() { public String toString() {
return "EntityStack:{" return "EntityStack:{"
+ "Entity:\"" + entity.getUniqueId().toString() + "\"," + "Entity:\"" + entity.toString() + "\","
+ "Amount:\"" + amount + "\"," + "Amount:\"" + amount + "\","
+ "}"; + "}";
} }

View File

@ -13,7 +13,7 @@ public class EntityStackManager {
private static final Map<UUID, EntityStack> stacks = new HashMap<>(); private static final Map<UUID, EntityStack> stacks = new HashMap<>();
public EntityStack addStack(EntityStack stack) { public EntityStack addStack(EntityStack stack) {
stacks.put(stack.getEntity().getUniqueId(), stack); stacks.put(stack.getEntityUniqueId(), stack);
return stack; return stack;
} }
@ -29,6 +29,10 @@ public class EntityStackManager {
return stacks.containsKey(entity.getUniqueId()); return stacks.containsKey(entity.getUniqueId());
} }
public EntityStack removeStack(UUID entity) {
return stacks.remove(entity);
}
public EntityStack removeStack(Entity entity) { public EntityStack removeStack(Entity entity) {
return stacks.remove(entity.getUniqueId()); return stacks.remove(entity.getUniqueId());
} }

View File

@ -19,7 +19,7 @@ public abstract class Storage {
public Storage(UltimateStacker instance) { public Storage(UltimateStacker instance) {
this.instance = instance; this.instance = instance;
this.dataFile = new ConfigWrapper(instance, "", "data.yml"); this.dataFile = new ConfigWrapper(instance, "", "data.yml");
this.dataFile.createNewFile(null, "EpicHoppers Data File"); this.dataFile.createNewFile(null, "UltimateStacker Data File");
this.dataFile.getConfig().options().copyDefaults(true); this.dataFile.getConfig().options().copyDefaults(true);
this.dataFile.saveConfig(); this.dataFile.saveConfig();
} }
@ -32,7 +32,7 @@ public abstract class Storage {
public void updateData(UltimateStacker instance) { public void updateData(UltimateStacker instance) {
for (EntityStack stack : instance.getEntityStackManager().getStacks().values()) { for (EntityStack stack : instance.getEntityStackManager().getStacks().values()) {
prepareSaveItem("entities", new StorageItem("uuid", stack.getEntity().getUniqueId().toString()), prepareSaveItem("entities", new StorageItem("uuid", stack.getEntityUniqueId().toString()),
new StorageItem("amount", stack.getAmount())); new StorageItem("amount", stack.getAmount()));
} }