فهرست منبع

More work on Serialization & A bit of optimization

lennartVH01 9 سال پیش
والد
کامیت
dc56d3171e

+ 207 - 50
src/me/lennartVH01/AbbaGame.java

@@ -2,6 +2,7 @@ package me.lennartVH01;
 
 
 
+import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
 import java.util.List;
@@ -11,9 +12,11 @@ import java.util.UUID;
 
 import org.bukkit.Bukkit;
 import org.bukkit.Location;
+import org.bukkit.block.Block;
 import org.bukkit.block.Chest;
 import org.bukkit.block.Sign;
 import org.bukkit.configuration.serialization.ConfigurationSerializable;
+import org.bukkit.entity.HumanEntity;
 import org.bukkit.entity.Player;
 import org.bukkit.inventory.Inventory;
 import org.bukkit.inventory.ItemStack;
@@ -23,6 +26,7 @@ import org.bukkit.scoreboard.Score;
 import org.bukkit.scoreboard.Scoreboard;
 
 
+
 public class AbbaGame implements ConfigurationSerializable{
 	private static Comparator<Tuple2<UUID, CalculatedScore>> scoreComparer = new Comparator<Tuple2<UUID, CalculatedScore>>() {
 
@@ -45,11 +49,13 @@ public class AbbaGame implements ConfigurationSerializable{
 	public int playerCap;
 	public List<UUID> players;
 	public List<Tuple2<UUID, CalculatedScore>> endStats = new ArrayList<Tuple2<UUID, CalculatedScore>>();
-	public Map<UUID, Chest> playerChests = new HashMap<UUID, Chest>();
+	
+	AbbaChestPlayerList chestList;
+	
+	
 	public Map<UUID, List<ItemStack>> leftovers = new HashMap<UUID, List<ItemStack>>();
 	public List<ItemStack> collectedItems = new ArrayList<ItemStack>();
-	public List<Chest> chests = new ArrayList<Chest>();
-	public List<Sign> signs = new ArrayList<Sign>();
+	
 	private Scoreboard scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
 	private Objective abbaObjective = scoreboard.registerNewObjective("AbbaStats", "dummy");
 	private Score timer = abbaObjective.getScore("Time Remaining");
@@ -69,6 +75,8 @@ public class AbbaGame implements ConfigurationSerializable{
 		}
 		abbaObjective.setDisplaySlot(DisplaySlot.SIDEBAR);
 		
+		chestList = new AbbaChestPlayerList(playerCap);
+		
 		for(ValueItemPair pair:AbbaTools.itemPairs){
 			ItemStack stack = pair.getItemStack().clone();
 			stack.setAmount(0);
@@ -78,7 +86,8 @@ public class AbbaGame implements ConfigurationSerializable{
 	
 	
 	public void destroy(){
-		for(Sign s:signs){
+		for(AbbaChest chestSignPair:chestList.getChests()){
+			Sign s = chestSignPair.getSign();
 			s.setLine(0, "");
 			//s.setLine(1, "");
 			//s.setLine(2, "");
@@ -186,11 +195,13 @@ public class AbbaGame implements ConfigurationSerializable{
 	}
 	
 	public void calcScores() {
-		for(UUID uuid:playerChests.keySet()){
-			Chest chest = playerChests.get(uuid);
-			Sign sign = signs.get(chests.indexOf(chest));
+		for(AbbaChest abbaChest:chestList.getOccupiedChests()){
+			UUID id = abbaChest.getOwnerId();
 			
-			Player p = plugin.getServer().getPlayer(uuid);
+			Chest chest = abbaChest.getChest();
+			Sign sign = abbaChest.getSign();
+			
+			Player p = plugin.getServer().getPlayer(id);
 			
 			Score score = abbaObjective.getScore(p.getName());
 			CalculatedScore stat = AbbaTools.calcScore(chest.getInventory());
@@ -215,15 +226,16 @@ public class AbbaGame implements ConfigurationSerializable{
 		List<Integer> LeaderBoardWeights = plugin.getConfig().getIntegerList("WinWeights.Top");
 		int AllPlayerWeight = plugin.getConfig().getInt("WinWeights.All");
 		int totalWeight = 0;
-		for(int i = 0; i < Math.min(LeaderBoardWeights.size(), playerChests.size()); i++){
+		for(int i = 0; i < Math.min(LeaderBoardWeights.size(), players.size()); i++){
 			totalWeight += LeaderBoardWeights.get(i);
 		}
-		if(playerChests.size() > LeaderBoardWeights.size()){
-			totalWeight += (playerChests.size() - LeaderBoardWeights.size()) * AllPlayerWeight;
+		if(players.size() > LeaderBoardWeights.size()){
+			totalWeight += (players.size() - LeaderBoardWeights.size()) * AllPlayerWeight;
 		}
 		
-		for(int i = 0; i < Math.min(LeaderBoardWeights.size(), playerChests.size()); i++){
-			Inventory chestInv = playerChests.get(endStats.get(i).arg1).getInventory();
+		for(int i = 0; i < Math.min(LeaderBoardWeights.size(), players.size()); i++){
+			Inventory chestInv = chestList.get(endStats.get(i).arg1).getChest().getInventory();
+			
 			List<ItemStack> leftoverList = new ArrayList<ItemStack>();
 			leftovers.put(endStats.get(i).arg1, leftoverList);
 			for(ItemStack stack: collectedItems){
@@ -241,13 +253,14 @@ public class AbbaGame implements ConfigurationSerializable{
 			totalWeight -= LeaderBoardWeights.get(i);
 			
 		}
-		for(int i = LeaderBoardWeights.size(); i < playerChests.size(); i++){
-			Inventory chestInv = playerChests.get(endStats.get(i).arg1).getInventory();
+		int playerChestSize = chestList.getOccupiedChests().size();
+		for(int i = LeaderBoardWeights.size(); i < playerChestSize; i++){
+			Inventory chestInv = chestList.get(endStats.get(i).arg1).getChest().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());
+				int newAmount = (int) (stack.getAmount() / (playerChestSize - LeaderBoardWeights.size()));
+				int left = stack.getAmount() % (playerChestSize - LeaderBoardWeights.size());
 				if(i - LeaderBoardWeights.size() < left){
 					newAmount++;
 				}
@@ -259,26 +272,22 @@ public class AbbaGame implements ConfigurationSerializable{
 		}
 	}
 	
-	public void open(){
-		open = true;
-	}
-	public void close(){
-		open = false;
-	}
+	
 	public boolean addChest(Chest chest, Sign sign){
-		if(!chests.contains(chest)){
-			chests.add(chest);
-			signs.add(sign);
-			sign.setLine(0, "§9[" + name + "]");
-			sign.update();
-			
-			return true;
+		if(chestList.contains(chest)){
+			return false;
 		}
-		return false;
+		chestList.addEmptyChest(new AbbaChest(chest, sign));
+		sign.setLine(0, "§9[" + name + "]");
+		sign.update();
+		
+		return true;
 	}
 	
 	
-	
+	public void setOpen(boolean open){
+		this.open = open;
+	}
 	public boolean isOpen(){
 		return open;
 	}
@@ -289,8 +298,8 @@ public class AbbaGame implements ConfigurationSerializable{
 		return players.size();
 	}
 	public int getMaxPlayers(){
-		if(playerCap == -1 || chests.size() < playerCap){
-			return chests.size();
+		if(playerCap == -1 || chestList.size() < playerCap){
+			return chestList.size();
 		}else{
 			return playerCap;
 		}
@@ -301,11 +310,18 @@ public class AbbaGame implements ConfigurationSerializable{
 	public Location getSpawn(){
 		return spawn;
 	}
+	public void onChestOpen(Inventory inv, HumanEntity player) {
+		
+	}
+	
+	
+	
+	
 	public JoinResult addPlayer(Player p){
 		if(!Permission.hasPermission(p, Permission.JOIN_FULL) && players.size() >= playerCap){
 			return JoinResult.FAIL_FULL;
 		}
-		if(players.size() >= chests.size()){
+		if(players.size() >= chestList.size()){
 			return JoinResult.FAIL_NOCHEST;
 		}
 		if(!open && !Permission.hasPermission(p, Permission.JOIN_CLOSED)){
@@ -313,25 +329,23 @@ public class AbbaGame implements ConfigurationSerializable{
 		}
 		//TODO Add stuff here for whitelist aswell
 		
-		
+		AbbaChest claimedChest = chestList.claimChest(p.getUniqueId());
 		players.add(p.getUniqueId());
-		int index = playerChests.size();
-		Chest chest = chests.get(index);
-		Sign sign = signs.get(index);
-		sign.setLine(1, p.getName());
-		sign.update();
-		playerChests.put(p.getUniqueId(), chest);
+		
+		
+		claimedChest.getSign().setLine(1, p.getName());
+		claimedChest.getSign().update();
+		
 		
 		return JoinResult.SUCCESS;
 	}
 	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()));
-		Sign sign = signs.get(index);
-		sign.setLine(1, "");
-		sign.update();
+		plugin.getServer().getPlayer(id).setScoreboard(plugin.getServer().getScoreboardManager().getMainScoreboard());
+		AbbaChest aChest = chestList.freeChest(id);
+		
+		aChest.getSign().setLine(1, "");
+		aChest.getSign().update();
 	}
 	
 
@@ -345,8 +359,8 @@ public class AbbaGame implements ConfigurationSerializable{
 	
 	
 	
-	public void setDuration(long newDuration) {
-		duration = (int) newDuration;
+	public void setDuration(int duration) {
+		this.duration = duration;
 		
 	}
 
@@ -386,12 +400,43 @@ public class AbbaGame implements ConfigurationSerializable{
 		abbaMap.put("Duration", duration);
 		abbaMap.put("PlayerCap", playerCap);
 		
+		List<String> playerChestList = new ArrayList<String>();
+		for(AbbaChest aChest:chestList.getOccupiedChests()){
+			playerChestList.add(String.format("%d;%d;%d;%s", aChest.getChest().getX(), aChest.getChest().getY(), aChest.getChest().getZ(), aChest.getOwnerId().toString()));
+		}
+		for(AbbaChest aChest:chestList.getFreeChests()){
+			playerChestList.add(String.format("%d;%d;%d", aChest.getChest().getX(), aChest.getChest().getY(), aChest.getChest().getZ()));
+		}
+		abbaMap.put("Chests", playerChestList);
+		//TODO SERIALIZE PLAYERS
+		
+		
 		
 		return abbaMap;
 	}
+	
+	@SuppressWarnings("unchecked")
 	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"));
 		
+		game.setOpen((boolean) inputMap.get("Open"));
+		game.setDuration((int) inputMap.get("Duration"));
+		game.setPlayerCap((int) inputMap.get("PlayerCap"));
+		
+		for(String input:(List<String>) inputMap.get("Chests")){
+			String[] args = input.split(";");
+			Block sign = game.spawn.getWorld().getBlockAt(Integer.parseInt(args[0]), Integer.parseInt(args[1]), Integer.parseInt(args[2]));
+			
+			AbbaChest aChest = game.new AbbaChest((Chest) BlockUtils.getAttachedBlock(sign), (Sign) sign);
+			if(args.length >= 4){
+				UUID id = UUID.fromString(args[3]);
+				game.chestList.addEmptyChest(aChest);
+				game.chestList.claimChest(id);
+				game.players.add(id);
+				
+			}
+		}
+		
 		switch((String) inputMap.get("State")){
 		
 		case "PAUSED":
@@ -412,6 +457,12 @@ public class AbbaGame implements ConfigurationSerializable{
 		
 		return game;
 	}
+	public void setPlayerCap(int playerCap) {
+		this.playerCap = playerCap;
+	}
+	public int getPlayerCap(){
+		return playerCap;
+	}
 	public List<UUID> getPlayerIDs() {
 		return players;
 	}
@@ -433,4 +484,110 @@ public class AbbaGame implements ConfigurationSerializable{
 		CONCLUDED
 		
 	}
+	
+	
+	
+	public class AbbaChest{
+		private Chest chest;
+		private Sign sign;
+		private UUID ownerId;
+		public AbbaChest(Chest chest, Sign sign){
+			this.chest = chest;
+			this.sign = sign;
+		}
+		
+		
+		
+		
+		public Chest getChest(){return chest;}
+		public void setChest(Chest chest){this.chest = chest;}
+		public Sign getSign(){return sign;}
+		public void setSign(Sign sign){this.sign = sign;}
+		public UUID getOwnerId(){return ownerId;}
+		public void setOwnerId(UUID ownerId){this.ownerId = ownerId;}
+	}
+	
+	public class AbbaChestPlayerList{
+		private int playerLength = 0;
+		private List<AbbaChest> chests;
+		//creates An ArrayList with two "sublists, each building toward eachother"
+		public AbbaChestPlayerList(int initialSize){
+			chests = new ArrayList<AbbaChest>(initialSize);
+		}
+		
+		
+		public int size() {
+			return chests.size();
+		}
+
+
+		public AbbaChest claimChest(UUID id){
+			AbbaChest chest = chests.get(playerLength);
+			chest.setOwnerId(id);
+			playerLength++;
+			return chest;
+		}
+		public AbbaChest freeChest(UUID id){
+			for(int i = 0; i < playerLength; i++){
+				AbbaChest chest = chests.get(i);
+				if(chest.getOwnerId().equals(id)){
+					chest.setOwnerId(null);
+					playerLength--;
+					Collections.swap(chests, i, playerLength);
+					return chest;
+				}
+			}
+			return null;
+		}
+		public void remove(AbbaChest chest){
+			chests.remove(chest);
+		}
+		public void addEmptyChest(AbbaChest c){
+			chests.add(c);
+		}
+		public List<AbbaChest> getOccupiedChests(){
+			return chests.subList(0, playerLength);
+		}
+		public List<AbbaChest> getFreeChests(){
+			return chests.subList(playerLength, chests.size());
+		}
+		public List<AbbaChest> getChests(){
+			return chests;
+		}
+		public AbbaChest get(int i){
+			return chests.get(i);
+		}
+		public AbbaChest get(UUID id){
+			for(int i = 0; i < playerLength; i++){
+				if(chests.get(i).getOwnerId().equals(id)){
+					return chests.get(i);
+				}
+			}
+			return null;
+		}
+		public boolean contains(AbbaChest abbaChest){
+			for(AbbaChest aChest:chests){
+				if(aChest.equals(abbaChest)){
+					return true;
+				}
+			}
+			return false;
+		}
+		public boolean contains(Chest chest){
+			for(AbbaChest aChest:chests){
+				if(chest.equals(aChest.getChest())){
+					return true;
+				}
+			}
+			return false;
+		}
+		public boolean contains(Sign sign){
+			for(AbbaChest aChest:chests){
+				if(sign.equals(aChest.getSign())){
+					return true;
+				}
+			}
+			return false;
+		}
+	}
 }

+ 18 - 16
src/me/lennartVH01/AbbaTools.java

@@ -2,7 +2,6 @@ package me.lennartVH01;
 
 import java.util.ArrayList;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.UUID;
@@ -10,10 +9,11 @@ import java.util.UUID;
 import me.lennartVH01.AbbaGame.JoinResult;
 
 import org.bukkit.Location;
-import org.bukkit.configuration.ConfigurationSection;
 import org.bukkit.configuration.file.FileConfiguration;
-import org.bukkit.configuration.serialization.ConfigurationSerializable;
 import org.bukkit.entity.Player;
+import org.bukkit.event.block.BlockBreakEvent;
+import org.bukkit.event.block.SignChangeEvent;
+import org.bukkit.event.inventory.InventoryOpenEvent;
 import org.bukkit.inventory.Inventory;
 import org.bukkit.inventory.ItemStack;
 
@@ -22,9 +22,10 @@ import org.bukkit.inventory.ItemStack;
 public class AbbaTools{
 	public static Main plugin;
 	public static List<ValueItemPair> itemPairs;
-	public static List<AbbaGame> games = new ArrayList<AbbaGame>();
+	private static List<AbbaGame> games = new ArrayList<AbbaGame>();
 	
-	public static Map<UUID, AbbaGame> playerGameMap = new HashMap<UUID, AbbaGame>();
+	private static Map<UUID, AbbaGame> playerGameMap = new HashMap<UUID, AbbaGame>();
+	//private static Map<Location, AbbaGame> chestGameMap = new HashMap<Location, AbbaGame>();
 	
 	
 	
@@ -127,20 +128,20 @@ public class AbbaTools{
 	public static List<AbbaGame> getGames() {
 		return games;
 	}
-
-
-
-	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 onChestOpen(InventoryOpenEvent e){
+		
 	}
+	public static void onSignBreak(BlockBreakEvent e){
+		
+	}
+	public static void onSignPlace(SignChangeEvent e){
+		
+	}
+	
+	
 	public static void deserialize(List<AbbaGame> gameList){
-		playerGameMap = new HashMap<UUID, AbbaGame>();
+		games = gameList;
 		for(AbbaGame game:gameList){
-			games.add(game);
 			for(UUID id:game.getPlayerIDs()){
 				playerGameMap.put(id, game);
 			}
@@ -148,4 +149,5 @@ public class AbbaTools{
 	}
 	
 	
+	
 }

+ 22 - 0
src/me/lennartVH01/EventListener.java

@@ -1,7 +1,12 @@
 package me.lennartVH01;
 
+import org.bukkit.block.Block;
 import org.bukkit.event.EventHandler;
 import org.bukkit.event.Listener;
+import org.bukkit.event.block.BlockBreakEvent;
+import org.bukkit.event.block.SignChangeEvent;
+import org.bukkit.event.inventory.InventoryOpenEvent;
+import org.bukkit.event.inventory.InventoryType;
 import org.bukkit.event.player.PlayerQuitEvent;
 
 public class EventListener implements Listener{
@@ -15,6 +20,23 @@ public class EventListener implements Listener{
 		AbbaTools.leave(e.getPlayer().getUniqueId());
 	}
 	
+	@EventHandler(ignoreCancelled = true)
+	public void onChestOpen(InventoryOpenEvent e){
+		if(e.getInventory().getType() == InventoryType.CHEST){
+			AbbaTools.onChestOpen(e);
+		}
+	}
 	
+	@EventHandler(ignoreCancelled = true)
+	public void onSignPlace(SignChangeEvent e){
+		AbbaTools.onSignPlace(e);
+	}
 	
+	@EventHandler(ignoreCancelled = true)
+	public void onSignBreak(BlockBreakEvent e){
+		Block brokenBlock = e.getBlock();
+		if(BlockUtils.isSign(brokenBlock)){
+			AbbaTools.onSignBreak(e);
+		}
+	}
 }

+ 2 - 3
src/me/lennartVH01/Main.java

@@ -16,7 +16,6 @@ import org.bukkit.Location;
 import org.bukkit.Material;
 import org.bukkit.World;
 import org.bukkit.block.Block;
-import org.bukkit.block.BlockFace;
 import org.bukkit.block.Chest;
 import org.bukkit.block.Sign;
 import org.bukkit.command.Command;
@@ -398,7 +397,7 @@ public class Main extends JavaPlugin{
 							return false;
 						}
 					}
-					game.open();
+					game.setOpen(true);
 					sender.sendMessage("Opened game \"" + args[1] + "\"");
 					return true;
 				}else{
@@ -424,7 +423,7 @@ public class Main extends JavaPlugin{
 							return false;
 						}
 					}
-					game.close();
+					game.setOpen(false);
 					sender.sendMessage("Closed game \"" + args[1] + "\"");
 					return true;
 				}else{

+ 30 - 0
src/me/lennartVH01/Tuple3.java

@@ -0,0 +1,30 @@
+package me.lennartVH01;
+
+public class Tuple3<S, U, V>{
+	S arg1;
+	U arg2;
+	V arg3;
+	public Tuple3(S arg1, U arg2, V arg3){
+		this.arg1 = arg1;
+		this.arg2 = arg2;
+		this.arg3 = arg3;
+	}
+	public S getArg1() {
+		return arg1;
+	}
+	public void setArg1(S arg1) {
+		this.arg1 = arg1;
+	}
+	public U getArg2() {
+		return arg2;
+	}
+	public void setArg2(U arg2) {
+		this.arg2 = arg2;
+	}
+	public V getArg3() {
+		return arg3;
+	}
+	public void setArg3(V arg3) {
+		this.arg3 = arg3;
+	}
+}