Dateien nach "src/main/java/craftersland/bridge/inventory" hochladen
This commit is contained in:
@@ -0,0 +1,66 @@
|
|||||||
|
package net.craftersland.bridge.inventory;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
|
public class BackgroundTask {
|
||||||
|
|
||||||
|
private Inv m;
|
||||||
|
|
||||||
|
public BackgroundTask(Inv m) {
|
||||||
|
this.m = m;
|
||||||
|
runTask();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void runTask() {
|
||||||
|
if (m.getConfigHandler().getBoolean("General.saveDataTask.enabled") == true) {
|
||||||
|
Inv.log.info("Data save task is enabled.");
|
||||||
|
Bukkit.getScheduler().runTaskTimerAsynchronously(m, new Runnable() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
runSaveData();
|
||||||
|
}
|
||||||
|
|
||||||
|
}, m.getConfigHandler().getInteger("General.saveDataTask.interval") * 60 * 20L, m.getConfigHandler().getInteger("General.saveDataTask.interval") * 60 * 20L);
|
||||||
|
} else {
|
||||||
|
Inv.log.info("Data save task is disabled.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void runSaveData() {
|
||||||
|
if (m.getConfigHandler().getBoolean("General.saveDataTask.enabled") == true) {
|
||||||
|
if (Bukkit.getOnlinePlayers().isEmpty() == false) {
|
||||||
|
List<Player> onlinePlayers = new ArrayList<Player>(Bukkit.getOnlinePlayers());
|
||||||
|
if (m.getConfigHandler().getBoolean("General.saveDataTask.hideLogMessages") == false) {
|
||||||
|
Inv.log.info("Saving online players data...");
|
||||||
|
}
|
||||||
|
for (Player p : onlinePlayers) {
|
||||||
|
if (p.isOnline() == true) {
|
||||||
|
m.getInventoryDataHandler().onDataSaveFunction(p, false, "false", null, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (m.getConfigHandler().getBoolean("General.saveDataTask.hideLogMessages") == false) {
|
||||||
|
Inv.log.info("Data save complete for " + onlinePlayers.size() + " players.");
|
||||||
|
}
|
||||||
|
onlinePlayers.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onShutDownDataSave() {
|
||||||
|
Inv.log.info("Saving online players data...");
|
||||||
|
List<Player> onlinePlayers = new ArrayList<Player>(Bukkit.getOnlinePlayers());
|
||||||
|
|
||||||
|
for (Player p : onlinePlayers) {
|
||||||
|
if (p.isOnline() == true) {
|
||||||
|
m.getInventoryDataHandler().onDataSaveFunction(p, false, "true", null, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Inv.log.info("Data save complete for " + onlinePlayers.size() + " players.");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,68 @@
|
|||||||
|
package net.craftersland.bridge.inventory;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
public class ConfigHandler {
|
||||||
|
|
||||||
|
private Inv inv;
|
||||||
|
|
||||||
|
public ConfigHandler(Inv inv) {
|
||||||
|
this.inv = inv;
|
||||||
|
loadConfig();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void loadConfig() {
|
||||||
|
File pluginFolder = new File(inv.getDataFolder().getAbsolutePath());
|
||||||
|
if (!pluginFolder.exists()) {
|
||||||
|
pluginFolder.mkdir();
|
||||||
|
}
|
||||||
|
File configFile = new File(inv.getDataFolder() + System.getProperty("file.separator") + "config.yml");
|
||||||
|
if (!configFile.exists()) {
|
||||||
|
Inv.log.info("No config file found! Creating new one...");
|
||||||
|
inv.saveDefaultConfig();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Inv.log.info("Loading the config file...");
|
||||||
|
inv.getConfig().load(configFile);
|
||||||
|
} catch (Exception e) {
|
||||||
|
Inv.log.severe("Could not load the config file! You need to regenerate the config! Error: " + e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getString(String key) {
|
||||||
|
if (!inv.getConfig().contains(key)) {
|
||||||
|
inv.getLogger().severe("Could not locate " + key + " in the config.yml inside of the " + Inv.pluginName + " folder! (Try generating a new one by deleting the current)");
|
||||||
|
return "errorCouldNotLocateInConfigYml:" + key;
|
||||||
|
} else {
|
||||||
|
return inv.getConfig().getString(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getStringWithColor(String key) {
|
||||||
|
if (!inv.getConfig().contains(key)) {
|
||||||
|
inv.getLogger().severe("Could not locate " + key + " in the config.yml inside of the " + Inv.pluginName + " folder! (Try generating a new one by deleting the current)");
|
||||||
|
return "errorCouldNotLocateInConfigYml:" + key;
|
||||||
|
} else {
|
||||||
|
return inv.getConfig().getString(key).replaceAll("&", "§");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getInteger(String key) {
|
||||||
|
if (!inv.getConfig().contains(key)) {
|
||||||
|
inv.getLogger().severe("Could not locate " + key + " in the config.yml inside of the " + Inv.pluginName + " folder! (Try generating a new one by deleting the current)");
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
return inv.getConfig().getInt(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Boolean getBoolean(String key) {
|
||||||
|
if (!inv.getConfig().contains(key)) {
|
||||||
|
inv.getLogger().severe("Could not locate " + key + " in the config.yml inside of the " + Inv.pluginName + " folder! (Try generating a new one by deleting the current)");
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
return inv.getConfig().getBoolean(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
118
src/main/java/craftersland/bridge/inventory/Inv.java
Normal file
118
src/main/java/craftersland/bridge/inventory/Inv.java
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
package net.craftersland.bridge.inventory;
|
||||||
|
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import net.craftersland.bridge.inventory.database.InvMysqlInterface;
|
||||||
|
import net.craftersland.bridge.inventory.database.MysqlSetup;
|
||||||
|
import net.craftersland.bridge.inventory.events.DropItem;
|
||||||
|
import net.craftersland.bridge.inventory.events.InventoryClick;
|
||||||
|
import net.craftersland.bridge.inventory.events.PlayerJoin;
|
||||||
|
import net.craftersland.bridge.inventory.events.PlayerQuit;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.event.HandlerList;
|
||||||
|
import org.bukkit.plugin.PluginManager;
|
||||||
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
|
public class Inv extends JavaPlugin {
|
||||||
|
|
||||||
|
public static Logger log;
|
||||||
|
public boolean useProtocolLib = false;
|
||||||
|
public static String pluginName = "MysqlInventoryBridge";
|
||||||
|
//public Set<String> playersSync = new HashSet<String>();
|
||||||
|
public static boolean is19Server = true;
|
||||||
|
public static boolean is13Server = false;
|
||||||
|
public static boolean isDisabling = false;
|
||||||
|
|
||||||
|
private static ConfigHandler configHandler;
|
||||||
|
private static SoundHandler sH;
|
||||||
|
private static MysqlSetup databaseManager;
|
||||||
|
private static InvMysqlInterface invMysqlInterface;
|
||||||
|
private static InventoryDataHandler idH;
|
||||||
|
private static BackgroundTask bt;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onEnable() {
|
||||||
|
log = getLogger();
|
||||||
|
getMcVersion();
|
||||||
|
configHandler = new ConfigHandler(this);
|
||||||
|
sH = new SoundHandler(this);
|
||||||
|
checkDependency();
|
||||||
|
bt = new BackgroundTask(this);
|
||||||
|
databaseManager = new MysqlSetup(this);
|
||||||
|
invMysqlInterface = new InvMysqlInterface(this);
|
||||||
|
idH = new InventoryDataHandler(this);
|
||||||
|
//Register Listeners
|
||||||
|
PluginManager pm = getServer().getPluginManager();
|
||||||
|
pm.registerEvents(new PlayerJoin(this), this);
|
||||||
|
pm.registerEvents(new PlayerQuit(this), this);
|
||||||
|
pm.registerEvents(new DropItem(this), this);
|
||||||
|
pm.registerEvents(new InventoryClick(this), this);
|
||||||
|
log.info(pluginName + " loaded successfully!");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDisable() {
|
||||||
|
isDisabling = true;
|
||||||
|
Bukkit.getScheduler().cancelTasks(this);
|
||||||
|
HandlerList.unregisterAll(this);
|
||||||
|
if (databaseManager.getConnection() != null) {
|
||||||
|
bt.onShutDownDataSave();
|
||||||
|
databaseManager.closeConnection();
|
||||||
|
}
|
||||||
|
log.info(pluginName + " is disabled!");
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConfigHandler getConfigHandler() {
|
||||||
|
return configHandler;
|
||||||
|
}
|
||||||
|
public MysqlSetup getDatabaseManager() {
|
||||||
|
return databaseManager;
|
||||||
|
}
|
||||||
|
public InvMysqlInterface getInvMysqlInterface() {
|
||||||
|
return invMysqlInterface;
|
||||||
|
}
|
||||||
|
public SoundHandler getSoundHandler() {
|
||||||
|
return sH;
|
||||||
|
}
|
||||||
|
public BackgroundTask getBackgroundTask() {
|
||||||
|
return bt;
|
||||||
|
}
|
||||||
|
public InventoryDataHandler getInventoryDataHandler() {
|
||||||
|
return idH;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean getMcVersion() {
|
||||||
|
String[] serverVersion = Bukkit.getBukkitVersion().split("-");
|
||||||
|
String version = serverVersion[0];
|
||||||
|
|
||||||
|
if (version.matches("1.7.10") || version.matches("1.7.9") || version.matches("1.7.5") || version.matches("1.7.2") || version.matches("1.8.8") || version.matches("1.8.3") || version.matches("1.8.4") || version.matches("1.8")) {
|
||||||
|
is19Server = false;
|
||||||
|
return true;
|
||||||
|
} else if (version.matches("1.13") || version.matches("1.13.1") || version.matches("1.13.2")) {
|
||||||
|
is13Server = true;
|
||||||
|
return true;
|
||||||
|
} else if (version.matches("1.14") || version.matches("1.14.1") || version.matches("1.14.2") || version.matches("1.14.3") || version.matches("1.14.4")) {
|
||||||
|
is13Server = true;
|
||||||
|
return true;
|
||||||
|
} else if (version.matches("1.15") || version.matches("1.15.1") || version.matches("1.15.2")) {
|
||||||
|
is13Server = true;
|
||||||
|
return true;
|
||||||
|
} else if (version.matches("1.16") || version.matches("1.16.1") || version.matches("1.16.2") || version.matches("1.16.3")) {
|
||||||
|
is13Server = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkDependency() {
|
||||||
|
//Check dependency
|
||||||
|
if (Bukkit.getPluginManager().getPlugin("ProtocolLib") != null) {
|
||||||
|
useProtocolLib = true;
|
||||||
|
log.info("ProtocolLib dependency found.");
|
||||||
|
} else {
|
||||||
|
useProtocolLib = false;
|
||||||
|
log.warning("ProtocolLib dependency not found. No support for modded items NBT data!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,279 @@
|
|||||||
|
package net.craftersland.bridge.inventory;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
|
import net.craftersland.bridge.inventory.objects.DatabaseInventoryData;
|
||||||
|
import net.craftersland.bridge.inventory.objects.InventorySyncData;
|
||||||
|
import net.craftersland.bridge.inventory.objects.InventorySyncTask;
|
||||||
|
|
||||||
|
public class InventoryDataHandler {
|
||||||
|
|
||||||
|
private Inv pd;
|
||||||
|
private Set<Player> playersInSync = new HashSet<Player>();
|
||||||
|
private Set<Player> playersDisconnectSave = new HashSet<Player>();
|
||||||
|
|
||||||
|
public InventoryDataHandler(Inv pd) {
|
||||||
|
this.pd = pd;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isSyncComplete(Player p) {
|
||||||
|
if (playersInSync.contains(p) == true) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void dataCleanup(Player p) {
|
||||||
|
playersInSync.remove(p);
|
||||||
|
playersDisconnectSave.remove(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPlayerData(final Player p, DatabaseInventoryData data, InventorySyncData syncData, boolean cancelTask) {
|
||||||
|
if (playersInSync.contains(p) == false) {
|
||||||
|
//Inventory and Armor sync for lower mc versions then 1.9
|
||||||
|
if (Inv.is19Server == false) {
|
||||||
|
setInventory(p, data, syncData);
|
||||||
|
if (pd.getConfigHandler().getBoolean("General.syncArmorEnabled") == true) {
|
||||||
|
setArmor(p, data, syncData);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
//Inventory and Armor sync for 1.9 and up.
|
||||||
|
if (pd.getConfigHandler().getBoolean("General.syncArmorEnabled") == true) {
|
||||||
|
setInventory(p, data, syncData);
|
||||||
|
} else if (pd.getConfigHandler().getBoolean("General.syncArmorEnabled") == false) {
|
||||||
|
//TODO fix
|
||||||
|
setInventoryNew(p, data, syncData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pd.getInvMysqlInterface().setSyncStatus(p, "false");
|
||||||
|
Bukkit.getScheduler().runTaskLaterAsynchronously(pd, new Runnable() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
playersInSync.add(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
}, 2L);
|
||||||
|
data = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onDataSaveFunction(Player p, Boolean datacleanup, String syncStatus, ItemStack[] inventoryDisconnect, ItemStack[] armorDisconnect) {
|
||||||
|
if (playersDisconnectSave.contains(p) == true) {
|
||||||
|
if (pd.getConfigHandler().getBoolean("Debug.InventorySync") == true) {
|
||||||
|
Inv.log.info("Inventory Debug - Save Data - Canceled - " + p.getName());
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (datacleanup == true) {
|
||||||
|
playersDisconnectSave.add(p);
|
||||||
|
}
|
||||||
|
boolean isPlayerInSync = playersInSync.contains(p);
|
||||||
|
if (isPlayerInSync == true) {
|
||||||
|
String inv = "none";
|
||||||
|
String armor = "none";
|
||||||
|
if (pd.getConfigHandler().getBoolean("Debug.InventorySync") == true) {
|
||||||
|
Inv.log.info("Inventory Debug - Save Data - Start - " + p.getName());
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (inventoryDisconnect != null) {
|
||||||
|
if (pd.getConfigHandler().getBoolean("Debug.InventorySync") == true) {
|
||||||
|
Inv.log.info("Inventory Debug - Set Data - Saving disconnect inventory - " + p.getName());
|
||||||
|
}
|
||||||
|
inv = encodeItems(inventoryDisconnect);
|
||||||
|
} else {
|
||||||
|
if (pd.getConfigHandler().getBoolean("Debug.InventorySync") == true) {
|
||||||
|
Inv.log.info("Inventory Debug - Set Data - Saving inventory - " + p.getName());
|
||||||
|
}
|
||||||
|
inv = encodeItems(p.getInventory().getContents());
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
if (pd.getConfigHandler().getBoolean("General.syncArmorEnabled") == true) {
|
||||||
|
try {
|
||||||
|
if (inventoryDisconnect != null) {
|
||||||
|
armor = encodeItems(armorDisconnect);
|
||||||
|
} else {
|
||||||
|
armor = encodeItems(p.getInventory().getArmorContents());
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pd.getInvMysqlInterface().setData(p, inv, armor, syncStatus);
|
||||||
|
}
|
||||||
|
if (datacleanup == true) {
|
||||||
|
dataCleanup(p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onJoinFunction(final Player p) {
|
||||||
|
if (Inv.isDisabling == false) {
|
||||||
|
if (playersInSync.contains(p) == false) {
|
||||||
|
if (pd.getInvMysqlInterface().hasAccount(p) == true) {
|
||||||
|
final InventorySyncData syncData = new InventorySyncData();
|
||||||
|
backupAndReset(p, syncData);
|
||||||
|
DatabaseInventoryData data = pd.getInvMysqlInterface().getData(p);
|
||||||
|
if (data.getSyncStatus().matches("true")) {
|
||||||
|
setPlayerData(p, data, syncData, false);
|
||||||
|
} else {
|
||||||
|
new InventorySyncTask(pd, System.currentTimeMillis(), p, syncData).runTaskTimerAsynchronously(pd, 10L, 10L);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
playersInSync.add(p);
|
||||||
|
onDataSaveFunction(p, false, "false", null, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void backupAndReset(Player p, InventorySyncData syncData) {
|
||||||
|
syncData.setBackupInventory(p.getInventory().getContents());
|
||||||
|
syncData.setBackupArmor(p.getInventory().getArmorContents());
|
||||||
|
p.setItemOnCursor(null);
|
||||||
|
p.getInventory().clear();
|
||||||
|
p.updateInventory();
|
||||||
|
if (pd.getConfigHandler().getBoolean("General.syncArmorEnabled") == true) {
|
||||||
|
syncData.setBackupArmor(p.getInventory().getArmorContents());
|
||||||
|
p.getInventory().setHelmet(null);
|
||||||
|
p.getInventory().setChestplate(null);
|
||||||
|
p.getInventory().setLeggings(null);
|
||||||
|
p.getInventory().setBoots(null);
|
||||||
|
p.updateInventory();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStack[] getInventory(Player p) {
|
||||||
|
return p.getInventory().getContents();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStack[] getArmor(Player p) {
|
||||||
|
if (pd.getConfigHandler().getBoolean("General.syncArmorEnabled") == true) {
|
||||||
|
return p.getInventory().getArmorContents();
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setInventory(final Player p, DatabaseInventoryData data, InventorySyncData syncData) {
|
||||||
|
if (pd.getConfigHandler().getBoolean("Debug.InventorySync") == true) {
|
||||||
|
Inv.log.info("Inventory Debug - Set Data - Start- " + p.getName());
|
||||||
|
}
|
||||||
|
if (data.getRawInventory().matches("none") == false) {
|
||||||
|
if (pd.getConfigHandler().getBoolean("Debug.InventorySync") == true) {
|
||||||
|
Inv.log.info("Inventory Debug - Set Data - Loading inventory - " + p.getName());
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
p.getInventory().setContents(decodeItems(data.getRawInventory()));
|
||||||
|
if (pd.getConfigHandler().getBoolean("Debug.InventorySync") == true) {
|
||||||
|
Inv.log.info("Inventory Debug - Set Data - Inventory set - " + p.getName());
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
if (syncData.getBackupInventory() != null) {
|
||||||
|
if (pd.getConfigHandler().getBoolean("Debug.InventorySync") == true) {
|
||||||
|
Inv.log.info("Inventory Debug - Set Data - Loading backup inventory - " + p.getName());
|
||||||
|
}
|
||||||
|
p.getInventory().setContents(syncData.getBackupInventory());
|
||||||
|
p.sendMessage(pd.getConfigHandler().getStringWithColor("ChatMessage.inventorySyncError"));
|
||||||
|
pd.getSoundHandler().sendPlingSound(p);
|
||||||
|
p.sendMessage(pd.getConfigHandler().getStringWithColor("ChatMessage.inventorySyncBackup"));
|
||||||
|
} else {
|
||||||
|
if (pd.getConfigHandler().getBoolean("Debug.InventorySync") == true) {
|
||||||
|
Inv.log.info("Inventory Debug - Set Data - No backup inventory found! - " + p.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (pd.getConfigHandler().getBoolean("Debug.InventorySync") == true) {
|
||||||
|
Inv.log.info("Inventory Debug - Set Data - Restoring local inventory - " + p.getName());
|
||||||
|
}
|
||||||
|
p.getInventory().setContents(syncData.getBackupInventory());
|
||||||
|
}
|
||||||
|
p.updateInventory();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setInventoryNew(final Player p, DatabaseInventoryData data, InventorySyncData syncData) {
|
||||||
|
if (pd.getConfigHandler().getBoolean("Debug.InventorySync") == true) {
|
||||||
|
Inv.log.info("Inventory Debug - Set Data - Start- " + p.getName());
|
||||||
|
}
|
||||||
|
if (data.getRawInventory().matches("none") == false) {
|
||||||
|
if (pd.getConfigHandler().getBoolean("Debug.InventorySync") == true) {
|
||||||
|
Inv.log.info("Inventory Debug - Set Data - Loading inventory - " + p.getName());
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
p.getInventory().setContents(decodeItems(data.getRawInventory()));
|
||||||
|
p.getInventory().setArmorContents(syncData.getBackupArmor());
|
||||||
|
if (pd.getConfigHandler().getBoolean("Debug.InventorySync") == true) {
|
||||||
|
Inv.log.info("Inventory Debug - Set Data - Inventory set - " + p.getName());
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
if (syncData.getBackupInventory() != null) {
|
||||||
|
if (pd.getConfigHandler().getBoolean("Debug.InventorySync") == true) {
|
||||||
|
Inv.log.info("Inventory Debug - Set Data - Loading backup inventory - " + p.getName());
|
||||||
|
}
|
||||||
|
p.getInventory().setContents(syncData.getBackupInventory());
|
||||||
|
p.sendMessage(pd.getConfigHandler().getStringWithColor("ChatMessage.inventorySyncError"));
|
||||||
|
pd.getSoundHandler().sendPlingSound(p);
|
||||||
|
p.sendMessage(pd.getConfigHandler().getStringWithColor("ChatMessage.inventorySyncBackup"));
|
||||||
|
} else {
|
||||||
|
if (pd.getConfigHandler().getBoolean("Debug.InventorySync") == true) {
|
||||||
|
Inv.log.info("Inventory Debug - Set Data - No backup inventory found! - " + p.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (pd.getConfigHandler().getBoolean("Debug.InventorySync") == true) {
|
||||||
|
Inv.log.info("Inventory Debug - Set Data - Restoring local inventory - " + p.getName());
|
||||||
|
}
|
||||||
|
p.getInventory().setContents(syncData.getBackupInventory());
|
||||||
|
}
|
||||||
|
p.updateInventory();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setArmor(final Player p, DatabaseInventoryData data, InventorySyncData syncData) {
|
||||||
|
if (data.getRawArmor().matches("none") == false) {
|
||||||
|
try {
|
||||||
|
p.getInventory().setArmorContents(decodeItems(data.getRawArmor()));
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
p.getInventory().setArmorContents(syncData.getBackupArmor());
|
||||||
|
p.sendMessage(pd.getConfigHandler().getStringWithColor("ChatMessage.armorSyncError"));
|
||||||
|
pd.getSoundHandler().sendPlingSound(p);
|
||||||
|
p.sendMessage(pd.getConfigHandler().getStringWithColor("ChatMessage.armorSyncBackup"));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
p.getInventory().setArmorContents(syncData.getBackupArmor());
|
||||||
|
}
|
||||||
|
p.updateInventory();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String encodeItems(ItemStack[] items) {
|
||||||
|
if (pd.useProtocolLib == true && pd.getConfigHandler().getBoolean("General.enableModdedItemsSupport") == true) {
|
||||||
|
return InventoryUtils.saveModdedStacksData(items);
|
||||||
|
} else {
|
||||||
|
return InventoryUtils.itemStackArrayToBase64(items);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStack[] decodeItems(String data) throws Exception {
|
||||||
|
if (pd.useProtocolLib == true && pd.getConfigHandler().getBoolean("General.enableModdedItemsSupport") == true) {
|
||||||
|
ItemStack[] it = InventoryUtils.restoreModdedStacks(data);
|
||||||
|
if (it == null) {
|
||||||
|
it = InventoryUtils.itemStackArrayFromBase64(data);
|
||||||
|
}
|
||||||
|
return it;
|
||||||
|
} else {
|
||||||
|
return InventoryUtils.itemStackArrayFromBase64(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
120
src/main/java/craftersland/bridge/inventory/InventoryUtils.java
Normal file
120
src/main/java/craftersland/bridge/inventory/InventoryUtils.java
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
package net.craftersland.bridge.inventory;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.inventory.Inventory;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.util.io.BukkitObjectInputStream;
|
||||||
|
import org.bukkit.util.io.BukkitObjectOutputStream;
|
||||||
|
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
|
||||||
|
|
||||||
|
import com.comphenix.protocol.utility.StreamSerializer;
|
||||||
|
|
||||||
|
public class InventoryUtils {
|
||||||
|
|
||||||
|
public static String toBase64(Inventory inventory) {
|
||||||
|
try {
|
||||||
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||||
|
BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);
|
||||||
|
|
||||||
|
dataOutput.writeInt(inventory.getSize());
|
||||||
|
|
||||||
|
for (int i = 0; i < inventory.getSize(); i++) {
|
||||||
|
dataOutput.writeObject(inventory.getItem(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
dataOutput.close();
|
||||||
|
return Base64Coder.encodeLines(outputStream.toByteArray());
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new IllegalStateException("Unable to save item stacks.", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Inventory fromBase64(String data) throws IOException {
|
||||||
|
try {
|
||||||
|
ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data));
|
||||||
|
BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
|
||||||
|
Inventory inventory = Bukkit.getServer().createInventory(null, dataInput.readInt());
|
||||||
|
|
||||||
|
for (int i = 0; i < inventory.getSize(); i++) {
|
||||||
|
inventory.setItem(i, (ItemStack) dataInput.readObject());
|
||||||
|
}
|
||||||
|
|
||||||
|
dataInput.close();
|
||||||
|
return inventory;
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
throw new IOException("Unable to decode class type.", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String itemStackArrayToBase64(ItemStack[] items) {
|
||||||
|
try {
|
||||||
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||||
|
BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);
|
||||||
|
|
||||||
|
dataOutput.writeInt(items.length);
|
||||||
|
|
||||||
|
for (int i = 0; i < items.length; i++) {
|
||||||
|
dataOutput.writeObject(items[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
dataOutput.close();
|
||||||
|
return Base64Coder.encodeLines(outputStream.toByteArray());
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new IllegalStateException("Unable to save item stacks.", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ItemStack[] itemStackArrayFromBase64(String data) throws IOException {
|
||||||
|
try {
|
||||||
|
ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data));
|
||||||
|
BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
|
||||||
|
ItemStack[] items = new ItemStack[dataInput.readInt()];
|
||||||
|
|
||||||
|
for (int i = 0; i < items.length; i++) {
|
||||||
|
items[i] = (ItemStack) dataInput.readObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
dataInput.close();
|
||||||
|
return items;
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
throw new IOException("Unable to decode class type.", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String saveModdedStacksData(ItemStack[] itemStacks) {
|
||||||
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
|
for (int i = 0; i < itemStacks.length; i++) {
|
||||||
|
if (i > 0) {
|
||||||
|
stringBuilder.append(";");
|
||||||
|
}
|
||||||
|
if (itemStacks[i] != null && itemStacks[i].getType() != Material.AIR) {
|
||||||
|
try {
|
||||||
|
stringBuilder.append(StreamSerializer.getDefault().serializeItemStack(itemStacks[i]));
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return stringBuilder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ItemStack[] restoreModdedStacks(String string) {
|
||||||
|
String[] strings = string.split(";");
|
||||||
|
ItemStack[] itemStacks = new ItemStack[strings.length];
|
||||||
|
for (int i = 0; i < strings.length; i++) {
|
||||||
|
if (!strings[i].isEmpty()) {
|
||||||
|
try {
|
||||||
|
itemStacks[i] = StreamSerializer.getDefault().deserializeItemStack(strings[i]);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return itemStacks;
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,46 @@
|
|||||||
|
package net.craftersland.bridge.inventory;
|
||||||
|
|
||||||
|
import org.bukkit.Sound;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
|
public class SoundHandler {
|
||||||
|
|
||||||
|
private Inv pd;
|
||||||
|
|
||||||
|
public SoundHandler(Inv pd) {
|
||||||
|
this.pd = pd;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendPlingSound(Player p) {
|
||||||
|
if (pd.getConfigHandler().getBoolean("General.disableSounds") == false) {
|
||||||
|
if (Inv.is13Server == true) {
|
||||||
|
p.playSound(p.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 3F, 3F);
|
||||||
|
} else if (Inv.is19Server == true) {
|
||||||
|
p.playSound(p.getLocation(), Sound.valueOf("BLOCK_NOTE_PLING"), 3F, 3F);
|
||||||
|
} else {
|
||||||
|
p.playSound(p.getLocation(), Sound.valueOf("NOTE_PLING"), 3F, 3F);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendLevelUpSound(Player p) {
|
||||||
|
if (pd.getConfigHandler().getBoolean("General.disableSounds") == false) {
|
||||||
|
if (Inv.is19Server == true) {
|
||||||
|
p.playSound(p.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1F, 1F);
|
||||||
|
} else {
|
||||||
|
p.playSound(p.getLocation(), Sound.valueOf("LEVEL_UP"), 1F, 1F);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendArrowHit(Player p) {
|
||||||
|
if (pd.getConfigHandler().getBoolean("General.disableSounds") == false) {
|
||||||
|
if (Inv.is19Server == true) {
|
||||||
|
p.playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 3F, 3F);
|
||||||
|
} else {
|
||||||
|
p.playSound(p.getLocation(), Sound.valueOf("SUCCESSFUL_HIT"), 3F, 3F);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user