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, () -> {
if (storage.containsGroup("entities")) {
for (StorageRow row : storage.getRowsByGroup("entities")) {
Entity entity = getEntityByUniqueId(UUID.fromString(row.getKey()));
if (entity == null) continue;
try {
EntityStack stack = new EntityStack(
entity,
UUID.fromString(row.getKey()),
row.get("amount").asInt());
this.entityStackManager.addStack(stack);
} catch (Exception e) {
console.sendMessage("Failed to load entity.");
e.printStackTrace();
}
}
}
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.
this.saveToFile();
if (hologram != null)

View File

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

View File

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

View File

@ -19,7 +19,7 @@ public abstract class Storage {
public Storage(UltimateStacker instance) {
this.instance = instance;
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.saveConfig();
}
@ -32,7 +32,7 @@ public abstract class Storage {
public void updateData(UltimateStacker instance) {
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()));
}