From 662c0622a0e106582307ee7be0b7d9652cf94fdf Mon Sep 17 00:00:00 2001 From: taoneill Date: Wed, 9 Feb 2011 23:50:35 -0500 Subject: [PATCH] Closes gh-12. Added rewards. Default reward when score cap is reach is one cake. Rewards can be customized just like loadouts. Added runnable task that will help to make loot drops configurable in the future. Added defaultSpawnStyle to War config. --- .../main/java/bukkit/tommytony/war/War.java | 63 ++++++++++++++----- .../tommytony/war/WarEntityListener.java | 10 ++- .../com/tommytony/war/LootDropperTask.java | 23 +++++++ .../main/java/com/tommytony/war/Warzone.java | 18 +++++- .../com/tommytony/war/mappers/WarMapper.java | 40 ++++++++++++ .../tommytony/war/mappers/WarzoneMapper.java | 25 ++++++++ 6 files changed, 158 insertions(+), 21 deletions(-) create mode 100644 war/src/main/java/com/tommytony/war/LootDropperTask.java diff --git a/war/src/main/java/bukkit/tommytony/war/War.java b/war/src/main/java/bukkit/tommytony/war/War.java index 3e00a6d..e2a4939 100644 --- a/war/src/main/java/bukkit/tommytony/war/War.java +++ b/war/src/main/java/bukkit/tommytony/war/War.java @@ -1,6 +1,18 @@ package bukkit.tommytony.war; -import org.bukkit.*; +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.bukkit.ChatColor; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.Server; +import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.block.BlockState; @@ -16,6 +28,7 @@ import org.bukkit.plugin.PluginLoader; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; +import com.nijikokun.bukkit.Permissions.Permissions; import com.tommytony.war.Monument; import com.tommytony.war.Team; import com.tommytony.war.TeamChatColors; @@ -28,16 +41,6 @@ import com.tommytony.war.mappers.VolumeMapper; import com.tommytony.war.mappers.WarMapper; import com.tommytony.war.mappers.WarzoneMapper; -import java.io.File; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.logging.Level; -import java.util.logging.Logger; - -import com.nijikokun.bukkit.Permissions.Permissions; - /** * * @author tommytony @@ -70,7 +73,12 @@ public class War extends JavaPlugin { private int defaultScoreCap = 10; private boolean defaultBlockHeads = false; private boolean defaultDropLootOnDeath = false; + private String defaultSpawnStyle = TeamSpawnStyles.BIG; + private final HashMap defaultReward = new HashMap(); + private boolean pvpInZonesOnly = false; + + private WarHub warHub; public void onDisable() { @@ -122,6 +130,7 @@ public class War extends JavaPlugin { this.defaultLifepool = 7; this.defaultFriendlyFire = false; this.defaultAutoAssignOnly = false; + this.getDefaultReward().put(0, new ItemStack(Material.CAKE, 1)); WarMapper.load(this); this.info("Done. War v"+ desc.getVersion() + " is on."); } @@ -448,7 +457,7 @@ public class War extends JavaPlugin { team.setTeamFlag(player.getLocation()); Location playerLoc = player.getLocation(); player.teleportTo(new Location(playerLoc.getWorld(), - playerLoc.getBlockX(), playerLoc.getBlockY() + 1, playerLoc.getBlockZ())); + playerLoc.getBlockX()+1, playerLoc.getBlockY(), playerLoc.getBlockZ()+1)); player.sendMessage(this.str("Team " + name + " flag moved.")); WarzoneMapper.save(this, warzone, false); } @@ -991,7 +1000,7 @@ public class War extends JavaPlugin { private boolean updateZoneFromNamedParams(Warzone warzone, String[] arguments) { try { - Map namedParams = new HashMap(); + Map namedParams = new HashMap(); for(String namedPair : arguments) { String[] pairSplit = namedPair.split(":"); if(pairSplit.length == 2) { @@ -1045,7 +1054,7 @@ public class War extends JavaPlugin { private boolean updateFromNamedParams(String[] arguments) { try { - Map namedParams = new HashMap(); + Map namedParams = new HashMap(); for(String namedPair : arguments) { String[] pairSplit = namedPair.split(":"); if(pairSplit.length == 2) { @@ -1077,10 +1086,20 @@ public class War extends JavaPlugin { String onOff = namedParams.get("pvpinzonesonly"); setPvpInZonesOnly(onOff.equals("on") || onOff.equals("true")); } - if(namedParams.containsKey("blockHeads")){ - String onOff = namedParams.get("blockHeads"); + if(namedParams.containsKey("blockheads")){ + String onOff = namedParams.get("blockheads"); setDefaultBlockHeads(onOff.equals("on") || onOff.equals("true")); } + if(namedParams.containsKey("spawnstyle")){ + String spawnStyle = namedParams.get("spawnstyle").toLowerCase(); + if(spawnStyle.equals(TeamSpawnStyles.SMALL)) { + setDefaultSpawnStyle(spawnStyle); + } else if (spawnStyle.equals(TeamSpawnStyles.FLAT)){ + setDefaultSpawnStyle(spawnStyle); + } else { + setDefaultSpawnStyle(TeamSpawnStyles.BIG); + } + } // if(namedParams.containsKey("dropLootOnDeath")){ // String onOff = namedParams.get("dropLootOnDeath"); // setDefaultDropLootOnDeath(onOff.equals("on") || onOff.equals("true")); @@ -1380,5 +1399,17 @@ public class War extends JavaPlugin { public boolean isDefaultDropLootOnDeath() { return defaultDropLootOnDeath; } + + public void setDefaultSpawnStyle(String defaultSpawnStyle) { + this.defaultSpawnStyle = defaultSpawnStyle; + } + + public String getDefaultSpawnStyle() { + return defaultSpawnStyle; + } + + public HashMap getDefaultReward() { + return defaultReward; + } } diff --git a/war/src/main/java/bukkit/tommytony/war/WarEntityListener.java b/war/src/main/java/bukkit/tommytony/war/WarEntityListener.java index b466aaf..65f3e7b 100644 --- a/war/src/main/java/bukkit/tommytony/war/WarEntityListener.java +++ b/war/src/main/java/bukkit/tommytony/war/WarEntityListener.java @@ -17,6 +17,7 @@ import org.bukkit.event.entity.EntityListener; import org.bukkit.event.entity.EntityDamageEvent.DamageCause; import org.bukkit.event.player.PlayerMoveEvent; +import com.tommytony.war.LootDropperTask; import com.tommytony.war.Team; import com.tommytony.war.Warzone; @@ -41,9 +42,12 @@ public class WarEntityListener extends EntityListener { if(team != null) { Warzone zone = war.getPlayerTeamWarzone(player.getName()); handleDeath(player, zone, team); - if(!zone.isDropLootOnDeath()) { - event.getDrops().clear(); // no loot - } +// if(zone.isDropLootOnDeath()) { +// war.getServer().getScheduler().scheduleAsyncDelayedTask(war, +// new LootDropperTask(player.getLocation(), event.getDrops()), +// 750); +// } + event.getDrops().clear(); // no loot } } } diff --git a/war/src/main/java/com/tommytony/war/LootDropperTask.java b/war/src/main/java/com/tommytony/war/LootDropperTask.java new file mode 100644 index 0000000..c78521e --- /dev/null +++ b/war/src/main/java/com/tommytony/war/LootDropperTask.java @@ -0,0 +1,23 @@ +package com.tommytony.war; + +import java.util.List; + +import org.bukkit.Location; +import org.bukkit.inventory.ItemStack; + +public class LootDropperTask implements Runnable { + + private final List drop; + private final Location location; + + public LootDropperTask(Location location, List drop) { + this.location = location; + this.drop = drop; + } + + public void run() { + for(ItemStack item : drop) { + location.getWorld().dropItemNaturally(location, item); + } + } +} diff --git a/war/src/main/java/com/tommytony/war/Warzone.java b/war/src/main/java/com/tommytony/war/Warzone.java index fe3bee0..0d7acd8 100644 --- a/war/src/main/java/com/tommytony/war/Warzone.java +++ b/war/src/main/java/com/tommytony/war/Warzone.java @@ -36,11 +36,12 @@ public class Warzone { private Location teleport; private boolean friendlyFire; private int lifePool; - private HashMap loadout = new HashMap(); + private HashMap loadout = new HashMap(); private boolean drawZoneOutline; private int teamCap = 5; private int scoreCap = 5; private String spawnStyle = TeamSpawnStyles.BIG; + private HashMap reward = new HashMap(); private HashMap inventories = new HashMap(); private HashMap flagThieves = new HashMap(); @@ -952,7 +953,12 @@ public class Warzone { } tp.teleportTo(this.getTeleport()); } - + if(winnersStr.contains(t.getName())) { + // give reward + for(Integer slot : getReward().keySet()){ + tp.getInventory().addItem(getReward().get(slot)); + } + } } t.setPoints(0); t.getPlayers().clear(); // empty the team @@ -991,5 +997,13 @@ public class Warzone { return spawnStyle; } + public void setReward(HashMap reward) { + this.reward = reward; + } + + public HashMap getReward() { + return reward; + } + } diff --git a/war/src/main/java/com/tommytony/war/mappers/WarMapper.java b/war/src/main/java/com/tommytony/war/mappers/WarMapper.java index b07e13a..2d3a104 100644 --- a/war/src/main/java/com/tommytony/war/mappers/WarMapper.java +++ b/war/src/main/java/com/tommytony/war/mappers/WarMapper.java @@ -9,6 +9,7 @@ import org.bukkit.inventory.ItemStack; import bukkit.tommytony.war.War; +import com.tommytony.war.TeamSpawnStyles; import com.tommytony.war.WarHub; import com.tommytony.war.Warzone; import com.tommytony.war.volumes.Volume; @@ -110,6 +111,33 @@ public class WarMapper { // defaultBlockHeads war.setDefaultBlockHeads(warConfig.getBoolean("defaultBlockHeads")); + // defaultSpawnStyle + String spawnStyle = warConfig.getString("defaultspawnStyle"); + if(spawnStyle != null && !spawnStyle.equals("")){ + spawnStyle = spawnStyle.toLowerCase(); + if(spawnStyle.equals(TeamSpawnStyles.SMALL)) { + war.setDefaultSpawnStyle(spawnStyle); + } else if (spawnStyle.equals(TeamSpawnStyles.FLAT)){ + war.setDefaultSpawnStyle(spawnStyle); + } + // default is already initialized to BIG (see Warzone) + } + + // defaultReward + String defaultRewardStr = warConfig.getString("defaultReward"); + if(defaultRewardStr != null && !defaultRewardStr.equals("")) { + String[] defaultRewardStrSplit = defaultRewardStr.split(";"); + war.getDefaultReward().clear(); + for(String itemStr : defaultRewardStrSplit) { + if(itemStr != null && !itemStr.equals("")) { + String[] itemStrSplit = itemStr.split(","); + ItemStack item = new ItemStack(Integer.parseInt(itemStrSplit[0]), + Integer.parseInt(itemStrSplit[1])); + war.getDefaultReward().put(Integer.parseInt(itemStrSplit[2]), item); + } + } + } + // defaultDropLootOnDeath //war.setDefaultDropLootOnDeath(warConfig.getBoolean("defaultDropLootOnDeath")); @@ -200,6 +228,18 @@ public class WarMapper { // defaultBlockHeads warConfig.setBoolean("defaultBlockHeads", war.isDefaultBlockHeads()); + // spawnStyle + warConfig.setString("spawnStyle", war.getDefaultSpawnStyle()); + + // defaultReward + String defaultRewardStr = ""; + HashMap rewardItems = war.getDefaultReward(); + for(Integer slot : rewardItems.keySet()) { + ItemStack item = items.get(slot); + defaultRewardStr += item.getTypeId() + "," + item.getAmount() + "," + slot + ";"; + } + warConfig.setString("defaultReward", defaultRewardStr); + // defaultDropLootOnDeath //warConfig.setBoolean("defaultDropLootOnDeath", war.isDefaultDropLootOnDeath()); diff --git a/war/src/main/java/com/tommytony/war/mappers/WarzoneMapper.java b/war/src/main/java/com/tommytony/war/mappers/WarzoneMapper.java index 795ceae..df523ef 100644 --- a/war/src/main/java/com/tommytony/war/mappers/WarzoneMapper.java +++ b/war/src/main/java/com/tommytony/war/mappers/WarzoneMapper.java @@ -193,6 +193,21 @@ public class WarzoneMapper { // default is already initialized to BIG (see Warzone) } + // reward + String rewardStr = warzoneConfig.getString("reward"); + if(rewardStr != null && !rewardStr.equals("")) { + String[] rewardStrSplit = rewardStr.split(";"); + warzone.getReward().clear(); + for(String itemStr : rewardStrSplit) { + if(itemStr != null && !itemStr.equals("")) { + String[] itemStrSplit = itemStr.split(","); + ItemStack item = new ItemStack(Integer.parseInt(itemStrSplit[0]), + Integer.parseInt(itemStrSplit[1])); + warzone.getReward().put(Integer.parseInt(itemStrSplit[2]), item); + } + } + } + // dropLootOnDeath //warzone.setDropLootOnDeath(warzoneConfig.getBoolean("dropLootOnDeath")); @@ -365,6 +380,16 @@ public class WarzoneMapper { // spawnStyle warzoneConfig.setString("spawnStyle", warzone.getSpawnStyle()); + // reward + String rewardStr = ""; + HashMap rewardItems = warzone.getReward(); + for(Integer slot : rewardItems.keySet()) { + ItemStack item = items.get(slot); + rewardStr += item.getTypeId() + "," + item.getAmount() + "," + slot + ";"; + } + warzoneConfig.setString("reward", rewardStr); + + // defaultDropLootOnDeath //warzoneConfig.setBoolean("dropLootOnDeath", warzone.isDropLootOnDeath());