From 70948b696766deade22eef904180a791e85af30e Mon Sep 17 00:00:00 2001 From: cnr Date: Wed, 25 Jan 2017 21:10:32 -0600 Subject: [PATCH] Refactor PowerPlayClubItem instead of using an event --- .../gui/buttons/PowerPlayClubButton.java | 9 +- .../src/mineplex/core/pet/PetManager.java | 10 --- .../PowerPlayClubRepository.java | 18 +--- .../powerplayclub/PowerPlayClubRewards.java | 88 +++++++++++-------- .../event/PowerPlayRewardPetEvent.java | 42 --------- 5 files changed, 56 insertions(+), 111 deletions(-) delete mode 100644 Plugins/Mineplex.Core/src/mineplex/core/powerplayclub/event/PowerPlayRewardPetEvent.java diff --git a/Plugins/Mineplex.Core/src/mineplex/core/bonuses/gui/buttons/PowerPlayClubButton.java b/Plugins/Mineplex.Core/src/mineplex/core/bonuses/gui/buttons/PowerPlayClubButton.java index 2d231cdd3..67b1ae492 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/bonuses/gui/buttons/PowerPlayClubButton.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/bonuses/gui/buttons/PowerPlayClubButton.java @@ -21,7 +21,6 @@ import mineplex.core.bonuses.BonusManager; import mineplex.core.common.util.C; import mineplex.core.common.util.F; import mineplex.core.common.util.UtilPlayer; -import mineplex.core.donation.DonationManager; import mineplex.core.gui.GuiItem; import mineplex.core.inventory.InventoryManager; import mineplex.core.powerplayclub.PowerPlayClubRepository; @@ -36,16 +35,12 @@ public class PowerPlayClubButton implements GuiItem private Player _player; private PowerPlayClubRepository _powerPlayClubRepository; private InventoryManager _inventoryManager; - private DonationManager _donationManager; - private BonusManager _bonusManager; public PowerPlayClubButton(Player player, BonusManager manager) { _player = player; - _bonusManager = manager; _powerPlayClubRepository = manager.getPowerPlayClubRepository(); _inventoryManager = manager.getInventoryManager(); - _donationManager = manager.getDonationManager(); } @Override @@ -132,7 +127,7 @@ public class PowerPlayClubButton implements GuiItem itemName = C.cRedB + "Power Play Club"; lore.add(C.cYellow + YearMonth.now().getMonth().getDisplayName(TextStyle.FULL, Locale.US) + "'s Cosmetic"); - lore.add(C.cWhite + " " + PowerPlayClubRewards.rewards().get(YearMonth.now()).getPrize()); + lore.add(C.cWhite + " " + PowerPlayClubRewards.rewards().get(YearMonth.now()).getPrizeName()); lore.add(" "); lore.addAll(buildOtherRewardsLore(1)); lore.add(C.cRed + "Get Power Play Club months at"); @@ -152,7 +147,7 @@ public class PowerPlayClubButton implements GuiItem .forEach(entry -> { YearMonth yearMonth = entry.getKey(); - lore.add(C.cWhite + " " + entry.getValue().getPrize() + " " + C.cGold + yearMonth.getMonth().getDisplayName(TextStyle.SHORT, Locale.US) + " " + yearMonth.getYear()); + lore.add(C.cWhite + " " + entry.getValue().getPrizeName() + " " + C.cGold + yearMonth.getMonth().getDisplayName(TextStyle.SHORT, Locale.US) + " " + yearMonth.getYear()); }); lore.add(" "); return lore; diff --git a/Plugins/Mineplex.Core/src/mineplex/core/pet/PetManager.java b/Plugins/Mineplex.Core/src/mineplex/core/pet/PetManager.java index a25e154d8..e9c7ff2e4 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/pet/PetManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/pet/PetManager.java @@ -72,7 +72,6 @@ import mineplex.core.inventory.InventoryManager; import mineplex.core.itemstack.ItemStackFactory; import mineplex.core.pet.repository.PetRepository; import mineplex.core.pet.repository.token.ClientPetTokenWrapper; -import mineplex.core.powerplayclub.event.PowerPlayRewardPetEvent; import mineplex.core.updater.UpdateType; import mineplex.core.updater.event.UpdateEvent; @@ -625,15 +624,6 @@ public class PetManager extends MiniClientPlugin } } - @EventHandler - public void onPowerPlayRewardPet(PowerPlayRewardPetEvent event) - { - Player player = event.getPlayer(); - PetType petType = event.getPetType(); - if (!Get(player).getPets().containsKey(petType)) - Get(player).getPets().put(petType, petType.getName()); - } - @Override protected PetClient addPlayer(UUID uuid) { diff --git a/Plugins/Mineplex.Core/src/mineplex/core/powerplayclub/PowerPlayClubRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/powerplayclub/PowerPlayClubRepository.java index df3370a71..e28b0509f 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/powerplayclub/PowerPlayClubRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/powerplayclub/PowerPlayClubRepository.java @@ -26,7 +26,6 @@ import org.bukkit.plugin.java.JavaPlugin; import mineplex.core.account.CoreClientManager; import mineplex.core.account.ILoginProcessor; import mineplex.core.donation.DonationManager; -import mineplex.core.powerplayclub.event.PowerPlayRewardPetEvent; import mineplex.serverdata.database.DBPool; public class PowerPlayClubRepository implements Listener { @@ -106,22 +105,7 @@ public class PowerPlayClubRepository implements Listener { List list = PowerPlayClubRewards.rewardsForMonths(cached.getUsableCosmeticMonths()); - for (PowerPlayClubRewards.PowerPlayClubItem item : list) - { - if (item.isPet()) - { - PowerPlayRewardPetEvent ppcRewardPetEvent = new PowerPlayRewardPetEvent(player, item.getPetType()); - Bukkit.getPluginManager().callEvent(ppcRewardPetEvent); - } - else - { - _donationManager.Get(player).addOwnedUnknownSalesPackage(item.getPrize()); - } - } - - /*PowerPlayClubRewards.rewardsForMonths(cached.getUsableCosmeticMonths()).stream() - .map(PowerPlayClubRewards.PowerPlayClubItem::getPrize) - .forEach(_donationManager.Get(player)::addOwnedUnknownSalesPackage);*/ + PowerPlayClubRewards.rewardsForMonths(cached.getUsableCosmeticMonths()).forEach(item -> item.reward(player)); // Gives Metal Man for anyone subscribed if (cached.getUsableCosmeticMonths().size() > 0) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/powerplayclub/PowerPlayClubRewards.java b/Plugins/Mineplex.Core/src/mineplex/core/powerplayclub/PowerPlayClubRewards.java index 6cfdf4347..cf4367873 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/powerplayclub/PowerPlayClubRewards.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/powerplayclub/PowerPlayClubRewards.java @@ -8,18 +8,19 @@ import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; -import org.bukkit.Material; import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; import com.google.common.collect.ImmutableMap; -import mineplex.core.common.skin.SkinData; +import mineplex.core.Managers; import mineplex.core.common.util.BukkitFuture; import mineplex.core.common.util.F; import mineplex.core.common.util.UtilPlayer; +import mineplex.core.donation.DonationManager; import mineplex.core.inventory.InventoryManager; import mineplex.core.inventory.data.Item; +import mineplex.core.pet.PetClient; +import mineplex.core.pet.PetManager; import mineplex.core.pet.PetType; public class PowerPlayClubRewards @@ -28,55 +29,72 @@ public class PowerPlayClubRewards public static final int CHESTS_PER_MONTH = 1; private static final Map rewards = ImmutableMap.builder() - .put(YearMonth.of(2016, Month.SEPTEMBER), new PowerPlayClubItem("Squid Morph", new ItemStack(Material.INK_SACK))) - .put(YearMonth.of(2016, Month.OCTOBER), new PowerPlayClubItem("Witch Morph", SkinData.WITCH.getSkull())) - .put(YearMonth.of(2016, Month.NOVEMBER), new PowerPlayClubItem("Turkey Morph", SkinData.TURKEY.getSkull())) - .put(YearMonth.of(2016, Month.DECEMBER), new PowerPlayClubItem("Santa Morph", SkinData.SANTA.getSkull())) - .put(YearMonth.of(2017, Month.JANUARY), new PowerPlayClubItem("Over Easy Morph", new ItemStack(Material.EGG))) - .put(YearMonth.of(2017, Month.FEBRUARY), new PowerPlayClubItem(PetType.TRUE_LOVE_PET, new ItemStack(Material.APPLE))) + .put(YearMonth.of(2016, Month.SEPTEMBER), new UnknownSalesPackageItem("Squid Morph")) + .put(YearMonth.of(2016, Month.OCTOBER), new UnknownSalesPackageItem("Witch Morph")) + .put(YearMonth.of(2016, Month.NOVEMBER), new UnknownSalesPackageItem("Turkey Morph")) + .put(YearMonth.of(2016, Month.DECEMBER), new UnknownSalesPackageItem("Santa Morph")) + .put(YearMonth.of(2017, Month.JANUARY), new UnknownSalesPackageItem("Over Easy Morph")) + .put(YearMonth.of(2017, Month.FEBRUARY), new PetItem(PetType.TRUE_LOVE_PET)) .build(); - public static class PowerPlayClubItem + public interface PowerPlayClubItem { - private final String _prize; - private final ItemStack _display; - private final boolean _isPet; - private final PetType _petType; + // The name of the Power Play Club prize to be shown as lore in Carl's GUI + String getPrizeName(); + // Give the player this reward + void reward(Player player); + } - PowerPlayClubItem(String prize, ItemStack display) + private static class UnknownSalesPackageItem implements PowerPlayClubItem + { + private static final DonationManager _donationManager = Managers.require(DonationManager.class); + private final String _name; + + UnknownSalesPackageItem(String name) { - _prize = prize; - _display = display; - _isPet = false; - _petType = null; + _name = name; } - PowerPlayClubItem(PetType prize, ItemStack display) + @Override + public String getPrizeName() { - _prize = prize.getName(); - _display = display; - _isPet = true; - _petType = prize; + return _name; } - public String getPrize() + @Override + public void reward(Player player) { - return _prize; + _donationManager.Get(player).addOwnedUnknownSalesPackage(_name); + } + } + + private static class PetItem implements PowerPlayClubItem + { + private final PetType _type; + + PetItem(PetType type) + { + _type = type; } - public ItemStack getDisplay() + @Override + public String getPrizeName() { - return _display; + return _type.getName() + " Pet"; } - public boolean isPet() + @Override + public void reward(Player player) { - return _isPet; - } - - public PetType getPetType() - { - return _petType; + PetManager petManager = Managers.get(PetManager.class); + if (petManager != null) + { + PetClient client = petManager.Get(player); + if (!client.getPets().containsKey(_type)) + { + client.getPets().put(_type, _type.getName()); + } + } } } diff --git a/Plugins/Mineplex.Core/src/mineplex/core/powerplayclub/event/PowerPlayRewardPetEvent.java b/Plugins/Mineplex.Core/src/mineplex/core/powerplayclub/event/PowerPlayRewardPetEvent.java deleted file mode 100644 index 5f6f657fe..000000000 --- a/Plugins/Mineplex.Core/src/mineplex/core/powerplayclub/event/PowerPlayRewardPetEvent.java +++ /dev/null @@ -1,42 +0,0 @@ -package mineplex.core.powerplayclub.event; - -import org.bukkit.entity.Player; -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -import mineplex.core.pet.PetType; - -public class PowerPlayRewardPetEvent extends Event -{ - - private static final HandlerList handlers = new HandlerList(); - - private final Player _player; - private final PetType _petType; - - public PowerPlayRewardPetEvent(Player player, PetType petType) - { - _player = player; - _petType = petType; - } - - public Player getPlayer() - { - return _player; - } - - public PetType getPetType() - { - return _petType; - } - - public HandlerList getHandlers() - { - return handlers; - } - - public static HandlerList getHandlerList() - { - return handlers; - } -}