Add Favourite Nano Games!

This commit is contained in:
Sam 2018-09-07 00:45:50 +01:00 committed by Alexander Meech
parent 25013ea741
commit 2d1d017cb6
13 changed files with 534 additions and 92 deletions

View File

@ -15,7 +15,7 @@ public abstract class MiniClientPlugin<DataType> extends MiniPlugin
{
private static final Object _clientDataLock = new Object();
private Map<UUID, DataType> _clientData = new HashMap<>();
private final Map<UUID, DataType> _clientData = new HashMap<>();
public MiniClientPlugin(String moduleName, JavaPlugin plugin)
{
@ -50,10 +50,7 @@ public abstract class MiniClientPlugin<DataType> extends MiniPlugin
{
synchronized (_clientDataLock)
{
if (!_clientData.containsKey(uuid))
_clientData.put(uuid, addPlayer(uuid));
return _clientData.get(uuid);
return _clientData.computeIfAbsent(uuid, this::addPlayer);
}
}

View File

@ -0,0 +1,16 @@
package mineplex.core.game;
import org.bukkit.Material;
public interface Display
{
int getGameId();
String getName();
Material getMaterial();
byte getMaterialData();
}

View File

@ -7,7 +7,7 @@ import java.util.Map;
import org.bukkit.DyeColor;
import org.bukkit.Material;
public enum GameDisplay
public enum GameDisplay implements Display
{
//Mini
BaconBrawl("Bacon Brawl", Material.PORK, (byte)0, GameCategory.NONE, 1, true),
@ -121,13 +121,14 @@ public enum GameDisplay
Brawl("Brawl", Material.DIAMOND, (byte) 0, GameCategory.EVENT, 998, false);
String _name;
String _lobbyName;
Material _mat;
byte _data;
GameCategory _gameCategory;
final String _name;
final String _lobbyName;
final Material _mat;
final byte _data;
final GameCategory _gameCategory;
private int _gameId; // Unique identifying id for this gamemode (used for statistics)
private final int _gameId; // Unique identifying id for this gamemode (used for statistics)
@Override
public int getGameId() { return _gameId; }
private boolean _communityFavorite;
@ -149,6 +150,7 @@ public enum GameDisplay
_communityFavorite = communityFavorite;
}
@Override
public String getName()
{
return _name;
@ -159,11 +161,13 @@ public enum GameDisplay
return _lobbyName;
}
@Override
public Material getMaterial()
{
return _mat;
}
@Override
public byte getMaterialData()
{
return _data;
@ -174,14 +178,15 @@ public enum GameDisplay
return _gameCategory;
}
public static GameDisplay matchName(String name)
{
for (GameDisplay display : values())
{
if (display.getName().equalsIgnoreCase(name))
{
return display;
}
}
return null;
}

View File

@ -0,0 +1,90 @@
package mineplex.core.game;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import mineplex.core.common.util.UtilEnt;
public enum NanoDisplay implements Display
{
BAWK_BAWK(20, "Bawk Bawk's Weath", Material.EGG),
COPY_CAT(17, "Copy Cat", Material.MONSTER_EGG, UtilEnt.getEntityEggData(EntityType.OCELOT)),
CHICKEN_SHOOT(14, "Chicken Shoot", Material.BOW),
COLOUR_CHANGE(6, "Color Swap", Material.WOOL, (byte) 4),
DEATH_TAG(4, "Zombie Survival", Material.SKULL_ITEM, (byte) 2),
FIND_ORES(11, "Ores Ores Ores", Material.DIAMOND_PICKAXE),
HOT_POTATO(1, "Hot Potato", Material.POTATO_ITEM),
JUMP_ROPE(10, "Jump Rope", Material.LEASH),
KING_SLIME(8, "King Slime", Material.SLIME_BALL),
MICRO_BATTLE(3, "Nano Battle", Material.IRON_SWORD),
MOB_FARM(13, "Mob Farm", Material.MONSTER_EGG, UtilEnt.getEntityEggData(EntityType.PIG)),
MUSIC_MINECARTS(12, "Musical Minecarts", Material.MINECART),
QUICK(21, "Quick", Material.DIAMOND),
PARKOUR(19, "Hardcore Parkour", Material.FEATHER),
RED_GREEN_LIGHT(5, "Red Light Green Light", Material.WOOL, (byte) 5),
REVERSE_TAG(2, "Reverse Tag", Material.EMERALD),
SLIME_CYCLES(15, "Slime Cycles", Material.SLIME_BLOCK),
SNOWBALL_TROUBLE(18, "Snowball Trouble", Material.SNOW_BALL),
SPLEEF(9, "AAAAAAAA! Spleef", Material.DIAMOND_SPADE),
SPLOOR(7, "Sploor", Material.DIAMOND_BARDING),
TERRITORY(16, "Slime Territory", Material.MONSTER_EGG, UtilEnt.getEntityEggData(EntityType.SLIME)),
;
public static NanoDisplay getFromId(int gameId)
{
for (NanoDisplay display : values())
{
if (gameId == display._gameId)
{
return display;
}
}
return null;
}
private final int _gameId;
private final String _name;
private final Material _material;
private final byte _materialData;
NanoDisplay(int gameId, String name, Material material)
{
this(gameId, name, material, (byte) 0);
}
NanoDisplay(int gameId, String name, Material material, byte materialData)
{
_gameId = gameId;
_name = name;
_material = material;
_materialData = materialData;
}
@Override
public int getGameId()
{
return _gameId;
}
@Override
public String getName()
{
return _name;
}
@Override
public Material getMaterial()
{
return _material;
}
@Override
public byte getMaterialData()
{
return _materialData;
}
}

View File

@ -0,0 +1,129 @@
package mineplex.core.game.nano;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.Consumer;
import org.bukkit.entity.Player;
import mineplex.core.MiniDbClientPlugin;
import mineplex.core.ReflectivelyCreateMiniPlugin;
import mineplex.core.common.util.F;
import mineplex.core.donation.DonationManager;
import mineplex.core.game.NanoDisplay;
@ReflectivelyCreateMiniPlugin
public class NanoFavourite extends MiniDbClientPlugin<List<NanoDisplay>>
{
private static final int MAX_FAVOURITES = 3;
private final DonationManager _donationManager;
private final NanoRepository _repository;
private final NanoShop _shop;
private NanoFavourite()
{
super("Favourite Nano");
_donationManager = require(DonationManager.class);
_repository = new NanoRepository();
_shop = new NanoShop(this);
}
@Override
protected List<NanoDisplay> addPlayer(UUID uuid)
{
return new ArrayList<>();
}
@Override
public void processLoginResultSet(String playerName, UUID uuid, int accountId, ResultSet resultSet) throws SQLException
{
List<NanoDisplay> games = Get(uuid);
while (resultSet.next())
{
NanoDisplay display = NanoDisplay.getFromId(resultSet.getInt("gameId"));
if (display == null)
{
return;
}
games.add(display);
}
}
@Override
public String getQuery(int accountId, String uuid, String name)
{
return "SELECT gameId FROM accountFavouriteNano WHERE accountId=" + accountId + ";";
}
public void setFavourite(Consumer<Boolean> callback, Player player, NanoDisplay display, boolean favourite)
{
int accountId = ClientManager.getAccountId(player);
if (favourite && Get(player).size() >= MAX_FAVOURITES)
{
player.sendMessage(F.main(getName(), "You cannot set more than " + F.count(MAX_FAVOURITES) + " games as favorite!"));
if (callback != null)
{
callback.accept(false);
}
return;
}
runAsync(() ->
{
_repository.setFavourite(accountId, display.getGameId(), favourite);
runSync(() ->
{
if (favourite)
{
Get(player).add(display);
player.sendMessage(F.main(getName(), "Added " + F.name(display.getName()) + " to your favorite games list."));
}
else
{
Get(player).remove(display);
player.sendMessage(F.main(getName(), "Removed " + F.name(display.getName()) + " from your favorite games list."));
}
if (callback != null)
{
callback.accept(true);
}
});
});
}
public Map<NanoDisplay, Integer> getFavourites()
{
Map<NanoDisplay, Integer> favourites = new HashMap<>();
GetValues().forEach(games -> games.forEach(display -> favourites.put(display, favourites.getOrDefault(display, 0) + 1)));
return favourites;
}
public DonationManager getDonationManager()
{
return _donationManager;
}
public NanoShop getShop()
{
return _shop;
}
}

View File

@ -0,0 +1,74 @@
package mineplex.core.game.nano;
import java.util.List;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import mineplex.core.common.util.C;
import mineplex.core.common.util.F;
import mineplex.core.game.NanoDisplay;
import mineplex.core.itemstack.ItemBuilder;
import mineplex.core.recharge.Recharge;
import mineplex.core.shop.page.ShopPageBase;
public class NanoFavouritePage extends ShopPageBase<NanoFavourite, NanoShop>
{
NanoFavouritePage(NanoFavourite plugin, NanoShop shop, Player player)
{
super(plugin, shop, plugin.getClientManager(), plugin.getDonationManager(), "Favorite Games", player);
buildPage();
}
@Override
protected void buildPage()
{
addButtonNoAction(4, new ItemBuilder(Material.NETHER_STAR)
.setGlow(true)
.setTitle(C.cPurple + "Favorite Games")
.addLore("Setting games as your favourite will", "increase the chance of them being", "played in Nano Games lobbies.")
.build());
int slot = 10;
List<NanoDisplay> games = getPlugin().Get(getPlayer());
for (NanoDisplay display : NanoDisplay.values())
{
boolean favourite = games.contains(display);
String lore = favourite ? "Click to remove " + F.name(display.getName()) + " from" : "Click to add " + F.name(display.getName()) + " to";
addButton(slot, new ItemBuilder(display.getMaterial(), display.getMaterialData())
.setGlow(favourite)
.setTitle((favourite ? C.cGreenB : C.cYellowB) + display.getName())
.addLore(lore, "your favorite games list.")
.build(), (player, clickType) ->
{
if (!Recharge.Instance.use(player, getPlugin().getName(), 1000, false, false))
{
return;
}
getPlugin().setFavourite(success ->
{
if (success)
{
playAcceptSound(player);
refresh();
}
else
{
playDenySound(player);
player.closeInventory();
}
}, player, display, !favourite);
});
if (++slot % 9 == 8)
{
slot += 2;
}
}
}
}

View File

@ -0,0 +1,22 @@
package mineplex.core.game.nano;
import mineplex.serverdata.database.DBPool;
import mineplex.serverdata.database.RepositoryBase;
import mineplex.serverdata.database.column.ColumnInt;
public class NanoRepository extends RepositoryBase
{
private static final String ADD_FAVOURITE = "INSERT INTO accountFavouriteNano VALUES (?,?);";
private static final String REMOVE_FAVOURITE = "DELETE FROM accountFavouriteNano WHERE accountId=? AND gameId=?;";
NanoRepository()
{
super(DBPool.getAccount());
}
public void setFavourite(int accountId, int gameId, boolean favourite)
{
executeInsert(favourite ? ADD_FAVOURITE : REMOVE_FAVOURITE, null, new ColumnInt("accountId", accountId), new ColumnInt("gameId", gameId));
}
}

View File

@ -0,0 +1,21 @@
package mineplex.core.game.nano;
import org.bukkit.entity.Player;
import mineplex.core.shop.ShopBase;
import mineplex.core.shop.page.ShopPageBase;
public class NanoShop extends ShopBase<NanoFavourite>
{
NanoShop(NanoFavourite plugin)
{
super(plugin, plugin.getClientManager(), plugin.getDonationManager(), plugin.getName());
}
@Override
protected ShopPageBase<NanoFavourite, ? extends ShopBase<NanoFavourite>> buildPagesFor(Player player)
{
return new NanoFavouritePage(getPlugin(), this, player);
}
}

View File

@ -32,6 +32,7 @@ import mineplex.core.cosmetic.CosmeticManager;
import mineplex.core.disguise.DisguiseManager;
import mineplex.core.donation.DonationManager;
import mineplex.core.game.GameDisplay;
import mineplex.core.game.nano.NanoFavourite;
import mineplex.core.incognito.IncognitoManager;
import mineplex.core.incognito.events.IncognitoStatusChangeEvent;
import mineplex.core.serverConfig.ServerConfiguration;
@ -123,6 +124,7 @@ public class NanoManager extends MiniPlugin implements IRelation
// Lobby
private final LobbyManager _lobbyManager;
private final ReturnToHubManager _toHubManager;
// Currency
private final GameCurrencyManager _currencyManager;
@ -133,6 +135,7 @@ public class NanoManager extends MiniPlugin implements IRelation
// Game
private Game _game;
private final GameCycle _gameCycle;
private final NanoFavourite _favourite;
private NanoManager()
{
@ -189,7 +192,7 @@ public class NanoManager extends MiniPlugin implements IRelation
_gamePlayerManager = require(GamePlayerManager.class);
_lobbyManager = require(LobbyManager.class);
require(ReturnToHubManager.class);
_toHubManager = require(ReturnToHubManager.class);
_currencyManager = require(GameCurrencyManager.class);
@ -199,6 +202,7 @@ public class NanoManager extends MiniPlugin implements IRelation
require(AFKManager.class);
_gameCycle = require(GameCycle.class);
_favourite = require(NanoFavourite.class);
generatePermissions();
}
@ -427,6 +431,11 @@ public class NanoManager extends MiniPlugin implements IRelation
return _lobbyManager;
}
public ReturnToHubManager getToHubManager()
{
return _toHubManager;
}
public GameCurrencyManager getCurrencyManager()
{
return _currencyManager;
@ -451,4 +460,9 @@ public class NanoManager extends MiniPlugin implements IRelation
{
return _gameCycle;
}
public NanoFavourite getFavourite()
{
return _favourite;
}
}

View File

@ -5,6 +5,7 @@ import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.bukkit.entity.Player;
@ -16,6 +17,7 @@ import org.bukkit.scheduler.BukkitRunnable;
import mineplex.core.ReflectivelyCreateMiniPlugin;
import mineplex.core.common.util.UtilAlg;
import mineplex.core.common.util.UtilMath;
import mineplex.core.game.NanoDisplay;
import mineplex.game.nano.GameManager;
import mineplex.game.nano.NanoManager;
import mineplex.game.nano.game.Game;
@ -27,21 +29,23 @@ import mineplex.game.nano.game.event.GameStateChangeEvent;
public class GameCycle extends GameManager
{
private List<GameType> _gamePool;
private static final int RECENT_GAMES_SIZE = 5;
// Preferences
private GameType _gamePreference;
private String _mapPreference;
// Recent Games
private final List<GameType> _recentGames;
// Testing mode
private GameType _lastGame;
private boolean _testingMode;
private GameCycle()
{
super("Game Cycle");
_gamePool = new ArrayList<>();
_recentGames = new ArrayList<>(RECENT_GAMES_SIZE);
}
@EventHandler(priority = EventPriority.MONITOR)
@ -167,10 +171,15 @@ public class GameCycle extends GameManager
return "getNextMap was null";
}
_gamePool.remove(gameType);
_gamePreference = null;
_mapPreference = null;
_lastGame = gameType;
if (_recentGames.size() == RECENT_GAMES_SIZE)
{
_recentGames.remove(_recentGames.size() - 1);
}
_recentGames.add(0, gameType);
try
{
@ -197,28 +206,44 @@ public class GameCycle extends GameManager
}
// Testing mode
if (_testingMode && _lastGame != null)
if (_testingMode && !_recentGames.isEmpty())
{
log("Setting by last game");
return _lastGame;
return _recentGames.get(0);
}
if (_gamePool == null || _gamePool.isEmpty())
// The games mapped to the number of people that have them favourited
Map<NanoDisplay, Integer> favourites = _manager.getFavourite().getFavourites();
// Out of all games
List<GameType> gameTypes = Arrays.stream(GameType.values())
// Remove recent games and ones which no one has sent as their favourite
.filter(game -> !_recentGames.contains(game) && favourites.containsKey(game.getDisplay()))
// Then sort them inversely of how many people have them set as their favourites
.sorted((o1, o2) -> favourites.get(o2.getDisplay()).compareTo(favourites.get(o1.getDisplay())))
.collect(Collectors.toList());
log("Recent Games : " + _recentGames);
log("Favourites Sorted : " + gameTypes);
GameType gameType;
// No game? Happens if no one has anything set or there isn't a huge variety in the games selected as favourites
if (gameTypes.isEmpty())
{
List<GameType> gameTypes = new ArrayList<>(Arrays.asList(GameType.values()));
if (_lastGame != null)
{
gameTypes.remove(_lastGame);
}
log("No games in pool. Adding " + gameTypes);
_gamePool = gameTypes;
}
GameType gameType = UtilAlg.Random(_gamePool);
// Get a random game that hasn't been played recently
gameType = UtilAlg.Random(Arrays.stream(GameType.values())
.filter(game -> !_recentGames.contains(game))
.collect(Collectors.toList()));
log("Setting by Random : " + gameType);
}
else
{
// Get the most favourited game
gameType = gameTypes.get(0);
log("Setting by Favourite : " + gameType);
}
return gameType;
}

View File

@ -3,6 +3,7 @@ package mineplex.game.nano.game;
import java.io.File;
import mineplex.core.game.GameDisplay;
import mineplex.core.game.NanoDisplay;
import mineplex.game.nano.NanoManager;
import mineplex.game.nano.game.games.bawkbawk.BawkBawk;
import mineplex.game.nano.game.games.chickenshoot.ChickenShoot;
@ -10,6 +11,7 @@ import mineplex.game.nano.game.games.colourchange.ColourChange;
import mineplex.game.nano.game.games.copycat.CopyCat;
import mineplex.game.nano.game.games.deathtag.DeathTag;
import mineplex.game.nano.game.games.findores.FindOres;
import mineplex.game.nano.game.games.hotpotato.HotPotato;
import mineplex.game.nano.game.games.jumprope.JumpRope;
import mineplex.game.nano.game.games.kingslime.KingSlime;
import mineplex.game.nano.game.games.microbattle.MicroBattle;
@ -23,35 +25,32 @@ import mineplex.game.nano.game.games.reversetag.ReverseTag;
import mineplex.game.nano.game.games.slimecycles.SlimeCycles;
import mineplex.game.nano.game.games.spleef.Spleef;
import mineplex.game.nano.game.games.sploor.Sploor;
import mineplex.game.nano.game.games.hotpotato.HotPotato;
import mineplex.game.nano.game.games.territory.Territory;
public enum GameType
{
HOT_POTATO(HotPotato.class, "Hot Potato"),
REVERSE_TAG(ReverseTag.class, "Reverse Tag"),
MICRO_BATTLE(MicroBattle.class, "Nano Battle", mapsFromArcade(GameDisplay.Micro)),
DEATH_TAG(DeathTag.class, "Zombie Survival", mapsFromArcade(GameDisplay.DeathTag)),
RED_GREEN_LIGHT(RedGreenLight.class, "Red Light Green Light"),
COLOUR_CHANGE(ColourChange.class, "Color Swap"),
SPLOOR(Sploor.class, "Sploor"),
KING_SLIME(KingSlime.class, "King Slime"),
SPLEEF(Spleef.class, "AAAAAAAA! Spleef", mapsFromArcade(GameDisplay.Runner)),
JUMP_ROPE(JumpRope.class, "Jump Rope"),
FIND_ORES(FindOres.class, "Ores Ores Ores"),
MUSIC_MINECARTS(MusicMinecarts.class, "Musical Minecarts"),
MOB_FARM(MobFarm.class, "Mob Farm"),
CHICKEN_SHOOT(ChickenShoot.class, "Chicken Shoot", mapsFromNano("Mob Farm")),
SLIME_CYCLES(SlimeCycles.class, "Slime Cycles"),
TERRITORY(Territory.class, "Slime Territory"),
COPY_CAT(CopyCat.class, "Copy Cat"),
SNOWBALL_TROUBLE(SnowballTrouble.class, "Snowball Trouble", mapsFromArcade(GameDisplay.Quiver)),
PARKOUR(Parkour.class, "Hardcore Parkour"),
BAWK_BAWK(BawkBawk.class, "Bawk Bawk's Wrath"),
QUICK(Quick.class, "Quick")
;
HOT_POTATO(HotPotato.class, NanoDisplay.HOT_POTATO),
REVERSE_TAG(ReverseTag.class, NanoDisplay.REVERSE_TAG),
MICRO_BATTLE(MicroBattle.class, NanoDisplay.MICRO_BATTLE, mapsFromArcade(GameDisplay.Micro)),
DEATH_TAG(DeathTag.class, NanoDisplay.DEATH_TAG, mapsFromArcade(GameDisplay.DeathTag)),
RED_GREEN_LIGHT(RedGreenLight.class, NanoDisplay.RED_GREEN_LIGHT),
COLOUR_CHANGE(ColourChange.class, NanoDisplay.COLOUR_CHANGE),
SPLOOR(Sploor.class, NanoDisplay.SPLOOR),
KING_SLIME(KingSlime.class, NanoDisplay.KING_SLIME),
SPLEEF(Spleef.class, NanoDisplay.SPLEEF, mapsFromArcade(GameDisplay.Runner)),
JUMP_ROPE(JumpRope.class, NanoDisplay.JUMP_ROPE),
FIND_ORES(FindOres.class, NanoDisplay.FIND_ORES),
MUSIC_MINECARTS(MusicMinecarts.class, NanoDisplay.MUSIC_MINECARTS),
MOB_FARM(MobFarm.class, NanoDisplay.MOB_FARM),
CHICKEN_SHOOT(ChickenShoot.class, NanoDisplay.CHICKEN_SHOOT, mapsFromNano(NanoDisplay.MOB_FARM)),
SLIME_CYCLES(SlimeCycles.class, NanoDisplay.SLIME_CYCLES),
TERRITORY(Territory.class, NanoDisplay.TERRITORY),
COPY_CAT(CopyCat.class, NanoDisplay.COPY_CAT),
SNOWBALL_TROUBLE(SnowballTrouble.class, NanoDisplay.SNOWBALL_TROUBLE, mapsFromArcade(GameDisplay.Quiver)),
PARKOUR(Parkour.class, NanoDisplay.PARKOUR),
BAWK_BAWK(BawkBawk.class, NanoDisplay.BAWK_BAWK),
QUICK(Quick.class, NanoDisplay.QUICK);
private static String maps()
{
@ -63,24 +62,24 @@ public enum GameType
return maps() + File.separator + display.getName();
}
private static String mapsFromNano(String name)
private static String mapsFromNano(NanoDisplay display)
{
return mapsFromArcade(NanoManager.getGameDisplay()) + File.separator + name;
return mapsFromArcade(NanoManager.getGameDisplay()) + File.separator + display.getName();
}
private final Class<? extends Game> _gameClass;
private final String _name;
private final NanoDisplay _display;
private final String _mapDirectory;
GameType(Class<? extends Game> gameClass, String name)
GameType(Class<? extends Game> gameClass, NanoDisplay display)
{
this(gameClass, name, mapsFromNano(name));
this(gameClass, display, mapsFromNano(display));
}
GameType(Class<? extends Game> gameClass, String name, String mapDirectory)
GameType(Class<? extends Game> gameClass, NanoDisplay display, String mapDirectory)
{
_gameClass = gameClass;
_name = name;
_display = display;
_mapDirectory = mapDirectory;
}
@ -89,9 +88,14 @@ public enum GameType
return _gameClass;
}
public NanoDisplay getDisplay()
{
return _display;
}
public String getName()
{
return _name;
return _display.getName();
}
public String getMapDirectory()

View File

@ -147,6 +147,7 @@ public class LobbyManager extends GameManager
NanoPlayer.setSpectating(player, false);
player.teleport(_spawn);
_manager.getToHubManager().giveItems(player);
}
public void ensureInLobby()

View File

@ -5,7 +5,9 @@ import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import mineplex.core.ReflectivelyCreateMiniPlugin;
import mineplex.core.account.permissions.Permission;
@ -20,6 +22,7 @@ import mineplex.core.portal.Intent;
import mineplex.core.portal.Portal;
import mineplex.core.recharge.Recharge;
import mineplex.game.nano.GameManager;
import mineplex.game.nano.game.Game;
import mineplex.game.nano.game.Game.GameState;
import mineplex.game.nano.game.event.GameStateChangeEvent;
import mineplex.game.nano.game.event.PlayerStateChangeEvent;
@ -30,13 +33,17 @@ public class ReturnToHubManager extends GameManager
public enum Perm implements Permission
{
RETURN_TO_HUB_COMMAND
COMMANDS
}
private static final ItemStack HUB_CLOCK = new ItemBuilder(Material.WATCH)
.setTitle(C.cGreen + "Cash Out")
.addLore("", "Click to return to the hub and", "receive all your rewards from this game!")
.build();
private static final ItemStack FAVOURITE_STAR = new ItemBuilder(Material.NETHER_STAR)
.setTitle(C.cPurple + "Favorite Game")
.addLore("", "Click to open the favorite", "games menu!")
.build();
private ReturnToHubManager()
{
@ -47,13 +54,13 @@ public class ReturnToHubManager extends GameManager
private void generatePermissions()
{
PermissionGroup.PLAYER.setPermission(Perm.RETURN_TO_HUB_COMMAND, true, true);
PermissionGroup.PLAYER.setPermission(Perm.COMMANDS, true, true);
}
@Override
public void addCommands()
{
addCommand(new CommandBase<ReturnToHubManager>(this, Perm.RETURN_TO_HUB_COMMAND, "hub", "lobby", "leave")
addCommand(new CommandBase<ReturnToHubManager>(this, Perm.COMMANDS, "hub", "lobby", "leave")
{
@Override
public void Execute(Player caller, String[] args)
@ -61,6 +68,28 @@ public class ReturnToHubManager extends GameManager
sendToHub(caller);
}
});
addCommand(new CommandBase<ReturnToHubManager>(this, Perm.COMMANDS, "favorite", "favoritegames")
{
@Override
public void Execute(Player caller, String[] args)
{
openShop(caller);
}
});
}
@EventHandler(priority = EventPriority.MONITOR)
public void playerJoin(PlayerJoinEvent event)
{
Game game = _manager.getGame();
Player player = event.getPlayer();
if (game != null && game.isAlive(player))
{
return;
}
giveItems(player);
}
@EventHandler(priority = EventPriority.MONITOR)
@ -77,30 +106,11 @@ public class ReturnToHubManager extends GameManager
{
if (player.isOnline() && !_manager.getGame().isAlive(player))
{
giveClock(player);
giveItems(player);
}
}, 20);
}
@EventHandler
public void playerInteractClock(PlayerInteractEvent event)
{
if (!UtilEvent.isAction(event, ActionType.R))
{
return;
}
Player player = event.getPlayer();
ItemStack itemStack = player.getItemInHand();
if (!HUB_CLOCK.equals(itemStack))
{
return;
}
sendToHub(player);
}
@EventHandler(priority = EventPriority.HIGH)
public void prepare(GameStateChangeEvent event)
{
@ -111,13 +121,37 @@ public class ReturnToHubManager extends GameManager
for (Player player : _manager.getSpectators())
{
giveClock(player);
giveItems(player);
}
}
private void giveClock(Player player)
@EventHandler
public void playerInteract(PlayerInteractEvent event)
{
player.getInventory().setItem(8, HUB_CLOCK);
if (!UtilEvent.isAction(event, ActionType.R))
{
return;
}
Player player = event.getPlayer();
ItemStack itemStack = player.getItemInHand();
if (HUB_CLOCK.equals(itemStack))
{
sendToHub(player);
}
else if (FAVOURITE_STAR.equals(itemStack))
{
openShop(player);
}
}
public void giveItems(Player player)
{
PlayerInventory inventory = player.getInventory();
inventory.setItem(8, HUB_CLOCK);
inventory.setItem(7, FAVOURITE_STAR);
}
private void sendToHub(Player player)
@ -129,4 +163,14 @@ public class ReturnToHubManager extends GameManager
Portal.getInstance().sendPlayerToGenericServer(player, GenericServer.HUB, Intent.PLAYER_REQUEST);
}
private void openShop(Player player)
{
if (!Recharge.Instance.use(player, "Open Favourite", 2000, false, false))
{
return;
}
_manager.getFavourite().getShop().attemptShopOpen(player);
}
}