Browse Source

Implemented serialization and other things

lennartVH01 9 years ago
parent
commit
a1b1c41322

+ 12 - 1
config.yml

@@ -14,13 +14,24 @@ NotifyTimes:
 - 60
 - 60
 
 
 # Max players per game, users with permission AbbaCaving.joinFull can still join
 # Max players per game, users with permission AbbaCaving.joinFull can still join
-PlayerCap: 1
+PlayerCap: 10
 
 
 # Ratio of spoils given per player: 
 # Ratio of spoils given per player: 
 #   All: Amount to be spread over all participants
 #   All: Amount to be spread over all participants
 #   Pot: Amount to be added to general Pot
 #   Pot: Amount to be added to general Pot
 #   Top: first n players receive these chunks of total wins
 #   Top: first n players receive these chunks of total wins
 
 
+
+# If this is true, then the players who aren't in the Top will divide the total amount of items among themselves, If this is false, then every player's weight is added to the total weight
+FixPlayerWeight: false;
+#Example Game with 10 players
+# All: 1
+# Top: 
+# - 7
+# - 2
+# If FixPlayerWeight == true then the first player would receive 70% of winnings, second would receive 20% and the remaining 10% is divided among the 8 remaining players
+# If FixPlayerWeight == false then the first player would receive 7/(7+2+8*1)=7/17 of total winnings, second would receive 2/17 and every remaining player will get 1/17
+
 # Part of the winnings that go to each player
 # Part of the winnings that go to each player
 WinWeights:
 WinWeights:
   All: 1
   All: 1

+ 149 - 41
src/me/lennartVH01/AbbaGame.java

@@ -28,13 +28,13 @@ public class AbbaGame implements ConfigurationSerializable{
 
 
 		@Override
 		@Override
 		public int compare(Tuple2<UUID, CalculatedScore> arg0, Tuple2<UUID, CalculatedScore> arg1) {
 		public int compare(Tuple2<UUID, CalculatedScore> arg0, Tuple2<UUID, CalculatedScore> arg1) {
-			return arg0.getArg2().getTotal() - arg1.getArg2().getTotal();
+			return arg1.getArg2().getTotal() - arg0.getArg2().getTotal();
 		}
 		}
 		
 		
 	};
 	};
 	
 	
 	
 	
-	private Main plugin;
+	private static Main plugin;
 	public int taskId;
 	public int taskId;
 	public boolean open = false;
 	public boolean open = false;
 	public GameState state = GameState.WAITING;
 	public GameState state = GameState.WAITING;
@@ -42,11 +42,11 @@ public class AbbaGame implements ConfigurationSerializable{
 	public Location spawn;
 	public Location spawn;
 	public long endTime;
 	public long endTime;
 	public int duration;
 	public int duration;
-	public int countDownTime;
 	public int playerCap;
 	public int playerCap;
-	public List<Player> players;
+	public List<UUID> players;
 	public List<Tuple2<UUID, CalculatedScore>> endStats = new ArrayList<Tuple2<UUID, CalculatedScore>>();
 	public List<Tuple2<UUID, CalculatedScore>> endStats = new ArrayList<Tuple2<UUID, CalculatedScore>>();
 	public Map<UUID, Chest> playerChests = new HashMap<UUID, Chest>();
 	public Map<UUID, Chest> playerChests = new HashMap<UUID, Chest>();
+	public Map<UUID, List<ItemStack>> leftovers = new HashMap<UUID, List<ItemStack>>();
 	public List<ItemStack> collectedItems = new ArrayList<ItemStack>();
 	public List<ItemStack> collectedItems = new ArrayList<ItemStack>();
 	public List<Chest> chests = new ArrayList<Chest>();
 	public List<Chest> chests = new ArrayList<Chest>();
 	public List<Sign> signs = new ArrayList<Sign>();
 	public List<Sign> signs = new ArrayList<Sign>();
@@ -54,18 +54,18 @@ public class AbbaGame implements ConfigurationSerializable{
 	private Objective abbaObjective = scoreboard.registerNewObjective("AbbaStats", "dummy");
 	private Objective abbaObjective = scoreboard.registerNewObjective("AbbaStats", "dummy");
 	private Score timer = abbaObjective.getScore("Time Remaining");
 	private Score timer = abbaObjective.getScore("Time Remaining");
 	
 	
-	
-	public AbbaGame(Main plugin, String name, Location spawn, int duration, int playerCap, int countDownTime){
-		this.plugin = plugin;
+	public static void initialize(Main plugin){
+		AbbaGame.plugin = plugin;
+	}
+	public AbbaGame(String name, Location spawn, int duration, int playerCap){
 		this.name = name;
 		this.name = name;
 		this.spawn = spawn;
 		this.spawn = spawn;
 		this.duration = duration;
 		this.duration = duration;
 		this.playerCap = playerCap;
 		this.playerCap = playerCap;
-		this.countDownTime = countDownTime;
 		if(playerCap == -1){
 		if(playerCap == -1){
-			players = new ArrayList<Player>();
+			players = new ArrayList<UUID>();
 		}else{
 		}else{
-			players = new ArrayList<Player>(playerCap);
+			players = new ArrayList<UUID>(playerCap);
 		}
 		}
 		abbaObjective.setDisplaySlot(DisplaySlot.SIDEBAR);
 		abbaObjective.setDisplaySlot(DisplaySlot.SIDEBAR);
 		
 		
@@ -75,6 +75,8 @@ public class AbbaGame implements ConfigurationSerializable{
 			collectedItems.add(stack);
 			collectedItems.add(stack);
 		}
 		}
 	}
 	}
+	
+	
 	public void destroy(){
 	public void destroy(){
 		for(Sign s:signs){
 		for(Sign s:signs){
 			s.setLine(0, "");
 			s.setLine(0, "");
@@ -82,28 +84,28 @@ public class AbbaGame implements ConfigurationSerializable{
 			//s.setLine(2, "");
 			//s.setLine(2, "");
 			s.update();
 			s.update();
 		}
 		}
-		for(Player p:players){
-			plugin.playerMap.remove(p.getUniqueId());
-			p.sendMessage("§cGame ended!");
-			
+		for(int i = players.size() - 1; i >= 0; i--){	//loop array backwards for speed
+			removePlayer(players.get(i));
 			//probably tp people to spawn or something
 			//probably tp people to spawn or something
 			
 			
 		}
 		}
+		stopClock();
 	}
 	}
 	
 	
 	public void start() {
 	public void start() {
 		// TODO Add stuff like tp people to cave if neccecary
 		// TODO Add stuff like tp people to cave if neccecary
 		if(state == GameState.WAITING){
 		if(state == GameState.WAITING){
-			endTime = System.currentTimeMillis() + 1000 * countDownTime;
+			endTime = System.currentTimeMillis() + 5000;
 			state = GameState.COUNTDOWN;
 			state = GameState.COUNTDOWN;
-			for(Player p:players){
+			for(UUID id:players){
+				Player p = plugin.getServer().getPlayer(id);
 				p.sendMessage("§cGame starting!");
 				p.sendMessage("§cGame starting!");
 			}
 			}
 		}else if(state == GameState.PAUSED){
 		}else if(state == GameState.PAUSED){
 			endTime = System.currentTimeMillis() + 1000 * duration;
 			endTime = System.currentTimeMillis() + 1000 * duration;
 			state = GameState.RUNNING;
 			state = GameState.RUNNING;
-			for(Player p:players){
-				p.sendMessage("§cGame continuing!");
+			for(UUID id:players){
+				plugin.getServer().getPlayer(id).sendMessage("§cGame continuing!");
 			}
 			}
 			startClock(0);
 			startClock(0);
 		}
 		}
@@ -127,12 +129,13 @@ public class AbbaGame implements ConfigurationSerializable{
 			public void run() {
 			public void run() {
 				switch(state){
 				switch(state){
 				case COUNTDOWN:
 				case COUNTDOWN:
-					if(endTime - System.currentTimeMillis() <= 0){
+					if((endTime - System.currentTimeMillis())/1000 <= 0){
 						state = GameState.RUNNING;
 						state = GameState.RUNNING;
 						endTime = System.currentTimeMillis() + duration * 1000;
 						endTime = System.currentTimeMillis() + duration * 1000;
 						timer.setScore(duration);
 						timer.setScore(duration);
 						
 						
-						for(Player p:players){
+						for(UUID id:players){
+							Player p = plugin.getServer().getPlayer(id);
 							p.sendMessage("§cGOGOGO!");
 							p.sendMessage("§cGOGOGO!");
 							p.setScoreboard(scoreboard);
 							p.setScoreboard(scoreboard);
 						}
 						}
@@ -140,8 +143,8 @@ public class AbbaGame implements ConfigurationSerializable{
 						
 						
 					}else{
 					}else{
 						String message = "§c" + (endTime - System.currentTimeMillis())/1000;
 						String message = "§c" + (endTime - System.currentTimeMillis())/1000;
-						for(Player p:players){
-							p.sendMessage(message);
+						for(UUID id:players){
+							plugin.getServer().getPlayer(id).sendMessage(message);
 						}
 						}
 					}
 					}
 					
 					
@@ -158,7 +161,8 @@ public class AbbaGame implements ConfigurationSerializable{
 						state = GameState.FINISHED;
 						state = GameState.FINISHED;
 						scoreboard.resetScores("Time Remaining");
 						scoreboard.resetScores("Time Remaining");
 						
 						
-						for(Player p:players){
+						for(UUID id:players){
+							Player p = plugin.getServer().getPlayer(id);
 							p.sendMessage("Game ended!");
 							p.sendMessage("Game ended!");
 							p.teleport(spawn);
 							p.teleport(spawn);
 							
 							
@@ -210,18 +214,48 @@ public class AbbaGame implements ConfigurationSerializable{
 		
 		
 		List<Integer> LeaderBoardWeights = plugin.getConfig().getIntegerList("WinWeights.Top");
 		List<Integer> LeaderBoardWeights = plugin.getConfig().getIntegerList("WinWeights.Top");
 		int AllPlayerWeight = plugin.getConfig().getInt("WinWeights.All");
 		int AllPlayerWeight = plugin.getConfig().getInt("WinWeights.All");
-		int total = AllPlayerWeight;
-		for(int weight:LeaderBoardWeights){
-			total += weight;
+		int totalWeight = 0;
+		for(int i = 0; i < Math.min(LeaderBoardWeights.size(), playerChests.size()); i++){
+			totalWeight += LeaderBoardWeights.get(i);
+		}
+		if(playerChests.size() > LeaderBoardWeights.size()){
+			totalWeight += (playerChests.size() - LeaderBoardWeights.size()) * AllPlayerWeight;
 		}
 		}
 		
 		
-		for(int i = 0; i < LeaderBoardWeights.size(); i++){
-			Inventory chestInv = playerChests.get(endStats.get(0).arg1).getInventory();
+		for(int i = 0; i < Math.min(LeaderBoardWeights.size(), playerChests.size()); i++){
+			Inventory chestInv = playerChests.get(endStats.get(i).arg1).getInventory();
+			List<ItemStack> leftoverList = new ArrayList<ItemStack>();
+			leftovers.put(endStats.get(i).arg1, leftoverList);
 			for(ItemStack stack: collectedItems){
 			for(ItemStack stack: collectedItems){
+				ItemStack newStack = stack.clone();
+				newStack.setAmount((int) Math.ceil(stack.getAmount() * LeaderBoardWeights.get(i) / totalWeight));
+				stack.setAmount(stack.getAmount() - newStack.getAmount());
+				
+				if(newStack.getAmount() != 0){
+					leftoverList.addAll(chestInv.addItem(newStack).values());
+				}
+				
+				
 				
 				
 			}
 			}
+			totalWeight -= LeaderBoardWeights.get(i);
 			
 			
-			
+		}
+		for(int i = LeaderBoardWeights.size(); i < playerChests.size(); i++){
+			Inventory chestInv = playerChests.get(endStats.get(i).arg1).getInventory();
+			List<ItemStack> leftoverList = new ArrayList<ItemStack>();
+			leftovers.put(endStats.get(i).arg1, leftoverList);
+			for(ItemStack stack: collectedItems){
+				int newAmount = (int) (stack.getAmount() / (playerChests.size() - LeaderBoardWeights.size()));
+				int left = stack.getAmount() % (playerChests.size() - LeaderBoardWeights.size());
+				if(i - LeaderBoardWeights.size() < left){
+					newAmount++;
+				}
+				ItemStack newStack = stack.clone();
+				newStack.setAmount(newAmount);
+				
+				leftoverList.addAll(chestInv.addItem(newStack).values());
+			}
 		}
 		}
 	}
 	}
 	
 	
@@ -267,9 +301,20 @@ public class AbbaGame implements ConfigurationSerializable{
 	public Location getSpawn(){
 	public Location getSpawn(){
 		return spawn;
 		return spawn;
 	}
 	}
-
-	public void addPlayer(Player p) {
-		players.add(p);
+	public JoinResult addPlayer(Player p){
+		if(!Permission.hasPermission(p, Permission.JOIN_FULL) && players.size() >= playerCap){
+			return JoinResult.FAIL_FULL;
+		}
+		if(players.size() >= chests.size()){
+			return JoinResult.FAIL_NOCHEST;
+		}
+		if(!open && !Permission.hasPermission(p, Permission.JOIN_CLOSED)){
+			return JoinResult.FAIL_CLOSED;
+		}
+		//TODO Add stuff here for whitelist aswell
+		
+		
+		players.add(p.getUniqueId());
 		int index = playerChests.size();
 		int index = playerChests.size();
 		Chest chest = chests.get(index);
 		Chest chest = chests.get(index);
 		Sign sign = signs.get(index);
 		Sign sign = signs.get(index);
@@ -277,13 +322,14 @@ public class AbbaGame implements ConfigurationSerializable{
 		sign.update();
 		sign.update();
 		playerChests.put(p.getUniqueId(), chest);
 		playerChests.put(p.getUniqueId(), chest);
 		
 		
-		
+		return JoinResult.SUCCESS;
 	}
 	}
-	public void removePlayer(Player p) {
-		players.remove(p);
+	public void removePlayer(UUID id) {
+		players.remove(id);
+		Player p = plugin.getServer().getPlayer(id);
+		p.setScoreboard(plugin.getServer().getScoreboardManager().getMainScoreboard());
 		int index = chests.indexOf(playerChests.remove(p.getUniqueId()));
 		int index = chests.indexOf(playerChests.remove(p.getUniqueId()));
-		chests.remove(index);
-		Sign sign = signs.remove(index);
+		Sign sign = signs.get(index);
 		sign.setLine(1, "");
 		sign.setLine(1, "");
 		sign.update();
 		sign.update();
 	}
 	}
@@ -309,20 +355,82 @@ public class AbbaGame implements ConfigurationSerializable{
 		
 		
 	}
 	}
 	
 	
-	
+	// ==SERIALIZATION==
 	
 	
 	//TODO Make dis serializable
 	//TODO Make dis serializable
 	
 	
 	@Override
 	@Override
 	public Map<String, Object> serialize() {
 	public Map<String, Object> serialize() {
 		Map<String, Object> abbaMap = new HashMap<String, Object>();
 		Map<String, Object> abbaMap = new HashMap<String, Object>();
-		abbaMap.put("spawn", spawn);
+		abbaMap.put("Name", name);
+		abbaMap.put("Spawn", spawn);
+		
+		abbaMap.put("Open", open);
+		switch(state){
+		case WAITING:
+		case COUNTDOWN:
+			abbaMap.put("State", "WAITING");
+			break;
+		case RUNNING:
+		case PAUSED:
+			abbaMap.put("State", "PAUSED");
+			break;
+		case FINISHED:
+			abbaMap.put("State", "FINISHED");
+			break;
+		case CONCLUDED:
+			abbaMap.put("State", "CONCLUDED");
+			break;
+		}
+		
+		abbaMap.put("Duration", duration);
+		abbaMap.put("PlayerCap", playerCap);
 		
 		
 		
 		
 		return abbaMap;
 		return abbaMap;
 	}
 	}
-	public AbbaGame deserialize(Map<String, Object> inputMap){
-		return null;
+	public static AbbaGame deserialize(Map<String, Object> inputMap){
+		AbbaGame game = new AbbaGame((String) inputMap.get("Name"), (Location) inputMap.get("Spawn"), (int) inputMap.get("Duration"), (int) inputMap.get("PlayerCap"));
+		
+		switch((String) inputMap.get("State")){
+		
+		case "PAUSED":
+			game.state = GameState.PAUSED;
+			break;
+		case "FINISHED":
+			game.state = GameState.FINISHED;
+			break;
+		case "CONCLUDED":
+			game.state = GameState.CONCLUDED;
+			break;
+		default:	// WAITING also refers back to this state
+			game.state = GameState.WAITING;
+		}
+		
+		
+		
+		
+		return game;
+	}
+	public List<UUID> getPlayerIDs() {
+		return players;
 	}
 	}
 	
 	
+	public enum JoinResult{
+		SUCCESS,
+		FAIL_FULL,
+		FAIL_CLOSED,
+		FAIL_WHITELIST,
+		FAIL_NOCHEST,
+		
+	}
+	public enum GameState {
+		WAITING, 
+		COUNTDOWN,
+		RUNNING, 
+		PAUSED, 
+		FINISHED,
+		CONCLUDED
+		
+	}
 }
 }

+ 73 - 10
src/me/lennartVH01/AbbaTools.java

@@ -1,20 +1,32 @@
 package me.lennartVH01;
 package me.lennartVH01;
 
 
 import java.util.ArrayList;
 import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
 import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+
+import me.lennartVH01.AbbaGame.JoinResult;
 
 
 import org.bukkit.Location;
 import org.bukkit.Location;
+import org.bukkit.configuration.ConfigurationSection;
 import org.bukkit.configuration.file.FileConfiguration;
 import org.bukkit.configuration.file.FileConfiguration;
+import org.bukkit.configuration.serialization.ConfigurationSerializable;
+import org.bukkit.entity.Player;
 import org.bukkit.inventory.Inventory;
 import org.bukkit.inventory.Inventory;
 import org.bukkit.inventory.ItemStack;
 import org.bukkit.inventory.ItemStack;
 
 
 
 
 
 
-public class AbbaTools {
+public class AbbaTools{
 	public static Main plugin;
 	public static Main plugin;
 	public static List<ValueItemPair> itemPairs;
 	public static List<ValueItemPair> itemPairs;
 	public static List<AbbaGame> games = new ArrayList<AbbaGame>();
 	public static List<AbbaGame> games = new ArrayList<AbbaGame>();
 	
 	
+	public static Map<UUID, AbbaGame> playerGameMap = new HashMap<UUID, AbbaGame>();
+	
+	
 	
 	
 	public static void initialize(Main plugin, List<ValueItemPair> valueItemPairs) {
 	public static void initialize(Main plugin, List<ValueItemPair> valueItemPairs) {
 		AbbaTools.plugin = plugin;
 		AbbaTools.plugin = plugin;
@@ -26,19 +38,25 @@ public class AbbaTools {
 	
 	
 	public static void create(String name, Location spawn){
 	public static void create(String name, Location spawn){
 		FileConfiguration config = plugin.getConfig();
 		FileConfiguration config = plugin.getConfig();
-		AbbaGame game = new AbbaGame(plugin, name, spawn, config.getInt("GameDuration"), config.getInt("PlayerCap"), config.getInt("CountdownTime"));
+		AbbaGame game = new AbbaGame(name, spawn, config.getInt("GameDuration"), config.getInt("PlayerCap"));
 		games.add(game);
 		games.add(game);
 	}
 	}
 	public static boolean removeAbbaGame(String name){
 	public static boolean removeAbbaGame(String name){
-		for(int i = 0; i < games.size(); i++){
-			AbbaGame game = games.get(i);
-			if(game.name.equalsIgnoreCase(name)){
-				game.destroy();
-				games.remove(i);
-				return true;
-			}
+		AbbaGame game = getAbbaGame(name);
+		return removeAbbaGame(game);
+	}
+		
+	public static boolean removeAbbaGame(AbbaGame game){
+		if(game == null){
+			return false;
 		}
 		}
-		return false;
+		
+		for(UUID id:game.getPlayerIDs()){
+			playerGameMap.remove(id);// remove people from game Map
+		}
+		game.destroy();				// remove people from internal game Map
+		games.remove(game);
+		return true;
 	}
 	}
 	public static AbbaGame getAbbaGame(String name){
 	public static AbbaGame getAbbaGame(String name){
 		for(AbbaGame game:games){
 		for(AbbaGame game:games){
@@ -56,6 +74,33 @@ public class AbbaTools {
 			return null;
 			return null;
 		}
 		}
 	}
 	}
+	public static AbbaGame getAbbaGame(Player p){
+		return playerGameMap.get(p.getUniqueId());
+	}
+	
+	public static AbbaGame leave(UUID id){
+		AbbaGame game = playerGameMap.remove(plugin.getServer().getPlayer(id).getUniqueId());
+		if(game != null){
+			game.removePlayer(id);
+			return game;
+		}
+		return null;
+	}
+	public static AbbaGame.JoinResult join(Player p, AbbaGame game){
+		leave(p.getUniqueId());
+		
+		JoinResult result = game.addPlayer(p);
+		if(result == JoinResult.SUCCESS){
+			playerGameMap.put(p.getUniqueId(), game);
+		}
+		return result;
+		
+	}
+	public static boolean isInGame(Player p){
+		return playerGameMap.containsKey(p.getUniqueId());
+	}
+	
+	
 	public static ItemStack[] getContraband(Inventory i){
 	public static ItemStack[] getContraband(Inventory i){
 		return null;
 		return null;
 	}
 	}
@@ -84,5 +129,23 @@ public class AbbaTools {
 	}
 	}
 
 
 
 
+
+	public static List<Map<String, Object>> serialize() {
+		List<Map<String, Object>> serializedGames = new ArrayList<Map<String, Object>>();
+		for(AbbaGame game:games){
+			serializedGames.add(game.serialize());
+		}
+		return serializedGames;
+	}
+	public static void deserialize(List<AbbaGame> gameList){
+		playerGameMap = new HashMap<UUID, AbbaGame>();
+		for(AbbaGame game:gameList){
+			games.add(game);
+			for(UUID id:game.getPlayerIDs()){
+				playerGameMap.put(id, game);
+			}
+		}
+	}
+	
 	
 	
 }
 }

+ 1 - 4
src/me/lennartVH01/EventListener.java

@@ -12,10 +12,7 @@ public class EventListener implements Listener{
 	}
 	}
 	@EventHandler
 	@EventHandler
 	public void onPlayerQuit(PlayerQuitEvent e){
 	public void onPlayerQuit(PlayerQuitEvent e){
-		AbbaGame game = plugin.playerMap.remove(e.getPlayer().getUniqueId());
-		if(game != null){
-			game.removePlayer(e.getPlayer());
-		}
+		AbbaTools.leave(e.getPlayer().getUniqueId());
 	}
 	}
 	
 	
 	
 	

+ 0 - 9
src/me/lennartVH01/GameState.java

@@ -1,9 +0,0 @@
-package me.lennartVH01;
-
-public enum GameState {
-	WAITING, 
-	COUNTDOWN,
-	RUNNING, 
-	PAUSED, 
-	FINISHED
-}

+ 8 - 12
src/me/lennartVH01/ItemUtils.java

@@ -1,11 +1,7 @@
 package me.lennartVH01;
 package me.lennartVH01;
 
 
-import java.util.ArrayList;
-import java.util.List;
 import java.util.Map;
 import java.util.Map;
 
 
-import org.bukkit.inventory.ItemStack;
-
 
 
 public class ItemUtils {
 public class ItemUtils {
 	public static boolean isMapInMap(Map<String, Object> testMap, Map<String, Object> elderMap){
 	public static boolean isMapInMap(Map<String, Object> testMap, Map<String, Object> elderMap){
@@ -21,23 +17,23 @@ public class ItemUtils {
 		}
 		}
 		return true;
 		return true;
 	}
 	}
-	public static ItemStack[] getItemStacks(ItemStack item){
-		return getItemStacks(item, item.getAmount());
+	/*public static ItemStack[] takeOffItems(ItemStack item){
+		return takeOffItems(item, item.getAmount());
 	}
 	}
-	public static ItemStack[] getItemStacks(ItemStack item, int maxTakeOff){
-		ItemStack[] stacks = new ItemStack[(maxTakeOff+63) / 64]; //take ceil of itemStack size/64
+	public static ItemStack[] takeOffItems(ItemStack item, int maxTakeOff){
+		ItemStack[] stacks = new ItemStack[(maxTakeOff+item.getMaxStackSize()-1) / item.getMaxStackSize()]; //take ceil of itemStack size/64
 		
 		
 		item.setAmount(item.getAmount() - maxTakeOff);
 		item.setAmount(item.getAmount() - maxTakeOff);
-		for(int i = 0; i < maxTakeOff / 64; i++){
+		for(int i = 0; i < maxTakeOff / item.getMaxStackSize(); i++){
 			stacks[i] = item.clone();
 			stacks[i] = item.clone();
-			stacks[i].setAmount(64);
+			stacks[i].setAmount(item.getMaxStackSize());
 		}
 		}
-		int remainder = maxTakeOff % 64;
+		int remainder = maxTakeOff % item.getMaxStackSize();
 		if(remainder != 0){
 		if(remainder != 0){
 			stacks[stacks.length] = item.clone();
 			stacks[stacks.length] = item.clone();
 			stacks[stacks.length].setAmount(remainder);
 			stacks[stacks.length].setAmount(remainder);
 			
 			
 		}
 		}
 		return stacks;
 		return stacks;
-	}
+	}*/
 }
 }

+ 52 - 38
src/me/lennartVH01/Main.java

@@ -2,11 +2,12 @@ package me.lennartVH01;
 
 
 import java.util.ArrayList;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.Date;
-import java.util.HashMap;
 import java.util.HashSet;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Map;
 import java.util.List;
 import java.util.List;
-import java.util.UUID;
+import java.util.Set;
+import java.io.File;
+import java.io.IOException;
 import java.text.DateFormat;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.text.SimpleDateFormat;
 
 
@@ -21,6 +22,7 @@ import org.bukkit.block.Sign;
 import org.bukkit.command.Command;
 import org.bukkit.command.Command;
 import org.bukkit.command.CommandSender;
 import org.bukkit.command.CommandSender;
 import org.bukkit.configuration.file.FileConfiguration;
 import org.bukkit.configuration.file.FileConfiguration;
+import org.bukkit.configuration.file.YamlConfiguration;
 import org.bukkit.configuration.serialization.ConfigurationSerialization;
 import org.bukkit.configuration.serialization.ConfigurationSerialization;
 import org.bukkit.entity.Player;
 import org.bukkit.entity.Player;
 import org.bukkit.inventory.ItemStack;
 import org.bukkit.inventory.ItemStack;
@@ -38,8 +40,6 @@ public class Main extends JavaPlugin{
 	
 	
 	
 	
 	
 	
-	public Map<UUID, AbbaGame> playerMap = new HashMap<UUID, AbbaGame>();
-	
 	public List<AbbaGame> ongoingGames = new ArrayList<AbbaGame>();
 	public List<AbbaGame> ongoingGames = new ArrayList<AbbaGame>();
 	
 	
 	public final String[] abbaSubCommands = new String[]{"calc", "close", "config", "create", "info", "join", "leave", "list", "open", "reload", "remove", "start"};
 	public final String[] abbaSubCommands = new String[]{"calc", "close", "config", "create", "info", "join", "leave", "list", "open", "reload", "remove", "start"};
@@ -51,15 +51,16 @@ public class Main extends JavaPlugin{
 	
 	
 	public EventListener evtListener = new EventListener();
 	public EventListener evtListener = new EventListener();
 	
 	
+	
 	@Override
 	@Override
 	public void onEnable(){
 	public void onEnable(){
+		evtListener.initialize(this);
+		AbbaGame.initialize(this);
 		ConfigurationSerialization.registerClass(AbbaGame.class);
 		ConfigurationSerialization.registerClass(AbbaGame.class);
 		
 		
-		
-		
 		config = this.getConfig();
 		config = this.getConfig();
 		// Event handler
 		// Event handler
-		evtListener.initialize(this);
+		
 		getServer().getPluginManager().registerEvents(evtListener, this);
 		getServer().getPluginManager().registerEvents(evtListener, this);
 		
 		
 		
 		
@@ -88,11 +89,26 @@ public class Main extends JavaPlugin{
 		
 		
 		AbbaTools.initialize(this, valueItemPairs);
 		AbbaTools.initialize(this, valueItemPairs);
 		
 		
-		
+		FileConfiguration persist = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "persist.yml"));
+		@SuppressWarnings("unchecked")
+		List<AbbaGame> abbaList = (List<AbbaGame>) persist.getList("Games");
+		AbbaTools.deserialize((List<AbbaGame>) abbaList); 
 	}
 	}
 	@Override
 	@Override
 	public void onDisable(){
 	public void onDisable(){
+		File persistFile = new File(getDataFolder(), "persist.yml");
+		
+		
+		FileConfiguration persist = new YamlConfiguration();
+		
+		persist.set("Games", AbbaTools.getGames());
 		
 		
+		
+		try{
+			persist.save(persistFile);
+		}catch(IOException e){
+			System.out.println("[ERROR] Could not save to persist.yml! Reason:" + e.getMessage());
+		}
 	}
 	}
 	
 	
 	public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args){
 	public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args){
@@ -133,19 +149,14 @@ public class Main extends JavaPlugin{
 							}
 							}
 						}
 						}
 						
 						
-						AbbaGame oldGame = playerMap.get(p.getUniqueId());
+						AbbaGame oldGame = AbbaTools.getAbbaGame(p);
 						if(oldGame != null){
 						if(oldGame != null){
-							if(p.hasPermission("AbbaCaving.leave")){
-								oldGame.removePlayer(p);
-								playerMap.remove(p.getUniqueId());
-								p.sendMessage("Left game \"" + oldGame.getName() + "\"");
-							}else{
-								p.sendMessage("§cYou don't have permission to leave the current game!");
-							}
+							
+							p.sendMessage("Left game \"" + oldGame.getName() + "\"");
+							
 						}
 						}
 						
 						
-						game.addPlayer(p);
-						playerMap.put(p.getUniqueId(), game);
+						AbbaTools.join(p, game);
 						p.sendMessage("Joined game \"" + game.getName() + "\"");
 						p.sendMessage("Joined game \"" + game.getName() + "\"");
 						
 						
 						p.teleport(game.getSpawn());
 						p.teleport(game.getSpawn());
@@ -166,11 +177,14 @@ public class Main extends JavaPlugin{
 				if(sender instanceof Player){
 				if(sender instanceof Player){
 					if(sender.hasPermission("AbbaCaving.leave")){
 					if(sender.hasPermission("AbbaCaving.leave")){
 						Player p = (Player) sender;
 						Player p = (Player) sender;
-						AbbaGame game = playerMap.get(p.getUniqueId());
-						game.removePlayer(p);
-						playerMap.remove(p.getUniqueId());
-						p.sendMessage("Left game \"" + game.getName() + "\"");
-						return true;
+						AbbaGame game = AbbaTools.leave(p.getUniqueId());
+						if(game != null){
+							p.sendMessage("Left game \"" + game.getName() + "\"");
+							return true;
+						}else{
+							p.sendMessage("§cYou aren't in a game right now!");
+							return false;
+						}
 					}else{
 					}else{
 						sender.sendMessage(Messages.noPermissionError);
 						sender.sendMessage(Messages.noPermissionError);
 						return false;
 						return false;
@@ -271,7 +285,7 @@ public class Main extends JavaPlugin{
 					}else{
 					}else{
 						game = AbbaTools.getAbbaGame();
 						game = AbbaTools.getAbbaGame();
 					}
 					}
-					if(game.getState() != GameState.FINISHED){
+					if(game.getState() == AbbaGame.GameState.FINISHED){
 						game.calcScores();
 						game.calcScores();
 						
 						
 					}else{
 					}else{
@@ -461,6 +475,7 @@ public class Main extends JavaPlugin{
 						sender.sendMessage("§cGame already running!");
 						sender.sendMessage("§cGame already running!");
 						return false;
 						return false;
 					case FINISHED:
 					case FINISHED:
+					case CONCLUDED:
 						break;
 						break;
 					}
 					}
 					
 					
@@ -495,7 +510,7 @@ public class Main extends JavaPlugin{
 										sender.sendMessage("§cPlease specify a stricktly positive number");
 										sender.sendMessage("§cPlease specify a stricktly positive number");
 										return false;
 										return false;
 									}
 									}
-									if(game.getState() == GameState.RUNNING || game.getState() == GameState.PAUSED){
+									if(game.getState() == AbbaGame.GameState.RUNNING || game.getState() == AbbaGame.GameState.PAUSED){
 										game.setEndTime(System.currentTimeMillis() + newTime * 1000);
 										game.setEndTime(System.currentTimeMillis() + newTime * 1000);
 									}else{
 									}else{
 										game.setDuration(newTime);
 										game.setDuration(newTime);
@@ -509,21 +524,20 @@ public class Main extends JavaPlugin{
 								}
 								}
 							case "addchest":
 							case "addchest":
 								if(sender instanceof Player){
 								if(sender instanceof Player){
-									Block blockInFront = ((Player) sender).getTargetBlock(transparentBlocks, 5);
-									if(blockInFront.getState() instanceof Chest){
-										for(BlockFace face:new BlockFace[]{BlockFace.UP, BlockFace.DOWN, BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST}){
-											Block blockNextTo = blockInFront.getRelative(face);
-											if(blockNextTo.getState() instanceof Sign){
-												if(AbbaTools.getAbbaGame(args[1]).addChest((Chest) blockInFront.getState(), (Sign) blockNextTo.getState())){
-													sender.sendMessage("Chest created");
-													return true;
-												}else{
-													sender.sendMessage("§cChest already in a game!");
-													return false;
-												}
+									Block blockInFront = ((Player) sender).getTargetBlock((Set<Material>) null, 5);
+									if(BlockUtils.isSign(blockInFront)){
+										Block blockNextTo = BlockUtils.getAttachedBlock(blockInFront);
+										if(BlockUtils.isChest(blockNextTo)){
+											if(AbbaTools.getAbbaGame(args[1]).addChest((Chest) blockNextTo.getState(), (Sign) blockInFront.getState())){
+												sender.sendMessage("Chest created");
+												return true;
+											}else{
+												sender.sendMessage("§cChest already in a game!");
+												return false;
 											}
 											}
 										}
 										}
 										
 										
+										
 										sender.sendMessage("§cPut a sign next to the chest!");
 										sender.sendMessage("§cPut a sign next to the chest!");
 										return false;
 										return false;
 									}else{
 									}else{
@@ -577,7 +591,7 @@ public class Main extends JavaPlugin{
 				break;
 				break;
 			case 2:
 			case 2:
 				for(AbbaGame game:AbbaTools.getGames()){
 				for(AbbaGame game:AbbaTools.getGames()){
-					if(game.getName().startsWith(args[1])){
+					if(game.getName().toLowerCase().startsWith(args[1].toLowerCase())){
 						cmds.add(game.getName());
 						cmds.add(game.getName());
 					}
 					}
 				}
 				}

+ 26 - 0
src/me/lennartVH01/Permission.java

@@ -0,0 +1,26 @@
+package me.lennartVH01;
+
+import org.bukkit.command.CommandSender;
+
+
+
+public enum Permission{
+	
+	JOIN_FULL("AbbaCaving.joinFull"),
+	JOIN_CLOSED("AbbaCaving.joinClosed"),
+	JOIN_WHITELISTED("AbbaCaving.joinWhilelisted");
+	
+	private final String permission;
+	private Permission(String permission){
+		this.permission = permission;
+	}
+	public static boolean hasPermission(CommandSender sender, Permission perm){
+		return hasPermission(sender, perm.permission);
+	}
+	public static boolean hasPermission(CommandSender sender, String perm){
+		return sender.hasPermission(perm);
+	}
+	public String toString(){
+		return permission;
+	}
+}

+ 8 - 8
src/me/lennartVH01/Tuple2.java

@@ -1,22 +1,22 @@
 package me.lennartVH01;
 package me.lennartVH01;
 
 
-public class Tuple2<T1,T2>{
-	T1 arg1;
-	T2 arg2;
-	public Tuple2(T1 arg1, T2 arg2){
+public class Tuple2<S,U>{
+	S arg1;
+	U arg2;
+	public Tuple2(S arg1, U arg2){
 		this.arg1 = arg1;
 		this.arg1 = arg1;
 		this.arg2 = arg2;
 		this.arg2 = arg2;
 	}
 	}
-	public T1 getArg1() {
+	public S getArg1() {
 		return arg1;
 		return arg1;
 	}
 	}
-	public void setArg1(T1 arg1) {
+	public void setArg1(S arg1) {
 		this.arg1 = arg1;
 		this.arg1 = arg1;
 	}
 	}
-	public T2 getArg2() {
+	public U getArg2() {
 		return arg2;
 		return arg2;
 	}
 	}
-	public void setArg2(T2 arg2) {
+	public void setArg2(U arg2) {
 		this.arg2 = arg2;
 		this.arg2 = arg2;
 	}
 	}
 }
 }