Moved everything to CastleManager, changed to rightclick

This commit is contained in:
LCastr0 2017-04-27 15:12:33 -03:00
parent 124d22ce31
commit 02785d5dfd
4 changed files with 141 additions and 184 deletions

View File

@ -9,7 +9,10 @@ import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.entity.EntitySpawnEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.MiniPlugin;
@ -18,7 +21,6 @@ import mineplex.core.common.util.UtilAction;
import mineplex.core.common.util.UtilAlg;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.common.util.UtilWorld;
import mineplex.core.gadget.gadgets.particle.king.events.UpdateKingEvent;
import mineplex.core.gadget.gadgets.particle.king.types.King;
import mineplex.core.gadget.gadgets.particle.king.types.Peasant;
import mineplex.core.hologram.Hologram;
@ -140,30 +142,86 @@ public class CastleManager extends MiniPlugin
}
}
/**
* Removes old king from castle if they are still in it
* @param event
*/
@EventHandler
public void onUpdateKing(UpdateKingEvent event)
public void onTagPeasant(PlayerInteractEntityEvent event)
{
if (!isHub())
return;
King oldKing = event.getOldKing();
King newKing = event.getNewKing();
if (oldKing != null && oldKing.getKing().isOnline())
Player clicker = event.getPlayer();
if (!isKing(clicker))
return;
if (!(event.getRightClicked() instanceof Player))
return;
Player clicked = (Player) event.getRightClicked();
if (!isPeasant(clicked))
return;
King king = getKing(clicker);
Peasant peasant = getPeasant(clicked);
if (peasant.isInCooldown())
{
// Removes old king from the throne
if (UtilAlg.inBoundingBox(oldKing.getKing().getLocation(), _castleLocationA, _castleLocationB))
{
oldKing.getKing().teleport(_removedKingLocation);
UtilPlayer.message(oldKing.getKing(), F.main("Kingdom", "You are no longer the king of that castle!"));
}
UtilPlayer.message(clicker, F.main("Kingdom", "You can't tag " + F.name(clicked.getName()) + " yet! That player is cooling down!"));
return;
}
if (newKing != null && newKing.getKing().isOnline())
Bukkit.broadcastMessage(F.main("Kingdom", "" + F.name(newKing.getKing().getName()) + " is the new King of this lobby!"));
setKing(newKing);
if (king.hasPeasant(peasant))
{
UtilPlayer.message(clicker, F.main("Kingdom", "You can't tag " + F.name(clicked.getName()) + "! That player is already your peasant!"));
return;
}
if (peasant.getKing() != null)
{
King oldKing = peasant.getKing();
oldKing.removePeasant(peasant);
UtilPlayer.message(oldKing.getKing(), F.main("Kingdom", "" + F.name(clicker.getName()) + " stole one of your peasants! You now have " + F.count(king.amountOfPeasants()) + " peasants!"));
}
peasant.setCooldown();
king.addPeasant(peasant);
peasant.setKing(king);
UtilPlayer.message(clicker, F.main("Kingdom", "You tagged " + F.name(clicked.getName()) + " as a peasant! You now have " + F.count(king.amountOfPeasants()) + " peasants!"));
UtilPlayer.message(peasant.getPeasant(), F.main("Kingdom", "You were tagged by " + F.name(clicked.getName()) + " as a peasant!"));
updateLobbyKing();
}
/**
* Sets player as peasant
* @param event
*/
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event)
{
Player player = event.getPlayer();
setPlayerAsPeasant(player);
}
@EventHandler
public void onPlayerLeave(PlayerQuitEvent event)
{
Player player = event.getPlayer();
if (isPeasant(player))
{
Peasant peasant = getPeasant(player);
if (peasant.getKing() != null)
{
peasant.getKing().removePeasant(peasant);
}
removePeasant(player);
}
else if (isKing(player))
{
if (getKing(player) != null)
getKing(player).clearPeasants();
}
updateLobbyKing();
}
/**
@ -200,35 +258,57 @@ public class CastleManager extends MiniPlugin
}
}
private void updateKing(King oldKing, King newKing)
{
setKing(newKing);
if (!isHub())
return;
if (oldKing != null && oldKing.getKing().isOnline())
{
// Removes old king from the throne
if (UtilAlg.inBoundingBox(oldKing.getKing().getLocation(), _castleLocationA, _castleLocationB))
{
Entity target = oldKing.getKing();
while (target.getVehicle() != null)
{
target = target.getVehicle();
}
UtilAction.velocity(target, UtilAlg.getTrajectory(target.getLocation(), _removedKingLocation).multiply(-1), 1.5, true, 0.8, 0, 1.0, true);
UtilPlayer.message(oldKing.getKing(), F.main("Kingdom", "You are no longer the king of that castle!"));
}
}
if (newKing != null && newKing.getKing().isOnline())
Bukkit.broadcastMessage(F.main("Kingdom", "" + F.name(newKing.getKing().getName()) + " is the new King of this lobby!"));
}
public void setPlayerAsKing(Player player)
{
if (isKing(player))
return;
if (isPeasant(player))
removePeasant(player, false);
removePeasant(player);
_kings.put(player, new King(player));
}
public boolean isKing(Player player)
private boolean isKing(Player player)
{
return _kings.containsKey(player);
}
public void removeKing(Player player, boolean setPeasant)
private void removeKing(Player player)
{
if (!isKing(player))
return;
_kings.get(player).clearPeasants();
_kings.remove(player);
if (setPeasant)
setPlayerAsPeasant(player);
}
public King getKing(Player player)
private King getKing(Player player)
{
if (!isKing(player))
return null;
@ -242,28 +322,25 @@ public class CastleManager extends MiniPlugin
return;
if (isKing(player))
removeKing(player, false);
removeKing(player);
_peasants.put(player, new Peasant(player));
}
public boolean isPeasant(Player player)
private boolean isPeasant(Player player)
{
return _peasants.containsKey(player);
}
public void removePeasant(Player player, boolean setKing)
private void removePeasant(Player player)
{
if (!isPeasant(player))
return;
_peasants.remove(player);
if (setKing)
setPlayerAsKing(player);
}
public Peasant getPeasant(Player player)
private Peasant getPeasant(Player player)
{
if (!isPeasant(player))
return null;
@ -271,10 +348,13 @@ public class CastleManager extends MiniPlugin
return _peasants.get(player);
}
public King getLobbyKing()
public void updateLobbyKing()
{
if (_kings.size() == 0)
return null;
{
removeKing();
return;
}
King lobbyKing = null;
for (King king : _kings.values())
@ -284,6 +364,23 @@ public class CastleManager extends MiniPlugin
else if (lobbyKing.amountOfPeasants() < king.amountOfPeasants())
lobbyKing = king;
}
return lobbyKing;
if (_king != null && lobbyKing != null)
{
if (!_king.equals(lobbyKing))
{
updateKing(_king, lobbyKing);
}
}
else if (lobbyKing != null)
{
setKing(lobbyKing);
}
}
private void removeKing()
{
_king = null;
updateHologram();
}
}

View File

@ -3,30 +3,18 @@ package mineplex.core.gadget.gadgets.particle.king;
import java.awt.Color;
import java.time.Month;
import java.time.YearMonth;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.util.Vector;
import mineplex.core.common.shape.ShapeWings;
import mineplex.core.common.util.C;
import mineplex.core.common.util.F;
import mineplex.core.common.util.LineFormat;
import mineplex.core.common.util.UtilParticle;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.common.util.UtilText;
import mineplex.core.gadget.GadgetManager;
import mineplex.core.gadget.gadgets.particle.king.events.UpdateKingEvent;
import mineplex.core.gadget.gadgets.particle.king.types.King;
import mineplex.core.gadget.gadgets.particle.king.types.Peasant;
import mineplex.core.gadget.types.ParticleGadget;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
@ -36,14 +24,10 @@ public class ParticleKing extends ParticleGadget
private static final int CROWN_POINTS = 12;
private Map<Player, King> _kings = new HashMap<>();
private Map<Player, Long> _taggedPlayers = new HashMap<>();
private ShapeWings _capeRed = new ShapeWings(UtilParticle.ParticleType.RED_DUST.particleName, new Vector(0.2,0.2,0.2), 1, 0, false, ShapeWings.NO_ROTATION, ShapeWings.KINGS_CAPE);
private ShapeWings _capeWhite = new ShapeWings(UtilParticle.ParticleType.RED_DUST.particleName, new Vector(0.1,0.1,0.1), 1, 0, true, ShapeWings.NO_ROTATION, ShapeWings.KINGS_CAPE);
private CastleManager _castleManager;
private boolean _inLobby = false;
public ParticleKing(GadgetManager manager, CastleManager castleManager)
{
@ -54,8 +38,6 @@ public class ParticleKing extends ParticleGadget
-14,
Material.GOLD_HELMET, (byte) 0, YearMonth.of(2017, Month.MAY));
_castleManager = castleManager;
if (_castleManager.isHub())
_inLobby = true;
}
@Override
@ -94,10 +76,7 @@ public class ParticleKing extends ParticleGadget
{
super.enableCustom(player, message);
_castleManager.setPlayerAsKing(player);
if (_inLobby)
{
updateKing();
}
_castleManager.updateLobbyKing();
}
@Override
@ -105,131 +84,6 @@ public class ParticleKing extends ParticleGadget
{
super.disableCustom(player, message);
_castleManager.setPlayerAsPeasant(player);
if (_inLobby)
{
updateKing();
}
}
/**
* When a player clicks another player, sets them as peasant
* @param event
*/
@EventHandler
public void tagPlayer(EntityDamageByEntityEvent event)
{
if (!_inLobby)
return;
if (!(event.getDamager() instanceof Player))
return;
if (!(event.getEntity() instanceof Player))
return;
Player clicker = (Player) event.getDamager();
Player clicked = (Player) event.getEntity();
// Checks if particle is active
if (_castleManager.isPeasant(clicker))
return;
// Checks if the clicked player isn't a king
if (_castleManager.isKing(clicked))
return;
Peasant peasant = _castleManager.getPeasant(clicked);
// Checks if the clicked player isn't cooling down
if (peasant.isInCooldown())
{
UtilPlayer.message(clicker, F.main("Kingdom", "You can't tag " + F.name(clicked.getName()) + " yet! That player is cooling down!"));
return;
}
King king = _castleManager.getKing(clicker);
if (king.hasPeasant(peasant))
{
UtilPlayer.message(clicker, F.main("Kingdom", "You can't tag " + F.name(clicked.getName()) + "! That player is already your peasant!"));
return;
}
// Removes old king
if (peasant.getKing() != null)
{
King oldKing = peasant.getKing();
oldKing.removePeasant(peasant);
UtilPlayer.message(oldKing.getKing(), F.main("Kingdom", "" + F.name(clicker.getName()) + " stole one of your peasants! You now have " + F.count(king.amountOfPeasants()) + " peasants!"));
}
peasant.setCooldown();
king.addPeasant(peasant);
peasant.setKing(king);
UtilPlayer.message(clicker, F.main("Kingdom", "You tagged " + F.name(clicked.getName()) + " as a peasant! You now have " + F.count(king.amountOfPeasants()) + " peasants!"));
UtilPlayer.message(peasant.getPeasant(), F.main("Kingdom", "You were tagged by " + F.name(clicked.getName()) + " as a peasant!"));
updateKing();
}
/**
* Sets player as peasant
* @param event
*/
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event)
{
Player player = event.getPlayer();
_castleManager.setPlayerAsPeasant(player);
}
/**
* Removes peasant from king, or removes king from peasant
* @param event
*/
@EventHandler
public void onPlayerLeave(PlayerQuitEvent event)
{
if (!_inLobby)
return;
Player player = event.getPlayer();
if (_castleManager.isPeasant(player))
{
Peasant peasant = _castleManager.getPeasant(player);
if (peasant.getKing() != null)
{
peasant.setKing(null);
}
_castleManager.removePeasant(player, false);
}
else if (_castleManager.isKing(player))
{
_castleManager.removeKing(player, false);
}
updateKing();
}
/**
* Updates the current lobby king
*/
private void updateKing()
{
if (_inLobby)
{
King king = _castleManager.getLobbyKing();
King oldKing = _castleManager.getKing();
if (king != null && oldKing != null)
{
if (!king.equals(oldKing))
{
Bukkit.getPluginManager().callEvent(new UpdateKingEvent(king, oldKing));
}
} else
{
Bukkit.getPluginManager().callEvent(new UpdateKingEvent(king, oldKing));
}
}
_castleManager.updateLobbyKing();
}
}

View File

@ -43,6 +43,7 @@ public class King
public void clearPeasants()
{
_peasants.forEach(Peasant::removeKing);
_peasants.clear();
}
}

View File

@ -25,7 +25,7 @@ public class Peasant
public boolean isInCooldown()
{
return UtilTime.elapsed(_cooldown, PLAYER_COOLDOWN);
return !UtilTime.elapsed(_cooldown, PLAYER_COOLDOWN);
}
public void setCooldown()
@ -43,4 +43,9 @@ public class Peasant
_king = king;
}
public void removeKing()
{
_king = null;
}
}