diff --git a/.DS_Store b/.DS_Store index 97754fd..5b749f6 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/plugin.yml b/plugin.yml index 25e5172..f4a3f8a 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,3 +1,5 @@ main: main.MainPlugin version: 1.0.0 - name: PerWorldInventories \ No newline at end of file + name: PerWorldInventories + softdepend: + [Multiverse-Core] \ No newline at end of file diff --git a/src/.DS_Store b/src/.DS_Store index 4e5679e..5008ddf 100644 Binary files a/src/.DS_Store and b/src/.DS_Store differ diff --git a/src/cache/CacheInterface.java b/src/cache/CacheInterface.java new file mode 100644 index 0000000..10146c4 --- /dev/null +++ b/src/cache/CacheInterface.java @@ -0,0 +1,16 @@ +package cache; + +import org.bukkit.configuration.ConfigurationSection; + +public interface CacheInterface { + + public static ConfigurationSection worlds = main.MainPlugin.worlds; + public static ConfigurationSection players = main.MainPlugin.players; + + Object generateElement(String string); + + void putCache(String string); + + void initCache(); + +} diff --git a/src/cache/CacheRequestHandler.java b/src/cache/CacheRequestHandler.java new file mode 100644 index 0000000..4b5c9a7 --- /dev/null +++ b/src/cache/CacheRequestHandler.java @@ -0,0 +1,7 @@ +package cache; + +public class CacheRequestHandler { + + + +} diff --git a/src/cache/player/GameMode.java b/src/cache/player/GameMode.java new file mode 100644 index 0000000..a19536f --- /dev/null +++ b/src/cache/player/GameMode.java @@ -0,0 +1,10 @@ +package cache.player; + +import java.util.HashMap; + +public class GameMode { + + + //CACHE + public static HashMap cache; +} diff --git a/src/cache/player/Name.java b/src/cache/player/Name.java new file mode 100644 index 0000000..66a9b7b --- /dev/null +++ b/src/cache/player/Name.java @@ -0,0 +1,6 @@ +package cache.player; + +public class Name { + + +} diff --git a/src/cache/world/ShareSettings.java b/src/cache/world/ShareSettings.java new file mode 100644 index 0000000..bd2e2ab --- /dev/null +++ b/src/cache/world/ShareSettings.java @@ -0,0 +1,100 @@ +package cache.world; + +import java.util.HashMap; + +import cache.CacheInterface; + +public class ShareSettings implements CacheInterface { + + + /* ConfigurationSection "worlds" is a static import from main.MainPlugin.java, + * inherited from CacheInterface. + * + * It points to the section found at config.getConfigurationSection("worlds") + * and contains all world keys, child keys, and their values + * + * this is a static field backed by the actual contents of config.yml + */ + + + + + + //CACHE ELEMENT + public class E { + /* the optional share type (in or out) specifies that the world + * shares with its group in only one traffic direction. + */ + String group; + int t; + public E(String group, int type){ + this.group = group; + this.t = type;//0-default, 1-in, 2-out + } + } + + + //CACHE + public static volatile HashMap cache = new HashMap(); + /* this stores the share settings for each world + * in an easily-parsed format, mapping each world to + * an array e[] of two cache elements (inv and data) + */ + + + + + + //GENERATE ELEMENT (FROM CONFIG) + public E[] generateElement(String world){ + return new E[] + { + new E( + worlds.getString(world + ".share.inventory.group"), + worlds.getInt(world + ".share.inventory.type", 0) + ), + new E( + worlds.getString(world + ".share.playerdata.group"), + worlds.getInt(world + ".share.playerdata.type", 0) + ) + }; + } + + + //PUT CACHE + public void putCache(String world){ + E[] element = generateElement(world); + if ((element[0].group != null || element[1].group != null)) cache.put(world, element); + } + + + //INIT CACHE ( forEach -> putCache() ) + public void initCache(){ + worlds.getKeys(false).forEach(s -> { putCache(s); }); + } + + + + + + //COMPARE SETTINGS + public static boolean[] compare (String worldTo, String worldFrom) { + E[][] params = new E[][] { cache.get(worldTo), cache.get(worldFrom) }; + return params[0] == null || params [1] == null ? + new boolean[] {false, false} : + new boolean[] + { + params[0][0] == null || params[1][0] == null ? false : + + params[0][0].group == params[1][0].group //inv + && params[0][0].t == 0 ? true : params[0][0].t == 1 + && params[1][0].t == 0 ? true : params[1][0].t == 2 + , + params[0][1] == null || params[1][1] == null ? false : + + params[0][1].group == params[1][1].group //data + && params[0][1].t == 0 ? true : params[0][1].t == 1 + && params[1][1].t == 0 ? true : params[1][1].t == 2 + }; + } +} diff --git a/src/main/ListenerPlayerJoin.java b/src/main/ListenerPlayerJoin.java new file mode 100644 index 0000000..34703c1 --- /dev/null +++ b/src/main/ListenerPlayerJoin.java @@ -0,0 +1,21 @@ +package main; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; + +public class ListenerPlayerJoin implements Listener { + + MainPlugin plugin; + public ListenerPlayerJoin(MainPlugin plugin){ + this.plugin = plugin; + } + + + @EventHandler(priority = EventPriority.MONITOR) + public void onPlayerJoin(PlayerJoinEvent event){ + + } + +} diff --git a/src/main/ListenerPlayerWorldChange.java b/src/main/ListenerPlayerWorldChange.java new file mode 100644 index 0000000..25c027d --- /dev/null +++ b/src/main/ListenerPlayerWorldChange.java @@ -0,0 +1,32 @@ +package main; + +import static main.MainPlugin.debugClock; + +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerChangedWorldEvent; + +public class ListenerPlayerWorldChange implements Listener { + + MainPlugin plugin; + public ListenerPlayerWorldChange(MainPlugin plugin){ + this.plugin = plugin; + + } + + + @EventHandler(priority = EventPriority.MONITOR) + public void onWorldChange(PlayerChangedWorldEvent event){ + + debugClock = System.currentTimeMillis(); + + Player player = event.getPlayer(); player.sendMessage("updating... "); + String gamemode; + String worldTo = player.getWorld().getName(); + String worldFrom = event.getFrom().getName(); + + PlayerUpdater.update(player, worldTo, worldFrom); + } +} diff --git a/src/main/MainPlugin.java b/src/main/MainPlugin.java index 3c83ac0..06c3486 100644 --- a/src/main/MainPlugin.java +++ b/src/main/MainPlugin.java @@ -1,59 +1,35 @@ package main; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.plugin.java.JavaPlugin; public class MainPlugin extends JavaPlugin { - + + + public static MainPlugin plugin; + public static volatile FileConfiguration config; + + public static volatile ConfigurationSection worlds; + public static volatile ConfigurationSection players; + public static long debugClock; + + public void onEnable(){ - //getServer().getPluginManager().registerEvents(new WorldLoadListener(this), this); - getServer().getPluginManager().registerEvents(new WorldChangeListener(this), this); - MainPlugin.getPlugin(this.getClass()).getServer().getListeningPluginChannels(); - + getServer().getPluginManager().registerEvents(new ListenerPlayerJoin(this), this); + getServer().getPluginManager().registerEvents(new ListenerPlayerWorldChange(this), this); saveDefaultConfig(); - WorldChangeManager.init(this); - //BukkitTask task = new initWorldSettings().runTaskLater(this, 20); - } -} -/* - public void initWorldSettings(){ - Bukkit.getServer().getWorlds() - .stream() - .forEach(s -> { - FileConfiguration config = getConfig(); - String name = s.getName(); - - if (config.get(name + ".settings.shareinvgroup") == null) - config.set(name + ".settings.shareinvgroup", name); - - if (config.get(name + ".settings.sharedatagroup") == null) - config.set(name + ".settings.sharedatagroup", name); - - }); + + plugin = this; + config = getConfig(); + worlds = config.getConfigurationSection("worlds"); + players = config.getConfigurationSection("players"); + + if (!config.contains("worlds")) config.createSection("worlds"); + if (!config.contains("players")) config.createSection("players"); saveConfig(); + + new cache.world.ShareSettings().initCache(); } -} - - private void createConfig(){ - try { - - - if (!getDataFolder().exists()) - getDataFolder().mkdirs(); - - - File file = new File(getDataFolder(), "config.yml"); - - if (!file.exists()){ - getLogger().info("Config.yml not found, creating!"); - saveDefaultConfig(); - }else - getLogger().info("Config.yml found, loading!"); - - - }catch (Exception e) { - e.printStackTrace(); - } - } - -*/ \ No newline at end of file +} \ No newline at end of file diff --git a/src/main/WorldChangeManager.java b/src/main/PlayerUpdater.java similarity index 54% rename from src/main/WorldChangeManager.java rename to src/main/PlayerUpdater.java index d531d7f..2e46790 100644 --- a/src/main/WorldChangeManager.java +++ b/src/main/PlayerUpdater.java @@ -1,65 +1,19 @@ package main; -import java.util.Arrays; - -import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.craftbukkit.v1_10_R1.inventory.CraftInventory; import org.bukkit.entity.Player; import net.minecraft.server.v1_10_R1.IInventory; -public class WorldChangeManager { - - private static MainPlugin plugin; - private static FileConfiguration config; - public static void init(MainPlugin plugin){ //onEnable() - WorldChangeManager.plugin = plugin; - WorldChangeManager.config = plugin.getConfig(); - } - - - //EVALUATE SHARE SETTINGS - public static boolean[] compare (String worldTo, String worldFrom) { - boolean[] result = new boolean[] {false,false}; - String[][] params = - (String[][]) Arrays.asList - (new String[] { - (String) config.get(worldTo + ".settings.shareinvgroup"), - (String) config.get(worldFrom + ".settings.shareinvgroup"), - (String) config.get(worldTo + ".settings.sharedatagroup"), - (String) config.get(worldFrom + ".settings.sharedatagroup") - }) - .stream() - .map(s -> (String[]) s - .trim() - .replaceAll("( )", "") - .toLowerCase() - .split("\\*")) - .toArray(String[][]::new); - - /* Each world has two sharing settings: for inventory sharing, - * and for playerdata sharing. Each setting has two parameters: - * the sharegroup flag, which labels a group of sharing worlds, - * and an optional suffix, *in or *out, specifying that the world - * shares with its group in only one traffic direction - */ - - if ( - params[0][0].equals(params[1][0])//inv - && params[0].length > 1 ? params[0][1].equals("in") : true - && params[1].length > 1 ? params[1][1].equals("out") : true - ) - result[0] = true; - - if ( - params[2][0].equals(params[3][0])//data - && params[2].length > 1 ? params[2][1].equals("in") : true - && params[3].length > 1 ? params[3][1].equals("out") : true - ) - result[1] = true; - - return result; - } +import static cache.world.ShareSettings.compare; +import static main.MainPlugin.plugin; + +import java.util.stream.Collectors; + +import static main.MainPlugin.config; +import static main.MainPlugin.debugClock; + +public class PlayerUpdater { //VALUES USED BY THE UPDATE METHODS @@ -77,15 +31,18 @@ public class WorldChangeManager { this.uuid = player.getUniqueId().toString(); this.worldTo = worldTo; this.worldFrom = worldFrom; - this.pTo = worldTo + ".players." + uuid; - this.pFrom = worldFrom + ".players." + uuid; + this.pTo = "worlds." + worldTo + ".players." + uuid; + this.pFrom = "worlds." + worldFrom + ".players." + uuid; - boolean[] compare = compare(worldTo,worldFrom); + boolean[] compare = compare(worldTo, worldFrom); + //compare() is a static import from world.ShareSettings.java this.shareinv = compare[0]; this.sharedata = compare[1]; } } + + //MAIN UPDATE METHOD @@ -96,6 +53,10 @@ public class WorldChangeManager { updateLocation(values, player); updateInventories(values, player); //updatePlayerData(values, player); + + player.sendMessage("...done, " + (System.currentTimeMillis() - debugClock) + " ms"); + new cache.world.ShareSettings().initCache(); + player.sendMessage(cache.world.ShareSettings.cache.keySet().stream().collect(Collectors.joining(","))); } diff --git a/src/main/WorldChangeListener.java b/src/main/WorldChangeListener.java deleted file mode 100644 index 7763228..0000000 --- a/src/main/WorldChangeListener.java +++ /dev/null @@ -1,39 +0,0 @@ -package main; - -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerChangedWorldEvent; - -public class WorldChangeListener implements Listener { - - private static MainPlugin plugin; - private static FileConfiguration config; - public WorldChangeListener(MainPlugin plugin){ - WorldChangeListener.plugin = plugin; - WorldChangeListener.config = plugin.getConfig(); - } - - - @EventHandler(priority = EventPriority.MONITOR) - public void onWorldChangeListener(PlayerChangedWorldEvent event){ - Player player = event.getPlayer(); - String worldTo = player.getWorld().getName(); - String worldFrom = event.getFrom().getName(); - - initWorldSettings(worldTo); - initWorldSettings(worldFrom); - WorldChangeManager.update(player,worldTo, worldFrom); - } - - - public static void initWorldSettings(String world){ - if (config.get(world + ".settings.shareinvgroup") == null) - config.set(world + ".settings.shareinvgroup", world); - if (config.get(world + ".settings.sharedatagroup") == null) - config.set(world + ".settings.sharedatagroup", world); - plugin.saveConfig(); - } -} diff --git a/src/serializers/location.java b/src/serializers/location.java index 7248f84..234d5f9 100644 --- a/src/serializers/location.java +++ b/src/serializers/location.java @@ -10,10 +10,10 @@ public class location { public static String serialize(Location location){ return location.getBlockX() + "," - + location.getBlockZ() + "," + location.getBlockY() + "," - + location.getPitch() + "," - + location.getYaw(); + + location.getBlockZ() + "," + + location.getYaw() + "," + + location.getPitch(); }