Fixed all issues with F chest, and a massive increase in performance due to /f chest only serializing / deserializing on startup/shutdown instead of realtime.

This commit is contained in:
ProSavage 2018-09-09 12:40:45 -05:00
parent 9795d3c158
commit 4ad062dddb
7 changed files with 74 additions and 50 deletions

View File

@ -95,9 +95,9 @@ public interface Faction extends EconomyParticipator {
void setVault(Location vaultLocation); void setVault(Location vaultLocation);
Inventory getChest(); Inventory getChestInventory();
void setChest(Inventory inventory); void setChestSize(int chestSize);
void setBannerPattern(ItemStack banner); void setBannerPattern(ItemStack banner);

View File

@ -31,6 +31,7 @@ import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
@ -218,6 +219,7 @@ public class P extends MPlugin {
setupPlaceholderAPI(); setupPlaceholderAPI();
postEnable(); postEnable();
this.loadSuccessful = true; this.loadSuccessful = true;
@ -391,6 +393,7 @@ public class P extends MPlugin {
.registerTypeAdapter(accessTypeAdatper, new PermissionsMapTypeAdapter()) .registerTypeAdapter(accessTypeAdatper, new PermissionsMapTypeAdapter())
.registerTypeAdapter(LazyLocation.class, new MyLocationTypeAdapter()) .registerTypeAdapter(LazyLocation.class, new MyLocationTypeAdapter())
.registerTypeAdapter(mapFLocToStringSetType, new MapFLocToStringSetTypeAdapter()) .registerTypeAdapter(mapFLocToStringSetType, new MapFLocToStringSetTypeAdapter())
.registerTypeAdapter(Inventory.class, new InventoryTypeAdapter())
.registerTypeAdapterFactory(EnumTypeAdapter.ENUM_FACTORY); .registerTypeAdapterFactory(EnumTypeAdapter.ENUM_FACTORY);
} }

View File

@ -27,6 +27,7 @@ public class CmdChest extends FCommand {
@Override @Override
public void perform() { public void perform() {
if (!P.p.getConfig().getBoolean("fchest.Enabled")) { if (!P.p.getConfig().getBoolean("fchest.Enabled")) {
fme.sendMessage("This command is disabled!"); fme.sendMessage("This command is disabled!");
return; return;
@ -40,7 +41,7 @@ public class CmdChest extends FCommand {
} }
} }
me.openInventory(fme.getFaction().getChest()); me.openInventory(fme.getFaction().getChestInventory());
} }

View File

@ -611,27 +611,7 @@ public class FactionsPlayerListener implements Listener {
return (result.length() == 3 ? result + "0" : result) + "/hrs ago"; return (result.length() == 3 ? result + "0" : result) + "/hrs ago";
} }
@EventHandler
public void onCloseChest(InventoryCloseEvent e) {
if (e.getInventory().getTitle() == null) {
return;
}
if (e.getInventory().getTitle().equalsIgnoreCase(P.p.color(P.p.getConfig().getString("fchest.Inventory-Title")))) {
FPlayers.getInstance().getByPlayer((Player) e.getPlayer()).getFaction().setChest(e.getInventory());
}
}
@EventHandler
public void onCloseChest(InventoryClickEvent e) {
if (e.getInventory().getTitle() == null || e.getClickedInventory() == null) {
return;
}
if (e.getInventory().getTitle().equalsIgnoreCase(P.p.color(P.p.getConfig().getString("fchest.Inventory-Title")))) {
FPlayers.getInstance().getByPlayer((Player) e.getWhoClicked()).getFaction().setChest(e.getInventory());
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerMove(PlayerMoveEvent event) { public void onPlayerMove(PlayerMoveEvent event) {

View File

@ -0,0 +1,29 @@
package com.massivecraft.factions.util;
import com.google.gson.*;
import org.bukkit.inventory.Inventory;
import java.lang.reflect.Type;
public class InventoryTypeAdapter implements JsonSerializer<Inventory>, JsonDeserializer<Inventory> {
@Override
public JsonElement serialize(Inventory inventory, Type type, JsonSerializationContext jsonSerializationContext) {
JsonObject object = new JsonObject();
object.add("contents", new JsonPrimitive(InventoryUtil.toBase64(inventory)));
return object;
}
@Override
public Inventory deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) {
JsonObject object = jsonElement.getAsJsonObject();
return InventoryUtil.fromBase64(object.get("contents").getAsString());
}
}

View File

@ -4,8 +4,6 @@ import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.FPlayers; import com.massivecraft.factions.FPlayers;
import com.massivecraft.factions.Faction; import com.massivecraft.factions.Faction;
import com.massivecraft.factions.P; import com.massivecraft.factions.P;
import com.massivecraft.factions.zcore.util.TL;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
@ -187,7 +185,7 @@ public class FUpgradesGUI implements Listener {
} }
takeMoney(fme, cost); takeMoney(fme, cost);
fme.getFaction().setUpgrades("Chest", 3); fme.getFaction().setUpgrades("Chest", 3);
closeChests(fme.getFaction()); updateChests(fme.getFaction());
fme.getPlayer().closeInventory(); fme.getPlayer().closeInventory();
} }
if (chestLevel == 1) { if (chestLevel == 1) {
@ -197,7 +195,7 @@ public class FUpgradesGUI implements Listener {
} }
takeMoney(fme, cost); takeMoney(fme, cost);
fme.getFaction().setUpgrades("Chest", 2); fme.getFaction().setUpgrades("Chest", 2);
closeChests(fme.getFaction()); updateChests(fme.getFaction());
fme.getPlayer().closeInventory(); fme.getPlayer().closeInventory();
} }
if (chestLevel == 0) { if (chestLevel == 0) {
@ -207,7 +205,7 @@ public class FUpgradesGUI implements Listener {
} }
takeMoney(fme, cost); takeMoney(fme, cost);
fme.getFaction().setUpgrades("Chest", 1); fme.getFaction().setUpgrades("Chest", 1);
closeChests(fme.getFaction()); updateChests(fme.getFaction());
fme.getPlayer().closeInventory(); fme.getPlayer().closeInventory();
} }
} }
@ -216,7 +214,7 @@ public class FUpgradesGUI implements Listener {
} }
private void closeChests(Faction faction) { private void updateChests(Faction faction) {
for (Player player : faction.getOnlinePlayers()) { for (Player player : faction.getOnlinePlayers()) {
if (player.getInventory().getTitle() == null) { if (player.getInventory().getTitle() == null) {
return; return;
@ -226,6 +224,19 @@ public class FUpgradesGUI implements Listener {
player.closeInventory(); player.closeInventory();
} }
} }
int level = faction.getUpgrade("Chest");
int size = 9;
if (level == 1) {
size = P.p.getConfig().getInt("fupgrades.MainMenu.Chest.Chest-Size.level-1") * 9;
} else if (level == 2) {
size = P.p.getConfig().getInt("fupgrades.MainMenu.Chest.Chest-Size.level-2") * 9;
} else if (level == 3) {
size = P.p.getConfig().getInt("fupgrades.MainMenu.Chest.Chest-Size.level-3") * 9;
}
faction.setChestSize(size);
} }
private ItemStack[] buildItems(FPlayer fme) { private ItemStack[] buildItems(FPlayer fme) {

View File

@ -62,7 +62,7 @@ public abstract class MemoryFaction implements Faction, EconomyParticipator {
protected Role defaultRole; protected Role defaultRole;
protected Map<Permissable, Map<PermissableAction, Access>> permissions = new HashMap<>(); protected Map<Permissable, Map<PermissableAction, Access>> permissions = new HashMap<>();
protected Set<BanInfo> bans = new HashSet<>(); protected Set<BanInfo> bans = new HashSet<>();
String chestSerialized = null; Inventory chest;
Map<String, Object> bannerSerialized; Map<String, Object> bannerSerialized;
private long lastDeath; private long lastDeath;
@ -340,35 +340,35 @@ public abstract class MemoryFaction implements Faction, EconomyParticipator {
} }
@Override @Override
public Inventory getChest() { public Inventory getChestInventory() {
int level = getUpgrade("Chest"); if (chest != null) {
int size = 9; return chest;
if (level == 1) {
size = P.p.getConfig().getInt("fupgrades.MainMenu.Chest.Chest-Size.level-1") * 9;
} else if (level == 2) {
size = P.p.getConfig().getInt("fupgrades.MainMenu.Chest.Chest-Size.level-2") * 9;
} else if (level == 3) {
size = P.p.getConfig().getInt("fupgrades.MainMenu.Chest.Chest-Size.level-3") * 9;
}
Inventory inventory = Bukkit.createInventory(null, size, P.p.color(P.p.getConfig().getString("fchest.Inventory-Title")));
if (chestSerialized == null) {
return inventory;
} else { } else {
//long startTime = System.nanoTime(); int level = getUpgrade("Chest");
ItemStack[] contents = new ItemStack[0]; int size = 9;
if (level == 1) {
size = P.p.getConfig().getInt("fupgrades.MainMenu.Chest.Chest-Size.level-1") * 9;
} else if (level == 2) {
size = P.p.getConfig().getInt("fupgrades.MainMenu.Chest.Chest-Size.level-2") * 9;
} else if (level == 3) {
size = P.p.getConfig().getInt("fupgrades.MainMenu.Chest.Chest-Size.level-3") * 9;
}
contents = InventoryUtil.fromBase64(chestSerialized).getContents(); chest = Bukkit.createInventory(null, size);
return chest;
inventory.setContents(contents);
return inventory;
} }
} }
@Override @Override
public void setChest(Inventory inventory) { public void setChestSize(int chestSize) {
chestSerialized = InventoryUtil.toBase64(inventory); ItemStack[] contents = this.getChestInventory().getContents();
chest = Bukkit.createInventory(null, chestSize);
chest.setContents(contents);
} }
@Override @Override
public void setBannerPattern(ItemStack banner) { public void setBannerPattern(ItemStack banner) {
bannerSerialized = banner.serialize(); bannerSerialized = banner.serialize();