Code clean-up and documentation

This commit is contained in:
Shaun Bennett 2016-06-12 21:18:11 -05:00
parent cf87469400
commit aa785ed3d5
8 changed files with 76 additions and 45 deletions

View File

@ -29,17 +29,31 @@ import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.*;
/**
* BoosterManager handles the majority of logic for creating and getting Boosters. Every BoosterManager stores a cache
* for all boosters across all servers. We pull all boosters from the API when the server boots up. To keep them in sync,
* instead of consistently polling the API I have decided to go with a redis pub/sub solution to ensuring all boosters
* across all servers are up to date. Whenever the Booster API receives a call to add or modify boosters, it will publish
* an updated version of all boosters over redis.
*
* Boosters are enabled on live servers using "Booster Groups". A {@link mineplex.serverdata.data.ServerGroup} can specify
* which BoosterGroup applies to it. If there is no BoosterGroup, then it means the server does not use boosters. To add
* a BoosterGroup, you must add to the "boostergroups" set on redis (the same way the servergroups set works), otherwise
* the API will return an error saying that BoosterGroup does not exist. Currently BoosterGroups are no more than a String
* key for Boosters. In the future we may want to look into implementing BoosterGroup specific data such as default
* booster length and multiplier.
*
* @author Shaun Bennett
*/
public class BoosterManager extends MiniPlugin
{
// The InventoryManager item name for boosters. This is required to activate a booster on servers
public static final String BOOSTER_ITEM = "Game Booster";
// Item in arcade lobbies that opens the booster gui
public static final ItemStack INTERFACE_ITEM = ItemStackFactory.Instance.CreateStack(Material.EMERALD, (byte)0, 1, ChatColor.RESET + C.cGreen + "Booster Menu");
// Slot for the booster gui item
public static final int INTERFACE_SLOT = 6;
private BoosterRepository _repository;
@ -91,6 +105,10 @@ public class BoosterManager extends MiniPlugin
addCommand(new BoosterCommand(this));
}
/**
* Make an API call to grab all Boosters
*/
@Deprecated
public void getBoostersAsync(Callback<Map<String, List<Booster>>> callback)
{
runAsync(() -> {
@ -113,6 +131,33 @@ public class BoosterManager extends MiniPlugin
});
}
/**
* Make an API call to grab all boosters for a specific booster group
* @param boosterGroup
* @param callback
*/
@Deprecated
public void getBoostersAsync(String boosterGroup, Callback<List<Booster>> callback)
{
runAsync(() -> {
try
{
List<Booster> boosters = _repository.getBoosters(boosterGroup);
if (callback != null) runSync(() -> callback.run(boosters));
}
catch (Exception e)
{
System.err.println("Failed to grab boosters for boostergroup: " + boosterGroup);
e.printStackTrace();
}
});
}
/**
* Process the new boosterMap whenever a BoosterUpdateEvent is sent. This will compare itself to the current
* cached BoosterMap and call events when it finds a booster was activated or deactivated
* @param boosterMap The new booster map
*/
private void handleBoosterUpdate(Map<String, List<Booster>> boosterMap)
{
_boosterCache.entrySet().stream()
@ -174,9 +219,21 @@ public class BoosterManager extends MiniPlugin
tickBoosterCache();
}
/**
* Return all boosters for the active booster group
* @return list of boosters, or null if there is no active booster group
*/
public List<Booster> getBoosters()
{
return _boosterCache.get(_boosterGroup);
if (_boosterGroup == null || _boosterGroup.length() == 0)
{
return null;
}
else
{
List<Booster> boosters = _boosterCache.get(_boosterGroup);
return boosters == null ? Collections.emptyList() : boosters;
}
}
public long getBoostTime()
@ -201,10 +258,10 @@ public class BoosterManager extends MiniPlugin
public Booster getActiveBooster()
{
return getActiveBoosterFromCache(_boosterGroup);
return getActiveBooster(_boosterGroup);
}
public Booster getActiveBoosterFromCache(String boosterGroup)
public Booster getActiveBooster(String boosterGroup)
{
List<Booster> boosters = _boosterCache.get(boosterGroup);
if (boosters != null)
@ -219,22 +276,6 @@ public class BoosterManager extends MiniPlugin
return null;
}
public void getBoostersAsync(String serverGroup, Callback<List<Booster>> callback)
{
runAsync(() -> {
try
{
List<Booster> boosters = _repository.getBoosters(serverGroup);
if (callback != null) runSync(() -> callback.run(boosters));
}
catch (Exception e)
{
System.err.println("Failed to grab boosters for servergroup: " + serverGroup);
e.printStackTrace();
}
});
}
public void activateBooster(Player player, Callback<BoosterApiResponse> callback)
{
activateBooster(_boosterGroup, player, callback);
@ -262,6 +303,9 @@ public class BoosterManager extends MiniPlugin
_shop.attemptShopOpen(player);
}
/**
* Booster updates are sent from {@link mineplex.core.boosters.redis.BoosterUpdateListener}
*/
@EventHandler
public void onBoosterUpdate(BoosterUpdateEvent event)
{
@ -317,17 +361,6 @@ public class BoosterManager extends MiniPlugin
}
}
public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException
{
BoosterRepository repository = new BoosterRepository();
Map<String, List<Booster>> boosters = repository.getBoosters();
boosters.entrySet().forEach((entry) -> {
System.out.println("Booster Entry: " + entry.getKey());
System.out.println("Booster Size: " + entry.getValue().size());
entry.getValue().forEach(System.out::println);
});
}
private void callNextTick(Event event)
{
runSync(() -> getPluginManager().callEvent(event));

View File

@ -1,8 +0,0 @@
package mineplex.core.boosters;
/**
* @author Shaun Bennett
*/
public class BoosterMap
{
}

View File

@ -14,6 +14,8 @@ import java.util.Map;
import java.util.UUID;
/**
* Boosters interaction is handled through a web API. All data is represented as JSON and then parsed using gson.
*
* @author Shaun Bennett
*/
public class BoosterRepository extends ApiEndpoint

View File

@ -5,6 +5,9 @@ import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
/**
* Called when a Booster is activated. This will be called regardless of which "BoosterGroup" the current server is set
* to, so if you only want Boosters on the current BoosterGroup, you will need to verify it.
*
* @author Shaun Bennett
*/
public class BoosterActivateEvent extends Event

View File

@ -5,6 +5,8 @@ import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
/**
* Called when a Booster is finished.
*
* @author Shaun Bennett
*/
public class BoosterDeactivateEvent extends Event

View File

@ -8,6 +8,8 @@ import java.util.List;
import java.util.Map;
/**
* Called when {@link mineplex.core.boosters.redis.BoosterUpdateListener} receives updated Boosters over redis pubsub.
*
* @author Shaun Bennett
*/
public class BoosterUpdateEvent extends Event

View File

@ -215,7 +215,7 @@ public class ServerGameMenu extends ShopPageBase<ServerManager, QuickShop>
// Boosters
if (serverGroup != null)
{
Booster booster = getPlugin().getBoosterManager().getActiveBoosterFromCache(serverGroup);
Booster booster = getPlugin().getBoosterManager().getActiveBooster(serverGroup);
if (booster != null)
{
// append to start of lore

View File

@ -14,15 +14,12 @@ import mineplex.core.common.util.UtilWorld;
import mineplex.core.disguise.DisguiseManager;
import mineplex.core.hologram.HologramManager;
import mineplex.core.npc.NpcManager;
import nautilus.game.arcade.game.GameServerConfig;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.List;
/**
* @author Shaun Bennett
*/
@ -48,7 +45,7 @@ public class GameBoosterManager extends MiniPlugin
public Booster getActiveBooster()
{
return _boosterManager.getActiveBoosterFromCache(_boosterGroup);
return _boosterManager.getActiveBooster(_boosterGroup);
}
public void attemptTip(Player player)