From 25e034965d755a027ea35aa003cf43753fae98c9 Mon Sep 17 00:00:00 2001 From: taoneill Date: Fri, 9 Sep 2011 23:33:28 -0400 Subject: [PATCH] Closes gh-242. If you remove your blockhead/helmet, it gets reset, but you can't carry more than one. Incremented preview number. --- .../main/java/bukkit/tommytony/war/War.java | 6 +- .../tommytony/war/WarPlayerListener.java | 3 +- .../war/jobs/HelmetProtectionTask.java | 56 +++++++++++++++---- war/src/main/java/plugin.yml | 2 +- war/target/classes/plugin.yml | 2 +- 5 files changed, 53 insertions(+), 16 deletions(-) diff --git a/war/src/main/java/bukkit/tommytony/war/War.java b/war/src/main/java/bukkit/tommytony/war/War.java index 7a9ff28..ddbdd4f 100644 --- a/war/src/main/java/bukkit/tommytony/war/War.java +++ b/war/src/main/java/bukkit/tommytony/war/War.java @@ -79,6 +79,7 @@ public class War extends JavaPlugin { private boolean defaultResetOnEmpty = false, defaultResetOnLoad = false, defaultResetOnUnload = false; private TeamSpawnStyle defaultSpawnStyle = TeamSpawnStyle.BIG; private final HashMap defaultReward = new HashMap(); + private int helmetProtectionTask; public War() { super(); @@ -144,7 +145,8 @@ public class War extends JavaPlugin { this.getDefaultReward().put(0, new ItemStack(Material.CAKE, 1)); WarMapper.load(); - this.getServer().getScheduler().scheduleAsyncDelayedTask(this, new HelmetProtectionTask()); + HelmetProtectionTask helmetProtectionTask = new HelmetProtectionTask(); + this.helmetProtectionTask = this.getServer().getScheduler().scheduleSyncRepeatingTask(this, helmetProtectionTask, 250, 100); this.log("War v" + this.desc.getVersion() + " is on.", Level.INFO); } @@ -161,6 +163,8 @@ public class War extends JavaPlugin { this.warHub.getVolume().resetBlocks(); } + this.getServer().getScheduler().cancelTasks(this); + this.log("War v" + this.desc.getVersion() + " is off.", Level.INFO); this.setLoaded(false); } diff --git a/war/src/main/java/bukkit/tommytony/war/WarPlayerListener.java b/war/src/main/java/bukkit/tommytony/war/WarPlayerListener.java index daf0f4e..e73f0fe 100644 --- a/war/src/main/java/bukkit/tommytony/war/WarPlayerListener.java +++ b/war/src/main/java/bukkit/tommytony/war/WarPlayerListener.java @@ -67,14 +67,13 @@ public class WarPlayerListener extends PlayerListener { // a flag thief can't drop his flag War.war.badMsg(player, "Can't drop items while stealing flag. What are you doing?! Run!"); event.setCancelled(true); - } else { Item item = event.getItemDrop(); if (item != null) { ItemStack itemStack = item.getItemStack(); if (itemStack != null && itemStack.getType() == team.getKind().getMaterial() && itemStack.getData().getData() == team.getKind().getData()) { // Can't drop your team's kind block - War.war.badMsg(player, "Can't drop " + team.getName() + " block blocks."); + War.war.badMsg(player, "Can't drop " + team.getName() + " blocks."); event.setCancelled(true); return; } diff --git a/war/src/main/java/com/tommytony/war/jobs/HelmetProtectionTask.java b/war/src/main/java/com/tommytony/war/jobs/HelmetProtectionTask.java index 899f5ba..ca0ca8e 100644 --- a/war/src/main/java/com/tommytony/war/jobs/HelmetProtectionTask.java +++ b/war/src/main/java/com/tommytony/war/jobs/HelmetProtectionTask.java @@ -1,5 +1,6 @@ package com.tommytony.war.jobs; +import java.util.HashMap; import java.util.logging.Level; import org.bukkit.Material; @@ -14,7 +15,8 @@ import com.tommytony.war.Warzone; import bukkit.tommytony.war.War; /** - * Sets the helmet again onto the players heads + * Sets the helmet again onto the players heads. + * Also limits the number of blocks being held. * * @author Tim Düsterhus */ @@ -24,31 +26,63 @@ public class HelmetProtectionTask implements Runnable { * @see Runnable.run() */ public void run() { - while (true) { + if (War.war.isLoaded()) { for (Warzone zone : War.war.getWarzones()) { for (Team team : zone.getTeams()) { for (Player player : team.getPlayers()) { PlayerInventory playerInv = player.getInventory(); + Material teamBlockMaterial; if (zone.isBlockHeads()) { - playerInv.setHelmet(new ItemStack(team.getKind().getMaterial(), 1, (short) 1, new Byte(team.getKind().getData()))); + teamBlockMaterial = team.getKind().getMaterial(); + // 1) Replace missing block head + if (playerInv.getHelmet().getType() != teamBlockMaterial) { + playerInv.setHelmet(createBlockHead(team)); + } + // 2) Get rid of extra blocks in inventory: only keep one + HashMap blocks = playerInv.all(teamBlockMaterial); + if (blocks.size() > 1 || (blocks.size() == 1 && blocks.get(blocks.keySet().iterator().next()).getAmount() > 1)) { + int i = 0; + int removed = 0; + for (ItemStack item : playerInv.getContents()) { + // remove only same colored wool + if (item != null && item.getType() == teamBlockMaterial && item.getData().getData() == team.getKind().getData()) { + playerInv.clear(i); + removed++; + } + i++; + } + playerInv.setItem(playerInv.firstEmpty(), createBlockHead(team)); + if (removed > 1) { + War.war.badMsg(player, "All that " + team.getName() + " wool must have been heavy!"); + } + } } else { if (team.getKind() == TeamKind.GOLD) { - playerInv.setHelmet(new ItemStack(Material.GOLD_HELMET)); + teamBlockMaterial = Material.GOLD_HELMET; } else if (team.getKind() == TeamKind.DIAMOND) { - playerInv.setHelmet(new ItemStack(Material.DIAMOND_HELMET)); + teamBlockMaterial = Material.DIAMOND_HELMET; } else if (team.getKind() == TeamKind.IRON) { - playerInv.setHelmet(new ItemStack(Material.IRON_HELMET)); + teamBlockMaterial = Material.IRON_HELMET; } else { - playerInv.setHelmet(new ItemStack(Material.LEATHER_HELMET)); + teamBlockMaterial = Material.LEATHER_HELMET; + } + if (playerInv.getHelmet() != null && playerInv.getHelmet().getType() != teamBlockMaterial) { + playerInv.setHelmet(new ItemStack(teamBlockMaterial)); + } + HashMap helmets = playerInv.all(teamBlockMaterial); + if (helmets.size() > 1 || (helmets.size() == 1 && helmets.get(helmets.keySet().iterator().next()).getAmount() > 1)) { + playerInv.remove(teamBlockMaterial); + playerInv.setItem(playerInv.firstEmpty(), new ItemStack(teamBlockMaterial)); + War.war.badMsg(player, "All those helmets must have been heavy!"); } } } } } - try { - Thread.sleep((War.war.isLoaded()) ? 500 : 10000); - } catch (InterruptedException e) { - } } } + + private ItemStack createBlockHead(Team team) { + return new ItemStack(team.getKind().getMaterial(), 1, (short) 1, new Byte(team.getKind().getData())); + } } diff --git a/war/src/main/java/plugin.yml b/war/src/main/java/plugin.yml index 0dd5fee..8c97e8f 100644 --- a/war/src/main/java/plugin.yml +++ b/war/src/main/java/plugin.yml @@ -1,5 +1,5 @@ name: War -version: 1.6 (de Gaulle) PREVIEW 5 +version: 1.6 (de Gaulle) PREVIEW 6 description: Lets you create TDM and CTF (warzones) for a more structured PVP experience. author: tommytony website: war.tommytony.com diff --git a/war/target/classes/plugin.yml b/war/target/classes/plugin.yml index 0dd5fee..8c97e8f 100644 --- a/war/target/classes/plugin.yml +++ b/war/target/classes/plugin.yml @@ -1,5 +1,5 @@ name: War -version: 1.6 (de Gaulle) PREVIEW 5 +version: 1.6 (de Gaulle) PREVIEW 6 description: Lets you create TDM and CTF (warzones) for a more structured PVP experience. author: tommytony website: war.tommytony.com