Proper booster activation events

This commit is contained in:
Shaun Bennett 2016-06-08 19:22:25 -05:00
parent 37a412f1bf
commit c9d5b91c52
9 changed files with 149 additions and 83 deletions

View File

@ -93,32 +93,14 @@ public class Booster
Booster booster = (Booster) o;
if (_boosterId != booster._boosterId) return false;
if (_accountId != booster._accountId) return false;
if (_duration != booster._duration) return false;
if (Double.compare(booster._multiplier, _multiplier) != 0) return false;
if (!_playerName.equals(booster._playerName)) return false;
if (!_uuid.equals(booster._uuid)) return false;
if (!_startTime.equals(booster._startTime)) return false;
if (!_endTime.equals(booster._endTime)) return false;
return _activationTime.equals(booster._activationTime);
return _accountId == booster._accountId;
}
@Override
public int hashCode()
{
int result;
long temp;
result = _boosterId;
result = 31 * result + _playerName.hashCode();
result = 31 * result + _uuid.hashCode();
int result = _boosterId;
result = 31 * result + _accountId;
result = 31 * result + _duration;
temp = Double.doubleToLongBits(_multiplier);
result = 31 * result + (int) (temp ^ (temp >>> 32));
result = 31 * result + _startTime.hashCode();
result = 31 * result + _endTime.hashCode();
result = 31 * result + _activationTime.hashCode();
return result;
}

View File

@ -3,17 +3,17 @@ package mineplex.core.boosters;
import mineplex.core.MiniPlugin;
import mineplex.core.account.CoreClientManager;
import mineplex.core.boosters.command.BoosterCommand;
import mineplex.core.boosters.event.BoosterEnableEvent;
import mineplex.core.boosters.event.BoosterActivateEvent;
import mineplex.core.boosters.event.BoosterDeactivateEvent;
import mineplex.core.boosters.gui.BoosterShop;
import mineplex.core.boosters.tips.BoosterTipManager;
import mineplex.core.common.util.Callback;
import mineplex.core.common.util.UtilEvent;
import mineplex.core.donation.DonationManager;
import mineplex.core.inventory.InventoryManager;
import mineplex.core.serverConfig.ServerConfiguration;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.plugin.java.JavaPlugin;
@ -34,12 +34,10 @@ public class BoosterManager extends MiniPlugin
private BoosterTipManager _tipManager;
private BoosterShop _shop;
// Should we continually pull all boosters from the API and cache them? Used in Hub servers but not Arcade
private boolean _cacheAllBoosters;
private long _cacheLastUpdated;
private Map<String, List<Booster>> _boosterCache;
private Map<String, List<Booster>> _boosterCache = new HashMap<>();
public BoosterManager(JavaPlugin plugin, CoreClientManager clientManager, DonationManager donationManager, InventoryManager inventoryManager, boolean cacheAllBoosters)
public BoosterManager(JavaPlugin plugin, CoreClientManager clientManager, DonationManager donationManager, InventoryManager inventoryManager)
{
super("Booster Manager", plugin);
@ -48,9 +46,18 @@ public class BoosterManager extends MiniPlugin
_donationManager = donationManager;
_inventoryManager = inventoryManager;
_cacheAllBoosters = cacheAllBoosters;
_tipManager = new BoosterTipManager(plugin, clientManager, donationManager);
_shop = new BoosterShop(this, clientManager, donationManager);
try
{
_boosterCache = _repository.getBoosters();
}
catch (Exception e)
{
System.out.println("Failed to load boosters on server start.");
e.printStackTrace();
}
}
@Override
@ -59,11 +66,6 @@ public class BoosterManager extends MiniPlugin
addCommand(new BoosterCommand(this));
}
public Map<String, List<Booster>> getBoosterCache()
{
return _boosterCache == null ? new HashMap<>() : _boosterCache;
}
public void getBoostersAsync(Callback<Map<String, List<Booster>>> callback)
{
runAsync(() -> {
@ -74,18 +76,28 @@ public class BoosterManager extends MiniPlugin
long timeTaken = System.currentTimeMillis() - time;
runSync(() -> {
if (_boosterCache != null)
_boosterCache.entrySet().stream()
.filter(entry -> entry.getValue().size() > 0)
.filter(entry -> boosters.get(entry.getKey()) == null)
.forEach(entry -> callNextTick(new BoosterDeactivateEvent(entry.getValue().get(0))));
for (Map.Entry<String, List<Booster>> entry : boosters.entrySet())
{
for (Map.Entry<String, List<Booster>> entry : boosters.entrySet())
List<Booster> current = _boosterCache.get(entry.getKey());
if (current == null || current.get(0) == null)
{
List<Booster> current = _boosterCache.get(entry.getKey());
if (current == null || (current.size() < entry.getValue().size()))
getPluginManager().callEvent(new BoosterEnableEvent(entry.getValue().get(0)));
callNextTick(new BoosterActivateEvent(entry.getValue().get(0)));
}
else if (!current.get(0).equals(entry.getValue().get(0)))
{
callNextTick(new BoosterDeactivateEvent(current.get(0)));
callNextTick(new BoosterActivateEvent(entry.getValue().get(0)));
}
}
_cacheLastUpdated = System.currentTimeMillis();
// System.out.println("Got Boosters. Took: " + timeTaken + "ms");
System.out.println("Got Boosters. Took: " + timeTaken + "ms");
_boosterCache = boosters;
if (callback != null) callback.run(boosters);
});
@ -149,7 +161,7 @@ public class BoosterManager extends MiniPlugin
@EventHandler
public void updateCache(UpdateEvent event)
{
if (event.getType() == UpdateType.MIN_01)
if (event.getType() == UpdateType.SEC_20)
{
getBoostersAsync(null);
}
@ -189,4 +201,9 @@ public class BoosterManager extends MiniPlugin
entry.getValue().forEach(System.out::println);
});
}
private void callNextTick(Event event)
{
runSync(() -> getPluginManager().callEvent(event));
}
}

View File

@ -7,11 +7,11 @@ import org.bukkit.event.HandlerList;
/**
* @author Shaun Bennett
*/
public class BoosterEnableEvent extends Event
public class BoosterActivateEvent extends Event
{
private Booster _booster;
public BoosterEnableEvent(Booster booster)
public BoosterActivateEvent(Booster booster)
{
_booster = booster;
}

View File

@ -0,0 +1,36 @@
package mineplex.core.boosters.event;
import mineplex.core.boosters.Booster;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
/**
* @author Shaun Bennett
*/
public class BoosterDeactivateEvent extends Event
{
private Booster _booster;
public BoosterDeactivateEvent(Booster booster)
{
_booster = booster;
}
public Booster getBooster()
{
return _booster;
}
private static final HandlerList _handlers = new HandlerList();
private static HandlerList getHandlerList()
{
return _handlers;
}
@Override
public HandlerList getHandlers()
{
return getHandlerList();
}
}

View File

@ -31,6 +31,8 @@ import mineplex.serverdata.servers.ServerManager;
public class ProfileCacheManager extends MiniPlugin implements GameProfileRepository
{
private static ProfileCacheManager INSTANCE;
private YggdrasilGameProfileRepository _mojangProfileRepository;
private RedisDataRepository<ProfileData> _profileRepository;
private Gson _gson;
@ -65,8 +67,9 @@ public class ProfileCacheManager extends MiniPlugin implements GameProfileReposi
System.out.println("Are you using the correct modified Craftbukkit?");
System.out.println("================================================");
}
}
INSTANCE = this;
}
public GameProfile attemptToLoadProfile(String playerName)
{
@ -131,4 +134,9 @@ public class ProfileCacheManager extends MiniPlugin implements GameProfileReposi
ProfileData data = new ProfileData(profile.getId(), profile.getName(), _gson.toJson(profile.getProperties()));
_profileRepository.addElement(data, 60 * 60 * 24); // 1 day
}
public static ProfileCacheManager getInstance()
{
return INSTANCE;
}
}

View File

@ -154,7 +154,7 @@ public class Hub extends JavaPlugin implements IRelation
CustomDataManager customDataManager = new CustomDataManager(this, clientManager);
PersonalServerManager personalServerManager = new PersonalServerManager(this, clientManager);
BoosterManager boosterManager = new BoosterManager(this, clientManager, donationManager, inventoryManager, true);
BoosterManager boosterManager = new BoosterManager(this, clientManager, donationManager, inventoryManager);
HubManager hubManager = new HubManager(this, blockRestore, clientManager, incognito, donationManager, inventoryManager, conditionManager, disguiseManager, new TaskManager(this, clientManager, webServerAddress), portal, partyManager, preferenceManager, petManager, pollManager, statsManager, achievementManager, new HologramManager(this, packetHandler), npcManager, personalServerManager, packetHandler, punish, serverStatusManager, customDataManager, boosterManager);
QueueManager queueManager = new QueueManager(this, clientManager, donationManager, new EloManager(this, clientManager), partyManager);

View File

@ -164,7 +164,7 @@ public class Arcade extends JavaPlugin
PetManager petManager = new PetManager(this, _clientManager, _donationManager, inventoryManager, disguiseManager, creature, blockRestore, webServerAddress);
MountManager mountManager = new MountManager(this, _clientManager, _donationManager, blockRestore, disguiseManager);
GadgetManager gadgetManager = new GadgetManager(this, _clientManager, _donationManager, inventoryManager, mountManager, petManager, preferenceManager, disguiseManager, blockRestore, projectileManager, achievementManager, packetHandler, hologramManager);
BoosterManager boosterManager = new BoosterManager(this, _clientManager, _donationManager, inventoryManager, false);
BoosterManager boosterManager = new BoosterManager(this, _clientManager, _donationManager, inventoryManager);
CosmeticManager cosmeticManager = new CosmeticManager(this, _clientManager, _donationManager, inventoryManager, gadgetManager, mountManager, petManager, null, boosterManager);
cosmeticManager.setInterfaceSlot(7);
cosmeticManager.disableTeamArmor();

View File

@ -3,16 +3,15 @@ package nautilus.game.arcade.booster;
import com.mojang.authlib.GameProfile;
import mineplex.core.MiniPlugin;
import mineplex.core.boosters.Booster;
import mineplex.core.boosters.BoosterManager;
import mineplex.core.boosters.event.BoosterEnableEvent;
import mineplex.core.boosters.event.BoosterActivateEvent;
import mineplex.core.boosters.event.BoosterDeactivateEvent;
import mineplex.core.boosters.tips.BoosterTipManager;
import mineplex.core.boosters.tips.TipAddResult;
import mineplex.core.common.CurrencyType;
import mineplex.core.common.util.*;
import mineplex.core.disguise.DisguiseManager;
import mineplex.core.disguise.disguises.DisguisePlayer;
import mineplex.core.hologram.Hologram;
import mineplex.core.hologram.HologramManager;
import mineplex.core.profileCache.ProfileCacheManager;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import org.bukkit.Location;
@ -49,6 +48,7 @@ public class BoosterPodium extends MiniPlugin
_podiumLocation = podiumLocation;
addPodium();
updateNpcs();
}
public void addPodium()
@ -65,7 +65,9 @@ public class BoosterPodium extends MiniPlugin
public void updateNpcs()
{
System.out.println("Updating Npcs.");
Booster activeBooster = _gameBoosterManager.getActiveBooster();
System.out.println("Active booster: " + activeBooster);
if (activeBooster != null)
{
if (_activeArmorStand != null)
@ -76,18 +78,18 @@ public class BoosterPodium extends MiniPlugin
Location armorStandLocation = _podiumLocation.clone();
ArmorStand armorStand = _podiumLocation.getWorld().spawn(armorStandLocation, ArmorStand.class);
armorStand.setVisible(true);
armorStand.setCustomNameVisible(true);
armorStand.setCustomName(getNameString(activeBooster));
armorStand.setCustomNameVisible(false);
armorStand.setCustomName("");
armorStand.setGravity(true);
armorStand.setArms(true);
armorStand.setBasePlate(true);
armorStand.setRemoveWhenFarAway(false);
// armorStand.setHelmet(UtilSkull.getPlayerHead(activeBooster.getPlayerName(), activeBooster.getPlayerName(), null));
// armorStand.setChestplate(new ItemStack(Material.GOLD_CHESTPLATE));
// armorStand.setLeggings(new ItemStack(Material.GOLD_LEGGINGS));
// armorStand.setBoots(new ItemStack(Material.GOLD_BOOTS));
armorStand.setHelmet(UtilSkull.getPlayerHead(activeBooster.getPlayerName(), activeBooster.getPlayerName(), null));
armorStand.setChestplate(new ItemStack(Material.GOLD_CHESTPLATE));
armorStand.setLeggings(new ItemStack(Material.GOLD_LEGGINGS));
armorStand.setBoots(new ItemStack(Material.GOLD_BOOTS));
armorStand.setItemInHand(new ItemStack(Material.EMERALD));
if (_hologram == null)
@ -96,17 +98,26 @@ public class BoosterPodium extends MiniPlugin
_hologram.start();
}
/*
runAsync(() -> {
ProfileLoader loader = new ProfileLoader(activeBooster.getUuid().toString(), activeBooster.getPlayerName());
GameProfile profile = new GameProfile(activeBooster.getUuid(), "");
if (ProfileLoader.addProperties(profile))
GameProfile profile = null;
// try cache first
profile = ProfileCacheManager.getInstance().attemptToLoadProfile(activeBooster.getPlayerName());
if (profile == null)
{
runSync(() -> {
DisguisePlayer disguise = new DisguisePlayer(_activeArmorStand, profile);
_disguiseManager.disguise(disguise);
});
profile = new GameProfile(activeBooster.getUuid(), "");
ProfileLoader.addProperties(profile);
}
final GameProfile finalProfile = profile;
runSync(() -> {
DisguisePlayer disguise = new DisguisePlayer(_activeArmorStand, finalProfile);
_disguiseManager.disguise(disguise);
});
});
*/
_activeBooster = activeBooster;
_activeArmorStand = armorStand;
@ -166,7 +177,13 @@ public class BoosterPodium extends MiniPlugin
}
@EventHandler
public void onBoosterEnable(BoosterEnableEvent event)
public void onBoosterEnable(BoosterActivateEvent event)
{
updateNpcs();
}
@EventHandler
public void onBoosterDisable(BoosterDeactivateEvent event)
{
updateNpcs();
}

View File

@ -3,6 +3,8 @@ package nautilus.game.arcade.booster;
import mineplex.core.MiniPlugin;
import mineplex.core.boosters.Booster;
import mineplex.core.boosters.BoosterManager;
import mineplex.core.boosters.event.BoosterActivateEvent;
import mineplex.core.boosters.event.BoosterDeactivateEvent;
import mineplex.core.boosters.tips.BoosterTipManager;
import mineplex.core.boosters.tips.TipAddResult;
import mineplex.core.common.CurrencyType;
@ -14,12 +16,15 @@ import mineplex.core.hologram.HologramManager;
import nautilus.game.arcade.events.GameStateChangeEvent;
import nautilus.game.arcade.game.Game;
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 org.omg.PortableServer.IMPLICIT_ACTIVATION_POLICY_ID;
import java.util.List;
/**
* @author Shaun Bennett
*/
@ -29,7 +34,6 @@ public class GameBoosterManager extends MiniPlugin
private BoosterManager _boosterManager;
private BoosterPodium _boosterPodium;
private Booster _activeBooster;
public GameBoosterManager(JavaPlugin plugin, BoosterManager boosterManager, DisguiseManager disguiseManager, HologramManager hologramManager, GameServerConfig gameServerConfig)
{
@ -43,34 +47,20 @@ public class GameBoosterManager extends MiniPlugin
public Booster getActiveBooster()
{
return _activeBooster;
}
@EventHandler
public void update(GameStateChangeEvent event)
{
if (event.GetState() != Game.GameState.Loading)
return;
updateActiveBooster();
}
private void updateActiveBooster()
{
_boosterManager.getBoostersAsync(_gameServerConfig.ServerGroup, boosters -> {
_activeBooster = boosters.size() > 0 ? boosters.get(0) : null;
});
return _boosterManager.getActiveBoosterFromCache(_gameServerConfig.ServerGroup);
}
public void attemptTip(Player player)
{
if (_activeBooster == null)
Booster active = getActiveBooster();
if (active == null)
{
UtilPlayer.message(player, F.main("Tip", "There is no active booster to tip!"));
return;
}
_boosterManager.getTipManager().addTip(player, _activeBooster, result -> {
_boosterManager.getTipManager().addTip(player, active, result -> {
if (result == TipAddResult.SUCCESS)
{
UtilPlayer.message(player, F.main("Tip", "Thanks for your tip! You earned "
@ -85,4 +75,20 @@ public class GameBoosterManager extends MiniPlugin
}
});
}
@EventHandler
public void onActivate(BoosterActivateEvent event)
{
System.out.println("booster activate: " + event.getBooster());
Booster booster = event.getBooster();
Bukkit.broadcastMessage(F.main("Booster", F.name(booster.getPlayerName()) + " has activated a booster for " + booster.getMultiplier() + "x Gems!" ));
}
@EventHandler
public void onDeactivate(BoosterDeactivateEvent event)
{
System.out.println("booster deactivate: " + event.getBooster());
}
}