From e0284e2eb0969fd3a9bbf93ebf4c5a23cce88f22 Mon Sep 17 00:00:00 2001 From: Shaun Bennett Date: Mon, 29 Jun 2015 22:32:40 -0500 Subject: [PATCH] Loot for World Events --- .../src/mineplex/game/clans/Clans.java | 10 +-- .../game/clans/clans/ClansManager.java | 9 ++- .../game/clans/clans/loot/GearLoot.java | 21 ++++++ .../game/clans/clans/loot/GoldLoot.java | 27 ++++++++ .../game/clans/clans/loot/GoldTokenLoot.java | 27 ++++++++ .../mineplex/game/clans/clans/loot/ILoot.java | 9 +++ .../game/clans/clans/loot/ItemLoot.java | 41 +++++++++++ .../game/clans/clans/loot/LootManager.java | 68 +++++++++++++++++++ .../clans/worldevent/WorldEventManager.java | 10 ++- .../worldevent/event/undead/UndeadCamp.java | 32 +++++++-- .../clans/items/generation/WeightSet.java | 7 +- 11 files changed, 245 insertions(+), 16 deletions(-) create mode 100644 Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/loot/GearLoot.java create mode 100644 Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/loot/GoldLoot.java create mode 100644 Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/loot/GoldTokenLoot.java create mode 100644 Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/loot/ILoot.java create mode 100644 Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/loot/ItemLoot.java create mode 100644 Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/loot/LootManager.java diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/Clans.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/Clans.java index 61a896129..f34736245 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/Clans.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/Clans.java @@ -99,8 +99,12 @@ public class Clans extends JavaPlugin new Explosion(this, blockRestore); new FriendManager(this, _clientManager, preferenceManager, portal); new InventoryManager(this, _clientManager); + + // Enable custom-gear related managers + PacketHandler packetHandler = new PacketHandler(this); + GearManager customGear = new GearManager(this, packetHandler); - _clansManager = new ClansManager(this, serverStatusManager.getCurrentServerName(), _clientManager, _donationManager, blockRestore, teleport, chat, webServerAddress); + _clansManager = new ClansManager(this, serverStatusManager.getCurrentServerName(), _clientManager, _donationManager, blockRestore, teleport, chat, customGear, webServerAddress); new Recipes(this); new Farming(this); new BuildingShop(_clansManager, _clientManager, _donationManager); @@ -113,10 +117,6 @@ public class Clans extends JavaPlugin ((CraftWorld) world).getHandle().spigotConfig.itemMerge = 0; } - // Enable custom-gear related managers - PacketHandler packetHandler = new PacketHandler(this); - GearManager customGear = new GearManager(this, packetHandler); - //Updates getServer().getScheduler().scheduleSyncRepeatingTask(this, new Updater(this), 1, 1); diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansManager.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansManager.java index 7bd7b43bd..9243b8125 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansManager.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansManager.java @@ -40,6 +40,7 @@ import mineplex.game.clans.clans.commands.ClansAllyChatCommand; import mineplex.game.clans.clans.commands.ClansChatCommand; import mineplex.game.clans.clans.commands.ClansCommand; import mineplex.game.clans.clans.commands.ServerTimeCommand; +import mineplex.game.clans.clans.loot.LootManager; import mineplex.game.clans.clans.regions.ClansRegions; import mineplex.game.clans.clans.repository.ClanTerritory; import mineplex.game.clans.clans.repository.tokens.ClanMemberToken; @@ -53,6 +54,7 @@ import mineplex.game.clans.fields.Field; import mineplex.game.clans.gameplay.Gameplay; import mineplex.game.clans.gameplay.safelog.LoggingManager; import mineplex.game.clans.gameplay.safelog.npc.NPCManager; +import mineplex.game.clans.items.GearManager; import mineplex.game.clans.spawn.Spawn; import mineplex.minecraft.game.classcombat.Class.ClassManager; import mineplex.minecraft.game.classcombat.Class.repository.token.CustomBuildToken; @@ -113,7 +115,7 @@ public class ClansManager extends MiniClientPlugin implements IRelat public String[] denyClan = new String[] { "neut", "neutral", "sethome", "promote", "demote", "admin", "help", "create", "disband", "delete", "invite", "join", "kick", "ally", "trust", "claim", "unclaim", "territory", "home"}; - public ClansManager(JavaPlugin plugin, String serverName, CoreClientManager clientManager, DonationManager donationManager, BlockRestore blockRestore, Teleport teleport, Chat chat, String webServerAddress) + public ClansManager(JavaPlugin plugin, String serverName, CoreClientManager clientManager, DonationManager donationManager, BlockRestore blockRestore, Teleport teleport, Chat chat, GearManager gearManager, String webServerAddress) { super("Clans Manager", plugin); @@ -146,7 +148,7 @@ public class ClansManager extends MiniClientPlugin implements IRelat new Spawn(plugin); new NPCManager(this); new LoggingManager(plugin); - new GoldManager(this, _clientManager, donationManager); + GoldManager goldManager = new GoldManager(this, _clientManager, donationManager); DamageManager damageManager = new DamageManager(plugin, _combatManager, new NpcManager(plugin, creature), disguiseManager); @@ -173,7 +175,8 @@ public class ClansManager extends MiniClientPlugin implements IRelat ClanEnergyManager clanEnergyManager = new ClanEnergyManager(plugin, this, clientManager, donationManager); - _worldEvent = new WorldEventManager(plugin, this, damageManager); + LootManager lootManager = new LootManager(gearManager, goldManager); + _worldEvent = new WorldEventManager(plugin, this, damageManager, lootManager); for (ClanToken token : _clanDataAccess.getRepository().retrieveClans()) { diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/loot/GearLoot.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/loot/GearLoot.java new file mode 100644 index 000000000..8888f793d --- /dev/null +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/loot/GearLoot.java @@ -0,0 +1,21 @@ +package mineplex.game.clans.clans.loot; + +import org.bukkit.Location; + +import mineplex.game.clans.items.GearManager; + +public class GearLoot implements ILoot +{ + private GearManager _gearManager; + + public GearLoot(GearManager gearManager) + { + _gearManager = gearManager; + } + + @Override + public void dropLoot(Location location) + { + _gearManager.spawnItem(location); + } +} diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/loot/GoldLoot.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/loot/GoldLoot.java new file mode 100644 index 000000000..d33b7298e --- /dev/null +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/loot/GoldLoot.java @@ -0,0 +1,27 @@ +package mineplex.game.clans.clans.loot; + +import org.bukkit.Location; + +import mineplex.core.common.util.UtilMath; +import mineplex.game.clans.economy.GoldManager; + +public class GoldLoot implements ILoot +{ + private GoldManager _goldManager; + private int _min; + private int _max; + + public GoldLoot(GoldManager goldManager, int min, int max) + { + _goldManager = goldManager; + _min = min; + _max = max; + } + + @Override + public void dropLoot(Location location) + { + int count = _min + UtilMath.r(_max - _min); + _goldManager.dropGold(location, count); + } +} diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/loot/GoldTokenLoot.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/loot/GoldTokenLoot.java new file mode 100644 index 000000000..a3d35f63d --- /dev/null +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/loot/GoldTokenLoot.java @@ -0,0 +1,27 @@ +package mineplex.game.clans.clans.loot; + +import org.bukkit.Location; + +import mineplex.core.common.util.UtilMath; +import mineplex.game.clans.items.economy.GoldToken; + +public class GoldTokenLoot implements ILoot +{ + private int _minGold; + private int _maxGold; + + public GoldTokenLoot(int minGold, int maxGold) + { + _minGold = minGold; + _maxGold = maxGold; + } + + @Override + public void dropLoot(Location location) + { + int gold = _minGold + UtilMath.r(_maxGold - _minGold); + + GoldToken token = new GoldToken(gold); + location.getWorld().dropItemNaturally(location, token.toItemStack()); + } +} diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/loot/ILoot.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/loot/ILoot.java new file mode 100644 index 000000000..7b7b8d779 --- /dev/null +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/loot/ILoot.java @@ -0,0 +1,9 @@ +package mineplex.game.clans.clans.loot; + +import org.bukkit.Location; + +public interface ILoot +{ + public void dropLoot(Location location); + +} diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/loot/ItemLoot.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/loot/ItemLoot.java new file mode 100644 index 000000000..252ee37bd --- /dev/null +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/loot/ItemLoot.java @@ -0,0 +1,41 @@ +package mineplex.game.clans.clans.loot; + +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; + +import mineplex.core.common.util.UtilMath; + +public class ItemLoot implements ILoot +{ + private Material _material; + private byte _data; + private int _min; + private int _max; + + public ItemLoot(Material material) + { + this(material, 1, 1); + } + + public ItemLoot(Material material, int min, int max) + { + this(material, (byte) 0, min, max); + } + + public ItemLoot(Material material, byte data, int min, int max) + { + _material = material; + _data = data; + _min = min; + _max = max; + } + + @Override + public void dropLoot(Location location) + { + int count = _min + UtilMath.r(_max - _min); + ItemStack item = new ItemStack(_material, count, (short) 0, _data); + location.getWorld().dropItemNaturally(location, item); + } +} diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/loot/LootManager.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/loot/LootManager.java new file mode 100644 index 000000000..b53a3afac --- /dev/null +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/loot/LootManager.java @@ -0,0 +1,68 @@ +package mineplex.game.clans.clans.loot; + +import org.bukkit.Location; +import org.bukkit.Material; + +import mineplex.game.clans.economy.GoldManager; +import mineplex.game.clans.items.GearManager; +import mineplex.game.clans.items.generation.WeightSet; + +public class LootManager +{ + private GearManager _gearManager; + private GoldManager _goldManager; + + private WeightSet _commonSet; + private WeightSet _rareSet; + + public LootManager(GearManager gearManager, GoldManager goldManager) + { + _gearManager = gearManager; + _goldManager = goldManager; + + _commonSet = new WeightSet(); + _rareSet = new WeightSet(); + + populateCommon(); + populateRare(); + } + + private void populateCommon() + { + // Food + _commonSet.add(5, new ItemLoot(Material.CARROT, 1, 5)); + _commonSet.add(5, new ItemLoot(Material.APPLE, 1, 3)); + _commonSet.add(5, new ItemLoot(Material.COOKED_BEEF, 1, 3)); + _commonSet.add(5, new ItemLoot(Material.RAW_BEEF, 1, 4)); + _commonSet.add(5, new ItemLoot(Material.POTATO_ITEM, 1, 5)); + // Armor + _commonSet.add(2, new ItemLoot(Material.IRON_HELMET, 1, 1)); + _commonSet.add(2, new ItemLoot(Material.IRON_CHESTPLATE, 1, 1)); + _commonSet.add(2, new ItemLoot(Material.IRON_LEGGINGS, 1, 1)); + _commonSet.add(2, new ItemLoot(Material.IRON_BOOTS, 1, 1)); + + // Gear + _commonSet.add(2, new GearLoot(_gearManager)); + + // Gold + _commonSet.add(5, new GoldLoot(_goldManager, 100, 1000)); + _commonSet.add(5, new GoldTokenLoot(100, 1000)); + } + + private void populateRare() + { + // Gear + _rareSet.add(10, new GearLoot(_gearManager)); + } + + public void dropCommon(Location location) + { + _commonSet.generateRandom().dropLoot(location); + } + + public void dropRare(Location location) + { + _rareSet.generateRandom().dropLoot(location); + } + +} diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/worldevent/WorldEventManager.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/worldevent/WorldEventManager.java index adc702eca..adcefac7c 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/worldevent/WorldEventManager.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/worldevent/WorldEventManager.java @@ -29,6 +29,7 @@ import mineplex.core.scoreboard.elements.ScoreboardElement; import mineplex.core.updater.UpdateType; import mineplex.core.updater.event.UpdateEvent; import mineplex.game.clans.clans.ClansManager; +import mineplex.game.clans.clans.loot.LootManager; import mineplex.game.clans.clans.worldevent.command.WorldEventCommand; import mineplex.game.clans.clans.worldevent.event.EventState; import mineplex.game.clans.clans.worldevent.event.WorldEvent; @@ -42,11 +43,12 @@ public class WorldEventManager extends MiniPlugin implements ScoreboardElement private ClansManager _clansManager; private EventTerrainFinder _terrainFinder; private DamageManager _damageManager; + private LootManager _lootManager; private long _lastEventEnd; private long _nextEventStart; - public WorldEventManager(JavaPlugin plugin, ClansManager clansManager, DamageManager damageManager) + public WorldEventManager(JavaPlugin plugin, ClansManager clansManager, DamageManager damageManager, LootManager lootManager) { super("World Event", plugin); @@ -54,6 +56,7 @@ public class WorldEventManager extends MiniPlugin implements ScoreboardElement _terrainFinder = new EventTerrainFinder(this, clansManager); _clansManager = clansManager; _damageManager = damageManager; + _lootManager = lootManager; _events = new LinkedList(); _lastEventEnd = System.currentTimeMillis(); updateNextEventTime(); @@ -179,6 +182,11 @@ public class WorldEventManager extends MiniPlugin implements ScoreboardElement return _damageManager; } + public LootManager getLoot() + { + return _lootManager; + } + public EventTerrainFinder getTerrainFinder() { return _terrainFinder; diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/worldevent/event/undead/UndeadCamp.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/worldevent/event/undead/UndeadCamp.java index cf9959078..761cd9b50 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/worldevent/event/undead/UndeadCamp.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/worldevent/event/undead/UndeadCamp.java @@ -2,15 +2,22 @@ package mineplex.game.clans.clans.worldevent.event.undead; import java.util.HashSet; +import org.bukkit.Effect; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; +import org.bukkit.block.BlockState; +import org.bukkit.block.Chest; import org.bukkit.event.EventHandler; import org.bukkit.event.entity.EntityInteractEvent; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.ItemStack; import mineplex.core.common.util.UtilBlock; import mineplex.core.common.util.UtilMath; +import mineplex.core.common.util.UtilParticle; +import mineplex.core.common.util.UtilServer; import mineplex.core.common.util.UtilWorld; import mineplex.game.clans.clans.worldevent.WorldEventManager; import mineplex.game.clans.clans.worldevent.event.EventState; @@ -60,7 +67,6 @@ public class UndeadCamp extends WorldEvent @Override protected void customTick() { - System.out.println("tick"); if (getState() == EventState.PREPARE) { if (_hutCur < _hutMax) createHut(); @@ -332,15 +338,29 @@ public class UndeadCamp extends WorldEvent _poleCur++; } - @EventHandler - public void onChestOpen(EntityInteractEvent event) + private void openChest(Block block) { - Block block = event.getBlock(); + _chests.remove(block); + + // Effect + block.getWorld().playEffect(block.getLocation(), Effect.STEP_SOUND, Material.ENDER_CHEST.getId()); + + setBlock(block, Material.AIR); + + // Loot + int count = 1 + UtilMath.r(3); + for (int i = 0; i < count; i++) + getEventManager().getLoot().dropCommon(block.getLocation()); + } + + @EventHandler + public void onChestOpen(PlayerInteractEvent event) + { + Block block = event.getClickedBlock(); if (_chests.contains(block)) { - setBlock(event.getBlock(), Material.AIR); + openChest(block); event.setCancelled(true); - _chests.remove(block); } } } diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/items/generation/WeightSet.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/items/generation/WeightSet.java index 46c829c81..7af1d76a7 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/items/generation/WeightSet.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/items/generation/WeightSet.java @@ -12,7 +12,7 @@ public class WeightSet private Set> _weights; - private WeightSet() + public WeightSet() { _weights = new HashSet>(); } @@ -48,6 +48,11 @@ public class WeightSet _weights.add(new Weight(1, element)); // Constant weight of 1 means all elements are equally likely } } + + public void add(int weight, T element) + { + _weights.add(new Weight(weight, element)); + } private int getTotalWeight() {