Mineplex2018-withcommit/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/managers/GameLootManager.java
2014-09-18 14:35:59 -05:00

173 lines
5.0 KiB
Java

package nautilus.game.arcade.managers;
import java.util.HashSet;
import java.util.Random;
import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.FireworkEffect;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
import mineplex.core.common.util.C;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilFirework;
import mineplex.core.common.util.UtilServer;
import mineplex.core.common.util.UtilTitle;
import mineplex.core.reward.Reward;
import mineplex.core.reward.RewardData;
import mineplex.core.reward.RewardManager;
import mineplex.core.reward.RewardRarity;
import mineplex.core.reward.rewards.InventoryReward;
import nautilus.game.arcade.ArcadeManager;
import nautilus.game.arcade.events.GameStateChangeEvent;
import nautilus.game.arcade.game.Game.GameState;
public class GameLootManager implements Listener
{
private ArcadeManager Manager;
private RewardManager _rewardManager;
private HashSet<Player> _players = new HashSet<Player>();
private long _startTime = 0;
public GameLootManager(ArcadeManager manager)
{
Manager = manager;
Manager.GetPluginManager().registerEvents(this, Manager.GetPlugin());
_rewardManager = new RewardManager(Manager.GetDonation(), Manager.getInventoryManager(),
100, 300,
750, 1500,
2000, 3000,
10000, 20000,
false);
//Chest
_rewardManager.addReward(new InventoryReward(_rewardManager, Manager.getInventoryManager(), "Treasure Chest", "Treasure Chest", 1, 1,
new ItemStack(Material.CHEST), RewardRarity.COMMON, 2));
//Key
_rewardManager.addReward(new InventoryReward(_rewardManager, Manager.getInventoryManager(), "Treasure Key", "Treasure Key", 1, 1,
new ItemStack(Material.DIAMOND), RewardRarity.UNCOMMON, 500));
}
@EventHandler
public void registerPlayers(GameStateChangeEvent event)
{
if (!Manager.IsRewardAchievements())
return;
if (event.GetState() != GameState.Live)
return;
for (Player player : event.GetGame().GetPlayers(true))
_players.add(player);
_startTime = System.currentTimeMillis();
}
@EventHandler
public void unregisterPlayer(PlayerQuitEvent event)
{
_players.remove(event.getPlayer());
}
@EventHandler(priority = EventPriority.MONITOR)
public void giveLoot(final GameStateChangeEvent event)
{
if (!Manager.IsRewardItems())
return;
if (event.GetState() != GameState.Dead)
return;
UtilServer.getServer().getScheduler().scheduleSyncDelayedTask(Manager.GetPlugin(), new Runnable()
{
public void run()
{
for (Player player : _players)
{
giveReward(player);
}
_players.clear();
}
}, 240);
//Delay after Achievements
}
public void giveReward(Player player)
{
double chance = Math.min(0.5, 0.12 + (System.currentTimeMillis() - _startTime)/3600000d);
if (Math.random() > chance)
return;
Reward reward = _rewardManager.nextReward(player, null, false, false);
RewardData rewardData = reward.giveReward("GameLoot", player);
String outputName = reward.getRarity().getColor() + rewardData.getFriendlyName();
//Log
System.out.println(F.name(player.getName()) + " found " + reward.getRarity().getName() + " treasure " + outputName);
//Self Display
UtilTitle.display(C.cDGreen + C.Bold + "Loot", "You found " + outputName, 20, 120, 20, player);
Random _random = new Random();
//Announce
if (reward.getRarity() != RewardRarity.COMMON)
{
Bukkit.broadcastMessage(F.main("Loot", F.name(player.getName()) + " found " + reward.getRarity().getName() + " treasure " + outputName));
}
//Effect
if (reward.getRarity() == RewardRarity.UNCOMMON)
{
FireworkEffect effect = FireworkEffect.builder().withColor(Color.fromRGB(_random.nextInt(255), _random.nextInt(255), _random.nextInt(255)))
.withFade(Color.fromRGB(_random.nextInt(255), _random.nextInt(255), _random.nextInt(255)))
.with(FireworkEffect.Type.STAR)
.build();
UtilFirework.playFirework(player.getEyeLocation(), effect);
}
else if (reward.getRarity() == RewardRarity.RARE)
{
FireworkEffect effect = FireworkEffect.builder().with(FireworkEffect.Type.BALL).withColor(Color.YELLOW).withFade(Color.WHITE).build();
UtilFirework.playFirework(player.getEyeLocation(), effect);
}
else if (reward.getRarity() == RewardRarity.LEGENDARY)
{
FireworkEffect effect = FireworkEffect.builder().with(FireworkEffect.Type.BALL_LARGE).withColor(Color.RED).withFade(Color.BLACK).build();
UtilFirework.playFirework(player.getEyeLocation(), effect);
}
}
@EventHandler
public void command(PlayerCommandPreprocessEvent event)
{
if (!event.getPlayer().isOp())
return;
//TODO Remove
if (event.getMessage().startsWith("/lootdebug"))
{
event.getPlayer().sendMessage(C.cGreen + C.Bold + "Loot Debug...");
giveReward(event.getPlayer());
event.setCancelled(true);
}
}
}