Finished up some issues with inventory saving, using string builder for serialzation to improve performance.

This commit is contained in:
ProSavage 2018-05-12 11:29:38 -05:00
parent e948f56fd2
commit cb947404f6
4 changed files with 139 additions and 73 deletions

View File

@ -337,6 +337,17 @@ public class FactionsPlayerListener implements Listener {
}
}
@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());
}
}
// Holds the next time a player can have a map shown.
private HashMap<UUID, Long> showTimes = new HashMap<>();

View File

@ -2,6 +2,7 @@ package com.massivecraft.factions.zcore.fupgrades;
import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.FPlayers;
import com.massivecraft.factions.Faction;
import com.massivecraft.factions.P;
import com.massivecraft.factions.zcore.util.TL;
import net.milkbowl.vault.economy.Economy;
@ -171,7 +172,7 @@ public class FUpgradesGUI implements Listener {
}
int chestLevel = fme.getFaction().getUpgrade("Chest");
if (e.getCurrentItem().equals(chestitem)) {
if (expLevel == 3) {
if (chestLevel == 3) {
return;
}
if (chestLevel == 2) {
@ -181,6 +182,7 @@ public class FUpgradesGUI implements Listener {
}
takeMoney(fme, cost);
fme.getFaction().setUpgrades("Chest", 3);
closeChests(fme.getFaction());
fme.getPlayer().closeInventory();
}
if (chestLevel == 1) {
@ -190,6 +192,7 @@ public class FUpgradesGUI implements Listener {
}
takeMoney(fme, cost);
fme.getFaction().setUpgrades("Chest", 2);
closeChests(fme.getFaction());
fme.getPlayer().closeInventory();
}
if (chestLevel == 0) {
@ -199,6 +202,7 @@ public class FUpgradesGUI implements Listener {
}
takeMoney(fme, cost);
fme.getFaction().setUpgrades("Chest", 1);
closeChests(fme.getFaction());
fme.getPlayer().closeInventory();
}
}
@ -208,7 +212,19 @@ public class FUpgradesGUI implements Listener {
}
public ItemStack[] buildItems(FPlayer fme) {
private void closeChests(Faction faction) {
for (Player player : faction.getOnlinePlayers()) {
if (player.getInventory().getTitle() == null) {
return;
}
String invName = P.p.color(P.p.getConfig().getString("fchest.Inventory-Title"));
if (player.getInventory().getTitle().equalsIgnoreCase(invName)) {
player.closeInventory();
}
}
}
private ItemStack[] buildItems(FPlayer fme) {
Material expMaterial = Material.getMaterial(P.p.getConfig().getString("fupgrades.MainMenu.EXP.EXPItem.Type"));
int expAmt = P.p.getConfig().getInt("fupgrades.MainMenu.EXP.EXPItem.Amount");
short expData = Short.parseShort(P.p.getConfig().getInt("fupgrades.MainMenu.EXP.EXPItem.Damage") + "");
@ -313,7 +329,7 @@ public class FUpgradesGUI implements Listener {
return items;
}
public boolean hasMoney(FPlayer fme, int amt) {
private boolean hasMoney(FPlayer fme, int amt) {
Economy econ = P.p.getEcon();
if (econ.getBalance(fme.getPlayer()) >= amt) {
return true;
@ -324,7 +340,7 @@ public class FUpgradesGUI implements Listener {
}
}
public void takeMoney(FPlayer fme, int amt) {
private void takeMoney(FPlayer fme, int amt) {
if (hasMoney(fme, amt)) {
Economy econ = P.p.getEcon();
econ.withdrawPlayer(fme.getPlayer(), amt);

View File

@ -15,8 +15,10 @@ import com.massivecraft.factions.zcore.fperms.Access;
import com.massivecraft.factions.zcore.fperms.Permissable;
import com.massivecraft.factions.zcore.fperms.PermissableAction;
import com.massivecraft.factions.zcore.util.TL;
import org.bukkit.*;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
@ -26,6 +28,9 @@ import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import static com.massivecraft.factions.zcore.util.InventoryUtil.InventoryToString;
import static com.massivecraft.factions.zcore.util.InventoryUtil.StringToInventory;
public abstract class MemoryFaction implements Faction, EconomyParticipator {
protected String id = null;
protected boolean peacefulExplosionsEnabled;
@ -255,85 +260,26 @@ public abstract class MemoryFaction implements Faction, EconomyParticipator {
if (chestSerialized == null) {
return inventory;
} else {
//long startTime = System.nanoTime();
Inventory contents = StringToInventory(chestSerialized);
inventory.setContents(contents.getContents());
// long endTime = System.nanoTime();
// long duration = (endTime - startTime);
// Bukkit.broadcastMessage("chest deserialization time: " + duration + " nano seconds");
return inventory;
}
}
@Override
public void setChest(Inventory inventory) {
// long startTime = System.nanoTime();
chestSerialized = InventoryToString(inventory);
// long endTime = System.nanoTime();
// long duration = (endTime - startTime);
// Bukkit.broadcastMessage("chest serialization time: " + duration + " nano seconds");
}
public String InventoryToString(Inventory invInventory) {
String serialization = invInventory.getSize() + ";";
for (int i = 0; i < invInventory.getSize(); i++) {
ItemStack is = invInventory.getItem(i);
if (is != null) {
String serializedItemStack = new String();
String isType = String.valueOf(is.getType().getId());
serializedItemStack += "t@" + isType;
if (is.getDurability() != 0) {
String isDurability = String.valueOf(is.getDurability());
serializedItemStack += ":d@" + isDurability;
}
if (is.getAmount() != 1) {
String isAmount = String.valueOf(is.getAmount());
serializedItemStack += ":a@" + isAmount;
}
Map<Enchantment, Integer> isEnch = is.getEnchantments();
if (isEnch.size() > 0) {
for (Entry<Enchantment, Integer> ench : isEnch.entrySet()) {
serializedItemStack += ":e@" + ench.getKey().getId() + "@" + ench.getValue();
}
}
serialization += i + "#" + serializedItemStack + ";";
}
}
return serialization;
}
public Inventory StringToInventory(String invString) {
String[] serializedBlocks = invString.split(";");
String invInfo = serializedBlocks[0];
Inventory deserializedInventory = Bukkit.getServer().createInventory(null, Integer.valueOf(invInfo));
for (int i = 1; i < serializedBlocks.length; i++) {
String[] serializedBlock = serializedBlocks[i].split("#");
int stackPosition = Integer.valueOf(serializedBlock[0]);
if (stackPosition >= deserializedInventory.getSize()) {
continue;
}
ItemStack is = null;
Boolean createdItemStack = false;
String[] serializedItemStack = serializedBlock[1].split(":");
for (String itemInfo : serializedItemStack) {
String[] itemAttribute = itemInfo.split("@");
if (itemAttribute[0].equals("t")) {
is = new ItemStack(Material.getMaterial(Integer.valueOf(itemAttribute[1])));
createdItemStack = true;
} else if (itemAttribute[0].equals("d") && createdItemStack) {
is.setDurability(Short.valueOf(itemAttribute[1]));
} else if (itemAttribute[0].equals("a") && createdItemStack) {
is.setAmount(Integer.valueOf(itemAttribute[1]));
} else if (itemAttribute[0].equals("e") && createdItemStack) {
is.addEnchantment(Enchantment.getById(Integer.valueOf(itemAttribute[1])), Integer.valueOf(itemAttribute[2]));
}
}
deserializedInventory.setItem(stackPosition, is);
}
return deserializedInventory;
}
@Override
public void setBannerPattern(ItemStack banner) {
@ -342,6 +288,9 @@ public abstract class MemoryFaction implements Faction, EconomyParticipator {
@Override
public ItemStack getBanner() {
if (bannerSerialized == null) {
return null;
}
return ItemStack.deserialize(bannerSerialized);
}

View File

@ -0,0 +1,90 @@
package com.massivecraft.factions.zcore.util;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import java.util.Map;
public class InventoryUtil {
public static String InventoryToString(Inventory invInventory) {
// String serialization = invInventory.getSize() + ";";
StringBuilder serialization = new StringBuilder(invInventory.getSize() + ";");
for (int i = 0; i < invInventory.getSize(); i++) {
ItemStack is = invInventory.getItem(i);
if (is != null) {
// String serializedItemStack = new String();
StringBuilder serializedItemStack = new StringBuilder();
String isType = String.valueOf(is.getType().getId());
// serializedItemStack += "t@" + isType;
serializedItemStack.append("t@" + isType);
if (is.getDurability() != 0) {
String isDurability = String.valueOf(is.getDurability());
//serializedItemStack += ":d@" + isDurability;
serializedItemStack.append(":d@" + isDurability);
}
if (is.getAmount() != 1) {
String isAmount = String.valueOf(is.getAmount());
//serializedItemStack += ":a@" + isAmount;
serializedItemStack.append(":a@" + isAmount);
}
Map<Enchantment, Integer> isEnch = is.getEnchantments();
if (isEnch.size() > 0) {
for (Map.Entry<Enchantment, Integer> ench : isEnch.entrySet()) {
// serializedItemStack += ":e@" + ench.getKey().getId() + "@" + ench.getValue();
serializedItemStack.append(":e@" + ench.getKey().getId() + "@" + ench.getValue());
}
}
// serialization += i + "#" + serializedItemStack + ";";
serialization.append(i + "#" + serializedItemStack + ";");
}
}
return serialization.toString();
}
public static Inventory StringToInventory(String invString) {
String[] serializedBlocks = invString.split(";");
String invInfo = serializedBlocks[0];
Inventory deserializedInventory = Bukkit.getServer().createInventory(null, Integer.valueOf(invInfo));
for (int i = 1; i < serializedBlocks.length; i++) {
String[] serializedBlock = serializedBlocks[i].split("#");
int stackPosition = Integer.valueOf(serializedBlock[0]);
if (stackPosition >= deserializedInventory.getSize()) {
continue;
}
ItemStack is = null;
Boolean createdItemStack = false;
String[] serializedItemStack = serializedBlock[1].split(":");
for (String itemInfo : serializedItemStack) {
String[] itemAttribute = itemInfo.split("@");
if (itemAttribute[0].equals("t")) {
is = new ItemStack(Material.getMaterial(Integer.valueOf(itemAttribute[1])));
createdItemStack = true;
} else if (itemAttribute[0].equals("d") && createdItemStack) {
is.setDurability(Short.valueOf(itemAttribute[1]));
} else if (itemAttribute[0].equals("a") && createdItemStack) {
is.setAmount(Integer.valueOf(itemAttribute[1]));
} else if (itemAttribute[0].equals("e") && createdItemStack) {
is.addEnchantment(Enchantment.getById(Integer.valueOf(itemAttribute[1])), Integer.valueOf(itemAttribute[2]));
}
}
deserializedInventory.setItem(stackPosition, is);
}
return deserializedInventory;
}
}