Explorar o código

Contraband Scanner

lennartVH01 %!s(int64=9) %!d(string=hai) anos
pai
achega
05f85ad446

+ 20 - 11
config.yml

@@ -16,14 +16,29 @@ NotifyTimes:
 # Max players per game, users with permission AbbaCaving.joinFull can still join
 PlayerCap: 10
 
+
+
+
+# Configures if the items should be redistributed according to the settings below or that the redistribution is done by the admin
+# true -> Plugin automatically gives each player their cut
+# false -> only displays player scores in scoreboard
+RedistributeItems: true
+
+# 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
+
+
 # Ratio of spoils given per player: 
 #   All: Amount to be spread over all participants
 #   Pot: Amount to be added to general Pot
 #   Top: first n players receive these chunks of total wins
+# Part of the winnings that go to each player
+WinWeights:
+  All: 1
+  Top:
+  - 7
+  - 2
 
-
-# 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: 
@@ -32,14 +47,6 @@ FixPlayerWeight: false;
 # 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
-WinWeights:
-  All: 1
-  Top:
-  - 7
-  - 2
-
-
 
 # Points awarded for each item at the end of an Abba Match
 ItemValues:
@@ -61,6 +68,8 @@ ItemValues:
   damage: 1
   Value: 10
 
+# Enables the contraband scanner
+ScanContraband: true
 # Prohibited items, automatically includes entries in BlockValues
 Contraband:
 - type: GOLDEN_APPLE

+ 8 - 1
src/me/lennartVH01/AbbaAdminCommand.java

@@ -161,7 +161,14 @@ public class AbbaAdminCommand implements CommandExecutor, TabCompleter{
 			}
 			switch(abbaGame.getState()){
 			case WAITING:
-				abbaGame.start();
+				List<String> badPlayerNames = abbaGame.start();
+				if(badPlayerNames.size() != 0){
+					sender.sendMessage("§cCannot start game! The following players are carrying contraband:");
+					for(String name:badPlayerNames){
+						sender.sendMessage("§c - " + name);
+					}
+					return false;
+				}
 				return true;
 			case PAUSED:
 				sender.sendMessage("§cGame paused!");

+ 19 - 19
src/me/lennartVH01/AbbaCommand.java

@@ -9,7 +9,6 @@ import org.bukkit.command.CommandExecutor;
 import org.bukkit.command.CommandSender;
 import org.bukkit.command.TabCompleter;
 import org.bukkit.entity.Player;
-import org.bukkit.inventory.ItemStack;
 
 public class AbbaCommand implements CommandExecutor, TabCompleter{
 	public final String[] abbaSubCommands = new String[]{"calc", "info", "join", "leave", "list"};
@@ -37,30 +36,31 @@ public class AbbaCommand implements CommandExecutor, TabCompleter{
 					sender.sendMessage("§cGame not found!");
 					return false;
 				}
-				if(!(game.isOpen() || p.hasPermission("AbbaCaving.joinClosed"))){
-					p.sendMessage("§cThis game is closed!");
-					return false;
-				}
-				if(!(game.hasRoom() || p.hasPermission("AbbaCaving.joinFull"))){
-					p.sendMessage("§cThis game is full!");
-					return false;
-				}
-				
-				if(!p.hasPermission("AbbaCaving.allowContraband")){
-					ItemStack[] contraband = AbbaTools.getContraband(p.getInventory());
-					if(contraband != null && contraband.length >= 1){
-						p.sendMessage("§cYou cannot carry " + contraband[0].getType().toString() + " with you on an abba game!");
-						return false;
-					}
-				}
-				
 				AbbaGame oldGame = AbbaTools.getAbbaGame(p);
 				if(oldGame != null){
 					p.sendMessage("Left game \"" + oldGame.getName() + "\"");
 					
 				}
 				
-				AbbaTools.join(p, game);
+				AbbaGame.JoinResult result = AbbaTools.join(p, game);
+				switch(result){
+				case SUCCESS:
+					break;
+				case FAIL_CLOSED:
+					sender.sendMessage(Messages.gameClosedError);
+					return false;
+				case FAIL_FULL:
+					sender.sendMessage(Messages.gameFullError);
+					return false;
+				case FAIL_NOCHEST:
+					sender.sendMessage(Messages.gameNoChestError);
+					return false;
+				case FAIL_WHITELIST:
+					sender.sendMessage(Messages.gameNotWhiteListedError);
+					return false;
+				case FAIL_CONTRABAND:
+					return false;
+				}
 				p.sendMessage("Joined game \"" + game.getName() + "\"");
 				
 				p.teleport(game.getSpawn());

+ 27 - 12
src/me/lennartVH01/AbbaGame.java

@@ -16,7 +16,6 @@ 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;
@@ -101,7 +100,18 @@ public class AbbaGame implements ConfigurationSerializable{
 		stopClock();
 	}
 	
-	public void start() {
+	public List<String> start() {
+		
+		List<String> badPlayeNames = new ArrayList<String>();
+		for(UUID id:players){
+			Player p = plugin.getServer().getPlayer(id);
+			if(AbbaTools.hasContraband(p.getInventory())){
+				badPlayeNames.add(p.getName());
+			}
+		}
+		if(badPlayeNames.size() != 0){
+			return badPlayeNames;
+		}
 		// TODO Add stuff like tp people to cave if neccecary
 		if(state == GameState.WAITING){
 			endTime = System.currentTimeMillis() + 5000;
@@ -122,6 +132,8 @@ public class AbbaGame implements ConfigurationSerializable{
 		
 		
 		startClock(20);
+		
+		return badPlayeNames; //should be empty
 	}
 	public void pause(){
 		//TODO Implement pausing
@@ -223,6 +235,11 @@ public class AbbaGame implements ConfigurationSerializable{
 		}
 		endStats.sort(scoreComparer);
 		
+		if(!plugin.getConfig().getBoolean("RedistributeItems")){
+			return;
+		}
+		// If configured to do so, the plugin will spread all the goodies around
+		
 		List<Integer> LeaderBoardWeights = plugin.getConfig().getIntegerList("WinWeights.Top");
 		int AllPlayerWeight = plugin.getConfig().getInt("WinWeights.All");
 		int totalWeight = 0;
@@ -310,9 +327,7 @@ public class AbbaGame implements ConfigurationSerializable{
 	public Location getSpawn(){
 		return spawn;
 	}
-	public void onChestOpen(Inventory inv, HumanEntity player) {
-		
-	}
+	
 	
 	
 	
@@ -329,12 +344,13 @@ public class AbbaGame implements ConfigurationSerializable{
 		}
 		//TODO Add stuff here for whitelist aswell
 		
-		ItemStack[] contraband = AbbaTools.getContraband(p.getInventory());
-		if(contraband.length >= 1){
-			p.sendMessage("You cannot join the game with any of the following items!");
+		List<ItemStack> contraband = AbbaTools.getContraband(p.getInventory());
+		if(contraband.size() >= 1){
+			p.sendMessage("You cannot join with any of the following items:");
 			for(ItemStack stack:contraband){
 				p.sendMessage(" - " + stack.getType().toString().toLowerCase());
 			}
+			return JoinResult.FAIL_CONTRABAND;
 		}
 		AbbaChest claimedChest = chestList.claimChest(p.getUniqueId());
 		players.add(p.getUniqueId());
@@ -480,8 +496,10 @@ public class AbbaGame implements ConfigurationSerializable{
 		FAIL_CLOSED,
 		FAIL_WHITELIST,
 		FAIL_NOCHEST,
+		FAIL_CONTRABAND
 		
 	}
+	
 	public enum GameState {
 		WAITING, 
 		COUNTDOWN,
@@ -502,10 +520,6 @@ public class AbbaGame implements ConfigurationSerializable{
 			this.chest = chest;
 			this.sign = sign;
 		}
-		
-		
-		
-		
 		public Chest getChest(){return chest;}
 		public void setChest(Chest chest){this.chest = chest;}
 		public Sign getSign(){return sign;}
@@ -514,6 +528,7 @@ public class AbbaGame implements ConfigurationSerializable{
 		public void setOwnerId(UUID ownerId){this.ownerId = ownerId;}
 	}
 	
+	
 	public class AbbaChestPlayerList{
 		private int playerLength = 0;
 		private List<AbbaChest> chests;

+ 30 - 4
src/me/lennartVH01/AbbaTools.java

@@ -22,6 +22,7 @@ import org.bukkit.inventory.ItemStack;
 public class AbbaTools{
 	public static Main plugin;
 	public static List<ValueItemPair> itemPairs;
+	public static List<ItemStack> contraband;
 	private static List<AbbaGame> games = new ArrayList<AbbaGame>();
 	
 	private static Map<UUID, AbbaGame> playerGameMap = new HashMap<UUID, AbbaGame>();
@@ -29,10 +30,10 @@ public class AbbaTools{
 	
 	
 	
-	public static void initialize(Main plugin, List<ValueItemPair> valueItemPairs) {
+	public static void initialize(Main plugin, List<ValueItemPair> valueItemPairs, List<ItemStack> contraband) {
 		AbbaTools.plugin = plugin;
 		itemPairs = valueItemPairs;
-		
+		AbbaTools.contraband = contraband;
 	}
 	
 	
@@ -102,8 +103,33 @@ public class AbbaTools{
 	}
 	
 	
-	public static ItemStack[] getContraband(Inventory i){
-		return null;
+	public static List<ItemStack> getContraband(Inventory i){
+		List<ItemStack> totalContraband = new ArrayList<ItemStack>();
+		if(!plugin.getConfig().getBoolean("ScanContraband")){
+			return totalContraband;
+		}
+		for(ItemStack detectionStack:contraband){
+			for(ItemStack testStack:i.getStorageContents()){
+				if(detectionStack.isSimilar(testStack)){
+					totalContraband.add(detectionStack);
+					break;	//breaks out of inner loop to go to next detect stack to get 
+				}
+			}
+		}
+		return totalContraband;
+	}
+	public static boolean hasContraband(Inventory i){
+		if(!plugin.getConfig().getBoolean("ScanContraband")){
+			return false;
+		}
+		for(ItemStack detectionStack:contraband){
+			for(ItemStack testStack:i.getStorageContents()){
+				if(detectionStack.isSimilar(testStack)){
+					return true;
+				}
+			}
+		}
+		return false;
 	}
 	
 	

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

@@ -64,6 +64,7 @@ public class Main extends JavaPlugin{
 		// get Item Point Values
 		List<Map<?,?>> itemPointMaps = config.getMapList("ItemValues");
 		List<ValueItemPair> valueItemPairs = new ArrayList<ValueItemPair>();
+		List<ItemStack> contrabandList = new ArrayList<ItemStack>();
 		for(Map<?,?> itemMapGeneric:itemPointMaps){
 			
 			//Put the torches and pitchforks away, config.getMapList() *should* return Map<String, Object> anyways
@@ -74,18 +75,30 @@ public class Main extends JavaPlugin{
 				continue;
 			int value = (int) itemMap.remove("Value");
 			ItemStack stack = ItemStack.deserialize(itemMap);
+			contrabandList.add(stack);
 			stack.setAmount(0);
 			valueItemPairs.add(new ValueItemPair(stack, value));
 		}
+		List<Map<?,?>> contrabandMap = config.getMapList("Contraband");
 		
-		AbbaTools.initialize(this, valueItemPairs);
+		for(Map<?,?> itemMapGeneric:contrabandMap){
+			//I did it agian :D
+			@SuppressWarnings("unchecked")
+			Map<String, Object> itemMap = (Map<String, Object>) itemMapGeneric;
+			
+			contrabandList.add(ItemStack.deserialize(itemMap));
+		}
+		
+		AbbaTools.initialize(this, valueItemPairs, contrabandList);
 		
 		FileConfiguration persist = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "persist.yml"));
 		
-		//Look I did it again :P
+		//I can see the rage in thy eyes
 		@SuppressWarnings("unchecked")
 		List<AbbaGame> abbaList = (List<AbbaGame>) persist.getList("Games");
-		AbbaTools.deserialize((List<AbbaGame>) abbaList); 
+		if(abbaList != null){
+			AbbaTools.deserialize((List<AbbaGame>) abbaList); 
+		}
 	}
 	@Override
 	public void onDisable(){

+ 6 - 0
src/me/lennartVH01/Messages.java

@@ -10,4 +10,10 @@ public class Messages {
 	public static final String basicHelpMessage = "Usage:\n - §aabba join§f: Joins the Abba Match\n - §aabba leave§f: Leaves current Abba Game\n - §aabba info§f: Displays info about an Abba Match\n - §aabba list§f: Lists all Abba Matches\n - §aabba calc§f: Calculates the score of your current inventory";
 	public static final String adminHelpMessage = "Usage:\n - §aabbaadmin create [game]§f: Creates an Abba Game\n - §aabbaadmin remove <game>§f: Removes an Abba Game\n - §aabbaadmin open [game]§f: Allows players to join an Abba Game\n - §aabbaadmin close [game]§f: Prevents players from joining an Abba Game\n - §aabbaadmin start [game]§f: Starts an Abba Game\n - §aabbaadmin calcscores§f: Use to calculate the scores and determine a winner at the end of a Match\n - §aabbaadmin config <game> <timer|addchest> ...§f: Configures an Abba Game\n - §aabbaadmin reload§f: Reloads the config";
 	
+	public static final String gameFullError = "§cThis game is full!";
+	public static final String gameClosedError = "§cThis game is closed!";
+	public static final String gameNoChestError = "§cThere aren't enough chests!";
+	public static final String gameNotWhiteListedError = "§cYou aren't whitelisted for this game!";
+	public static final String gameBlackListedError = "§cYou are blacklisted for this game!";
+	
 }