Browse Source

Implemented /abba calc & config interaction

lennartVH01 9 years ago
parent
commit
59bf818aef

+ 10 - 0
README.md

@@ -1,2 +1,12 @@
 # AbbaManager
 Simple Abba managing plugin
+
+## Usage
+//todo usage
+
+## Features
+// todo features
+
+## Planned Features
+- Allow betting on winner of game
+- iConomy/Vault/registry integration

+ 32 - 0
config.yml

@@ -0,0 +1,32 @@
+# Abba Caving Plugin by lennartVH01
+
+# Points awarded for each item at the end of an Abba Match
+ItemValues:
+- type: IRON_ORE
+  Value: 1
+- type: REDSTONE_ORE
+  Value: 2
+- type: GOLD_ORE
+  Value: 4
+- type: LAPIS_ORE
+  Value: 8
+- type: DIAMOND_ORE
+  Value: 10
+- type: EMERALD_ORE
+  Value: 10
+- type: DIAMOND_BARDING
+  Value: 10
+- type: GOLDEN_APPLE
+  damage: 1
+  Value: 10
+
+# Prohibited items, automatically includes entries in BlockValues
+Contraband:
+  - potion
+  - golden_apple
+
+# Game duration in seconds
+GameDuration: 1800
+
+# Max players per game, users with permission AbbaCaving.joinFull can still join
+PlayerCap: 10

+ 5 - 1
plugin.yml

@@ -12,6 +12,8 @@ permissions:
     default: true
   AbbaCaving.info:
     default: true
+  AbbaCaving.joinFull:
+    default: op
   AbbaCaving.create:
     default: op
   AbbaCaving.remove:
@@ -20,5 +22,7 @@ permissions:
     default: op
   AbbaCaving.close:
     default: op
-  AbbaCaving.joinClosed:
+  AbbaCaving.tp:
+    default: op
+  AbbaCaving.start:
     default: op

+ 37 - 2
src/me/lennartVH01/AbbaGame.java

@@ -1,19 +1,51 @@
 package me.lennartVH01;
 
+
+
+import java.util.List;
+
 import org.bukkit.Location;
+import org.bukkit.inventory.Inventory;
+import org.bukkit.inventory.ItemStack;
+
 
 public class AbbaGame {
 	public boolean open = false;
 	public String name;
 	public Location spawn;
+	public static List<ValueItemPair> itemPairs;
+	
+	public static void initialize(List<ValueItemPair> valueItemPairs) {
+		itemPairs = valueItemPairs;
+		
+	}
+	
+	
+	
+	
 	public AbbaGame(Location spawn, String name){
 		this.spawn = spawn;
 		this.name = name;
 	}
 	
 	
-	
-	
+	public static calculatedScore calcScore(Inventory inv){
+		calculatedScore points = new calculatedScore(itemPairs);
+		
+		//might not be the most efficient, should probably use a HashMap for the itemPairs array
+		for(int i = 0; i < itemPairs.size(); i++){
+			ValueItemPair itemPair = itemPairs.get(i);
+			ItemStack compStack = itemPair.getItemStack();
+			int pointValue = itemPair.getValue();
+			for(ItemStack invStack:inv.getStorageContents()){
+				if(compStack.isSimilar(invStack)){
+					points.add(i, invStack.getAmount(), pointValue);
+				}
+			}
+		}
+		
+		return points;
+	}
 	
 	public void open(){
 		open = true;
@@ -24,4 +56,7 @@ public class AbbaGame {
 	public boolean isOpen(){
 		return open;
 	}
+
+
+	
 }

+ 127 - 25
src/me/lennartVH01/Main.java

@@ -1,17 +1,40 @@
 package me.lennartVH01;
 
+import java.util.ArrayList;
 import java.util.Date;
+import java.util.Map;
+import java.util.List;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 
+import org.bukkit.Location;
+import org.bukkit.World;
 import org.bukkit.command.Command;
 import org.bukkit.command.CommandSender;
 import org.bukkit.configuration.file.FileConfiguration;
 import org.bukkit.entity.Player;
+import org.bukkit.inventory.ItemStack;
 import org.bukkit.plugin.PluginDescriptionFile;
 import org.bukkit.plugin.java.JavaPlugin;
 
 public class Main extends JavaPlugin{
+	
+	/*public static void main(String[] args){
+		ItemStack test = new ItemStack(Material.STONE);
+		
+		
+		
+		
+		System.out.println(test.serialize());
+		
+	}*/
+	
+	
+	
+	
+	
+	
+	
 	public final FileConfiguration config = this.getConfig();
 	
 	public static Main plugin;
@@ -20,25 +43,31 @@ public class Main extends JavaPlugin{
 	
 	@Override
 	public void onEnable(){
-		//Config
-		if(config.get("BlockValues") == null){
-			config.addDefault("BlockValues.iron_ore", 1);
-			config.addDefault("BlockValues.redstone_ore", 2);
-			config.addDefault("BlockValues.gold_ore", 4);
-			config.addDefault("BlockValues.lapis_ore", 8);
-			config.addDefault("BlockValues.diamond_ore", 10);
-			config.addDefault("BlockValues.emerald_ore", 10);
-		}
 		
+		//Config
 		config.options().copyDefaults(true);
-		this.saveConfig();
-		
+		saveConfig();
 		
+		// get Item Point Values
+		List<Map<?,?>> itemPointMaps = config.getMapList("ItemValues");
+		List<ValueItemPair> valueItemPairs = new ArrayList<ValueItemPair>();
+		for(Map<?,?> itemMapGeneric:itemPointMaps){
+			
+			//Put the torches and pitchforks away, config.getMapList() *should* return Map<String, Object> anyways
+			@SuppressWarnings("unchecked")
+			Map<String, Object> itemMap = (Map<String, Object>) itemMapGeneric;
+			
+			if(itemMap.get("Value") == null)
+				continue;
+			int value = (int) itemMap.remove("Value");
+			ItemStack stack = ItemStack.deserialize(itemMap);
+			stack.setAmount(0);
+			valueItemPairs.add(new ValueItemPair(stack, value));
+		}
 		
+		AbbaGame.initialize(valueItemPairs);
 		
 		
-		PluginDescriptionFile pluginDescFile = this.getDescription();
-		System.out.println(pluginDescFile.getName() + " [" + pluginDescFile.getVersion() + "] Initialized");
 	}
 	@Override
 	public void onDisable(){
@@ -47,13 +76,6 @@ public class Main extends JavaPlugin{
 	
 	public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args){
 		if(cmdLabel.equalsIgnoreCase("abba")){
-			Player p;
-			if(sender instanceof Player){
-				p = (Player) sender;
-			}else{
-				System.out.println("Must be ingame to use commands");
-				return false;
-			}
 			if(args.length == 0){
 				args = new String[]{"help"};
 			}
@@ -67,18 +89,94 @@ public class Main extends JavaPlugin{
 			case "info":
 				
 				break;
-			
+			case "calc":
+				if(sender.hasPermission("AbbaCaving.calc")){
+					Player calcPlayer;
+					if(args.length >= 2){
+						calcPlayer = getServer().getPlayer(args[1]);
+						if(calcPlayer == null){
+							sender.sendMessage("§cUnable to find player \"" + args[1] + "\"");
+							return false;
+						}
+					}else{
+						if(sender instanceof Player){
+							calcPlayer = (Player) sender;
+						}else{
+							sender.sendMessage("usage: /abba calc <Player>");
+							return false;
+						}
+					}
+					
+					calculatedScore score = AbbaGame.calcScore(calcPlayer.getInventory());
+					for(int i = 0; i < score.size(); i++){
+						if(score.getItemCount(i) != 0)
+							sender.sendMessage(score.getItemCount(i) + "x" + score.getItemStack(i).getType().toString() + ": " + score.getItemPoints(i));
+					}
+					sender.sendMessage("§aTotal Score: " + score.total);
+					
+					
+				}else{
+					sender.sendMessage("§cYou don't have permission to use this command!");
+					return false;
+				}
+				
+				
+				
+				break;
+				
+				
 			//Admin commands
 			case "create":
-				if(p.hasPermission("AbbaCaving.create")){
+				if(args.length <= 5 && !(sender instanceof Player)){
+					System.out.println("Must be ingame or specify world and coordinates");
+					return false;
+				}
+				if(sender.hasPermission("AbbaCaving.create")){
 					String gameName;
-					if(args.length > 1){
+					Location gameSpawn = null;
+					
+					// set gameName
+					if(args.length >= 2){
 						gameName = args[1];
 					}else{
 						DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH:mm");
 						gameName = "Abba" + dateFormat.format(new Date());
+						
+					}
+					
+					// set gameSpawn
+					if(args.length >= 5){
+						double spawnX, spawnY, spawnZ;
+						try{
+							spawnX = Double.parseDouble(args[2]);
+							spawnY = Double.parseDouble(args[3]);
+							spawnZ = Double.parseDouble(args[4]);
+						}catch(NumberFormatException e){
+							return false;
+						}
+						if(args.length >= 6){
+							World w = getServer().getWorld(args[5]);
+							if(w != null){
+								
+								gameSpawn = new Location(w, spawnX, spawnY, spawnZ);
+								
+							}else{
+								sender.sendMessage("§4Unknown world: \"" + args[5] + "\"");
+								return false;
+							}
+						}
+					}else{
+						if(sender instanceof Player){
+							gameSpawn = ((Player) sender).getLocation();
+						}else{
+							return false;
+						}
 					}
-					AbbaGame abbaGame = new AbbaGame(p.getLocation(), gameName);
+					
+					
+					
+					AbbaGame abbaGame = new AbbaGame(gameSpawn, gameName);
+					
 					
 				}
 				break;
@@ -86,11 +184,15 @@ public class Main extends JavaPlugin{
 				
 				break;
 			
+			
+			
+			
 			//Abba help
 			default:
-				p.sendMessage("Usage:\n - §aabba join§r: Joins the Abba Match\n - §aabba leave§f: Leaves current Abba Game\n - §aabba info§f: Displays info about an Abba Match\n - §aabba create§f: Creates an Abba Game at current location\n - §aabba remove§f: Stops game\n - §aabba open§f: Opens a game\n - §aabba close§f: Closes a game");
+				sender.sendMessage("Usage:\n - §aabba join§r: Joins the Abba Match\n - §aabba leave§f: Leaves current Abba Game\n - §aabba info§f: Displays info about an Abba Match\n - §aabba create§f: Creates an Abba Game at current location\n - §aabba remove§f: Stops game\n - §aabba open§f: Opens a game\n - §aabba close§f: Closes a game");
 				return false;
 			}
+			
 		}
 		
 		

+ 25 - 0
src/me/lennartVH01/ValueItemPair.java

@@ -0,0 +1,25 @@
+package me.lennartVH01;
+
+import org.bukkit.inventory.ItemStack;
+
+public class ValueItemPair {
+	public ItemStack itemStack;
+	public int value;
+	public ValueItemPair(ItemStack itemStack, int value){
+		this.itemStack = itemStack;
+		this.value = value;
+	}
+	public ItemStack getItemStack() {
+		return itemStack;
+	}
+	public void setItemStack(ItemStack itemStack) {
+		this.itemStack = itemStack;
+	}
+	public int getValue() {
+		return value;
+	}
+	public void setValue(int value) {
+		this.value = value;
+	}
+	
+}

+ 48 - 0
src/me/lennartVH01/calculatedScore.java

@@ -0,0 +1,48 @@
+package me.lennartVH01;
+
+import java.util.List;
+
+import org.bukkit.inventory.ItemStack;
+
+public class calculatedScore {
+	public int total = 0;
+	public ItemStack[] items;
+	public int[] itemCount;
+	public int[] itemPoints;
+	
+	public calculatedScore(List<ValueItemPair> itemPairs){
+		int size = itemPairs.size();
+		items = new ItemStack[size];
+		itemCount = new int[size];
+		itemPoints = new int[size];
+		for(int i = 0; i < itemPairs.size(); i++){
+			items[i] = itemPairs.get(i).getItemStack();
+		}
+	}
+
+
+	public void add(int i, int amount, int itemValue) {
+		itemPoints[i] += amount * itemValue;
+		itemCount[i] += amount;
+		total += amount * itemValue;
+	}
+
+
+	public int getTotal() {
+		return total;
+	}
+	public ItemStack getItemStack(int i){
+		return items[i];
+	}
+	public int getItemCount(int i){
+		return itemCount[i];
+	}
+	public int getItemPoints(int i){
+		return itemPoints[i];
+	}
+
+
+	public int size() {
+		return itemCount.length;
+	}
+}