IP management. Mson add methods.

This commit is contained in:
Olof Larsson 2015-09-15 15:55:48 +02:00
parent e56dfd9c05
commit ad7c63b88d
7 changed files with 128 additions and 34 deletions

View File

@ -27,6 +27,7 @@ import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.plugin.Plugin;
import com.massivecraft.massivecore.collections.MassiveMap;
import com.massivecraft.massivecore.event.EventMassiveCoreAfterPlayerRespawn;
import com.massivecraft.massivecore.event.EventMassiveCoreAfterPlayerTeleport;
import com.massivecraft.massivecore.event.EventMassiveCorePermissionDeniedFormat;
@ -184,8 +185,10 @@ public class MassiveCoreEngineMain extends EngineAbstract
// Note: For now we update both names and ids.
// That way collections in plugins that haven't yet undergone update will still work.
public static Map<String, PlayerLoginEvent> idToPlayerLoginEvent = new MassiveMap<String, PlayerLoginEvent>();
// This method sets the sender reference to what you decide.
public static void setSenderReferences(CommandSender sender, CommandSender reference)
public static void setSenderReferences(CommandSender sender, CommandSender reference, PlayerLoginEvent event)
{
if (MUtil.isntSender(sender)) return;
@ -193,6 +196,14 @@ public class MassiveCoreEngineMain extends EngineAbstract
if (id != null)
{
SenderColl.setSenderReferences(id, reference);
if (event == null)
{
idToPlayerLoginEvent.remove(id);
}
else
{
idToPlayerLoginEvent.put(id, event);
}
}
String name = IdUtil.getName(sender);
@ -203,22 +214,22 @@ public class MassiveCoreEngineMain extends EngineAbstract
}
// This method sets the sender reference based on it's online state.
public static void setSenderReferences(Player player)
public static void setSenderReferences(Player player, PlayerLoginEvent event)
{
Player reference = player;
if ( ! player.isOnline()) reference = null;
setSenderReferences(player, reference);
setSenderReferences(player, reference, event);
}
// Same as above but next tick.
public static void setSenderReferencesSoon(final Player player)
public static void setSenderReferencesSoon(final Player player, final PlayerLoginEvent event)
{
Bukkit.getScheduler().scheduleSyncDelayedTask(MassiveCore.get(), new Runnable()
{
@Override
public void run()
{
setSenderReferences(player);
setSenderReferences(player, event);
}
});
}
@ -229,11 +240,11 @@ public class MassiveCoreEngineMain extends EngineAbstract
final Player player = event.getPlayer();
// We set the reference at LOWEST so that it's present during this PlayerLoginEvent event.
setSenderReferences(player, player);
setSenderReferences(player, player, event);
// And the next tick we update the reference based on it's online state.
// Not all players succeed in logging in. They may for example be banned.
setSenderReferencesSoon(player);
setSenderReferencesSoon(player, null);
}
@EventHandler(priority = EventPriority.MONITOR)
@ -241,21 +252,21 @@ public class MassiveCoreEngineMain extends EngineAbstract
{
// PlayerQuitEvents are /probably/ trustworthy.
// We check ourselves the next tick just to be on the safe side.
setSenderReferencesSoon(event.getPlayer());
setSenderReferencesSoon(event.getPlayer(), null);
}
@EventHandler(priority = EventPriority.MONITOR)
public void setSenderReferencesRegisterMonitor(EventMassiveCoreSenderRegister event)
{
// This one we can however trust.
setSenderReferences(event.getSender(), event.getSender());
setSenderReferences(event.getSender(), event.getSender(), null);
}
@EventHandler(priority = EventPriority.MONITOR)
public void setSenderReferencesUnregisterMonitor(EventMassiveCoreSenderUnregister event)
{
// This one we can however trust.
setSenderReferences(event.getSender(), null);
setSenderReferences(event.getSender(), null, null);
}
// -------------------------------------------- //

View File

@ -198,6 +198,10 @@ public class Mixin
{
return getPlayedMixin().hasPlayedBefore(senderObject);
}
public static String getIp(Object senderObject)
{
return getPlayedMixin().getIp(senderObject);
}
// -------------------------------------------- //
// STATIC EXPOSE: VISIBILITY

View File

@ -7,4 +7,5 @@ public interface PlayedMixin
public Long getFirstPlayed(Object senderObject);
public Long getLastPlayed(Object senderObject);
public boolean hasPlayedBefore(Object senderObject);
public String getIp(Object senderObject);
}

View File

@ -4,8 +4,10 @@ import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.util.IdUtil;
import com.massivecraft.massivecore.util.MUtil;
public class PlayedMixinDefault extends PlayedMixinAbstract
{
@ -23,6 +25,8 @@ public class PlayedMixinDefault extends PlayedMixinAbstract
@Override
public Long getFirstPlayed(Object senderObject)
{
if (MUtil.isNpc(senderObject)) return null;
UUID uuid = IdUtil.getUuid(senderObject);
if (uuid == null) return null;
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(uuid);
@ -41,6 +45,8 @@ public class PlayedMixinDefault extends PlayedMixinAbstract
// We do in fact NOT want this commented out behavior
// It's important we can check the previous played time on join!
if (MUtil.isNpc(senderObject)) return null;
UUID uuid = IdUtil.getUuid(senderObject);
if (uuid == null) return null;
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(uuid);
@ -52,4 +58,12 @@ public class PlayedMixinDefault extends PlayedMixinAbstract
return ret;
}
@Override
public String getIp(Object senderObject)
{
CommandSender sender = IdUtil.getSender(senderObject);
if (MUtil.isntPlayer(senderObject)) return null;
return MUtil.getIp(sender);
}
}

View File

@ -238,6 +238,28 @@ public class Mson implements Serializable
public Mson extra(Mson[] extra) { return Mson.valueOf(text, color, bold, italic, underlined, strikethrough, obfuscated, clickEvent, hoverEvent, insertionString, ImmutableList.copyOf(extra), parent); }
public Mson parent(Mson parent) { return Mson.valueOf(text, color, bold, italic, underlined, strikethrough, obfuscated, clickEvent, hoverEvent, insertionString, extra, parent); }
// -------------------------------------------- //
// ADD
// -------------------------------------------- //
public Mson add(Object part)
{
return this.add(new Object[]{part});
}
public Mson add(Object... parts)
{
return this.add(Arrays.asList(parts));
}
public Mson add(Iterable<?> parts)
{
List<Mson> extra = new ArrayList<Mson>(this.getExtra());
List<Mson> msons = msons(parts);
extra.addAll(msons);
return this.extra(extra);
}
// -------------------------------------------- //
// CONVENIENCE MSON EVENT
// -------------------------------------------- //

View File

@ -116,6 +116,11 @@ public abstract class SenderEntity<E extends SenderEntity<E>> extends Entity<E>
return Mixin.hasPlayedBefore(this.getId());
}
public String getIp()
{
return Mixin.getIp(this.getId());
}
public boolean isVisible(Object watcherObject)
{
return Mixin.canSee(watcherObject, this);

View File

@ -3,6 +3,7 @@ package com.massivecraft.massivecore.util;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Arrays;
@ -45,6 +46,7 @@ import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.entity.EntityDamageEvent.DamageModifier;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
@ -174,11 +176,11 @@ public class MUtil
// -------------------------------------------- //
// The regex for a valid minecraft player name.
public final static Pattern playerNamePattern = Pattern.compile("^[a-zA-Z0-9_]{2,16}$");
public final static Pattern PATTERN_PLAYER_NAME = Pattern.compile("^[a-zA-Z0-9_]{2,16}$");
public static boolean isValidPlayerName(String string)
{
return playerNamePattern.matcher(string).matches();
return PATTERN_PLAYER_NAME.matcher(string).matches();
}
// -------------------------------------------- //
@ -209,6 +211,63 @@ public class MUtil
return asUuid(string) != null;
}
// -------------------------------------------- //
// IP
// -------------------------------------------- //
public static String getIp(CommandSender sender)
{
if (!(sender instanceof Player)) return null;
Player player = (Player)sender;
InetSocketAddress address = player.getAddress();
if (address != null) return getIp(address);
String id = IdUtil.getId(player);
PlayerLoginEvent event = MassiveCoreEngineMain.idToPlayerLoginEvent.get(id);
if (event != null) return getIp(event);
return null;
}
public static String getIp(InetSocketAddress address)
{
if (address == null) return null;
String ret = address.toString();
String[] parts = ret.split("/");
ret = parts[1];
parts = ret.split(":");
ret = parts[0];
return ret;
}
public static String getIp(PlayerLoginEvent event)
{
InetAddress address = event.getAddress();
return getIp(address);
}
public static String getIp(InetAddress address)
{
if (address == null) return null;
String ret = address.toString();
String[] parts = ret.split("/");
ret = parts[1];
return ret;
}
public static Pattern PATTERN_IPV4 = Pattern.compile("^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$");
public static boolean isIp(String string)
{
if (string == null) return false;
return PATTERN_IPV4.matcher(string).matches();
}
// -------------------------------------------- //
// IS(NT) NPC, SENDER, PLAYER
// -------------------------------------------- //
@ -691,28 +750,6 @@ public class MUtil
return Math.abs(d) <= Double.MAX_VALUE;
}
// -------------------------------------------- //
// GET IP
// -------------------------------------------- //
public static String getIp(CommandSender sender)
{
if (!(sender instanceof Player)) return null;
Player player = (Player)sender;
InetSocketAddress address = player.getAddress();
if (address == null) return null;
String ret = address.toString();
String[] parts = ret.split("/");
ret = parts[1];
parts = ret.split(":");
ret = parts[0];
return ret;
}
// -------------------------------------------- //
// PICK
// -------------------------------------------- //