many random updates again :P

This commit is contained in:
Olof Larsson 2012-10-01 13:18:49 +02:00
parent c670731a4e
commit 9e5866303e
17 changed files with 259 additions and 60 deletions

View File

@ -131,14 +131,14 @@ public class InternalListener implements Listener
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void syncOnPlayerLogin(PlayerLoginEvent event) public void syncOnPlayerLogin(PlayerLoginEvent event)
{ {
p.log("syncOnPlayerLogin", event.getPlayer().getName()); //p.log("syncOnPlayerLogin", event.getPlayer().getName());
this.syncAllForPlayer(event.getPlayer()); this.syncAllForPlayer(event.getPlayer());
} }
@EventHandler(priority = EventPriority.MONITOR) @EventHandler(priority = EventPriority.MONITOR)
public void syncOnPlayerLeave(MCorePlayerLeaveEvent event) public void syncOnPlayerLeave(MCorePlayerLeaveEvent event)
{ {
p.log("syncOnPlayerLeave", event.getPlayer().getName()); //p.log("syncOnPlayerLeave", event.getPlayer().getName());
this.syncAllForPlayer(event.getPlayer()); this.syncAllForPlayer(event.getPlayer());
} }

View File

@ -59,7 +59,7 @@ public abstract class MPlugin extends JavaPlugin implements Listener
public void postEnable() public void postEnable()
{ {
log(Txt.parse("=== ENABLE <g>COMPLELTE <i>(Took <h>"+(System.currentTimeMillis()-timeEnableStart)+"ms<i>) ===")); log(Txt.parse("=== ENABLE <g>COMPLETE <i>(Took <h>"+(System.currentTimeMillis()-timeEnableStart)+"ms<i>) ==="));
} }
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -48,9 +48,24 @@ public class SimpleConfig
if (this.file().isFile()) if (this.file().isFile())
{ {
String content = DiscUtil.readCatch(this.file()); String content = DiscUtil.readCatch(this.file());
if (contentRequestsDefaults(content)) return; Object toShallowLoad = null;
Object createdByGson = this.mplugin().gson.fromJson(content, this.getClass()); if (contentRequestsDefaults(content))
Accessor.get(this.getClass()).copy(createdByGson, this); {
try
{
toShallowLoad = this.getClass().newInstance();
}
catch (Exception e)
{
e.printStackTrace();
return;
}
}
else
{
toShallowLoad = this.mplugin().gson.fromJson(content, this.getClass());
}
Accessor.get(this.getClass()).copy(toShallowLoad, this);
} }
save(); save();
} }

View File

@ -20,11 +20,21 @@ public abstract class ARAbstractStringMatch implements ArgReader<String>
// Find all matches // Find all matches
Set<String> matches = new HashSet<String>(); Set<String> matches = new HashSet<String>();
String perfectMatch = null;
outerloop:
for (Collection<String> altColl : this.altColls()) for (Collection<String> altColl : this.altColls())
{ {
for (String alt : altColl) for (String alt : altColl)
{ {
if (this.matches(str, alt)) Integer matchDistance = this.matches(str, alt);
if (matchDistance == null) continue;
if (matchDistance == 0)
{
perfectMatch = alt;
break outerloop;
}
else
{ {
matches.add(alt); matches.add(alt);
} }
@ -32,7 +42,11 @@ public abstract class ARAbstractStringMatch implements ArgReader<String>
} }
// Set result and errors // Set result and errors
if (matches.size() == 1) if (perfectMatch != null)
{
result.setResult(perfectMatch);
}
else if (matches.size() == 1)
{ {
result.setResult(matches.iterator().next()); result.setResult(matches.iterator().next());
} }
@ -53,7 +67,10 @@ public abstract class ARAbstractStringMatch implements ArgReader<String>
// ABSTRACT // ABSTRACT
// -------------------------------------------- // // -------------------------------------------- //
public abstract boolean matches(String arg, String alt); // return null if no match
// return 0 if perfect match
// return >0 to declare distance from perfect match
public abstract Integer matches(String arg, String alt);
// -------------------------------------------- // // -------------------------------------------- //
// FIELDS // FIELDS

View File

@ -1,25 +1,25 @@
package com.massivecraft.mcore4.cmd.arg; package com.massivecraft.mcore4.cmd.arg;
import com.massivecraft.mcore4.cmd.MCommand; import com.massivecraft.mcore4.cmd.MCommand;
import com.massivecraft.mcore4.store.PlayerColl; import com.massivecraft.mcore4.store.Coll;
import com.massivecraft.mcore4.store.PlayerEntity; import com.massivecraft.mcore4.store.Entity;
public class ARPlayerEntity<T extends PlayerEntity<T>> implements ArgReader<T> public class ARStringEntity<E extends Entity<E, String>> implements ArgReader<E>
{ {
// -------------------------------------------- // // -------------------------------------------- //
// IMPLEMENTATION // IMPLEMENTATION
// -------------------------------------------- // // -------------------------------------------- //
@Override @Override
public ArgResult<T> read(String str, MCommand mcommand) public ArgResult<E> read(String str, MCommand mcommand)
{ {
ArgResult<T> result = new ArgResult<T>(); ArgResult<E> result = new ArgResult<E>();
ArgResult<String> innerResult = this.stringReader().read(str, mcommand); ArgResult<String> innerResult = this.stringReader().read(str, mcommand);
if (innerResult.hasResult()) if (innerResult.hasResult())
{ {
String playerName = innerResult.getResult(); String playerName = innerResult.getResult();
T entity = this.playerColl().get(playerName); E entity = this.coll().get(playerName);
result.setResult(entity); result.setResult(entity);
} }
else else
@ -37,17 +37,17 @@ public class ARPlayerEntity<T extends PlayerEntity<T>> implements ArgReader<T>
protected ArgReader<String> stringReader; protected ArgReader<String> stringReader;
public ArgReader<String> stringReader() { return this.stringReader; } public ArgReader<String> stringReader() { return this.stringReader; }
protected PlayerColl<T> playerColl; protected Coll<E, String> coll;
public PlayerColl<T> playerColl() { return this.playerColl; } public Coll<E, String> coll() { return this.coll; }
// -------------------------------------------- // // -------------------------------------------- //
// CONSTRUCT // CONSTRUCT
// -------------------------------------------- // // -------------------------------------------- //
public ARPlayerEntity(ArgReader<String> stringReader, PlayerColl<T> playerColl) public ARStringEntity(ArgReader<String> stringReader, Coll<E, String> coll)
{ {
this.stringReader = stringReader; this.stringReader = stringReader;
this.playerColl = playerColl; this.coll = coll;
} }
} }

View File

@ -9,9 +9,9 @@ public class ARStringMatchFullCI extends ARAbstractStringMatch
// -------------------------------------------- // // -------------------------------------------- //
@Override @Override
public boolean matches(String arg, String alt) public Integer matches(String arg, String alt)
{ {
return alt.equalsIgnoreCase(arg); return alt.equalsIgnoreCase(arg) ? 0 : null;
} }
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -9,9 +9,9 @@ public class ARStringMatchFullCS extends ARAbstractStringMatch
// -------------------------------------------- // // -------------------------------------------- //
@Override @Override
public boolean matches(String arg, String alt) public Integer matches(String arg, String alt)
{ {
return alt.equals(arg); return alt.equals(arg) ? 0 : null;
} }
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -9,11 +9,18 @@ public class ARStringMatchStartCI extends ARAbstractStringMatch
// -------------------------------------------- // // -------------------------------------------- //
@Override @Override
public boolean matches(String arg, String alt) public Integer matches(String arg, String alt)
{ {
arg = arg.toLowerCase(); arg = arg.toLowerCase();
alt = alt.toLowerCase(); alt = alt.toLowerCase();
return alt.startsWith(arg); if (alt.startsWith(arg))
{
return alt.length() - arg.length();
}
else
{
return null;
}
} }
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -9,9 +9,16 @@ public class ARStringMatchStartCS extends ARAbstractStringMatch
// -------------------------------------------- // // -------------------------------------------- //
@Override @Override
public boolean matches(String arg, String alt) public Integer matches(String arg, String alt)
{ {
return alt.startsWith(arg); if (alt.startsWith(arg))
{
return alt.length() - arg.length();
}
else
{
return null;
}
} }
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -8,8 +8,6 @@ import org.bukkit.entity.Player;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
import com.massivecraft.mcore4.MCore;
/** /**
* The MCorePlayerLeaveEvent is a non-cancellable event. * The MCorePlayerLeaveEvent is a non-cancellable event.
* It is run at the MONITOR of either PlayerKickEvent or PlayerQuitEvent. * It is run at the MONITOR of either PlayerKickEvent or PlayerQuitEvent.
@ -75,7 +73,7 @@ public class MCorePlayerLeaveEvent extends Event implements Runnable
if (doit) if (doit)
{ {
MCore.p.log("MCorePlayerLeaveEvent", caller, player.getDisplayName(), preDisconnect, message); //MCore.p.log("MCorePlayerLeaveEvent", caller, player.getDisplayName(), preDisconnect, message);
player2event.put(this.player.getName(), this); player2event.put(this.player.getName(), this);
Bukkit.getPluginManager().callEvent(this); Bukkit.getPluginManager().callEvent(this);
} }

View File

@ -72,6 +72,15 @@ public class Coll<E, L> implements CollInterface<E, L>
protected Set<L> ids = Collections.newSetFromMap(new ConcurrentHashMap<L, Boolean>()); protected Set<L> ids = Collections.newSetFromMap(new ConcurrentHashMap<L, Boolean>());
@Override public Collection<L> ids() { return Collections.unmodifiableCollection(this.ids); } @Override public Collection<L> ids() { return Collections.unmodifiableCollection(this.ids); }
@Override public Collection<L> idsRemote() { return this.db().driver().ids(this); } @Override public Collection<L> idsRemote() { return this.db().driver().ids(this); }
@Override public boolean containsEntity(E entity) { return this.entities.contains(entity); };
@Override
public boolean containsId(Object oid)
{
L id = this.idFix(oid);
if (id == null) return false;
return this.ids.contains(id);
}
protected Set<E> entities = Collections.newSetFromMap(new ConcurrentHashMap<E, Boolean>()); protected Set<E> entities = Collections.newSetFromMap(new ConcurrentHashMap<E, Boolean>());
@Override public Collection<E> getAll() { return Collections.unmodifiableCollection(this.entities); } @Override public Collection<E> getAll() { return Collections.unmodifiableCollection(this.entities); }
@ -112,6 +121,7 @@ public class Coll<E, L> implements CollInterface<E, L>
{ {
if (oid == null) return null; if (oid == null) return null;
if (oid.getClass() == this.idClass) return this.idClass.cast(oid); if (oid.getClass() == this.idClass) return this.idClass.cast(oid);
if (oid.getClass() == this.entityClass) return this.entity2id.get(oid);
return null; return null;
} }
@ -131,9 +141,19 @@ public class Coll<E, L> implements CollInterface<E, L>
// COPY AND CREATE // COPY AND CREATE
// -------------------------------------------- // // -------------------------------------------- //
@SuppressWarnings({ "rawtypes", "unchecked" })
public void copy(Object ofrom, Object oto) public void copy(Object ofrom, Object oto)
{ {
Accessor.get(this.entityClass()).copy(ofrom, oto); if (ofrom instanceof Entity)
{
Entity efrom = (Entity)ofrom;
Entity eto = (Entity)oto;
eto.load(efrom);
}
else
{
Accessor.get(this.entityClass()).copy(ofrom, oto);
}
} }
// This simply creates and returns a new instance // This simply creates and returns a new instance
@ -579,10 +599,16 @@ public class Coll<E, L> implements CollInterface<E, L>
@Override @Override
public void init() public void init()
{ {
if (instances.contains(this)) return; if (this.inited()) return;
this.syncAll(); this.syncAll();
this.examineThread = new ExamineThread<E, L>(this); this.examineThread = new ExamineThread<E, L>(this);
this.examineThread.start(); this.examineThread.start();
instances.add(this); instances.add(this);
} }
@Override
public boolean inited()
{
return instances.contains(this);
}
} }

View File

@ -36,6 +36,8 @@ public interface CollInterface<E, L>
// -------------------------------------------- // // -------------------------------------------- //
public Collection<L> ids(); public Collection<L> ids();
public Collection<L> idsRemote(); public Collection<L> idsRemote();
public boolean containsId(Object oid);
public boolean containsEntity(E entity);
public Collection<E> getAll(); public Collection<E> getAll();
public Collection<E> getAll(Predictate<E> where); public Collection<E> getAll(Predictate<E> where);
@ -141,6 +143,7 @@ public interface CollInterface<E, L>
// -------------------------------------------- // // -------------------------------------------- //
public void init(); public void init();
public boolean inited();
} }

View File

@ -10,7 +10,7 @@ import org.bukkit.entity.Player;
import com.massivecraft.mcore4.MCore; import com.massivecraft.mcore4.MCore;
import com.massivecraft.mcore4.MPlugin; import com.massivecraft.mcore4.MPlugin;
import com.massivecraft.mcore4.Predictate; import com.massivecraft.mcore4.Predictate;
import com.massivecraft.mcore4.cmd.arg.ARPlayerEntity; import com.massivecraft.mcore4.cmd.arg.ARStringEntity;
import com.massivecraft.mcore4.cmd.arg.ARStringMatchFullCI; import com.massivecraft.mcore4.cmd.arg.ARStringMatchFullCI;
import com.massivecraft.mcore4.cmd.arg.ARStringMatchStartCI; import com.massivecraft.mcore4.cmd.arg.ARStringMatchStartCI;
import com.massivecraft.mcore4.cmd.arg.ArgReader; import com.massivecraft.mcore4.cmd.arg.ArgReader;
@ -19,14 +19,47 @@ import com.massivecraft.mcore4.util.PlayerUtil;
public class PlayerColl<E extends PlayerEntity<E>> extends Coll<E, String> public class PlayerColl<E extends PlayerEntity<E>> extends Coll<E, String>
{ {
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
// Note that the lowercasing should be kept at either true or false.
protected boolean lowercasing = false;
public boolean isLowercasing() { return this.lowercasing; }
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public PlayerColl(Db<?> db, MPlugin mplugin, String name, Class<E> entityClass, boolean creative, boolean lowercasing)
{
super(db, mplugin, "ai", name, entityClass, String.class, creative);
this.lowercasing = lowercasing;
}
public PlayerColl(Db<?> db, MPlugin mplugin, String name, Class<E> entityClass, boolean creative)
{
this(db, mplugin, name, entityClass, creative, false);
}
public PlayerColl(Db<?> db, MPlugin mplugin, String name, Class<E> entityClass) public PlayerColl(Db<?> db, MPlugin mplugin, String name, Class<E> entityClass)
{ {
super(db, mplugin, "ai", name, entityClass, String.class, true); this(db, mplugin, name, entityClass, true);
}
public PlayerColl(MPlugin mplugin, String name, Class<E> entityClass, boolean creative, boolean lowercasing)
{
this(MCore.getDb(), mplugin, name, entityClass, creative, lowercasing);
}
public PlayerColl(MPlugin mplugin, String name, Class<E> entityClass, boolean creative)
{
this(MCore.getDb(), mplugin, name, entityClass, creative);
} }
public PlayerColl(MPlugin mplugin, String name, Class<E> entityClass) public PlayerColl(MPlugin mplugin, String name, Class<E> entityClass)
{ {
super(MCore.getDb(), mplugin, "ai", name, entityClass, String.class, true); this(MCore.getDb(), mplugin, name, entityClass);
} }
// -------------------------------------------- // // -------------------------------------------- //
@ -37,7 +70,9 @@ public class PlayerColl<E extends PlayerEntity<E>> extends Coll<E, String>
public String idFix(Object oid) public String idFix(Object oid)
{ {
if (oid == null) return null; if (oid == null) return null;
return MUtil.extract(String.class, "playerName", oid); String ret = MUtil.extract(String.class, "playerName", oid);
if (ret == null) return ret;
return this.lowercasing ? ret.toLowerCase() : ret;
} }
public Collection<E> getAllOnline() public Collection<E> getAllOnline()
@ -87,12 +122,12 @@ public class PlayerColl<E extends PlayerEntity<E>> extends Coll<E, String>
public ArgReader<E> argReaderPlayerFull() public ArgReader<E> argReaderPlayerFull()
{ {
return new ARPlayerEntity<E>(this.argReaderPlayerNameFull(), this); return new ARStringEntity<E>(this.argReaderPlayerNameFull(), this);
} }
public ArgReader<E> argReaderPlayerStart() public ArgReader<E> argReaderPlayerStart()
{ {
return new ARPlayerEntity<E>(this.argReaderPlayerNameStart(), this); return new ARStringEntity<E>(this.argReaderPlayerNameStart(), this);
} }
} }

View File

@ -2,6 +2,7 @@ package com.massivecraft.mcore4.store;
import java.util.Collection; import java.util.Collection;
import org.bukkit.Bukkit;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -13,7 +14,21 @@ public abstract class PlayerEntity<E extends PlayerEntity<E>> extends Entity<E,
{ {
public Player getPlayer() public Player getPlayer()
{ {
return PlayerUtil.getPlayerExact(this.getId()); if (this.getColl() == null) return null;
if (this.getColl().isLowercasing())
{
return Bukkit.getPlayerExact(this.getId());
}
else
{
return PlayerUtil.getPlayerExact(this.getId());
}
}
@Override
public PlayerColl<E> getColl()
{
return (PlayerColl<E>) super.getColl();
} }
public boolean isOnline() public boolean isOnline()

View File

@ -6,19 +6,55 @@ import java.util.Map.Entry;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.permissions.Permission; import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault;
import com.massivecraft.mcore4.Lang; import com.massivecraft.mcore4.Lang;
public class Perm public class Perm
{ {
public static String getPermissionDescription (String perm) // -------------------------------------------- //
// HAS
// -------------------------------------------- //
public static boolean has(CommandSender sender, Permission permission)
{
return has(sender, permission.getName());
}
public static boolean has(CommandSender sender, String perm)
{
if (sender == null) return false;
return sender.hasPermission(perm);
}
public static boolean has(CommandSender sender, Permission permission, boolean verbose)
{
return has(sender, permission.getName(), verbose);
}
public static boolean has(CommandSender sender, String perm, boolean verbose)
{
if (has(sender, perm))
{
return true;
}
else if (verbose && sender != null)
{
sender.sendMessage(getForbiddenMessage(perm));
}
return false;
}
// -------------------------------------------- //
// DESCRIPTIONS AND MESSAGES
// -------------------------------------------- //
public static String getPermissionDescription(String perm)
{ {
if (perm == null) return Lang.permDoThat; if (perm == null) return Lang.permDoThat;
Permission permission = Bukkit.getPluginManager().getPermission(perm); Permission permission = Bukkit.getPluginManager().getPermission(perm);
return getPermissionDescription(permission); return getPermissionDescription(permission);
} }
public static String getPermissionDescription (Permission perm) public static String getPermissionDescription(Permission perm)
{ {
if (perm == null) return Lang.permDoThat; if (perm == null) return Lang.permDoThat;
String desc = perm.getDescription(); String desc = perm.getDescription();
@ -31,24 +67,9 @@ public class Perm
return Txt.parse(Lang.permForbidden, getPermissionDescription(perm)); return Txt.parse(Lang.permForbidden, getPermissionDescription(perm));
} }
public static boolean has (CommandSender me, String perm) // -------------------------------------------- //
{ // RANDOM UTILS
if (me == null) return false; // -------------------------------------------- //
return me.hasPermission(perm);
}
public static boolean has (CommandSender me, String perm, boolean verbose)
{
if (has(me, perm))
{
return true;
}
else if (verbose && me != null)
{
me.sendMessage(getForbiddenMessage(perm));
}
return false;
}
public static <T> T pickFirstVal(CommandSender me, Map<String, T> perm2val) public static <T> T pickFirstVal(CommandSender me, Map<String, T> perm2val)
{ {
@ -63,4 +84,54 @@ public class Perm
return ret; return ret;
} }
// -------------------------------------------- //
// GET CREATIVE
// -------------------------------------------- //
public static Permission getCreative(String name)
{
return getCreative(name, null, null, null);
}
public static Permission getCreative(String name, String description)
{
return getCreative(name, description, null, null);
}
public static Permission getCreative(String name, PermissionDefault defaultValue)
{
return getCreative(name, null, defaultValue, null);
}
public static Permission getCreative(String name, String description, PermissionDefault defaultValue)
{
return getCreative(name, description, defaultValue, null);
}
public static Permission getCreative(String name, Map<String, Boolean> children)
{
return getCreative(name, null, null, children);
}
public static Permission getCreative(String name, String description, Map<String, Boolean> children)
{
return getCreative(name, description, null, children);
}
public static Permission getCreative(String name, PermissionDefault defaultValue, Map<String, Boolean> children)
{
return getCreative(name, null, defaultValue, children);
}
public static Permission getCreative(String name, String description, PermissionDefault defaultValue, Map<String, Boolean> children)
{
Permission ret = Bukkit.getPluginManager().getPermission(name);
if (ret == null)
{
ret = new Permission(name, description, defaultValue, children);
Bukkit.getPluginManager().addPermission(ret);
}
return ret;
}
} }

View File

@ -85,6 +85,7 @@ public class PlayerUtil implements Listener
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static Player getPlayerExact(String exactPlayerName) public static Player getPlayerExact(String exactPlayerName)
{ {
if (exactPlayerName == null) return null;
CraftServer craftServer = (CraftServer) Bukkit.getServer(); CraftServer craftServer = (CraftServer) Bukkit.getServer();
List<EntityPlayer> entityPlayers = craftServer.getHandle().players; List<EntityPlayer> entityPlayers = craftServer.getHandle().players;
for (EntityPlayer entityPlayer : entityPlayers) for (EntityPlayer entityPlayer : entityPlayers)

View File

@ -368,10 +368,14 @@ public class Txt
} }
public static ArrayList<String> getPage(List<String> lines, int pageHumanBased, String title) public static ArrayList<String> getPage(List<String> lines, int pageHumanBased, String title)
{
return getPage(lines, pageHumanBased, title, 9);
}
public static ArrayList<String> getPage(List<String> lines, int pageHumanBased, String title, int pageheight)
{ {
ArrayList<String> ret = new ArrayList<String>(); ArrayList<String> ret = new ArrayList<String>();
int pageZeroBased = pageHumanBased - 1; int pageZeroBased = pageHumanBased - 1;
int pageheight = 9;
int pagecount = (int)Math.ceil(((double)lines.size()) / pageheight); int pagecount = (int)Math.ceil(((double)lines.size()) / pageheight);
ret.add(titleize(title+parse("<a>")+" "+pageHumanBased+"/"+pagecount)); ret.add(titleize(title+parse("<a>")+" "+pageHumanBased+"/"+pagecount));