Make GameComponent generic and store an instance of the Game.

This commit is contained in:
Nate Mortensen 2016-12-19 16:23:10 -07:00 committed by cnr
parent 5c4cc48e86
commit e339b0352a
2 changed files with 16 additions and 12 deletions

View File

@ -6,12 +6,14 @@ import mineplex.core.lifetimes.ListenerComponent;
import java.util.Arrays;
public class GameComponent extends ListenerComponent implements Lifetimed
public class GameComponent<T extends Game> extends ListenerComponent implements Lifetimed
{
private final Lifetime _lifetime;
private final T _game;
public GameComponent(Game game, Game.GameState...active)
public GameComponent(T game, Game.GameState...active)
{
_game = game;
if (active == null || active.length == 0)
{
// Active for the entire duration of the game.
@ -28,4 +30,8 @@ public class GameComponent extends ListenerComponent implements Lifetimed
{
return _lifetime;
}
public T getGame() {
return _game;
}
}

View File

@ -22,9 +22,8 @@ import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
public class WizardSpellMenu extends GameComponent
public class WizardSpellMenu extends GameComponent<Wizards>
{
private Wizards _wizards;
private WizardSpellMenuShop _wizardShop;
private ItemStack _wizardSpells = new ItemBuilder(Material.ENCHANTED_BOOK).setTitle(C.cGold + "Wizard Spells")
.addLore(C.cGray + "Right click with this to view the spells").build();
@ -34,13 +33,12 @@ public class WizardSpellMenu extends GameComponent
super(wizards);
_wizardShop = new WizardSpellMenuShop(this, wizards.getArcadeManager().GetClients(), wizards.getArcadeManager()
.GetDonation(), wizards);
_wizards = wizards;
}
@EventHandler
public void onJoin(PlayerJoinEvent event)
{
if (_wizards.GetState() == GameState.Recruit || _wizards.GetState() == GameState.Live)
if (getGame().GetState() == GameState.Recruit || getGame().GetState() == GameState.Live)
{
event.getPlayer().getInventory().setItem(0, _wizardSpells);
}
@ -53,7 +51,7 @@ public class WizardSpellMenu extends GameComponent
{
public void run()
{
if (_wizards.IsLive())
if (getGame().IsLive())
{
event.getEntity().getInventory().setItem(0, _wizardSpells);
}
@ -66,7 +64,7 @@ public class WizardSpellMenu extends GameComponent
{
if (event.getMessage().equalsIgnoreCase("/spec"))
{
if (!_wizards.IsAlive(event.getPlayer())
if (!getGame().IsAlive(event.getPlayer())
&& !UtilInv.contains(event.getPlayer(), _wizardSpells.getType(), (byte) 0, 1))
{
event.getPlayer().getInventory().setItem(0, _wizardSpells);
@ -88,7 +86,7 @@ public class WizardSpellMenu extends GameComponent
{
for(Player player : UtilServer.GetPlayers())
{
if (!_wizards.IsAlive(player))
if (!getGame().IsAlive(player))
{
player.getInventory().setItem(0, _wizardSpells);
}
@ -100,7 +98,7 @@ public class WizardSpellMenu extends GameComponent
public void onInteract(PlayerInteractEvent event)
{
if (event.getAction() != Action.PHYSICAL && event.getAction().name().contains("RIGHT")
&& (!_wizards.IsLive() || !_wizards.IsAlive(event.getPlayer())))
&& (!getGame().IsLive() || !getGame().IsAlive(event.getPlayer())))
{
ItemStack item = event.getItem();
@ -112,11 +110,11 @@ public class WizardSpellMenu extends GameComponent
}
}
if (_wizards.IsLive() && _wizards.IsAlive(event.getPlayer()))
if (getGame().IsLive() && getGame().IsAlive(event.getPlayer()))
{
Player p = event.getPlayer();
if (p.getInventory().getHeldItemSlot() < _wizards.getWizard(p).getWandsOwned())
if (p.getInventory().getHeldItemSlot() < getGame().getWizard(p).getWandsOwned())
{
if (event.getAction().name().contains("RIGHT"))
{