Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pvp utils improvement #69

Merged
merged 3 commits into from
Jun 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/main/java/pvpmode/EnumPvPMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package pvpmode;

public enum EnumPvPMode
{

ON, OFF, WARMUP

}
5 changes: 5 additions & 0 deletions src/main/java/pvpmode/PvPData.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public PvPData (EntityPlayer player)
pvpDataTag = persistent.getCompoundTag (PVP_DATA_NBT_KEY);
}

/**
* Returns whether PvP is enabled for the player. Note that this doesn't
* mean that the player can actually do PvP - this depends on other
* parameters like the gamemode. Use {@link PvPUtils#getPvPMode} for this.
*/
public boolean isPvPEnabled ()
{
return pvpDataTag.getBoolean (PVP_ENABLED_NBT_KEY);
Expand Down
85 changes: 27 additions & 58 deletions src/main/java/pvpmode/PvPEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,48 +36,43 @@ public void interceptPvP (LivingAttackEvent event)
if (attacker == null || victim == null)
return;

PvPData attackerData = PvPUtils.getPvPData (attacker);
PvPData victimData = PvPUtils.getPvPData (victim);
EnumPvPMode attackerMode = PvPUtils.getPvPMode (attacker);
EnumPvPMode victimMode = PvPUtils.getPvPMode (victim);

if (attacker.capabilities.allowFlying)
{
if (attacker == event.source.getEntity ())
fly (attacker);

event.setCanceled (true);
return;
}

if (victim.capabilities.allowFlying)
{
event.setCanceled (true);
return;
}
boolean cancel = false;

if (attacker.capabilities.isCreativeMode)
if (attackerMode != EnumPvPMode.ON)
{
if (attacker == event.source.getEntity ())
gm1 (attacker);
{
if (PvPUtils.isCreativeMode (attacker))
{
PvPUtils.red (attacker, "You are in creative mode!");
}
else if (PvPUtils.canFly (attacker))
{
PvPUtils.red (attacker, "You are in fly mode!");
}
}
cancel = true;
}

if (cancel) {// For performance reasons
event.setCanceled (true);
return;
}

if (!victimData.isPvPEnabled ())
if (victimMode != EnumPvPMode.ON)
{
if (attacker == event.source.getEntity ())
disabled (attacker);

event.setCanceled (true);
return;
{
PvPUtils.red (attacker, "This player/unit has PvP disabled!");
}
cancel = true;
}

if (!attackerData.isPvPEnabled ())
{
if (cancel)
event.setCanceled (true);
return;
}

}

/*
Expand Down Expand Up @@ -121,12 +116,13 @@ public void onLivingUpdate (LivingUpdateEvent event)
if (!data.isPvPEnabled ())
{
data.setPvPEnabled (true);
warnServer (player);
PvPMode.cfg.sendChatMsg (new ChatComponentText (
EnumChatFormatting.RED + "WARNING: PvP is now enabled for " + player.getDisplayName () + "!"));
}
else
{
data.setPvPEnabled (false);
pvpOff (player);
PvPUtils.green (player, "PvP is now disabled for you.");
}

data.setPvPCooldown (time + PvPMode.cooldown);
Expand Down Expand Up @@ -170,8 +166,7 @@ public EntityPlayerMP getMaster (Entity entity)
if (o instanceof EntityPlayerMP)
return (EntityPlayerMP) o;
}
else
{
else {
// This entity is not a LOTR unit.
return null;
}
Expand Down Expand Up @@ -224,32 +219,6 @@ public EntityPlayerMP getMaster (Entity entity)
return null;
}

void fly (EntityPlayerMP player)
{
PvPUtils.red (player, "You are in fly mode!");
}

void gm1 (EntityPlayerMP player)
{
PvPUtils.red (player, "You are in creative mode!");
}

void disabled (EntityPlayerMP player)
{
PvPUtils.red (player, "This player/unit has PvP disabled!");
}

void warnServer (EntityPlayerMP player)
{
PvPMode.cfg.sendChatMsg (new ChatComponentText (
EnumChatFormatting.RED + "WARNING: PvP is now enabled for " + player.getDisplayName () + "!"));
}

void pvpOff (EntityPlayerMP player)
{
PvPUtils.green (player, "PvP is now disabled for you.");
}

public static void init ()
{
INSTANCE = new PvPEventHandler ();
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/pvpmode/PvPUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,51 @@ public static void postChatLines (ICommandSender recipient, String... messages)
{
postChatLines (recipient, null, messages);
}

/**
* Returns the PvPMode of the supplied player. If ON, the player can do PvP,
* otherwise not.
*/
public static EnumPvPMode getPvPMode (EntityPlayer player)
{
if (isCreativeMode (player) || canFly (player))
return EnumPvPMode.OFF;// This is not really my (CraftedMods) style,
// but I'm doing this for performance reasons
// here, because the PvPData will only be
// loaded if required.

PvPData data = PvPUtils.getPvPData (player);
return data.getPvPWarmup () == 0 ? data.isPvPEnabled () ? EnumPvPMode.ON : EnumPvPMode.OFF
: EnumPvPMode.WARMUP;
}

/**
* Returns whether the supplied player is in creative mode.
*/
public static boolean isCreativeMode (EntityPlayer player)
{
return player.capabilities.isCreativeMode;
}

/**
* Returns whether the supplied player can fly.
*/
public static boolean canFly (EntityPlayer player)
{
return player.capabilities.allowFlying;
}

/**
* Returns the distance between the two supplied players rounded with the
* distance rounding factor specified in the configuration file.
*/
public static int roundedDistanceBetween (EntityPlayerMP sender, EntityPlayerMP player)
{
double x = sender.posX - player.posX;
double z = sender.posZ - player.posZ;

double distance = Math.sqrt (x * x + z * z);

return (int) ( (distance) / PvPMode.roundFactor + 1) * PvPMode.roundFactor;
}
}
18 changes: 10 additions & 8 deletions src/main/java/pvpmode/command/PvPCommand.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package pvpmode.command;

import net.minecraft.command.*;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.entity.player.*;
import pvpmode.*;

public class PvPCommand extends CommandBase
Expand All @@ -28,13 +28,14 @@ public boolean canCommandSenderUseCommand (ICommandSender sender)
public void processCommand (ICommandSender sender, String[] args)
{
EntityPlayerMP player = getCommandSenderAsPlayer (sender);
EnumPvPMode currentMode = PvPUtils.getPvPMode (player);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why add this here and not check the mode in cancelPvPTimer?

Copy link
Member Author

@CraftedMods CraftedMods Jun 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because firstly I used it in the other function too but then I left the function as it was. It could also be moved to the function you've mentioned, but at least I like it to have "metadata" like this in a higher level because they could be used in future "child functions". But it's not that important, I think.

PvPData data = PvPUtils.getPvPData (player);

if (args.length > 0)
{
if (args[0].equals ("cancel"))
{
cancelPvPTimer (sender, data);
cancelPvPTimer (player, currentMode, data);
}
else
{
Expand All @@ -47,15 +48,16 @@ public void processCommand (ICommandSender sender, String[] args)
}
}

private void cancelPvPTimer (ICommandSender sender, PvPData data)
private void cancelPvPTimer (EntityPlayer player, EnumPvPMode mode, PvPData data)
{
long warmup = data.getPvPWarmup ();
if (warmup == 0)
PvPUtils.yellow (sender, "No PvP warmup to cancel.");
else
if (mode == EnumPvPMode.WARMUP)
{
data.setPvPWarmup (0);
PvPUtils.yellow (sender, "PvP warmup canceled.");
PvPUtils.yellow (player, "PvP warmup canceled.");
}
else
{
PvPUtils.yellow (player, "No PvP warmup to cancel.");
}
}

Expand Down
56 changes: 22 additions & 34 deletions src/main/java/pvpmode/command/PvPCommandList.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import net.minecraft.command.*;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.util.*;
import pvpmode.*;

public class PvPCommandList extends CommandBase
Expand All @@ -31,53 +30,42 @@ public boolean canCommandSenderUseCommand (ICommandSender sender)
public void processCommand (ICommandSender sender, String[] args)
{
EntityPlayerMP senderPlayer = getCommandSenderAsPlayer (sender);

ArrayList<String> safePlayers = new ArrayList<String> ();
ArrayList<String> warmupPlayers = new ArrayList<String> ();
ArrayList<String> unsafePlayers = new ArrayList<String> ();

for (Object o : PvPMode.cfg.playerEntityList)
{
EntityPlayerMP player = (EntityPlayerMP) o;
PvPData playerData = PvPUtils.getPvPData (player);
EnumPvPMode mode = PvPUtils.getPvPMode (player);

if (player.capabilities.isCreativeMode)
safePlayers.add ("[GM1] " + player.getDisplayName ());
else if (player.capabilities.allowFlying)
safePlayers.add ("[FLY] " + player.getDisplayName ());
else if (!playerData.isPvPEnabled ())
switch (mode)
{
long warmup = playerData.getPvPWarmup ();

if (warmup == 0)
safePlayers.add ("[OFF] " + player.getDisplayName ());
else
unsafePlayers.add (EnumChatFormatting.YELLOW + "[WARMUP] " + player.getDisplayName ()
+ " - " + (warmup - PvPUtils.getTime ()) + " seconds till PvP");
}
else
{
String message = EnumChatFormatting.RED + "[ON] " + player.getDisplayName ();

if (PvPUtils.getPvPData (senderPlayer).isPvPEnabled () && PvPMode.radar
&& senderPlayer != player)
message += " - ~" + roundedDistanceBetween (senderPlayer, player) + " blocks";

unsafePlayers.add (message);
case OFF:
safePlayers.add (String.format ("[OFF%s] %s", PvPUtils.isCreativeMode (player) ? ":GM1"
: PvPUtils.canFly (player) ? ":FLY" : "", player.getDisplayName ()));
break;
case ON:
unsafePlayers.add (String.format ("[ON] %s %s", player.getDisplayName (), ((PvPMode.radar
&& senderPlayer != player)
? String.format ("- ~%d blocks",
PvPUtils.roundedDistanceBetween (senderPlayer, player))
: "")));
break;
case WARMUP:
warmupPlayers.add (String.format ("[WARMUP] %s - %d seconds till PvP", player.getDisplayName (),
(PvPUtils.getPvPData (player).getPvPWarmup () - PvPUtils.getTime ())));
break;
}
}

for (String line : unsafePlayers)
sender.addChatMessage (new ChatComponentText (line));
PvPUtils.red (sender, line);
for (String line : warmupPlayers)
PvPUtils.yellow (sender, line);
for (String line : safePlayers)
PvPUtils.green (sender, line);
}

int roundedDistanceBetween (EntityPlayerMP sender, EntityPlayerMP player)
{
double x = sender.posX - player.posX;
double z = sender.posZ - player.posZ;

double distance = Math.sqrt (x * x + z * z);

return (int) ( (distance) / PvPMode.roundFactor + 1) * PvPMode.roundFactor;
}
}