Player messages, support for more errorMessages, new color codes and some private to protected.

This commit is contained in:
Olof Larsson 2012-01-04 03:19:33 +01:00
parent 155344a7ad
commit 13051a8684
12 changed files with 144 additions and 35 deletions

View File

@ -6,6 +6,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.bukkit.Material;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -13,6 +14,7 @@ import com.massivecraft.mcore1.cmd.arg.AHBoolean;
import com.massivecraft.mcore1.cmd.arg.AHDouble; import com.massivecraft.mcore1.cmd.arg.AHDouble;
import com.massivecraft.mcore1.cmd.arg.AHFloat; import com.massivecraft.mcore1.cmd.arg.AHFloat;
import com.massivecraft.mcore1.cmd.arg.AHInteger; import com.massivecraft.mcore1.cmd.arg.AHInteger;
import com.massivecraft.mcore1.cmd.arg.AHMaterial;
import com.massivecraft.mcore1.cmd.arg.AHPlayer; import com.massivecraft.mcore1.cmd.arg.AHPlayer;
import com.massivecraft.mcore1.cmd.arg.IArgHandler; import com.massivecraft.mcore1.cmd.arg.IArgHandler;
@ -48,9 +50,10 @@ public class Cmd
public Cmd() public Cmd()
{ {
this.setArgHandler(Boolean.class, new AHBoolean()); this.setArgHandler(Boolean.class, new AHBoolean());
this.setArgHandler(Integer.class, new AHInteger());
this.setArgHandler(Float.class, new AHFloat());
this.setArgHandler(Double.class, new AHDouble()); this.setArgHandler(Double.class, new AHDouble());
this.setArgHandler(Float.class, new AHFloat());
this.setArgHandler(Integer.class, new AHInteger());
this.setArgHandler(Material.class, new AHMaterial());
this.setArgHandler(Player.class, new AHPlayer()); this.setArgHandler(Player.class, new AHPlayer());
} }
} }

View File

@ -381,12 +381,20 @@ public abstract class MCommand
sender.sendMessage(Txt.parse(str)); sender.sendMessage(Txt.parse(str));
} }
public void msg(Collection<String> msgs)
{
for(String msg : msgs)
{
this.msg(msg);
}
}
public void sendMessage(String msg) public void sendMessage(String msg)
{ {
sender.sendMessage(msg); sender.sendMessage(msg);
} }
public void sendMessage(List<String> msgs) public void sendMessage(Collection<String> msgs)
{ {
for(String msg : msgs) for(String msg : msgs)
{ {

View File

@ -1,18 +1,21 @@
package com.massivecraft.mcore1.cmd.arg; package com.massivecraft.mcore1.cmd.arg;
import java.util.ArrayList;
import java.util.Collection;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import com.massivecraft.mcore1.MPlugin; import com.massivecraft.mcore1.MPlugin;
public abstract class AHBase<T> implements IArgHandler<T> public abstract class AHBase<T> implements IArgHandler<T>
{ {
protected String error = null; protected Collection<String> error = new ArrayList<String>();
@Override @Override
public abstract T parse(String str, String style, CommandSender sender, MPlugin p); public abstract T parse(String str, String style, CommandSender sender, MPlugin p);
@Override @Override
public String getError() public Collection<String> getError()
{ {
return this.error; return this.error;
} }

View File

@ -0,0 +1,24 @@
package com.massivecraft.mcore1.cmd.arg;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import com.massivecraft.mcore1.MPlugin;
public class AHMaterial extends AHBase<Material>
{
@Override
public Material parse(String str, String style, CommandSender sender, MPlugin p)
{
this.error.clear();
Material ret = Material.matchMaterial(str);
if (ret == null)
{
this.error.add("<b>No material matching \"<p>"+str+"<b>\".");
this.error.add("<i>Suggestion: <aqua>http://www.minecraftwiki.net/wiki/Data_values");
}
return ret;
}
}

View File

@ -13,7 +13,7 @@ public class AHPlayer extends AHBase<Player>
@Override @Override
public Player parse(String str, String style, CommandSender sender, MPlugin p) public Player parse(String str, String style, CommandSender sender, MPlugin p)
{ {
this.error = null; this.error.clear();
if (str == null) return null; if (str == null) return null;
if (style != null && style.equals("match")) if (style != null && style.equals("match"))
@ -23,7 +23,7 @@ public class AHPlayer extends AHBase<Player>
{ {
return players.get(0); return players.get(0);
} }
this.error = "<b>No online player's name begins with \"<p>"+str+"<b>\"."; this.error.add("<b>No online player's name begins with \"<p>"+str+"<b>\".");
} }
else else
{ {
@ -32,7 +32,7 @@ public class AHPlayer extends AHBase<Player>
{ {
return player; return player;
} }
this.error = "<b>No player online with the exact name \"<p>"+str+"<b>\"."; this.error.add("<b>No player online with the exact name \"<p>"+str+"<b>\".");
} }
return null; return null;
} }

View File

@ -19,7 +19,7 @@ public abstract class AHPlayerWrapper<T> extends AHBase<T>
@Override @Override
public T parse(String str, String style, CommandSender sender, MPlugin p) public T parse(String str, String style, CommandSender sender, MPlugin p)
{ {
this.error = null; this.error.clear();
if (str == null) return null; if (str == null) return null;
IClassManager<T> manager = this.getManager(p); IClassManager<T> manager = this.getManager(p);
@ -32,7 +32,7 @@ public abstract class AHPlayerWrapper<T> extends AHBase<T>
{ {
return ret; return ret;
} }
this.error = "<b>No player name begins with \"<p>"+str+"<b>\"."; this.error.add("<b>No player name begins with \"<p>"+str+"<b>\".");
} }
else if (style != null && style.equalsIgnoreCase("matchany")) else if (style != null && style.equalsIgnoreCase("matchany"))
{ {
@ -41,7 +41,7 @@ public abstract class AHPlayerWrapper<T> extends AHBase<T>
{ {
return ret; return ret;
} }
this.error = "<b>No player name begins with \"<p>"+str+"<b>\"."; this.error.add("<b>No player name begins with \"<p>"+str+"<b>\".");
} }
else else
{ {
@ -51,7 +51,7 @@ public abstract class AHPlayerWrapper<T> extends AHBase<T>
return ret; return ret;
} }
this.error = "<b>No player with the exact name \"<p>"+str+"<b>\"."; this.error.add("<b>No player with the exact name \"<p>"+str+"<b>\".");
} }
return null; return null;
} }

View File

@ -13,7 +13,7 @@ public abstract class AHPrimitive<T> extends AHBase<T>
@Override @Override
public T parse(String str, String style, CommandSender sender, MPlugin p) public T parse(String str, String style, CommandSender sender, MPlugin p)
{ {
this.error = null; this.error.clear();
if (str == null) return null; if (str == null) return null;
try try
{ {
@ -22,7 +22,7 @@ public abstract class AHPrimitive<T> extends AHBase<T>
} }
catch (Exception e) catch (Exception e)
{ {
this.error = "<b>\"<p>"+str+"<b>\" is not a valid "+this.getPrimitiveName()+"."; this.error.add("<b>\"<p>"+str+"<b>\" is not a valid "+this.getPrimitiveName()+".");
} }
return null; return null;
} }

View File

@ -1,5 +1,7 @@
package com.massivecraft.mcore1.cmd.arg; package com.massivecraft.mcore1.cmd.arg;
import java.util.Collection;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import com.massivecraft.mcore1.MPlugin; import com.massivecraft.mcore1.MPlugin;
@ -10,5 +12,5 @@ public interface IArgHandler<T>
public T parse(String str, String style, CommandSender sender, MPlugin p); public T parse(String str, String style, CommandSender sender, MPlugin p);
// Error here - or null. // Error here - or null.
public String getError(); public Collection<String> getError();
} }

View File

@ -1,10 +1,12 @@
package com.massivecraft.mcore1.persist; package com.massivecraft.mcore1.persist;
import java.util.List; import java.util.Collection;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.massivecraft.mcore1.util.Txt;
public abstract class PlayerEntity<T extends PlayerEntity<T>> extends Entity<T> public abstract class PlayerEntity<T extends PlayerEntity<T>> extends Entity<T>
{ {
@ -34,7 +36,7 @@ public abstract class PlayerEntity<T extends PlayerEntity<T>> extends Entity<T>
player.sendMessage(msg); player.sendMessage(msg);
} }
public void sendMessage(List<String> msgs) public void sendMessage(Collection<String> msgs)
{ {
Player player = this.getPlayer(); Player player = this.getPlayer();
if (player == null) return; if (player == null) return;
@ -43,4 +45,24 @@ public abstract class PlayerEntity<T extends PlayerEntity<T>> extends Entity<T>
player.sendMessage(msg); player.sendMessage(msg);
} }
} }
public void msg(String msg)
{
this.sendMessage(Txt.parse(msg));
}
public void msg(String msg, Object... args)
{
this.sendMessage(Txt.parse(msg, args));
}
public void msg(Collection<String> msgs)
{
Player player = this.getPlayer();
if (player == null) return;
for(String msg : msgs)
{
player.sendMessage(Txt.parse(msg));
}
}
} }

View File

@ -21,29 +21,29 @@ public abstract class GsonClassManager<T> implements IClassManager<T>
// -------------------------------------------- // // -------------------------------------------- //
// NON INTERFACE // NON INTERFACE
// -------------------------------------------- // // -------------------------------------------- //
private final static String DOTJSON = ".json"; protected final static String DOTJSON = ".json";
private Set<String> ids; protected Set<String> ids;
private Set<T> entities; protected Set<T> entities;
private Map<String, T> id2entity; protected Map<String, T> id2entity;
private Map<T, String> entity2id; protected Map<T, String> entity2id;
private long idCurrent = 1; protected long idCurrent = 1;
private Gson gson; protected Gson gson;
public Gson getGson() { return gson; } public Gson getGson() { return gson; }
public void setGson(Gson gson) { this.gson = gson; } public void setGson(Gson gson) { this.gson = gson; }
private File folder; protected File folder;
public File getFolder() { return folder; } public File getFolder() { return folder; }
public void setFolder(File val) { this.folder = val; this.folder.mkdirs(); } public void setFolder(File val) { this.folder = val; this.folder.mkdirs(); }
private boolean creative; protected boolean creative;
@Override @Override
public boolean getIsCreative() { return this.creative; } public boolean getIsCreative() { return this.creative; }
@Override @Override
public void setIsCreative(boolean val) { this.creative = val; } public void setIsCreative(boolean val) { this.creative = val; }
private boolean didLoadAll = false; protected boolean didLoadAll = false;
private void loadIds() protected void loadIds()
{ {
for(File file : this.getFolder().listFiles(JsonFileFilter.getInstance())) for(File file : this.getFolder().listFiles(JsonFileFilter.getInstance()))
{ {
@ -51,14 +51,14 @@ public abstract class GsonClassManager<T> implements IClassManager<T>
} }
} }
private String idFromFile(File file) protected String idFromFile(File file)
{ {
if (file == null) return null; if (file == null) return null;
String name = file.getName(); String name = file.getName();
return name.substring(0, name.length()-5); return name.substring(0, name.length()-5);
} }
private File fileFromId(Object oid) protected File fileFromId(Object oid)
{ {
String id = this.idFix(oid); String id = this.idFix(oid);
if (id == null) return null; if (id == null) return null;
@ -148,7 +148,7 @@ public abstract class GsonClassManager<T> implements IClassManager<T>
return this.attach(entity, oid, false); return this.attach(entity, oid, false);
} }
private synchronized String attach(T entity, Object oid, boolean allowExistingIdUsage) protected synchronized String attach(T entity, Object oid, boolean allowExistingIdUsage)
{ {
// Check entity // Check entity
if (entity == null) return null; if (entity == null) return null;
@ -197,7 +197,7 @@ public abstract class GsonClassManager<T> implements IClassManager<T>
} }
// Assumes the id is correct! For internal use only! // Assumes the id is correct! For internal use only!
private synchronized void detach(T entity, String id) protected synchronized void detach(T entity, String id)
{ {
if (id != null) if (id != null)
{ {
@ -213,7 +213,7 @@ public abstract class GsonClassManager<T> implements IClassManager<T>
} }
} }
private void removeFile(String id) protected void removeFile(String id)
{ {
File file = this.fileFromId(id); File file = this.fileFromId(id);
if (file.exists()) if (file.exists())
@ -251,7 +251,7 @@ public abstract class GsonClassManager<T> implements IClassManager<T>
return this.save(id, entity); return this.save(id, entity);
} }
private boolean save(String id, T entity) protected boolean save(String id, T entity)
{ {
if (id == null) return false; if (id == null) return false;
if (entity == null) return false; if (entity == null) return false;

View File

@ -1,10 +1,18 @@
package com.massivecraft.mcore1.util; package com.massivecraft.mcore1.util;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet;
import java.util.Random; import java.util.Random;
import net.minecraft.server.ChunkPosition;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.Packet60Explosion;
import org.bukkit.Bukkit;
import org.bukkit.Effect; import org.bukkit.Effect;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.CraftWorld;
// http://mc.kev009.com/Protocol // http://mc.kev009.com/Protocol
// ----------------------------- // -----------------------------
@ -82,6 +90,38 @@ public class SmokeUtil
} }
} }
// Fake Explosion ========
public static void fakeExplosion(Location location, int radius)
{
if (location == null) return;
HashSet<ChunkPosition> blocks = new HashSet<ChunkPosition>();
int squRadius = radius * radius;
for(int x = -radius; x <= radius; x++)
{
for(int y = -radius; y <= radius; y++)
{
for(int z = -radius; z <= radius; z++)
{
if(x*x+y*y+z*z+x+y+z+0.75 < squRadius) // ???
{
blocks.add(new ChunkPosition(
(int)(location.getX()+x+0.5),
(int)(location.getY()+y+0.5),
(int)(location.getZ()+z+0.5)));
}
}
}
}
Packet60Explosion packet = new Packet60Explosion(location.getX(),location.getY(), location.getZ(), 0.1f, blocks);
CraftServer craftServer = (CraftServer) Bukkit.getServer();
MinecraftServer minecraftServer = craftServer.getServer();
minecraftServer.serverConfigurationManager.sendPacketNearby(location.getX(), location.getY(), location.getZ(), 64, ((CraftWorld)location.getWorld()).getHandle().dimension, packet);
}
// -------------------------------------------- // // -------------------------------------------- //
// Attach continuous effects to or locations // Attach continuous effects to or locations
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -74,8 +74,15 @@ public class Txt
parseReplacements.put("<good>", "\u00A7a"); parseReplacements.put("<good>", "\u00A7a");
parseReplacements.put("<b>", "\u00A7c"); parseReplacements.put("<b>", "\u00A7c");
parseReplacements.put("<bad>", "\u00A7c"); parseReplacements.put("<bad>", "\u00A7c");
parseReplacements.put("<k>", "\u00A7b");
parseReplacements.put("<key>", "\u00A7b");
parseReplacements.put("<v>", "\u00A7d");
parseReplacements.put("<value>", "\u00A7d");
parseReplacements.put("<h>", "\u00A7d"); parseReplacements.put("<h>", "\u00A7d");
parseReplacements.put("<highlight>", "\u00A7d"); parseReplacements.put("<highlight>", "\u00A7d");
parseReplacements.put("<c>", "\u00A7b"); parseReplacements.put("<c>", "\u00A7b");
parseReplacements.put("<command>", "\u00A7b"); parseReplacements.put("<command>", "\u00A7b");
parseReplacements.put("<p>", "\u00A73"); parseReplacements.put("<p>", "\u00A73");
@ -285,7 +292,7 @@ public class Txt
int pageheight = 9; int pageheight = 9;
int pagecount = (lines.size() / pageheight)+1; int pagecount = (lines.size() / pageheight)+1;
ret.add(titleize(title+" "+pageHumanBased+"/"+pagecount)); ret.add(titleize(title+parse("<a>")+" "+pageHumanBased+"/"+pagecount));
if (pagecount == 0) if (pagecount == 0)
{ {