Merge pull request #1 from TBMCPlugins/iie

Merge iie's code into NorbiPeti's code
This commit is contained in:
Norbi Peti 2016-11-05 21:57:36 +01:00 committed by GitHub
commit e95822ab94
12 changed files with 971 additions and 675 deletions

BIN
.DS_Store vendored

Binary file not shown.

View file

@ -1,3 +1,5 @@
main: buttondevteam.perworld.main.MainPlugin main: buttondevteam.perworld.main.MainPlugin
version: 1.0.0 version: 1.0.0
name: PerWorldInventories name: PerWorldInventories
softdepend:
[Multiverse-Core]

BIN
src/.DS_Store vendored

Binary file not shown.

View file

@ -0,0 +1,16 @@
package buttondevteam.perworld.cache;
import org.bukkit.configuration.ConfigurationSection;
public interface CacheInterface {
public static ConfigurationSection worlds = buttondevteam.perworld.main.MainPlugin.worlds;
public static ConfigurationSection players = buttondevteam.perworld.main.MainPlugin.players;
Object generateElement(String string);
void putCache(String string);
void initCache();
}

View file

@ -0,0 +1,7 @@
package buttondevteam.perworld.cache;
public class CacheRequestHandler {
}

View file

@ -0,0 +1,10 @@
package buttondevteam.perworld.cache.player;
import java.util.HashMap;
public class GameMode {
//CACHE
public static HashMap<String,Integer> cache;
}

View file

@ -0,0 +1,6 @@
package buttondevteam.perworld.cache.player;
public class Name {
}

View file

@ -0,0 +1,100 @@
package buttondevteam.perworld.cache.world;
import java.util.HashMap;
import buttondevteam.perworld.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<String,E[]> cache = new HashMap<String,E[]>();
/* 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
};
}
}

View file

@ -0,0 +1,21 @@
package buttondevteam.perworld.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){
}
}

View file

@ -0,0 +1,32 @@
package buttondevteam.perworld.main;
import static buttondevteam.perworld.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);
}
}

View file

@ -0,0 +1,102 @@
package buttondevteam.perworld.main;
import org.bukkit.craftbukkit.v1_10_R1.inventory.CraftInventory;
import org.bukkit.entity.Player;
import net.minecraft.server.v1_10_R1.IInventory;
import java.util.stream.Collectors;
import static buttondevteam.perworld.cache.world.ShareSettings.compare;
import static buttondevteam.perworld.main.MainPlugin.config;
import static buttondevteam.perworld.main.MainPlugin.debugClock;
import static buttondevteam.perworld.main.MainPlugin.plugin;
public class PlayerUpdater {
//VALUES USED BY THE UPDATE METHODS
public static class Values {
public String uuid;
public String worldTo;
public String worldFrom;
public String pTo;
public String pFrom;
public boolean shareinv;
public boolean sharedata;
public Values (Player player, String worldTo, String worldFrom){
this.uuid = player.getUniqueId().toString();
this.worldTo = worldTo;
this.worldFrom = worldFrom;
this.pTo = "worlds." + worldTo + ".players." + uuid;
this.pFrom = "worlds." + worldFrom + ".players." + uuid;
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
public static void update(Player player, String worldTo, String worldFrom){
final Values values = new Values(player, worldTo, worldFrom);
updateLocation(values, player);
updateInventories(values, player);
//updatePlayerData(values, player);
player.sendMessage("...done, " + (System.currentTimeMillis() - debugClock) + " ms");
new buttondevteam.perworld.cache.world.ShareSettings().initCache();
player.sendMessage(buttondevteam.perworld.cache.world.ShareSettings.cache.keySet().stream().collect(Collectors.joining(",")));
}
//UPDATE LOCATION
public static void updateLocation(Values values, Player player){
config.set(
values.pFrom + ".location",
buttondevteam.perworld.serializers.location.serialize(player.getLocation())
);
plugin.saveConfig();
/* players are not automatically moved to their stored location,
* this is done only on request, in a dedicated teleport method
*/
}
//UPDATE INVENTORIES
public static void updateInventories(Values values, Player player){
IInventory inventory = ((CraftInventory) player.getInventory()).getInventory();
config.set(values.pFrom + ".inventory", buttondevteam.perworld.serializers.inventory.serialize(inventory));
plugin.saveConfig();
if (!values.shareinv)
buttondevteam.perworld.serializers.inventory.setFromSerialized(
inventory, (String) config.get(values.pTo + ".inventory")
);
IInventory enderchest = ((CraftInventory) player.getEnderChest()).getInventory();
config.set(values.pFrom + ".enderchest", buttondevteam.perworld.serializers.inventory.serialize(enderchest));
plugin.saveConfig();
if (!values.sharedata)
buttondevteam.perworld.serializers.inventory.setFromSerialized(
enderchest, (String) config.get(values.pTo + ".enderchest")
);
}
//UPDATE PLAYERDATA
public static void updatePlayerData(Values values, Player player){
config.set(values.pFrom + ".playerdata", buttondevteam.perworld.serializers.playerdata.serialize(player));
plugin.saveConfig();
if (!values.sharedata)
;
}
}