|
|
|
@ -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));
|
|
|
|
|