Closes #906
Add command confirmation for setowner
Require confirmation delayed by 1 tick
This commit is contained in:
Jesse Boyd 2016-03-01 02:01:52 +11:00
parent 43b7a7aba8
commit 841809b93d
10 changed files with 111 additions and 80 deletions

View File

@ -9,7 +9,7 @@ import java.util.Map;
* The {@code TAG_Compound} tag. * The {@code TAG_Compound} tag.
*/ */
public final class CompoundTag extends Tag { public final class CompoundTag extends Tag {
private final Map<String, Tag> value; private Map<String, Tag> value;
/** /**
* Creates the tag with an empty name. * Creates the tag with an empty name.
@ -56,7 +56,13 @@ public final class CompoundTag extends Tag {
* @return the new compound tag * @return the new compound tag
*/ */
public CompoundTag setValue(final Map<String, Tag> value) { public CompoundTag setValue(final Map<String, Tag> value) {
return new CompoundTag(getName(), value); if (value == null) {
this.value = Collections.unmodifiableMap(new HashMap<String, Tag>());
}
else {
this.value = Collections.unmodifiableMap(value);
}
return this;
} }
/** /**

View File

@ -2007,6 +2007,7 @@ public class PS {
final Map<String, Object> options = new HashMap<>(); final Map<String, Object> options = new HashMap<>();
// Command confirmation // Command confirmation
options.put("confirmation.setowner", Settings.CONFIRM_SETOWNER);
options.put("confirmation.clear", Settings.CONFIRM_CLEAR); options.put("confirmation.clear", Settings.CONFIRM_CLEAR);
options.put("confirmation.delete", Settings.CONFIRM_DELETE); options.put("confirmation.delete", Settings.CONFIRM_DELETE);
options.put("confirmation.unlink", Settings.CONFIRM_UNLINK); options.put("confirmation.unlink", Settings.CONFIRM_UNLINK);
@ -2136,6 +2137,7 @@ public class PS {
} }
// Command confirmation // Command confirmation
Settings.CONFIRM_SETOWNER = config.getBoolean("confirmation.setowner");
Settings.CONFIRM_CLEAR = config.getBoolean("confirmation.clear"); Settings.CONFIRM_CLEAR = config.getBoolean("confirmation.clear");
Settings.CONFIRM_DELETE = config.getBoolean("confirmation.delete"); Settings.CONFIRM_DELETE = config.getBoolean("confirmation.delete");
Settings.CONFIRM_UNLINK = config.getBoolean("confirmation.unlink"); Settings.CONFIRM_UNLINK = config.getBoolean("confirmation.unlink");

View File

@ -24,9 +24,7 @@ import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.config.Settings; import com.intellectualcrafters.plot.config.Settings;
import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.*;
import com.intellectualcrafters.plot.util.Permissions;
import com.intellectualcrafters.plot.util.UUIDHandler;
import com.plotsquared.general.commands.CommandDeclaration; import com.plotsquared.general.commands.CommandDeclaration;
import java.util.HashSet; import java.util.HashSet;
@ -43,7 +41,7 @@ requiredType = RequiredType.NONE)
public class Owner extends SetCommand { public class Owner extends SetCommand {
@Override @Override
public boolean set(PlotPlayer plr, Plot plot, String value) { public boolean set(final PlotPlayer plr, final Plot plot, String value) {
HashSet<Plot> plots = plot.getConnectedPlots(); HashSet<Plot> plots = plot.getConnectedPlots();
UUID uuid = null; UUID uuid = null;
String name = null; String name = null;
@ -75,7 +73,7 @@ public class Owner extends SetCommand {
C.ALREADY_OWNER.send(plr); C.ALREADY_OWNER.send(plr);
return false; return false;
} }
PlotPlayer other = UUIDHandler.getPlayer(uuid); final PlotPlayer other = UUIDHandler.getPlayer(uuid);
if (!Permissions.hasPermission(plr, "plots.admin.command.setowner")) { if (!Permissions.hasPermission(plr, "plots.admin.command.setowner")) {
if (other == null) { if (other == null) {
C.INVALID_PLAYER_OFFLINE.send(plr, value); C.INVALID_PLAYER_OFFLINE.send(plr, value);
@ -88,12 +86,23 @@ public class Owner extends SetCommand {
return false; return false;
} }
} }
final String finalName = name;
plot.setOwner(uuid); final UUID finalUUID = uuid;
plot.setSign(name); Runnable run = new Runnable() {
MainUtil.sendMessage(plr, C.SET_OWNER); @Override
if (other != null) { public void run() {
MainUtil.sendMessage(other, C.NOW_OWNER, plot.getArea() + ";" + plot.getId()); plot.setOwner(finalUUID);
plot.setSign(finalName);
MainUtil.sendMessage(plr, C.SET_OWNER);
if (other != null) {
MainUtil.sendMessage(other, C.NOW_OWNER, plot.getArea() + ";" + plot.getId());
}
}
};
if (Settings.CONFIRM_SETOWNER && !(Permissions.hasPermission(plr, "plots.confirm.bypass"))) {
CmdConfirm.addPending(plr, "/plot set owner " + value, run);
} else {
TaskManager.runTask(run);
} }
return true; return true;
} }

View File

@ -187,6 +187,7 @@ public class Settings {
/** /**
* Command confirmation * Command confirmation
*/ */
public static boolean CONFIRM_SETOWNER = true;
public static boolean CONFIRM_CLEAR = true; public static boolean CONFIRM_CLEAR = true;
public static boolean CONFIRM_DELETE = true; public static boolean CONFIRM_DELETE = true;
public static boolean CONFIRM_UNLINK = true; public static boolean CONFIRM_UNLINK = true;

View File

@ -1954,19 +1954,11 @@ public class SQLManager implements AbstractDB {
@Override @Override
public void setFlags(final Plot plot, final Collection<Flag> flags) { public void setFlags(final Plot plot, final Collection<Flag> flags) {
final StringBuilder flag_string = new StringBuilder(); final String flag_string = FlagManager.toString(flags);
int i = 0;
for (final Flag flag : flags) {
if (i != 0) {
flag_string.append(",");
}
flag_string.append(flag.getKey() + ":" + flag.getValueString().replaceAll(":", "\u00AF").replaceAll(",", "\u00B4"));
i++;
}
addPlotTask(plot, new UniqueStatement("setFlags") { addPlotTask(plot, new UniqueStatement("setFlags") {
@Override @Override
public void set(final PreparedStatement stmt) throws SQLException { public void set(final PreparedStatement stmt) throws SQLException {
stmt.setString(1, flag_string.toString()); stmt.setString(1, flag_string);
stmt.setInt(2, getId(plot)); stmt.setInt(2, getId(plot));
} }

View File

@ -20,11 +20,11 @@
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
package com.intellectualcrafters.plot.flag; package com.intellectualcrafters.plot.flag;
import com.intellectualcrafters.plot.util.StringMan;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import com.intellectualcrafters.plot.util.StringMan;
public class Flag implements Cloneable { public class Flag implements Cloneable {
private AbstractFlag key; private AbstractFlag key;
private Object value; private Object value;

View File

@ -23,21 +23,11 @@ package com.intellectualcrafters.plot.flag;
import com.intellectualcrafters.plot.PS; import com.intellectualcrafters.plot.PS;
import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.database.DBFunc; import com.intellectualcrafters.plot.database.DBFunc;
import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.*;
import com.intellectualcrafters.plot.object.PlotArea;
import com.intellectualcrafters.plot.object.PlotCluster;
import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.object.PlotSettings;
import com.intellectualcrafters.plot.object.RunnableVal;
import com.intellectualcrafters.plot.util.EventUtil; import com.intellectualcrafters.plot.util.EventUtil;
import com.intellectualcrafters.plot.util.Permissions; import com.intellectualcrafters.plot.util.Permissions;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/** /**
* Flag Manager Utility * Flag Manager Utility
@ -125,6 +115,19 @@ public class FlagManager {
return false; return false;
} }
public static String toString(Collection<Flag> flags) {
final StringBuilder flag_string = new StringBuilder();
int i = 0;
for (final Flag flag : flags) {
if (i != 0) {
flag_string.append(",");
}
flag_string.append(flag.getKey() + ":" + flag.getValueString().replaceAll(":", "\u00AF").replaceAll(",", "\u00B4"));
i++;
}
return flag_string.toString();
}
public static Flag getSettingFlag(final PlotArea area, final PlotSettings settings, final String id) { public static Flag getSettingFlag(final PlotArea area, final PlotSettings settings, final String id) {
Flag flag; Flag flag;
if (settings.flags.isEmpty() || (flag = settings.flags.get(id)) == null) { if (settings.flags.isEmpty() || (flag = settings.flags.get(id)) == null) {

View File

@ -4,20 +4,10 @@ import com.intellectualcrafters.plot.PS;
import com.intellectualcrafters.plot.commands.RequiredType; import com.intellectualcrafters.plot.commands.RequiredType;
import com.intellectualcrafters.plot.config.Settings; import com.intellectualcrafters.plot.config.Settings;
import com.intellectualcrafters.plot.database.DBFunc; import com.intellectualcrafters.plot.database.DBFunc;
import com.intellectualcrafters.plot.util.CmdConfirm; import com.intellectualcrafters.plot.util.*;
import com.intellectualcrafters.plot.util.EventUtil;
import com.intellectualcrafters.plot.util.ExpireManager;
import com.intellectualcrafters.plot.util.Permissions;
import com.intellectualcrafters.plot.util.PlotGamemode;
import com.intellectualcrafters.plot.util.PlotWeather;
import com.intellectualcrafters.plot.util.UUIDHandler;
import com.plotsquared.general.commands.CommandCaller; import com.plotsquared.general.commands.CommandCaller;
import java.util.HashMap; import java.util.*;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@ -393,7 +383,6 @@ public abstract class PlotPlayer implements CommandCaller {
} }
String name = getName(); String name = getName();
ExpireManager.dates.put(getUUID(), System.currentTimeMillis()); ExpireManager.dates.put(getUUID(), System.currentTimeMillis());
CmdConfirm.removePending(name);
UUIDHandler.getPlayers().remove(name); UUIDHandler.getPlayers().remove(name);
PS.get().IMP.unregister(this); PS.get().IMP.unregister(this);
} }

View File

@ -1,42 +1,27 @@
package com.intellectualcrafters.plot.util; package com.intellectualcrafters.plot.util;
import java.util.HashMap;
import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.object.CmdInstance; import com.intellectualcrafters.plot.object.CmdInstance;
import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.PlotPlayer;
public class CmdConfirm { public class CmdConfirm {
private static HashMap<String, CmdInstance> pending = new HashMap<>();
public static CmdInstance getPending(final PlotPlayer player) { public static CmdInstance getPending(final PlotPlayer player) {
if (player == null) { return player.<CmdInstance>getMeta("cmdConfirm");
return pending.get("__CONSOLE__");
}
return pending.get(player.getName());
} }
public static void removePending(final PlotPlayer player) { public static void removePending(final PlotPlayer player) {
if (player == null) { player.deleteMeta("cmdConfirm");
pending.remove("__CONSOLE__");
} else {
pending.remove(player.getName());
}
}
public static void removePending(final String name) {
pending.remove(name);
} }
public static void addPending(final PlotPlayer player, final String commandStr, final Runnable runnable) { public static void addPending(final PlotPlayer player, final String commandStr, final Runnable runnable) {
removePending(player);
MainUtil.sendMessage(player, C.REQUIRES_CONFIRM, commandStr); MainUtil.sendMessage(player, C.REQUIRES_CONFIRM, commandStr);
final CmdInstance cmd = new CmdInstance(runnable); TaskManager.runTaskLater(new Runnable() {
String name; @Override
if (player == null) { public void run() {
name = "__CONSOLE__"; final CmdInstance cmd = new CmdInstance(runnable);
} else { player.setMeta("cmdConfirm", cmd);
name = player.getName(); }
} }, 1);
pending.put(name, cmd);
} }
} }

View File

@ -6,6 +6,7 @@ import com.intellectualcrafters.json.JSONArray;
import com.intellectualcrafters.json.JSONException; import com.intellectualcrafters.json.JSONException;
import com.intellectualcrafters.plot.PS; import com.intellectualcrafters.plot.PS;
import com.intellectualcrafters.plot.config.Settings; import com.intellectualcrafters.plot.config.Settings;
import com.intellectualcrafters.plot.flag.Flag;
import com.intellectualcrafters.plot.generator.ClassicPlotWorld; import com.intellectualcrafters.plot.generator.ClassicPlotWorld;
import com.intellectualcrafters.plot.object.*; import com.intellectualcrafters.plot.object.*;
import com.intellectualcrafters.plot.object.schematic.PlotItem; import com.intellectualcrafters.plot.object.schematic.PlotItem;
@ -119,6 +120,17 @@ public abstract class SchematicHandler {
return; return;
} }
try { try {
// Set flags
if (plot.hasOwner()) {
Map<String, Tag> flags = schematic.getFlags();
if (!flags.isEmpty()) {
for (Map.Entry<String, Tag> entry : flags.entrySet()) {
plot.setFlag(entry.getKey(), StringTag.class.cast(entry.getValue()).getValue());
}
}
}
final Dimension demensions = schematic.getSchematicDimension(); final Dimension demensions = schematic.getSchematicDimension();
final int WIDTH = demensions.getX(); final int WIDTH = demensions.getX();
final int LENGTH = demensions.getZ(); final int LENGTH = demensions.getZ();
@ -362,6 +374,12 @@ public abstract class SchematicHandler {
final short height = ShortTag.class.cast(tagMap.get("Height")).getValue(); final short height = ShortTag.class.cast(tagMap.get("Height")).getValue();
final byte[] block_sml = ByteArrayTag.class.cast(tagMap.get("Blocks")).getValue(); final byte[] block_sml = ByteArrayTag.class.cast(tagMap.get("Blocks")).getValue();
final byte[] data = ByteArrayTag.class.cast(tagMap.get("Data")).getValue(); final byte[] data = ByteArrayTag.class.cast(tagMap.get("Data")).getValue();
final Map<String, Tag> flags;
if (tagMap.containsKey("Flags")) {
flags = CompoundTag.class.cast(tagMap.get("Flags")).getValue();
} else {
flags = null;
}
final short[] block = new short[block_sml.length]; final short[] block = new short[block_sml.length];
for (int i = 0; i < block.length; i++) { for (int i = 0; i < block.length; i++) {
@ -392,7 +410,7 @@ public abstract class SchematicHandler {
// Schematic schem = new Schematic(collection, dimension, file); // Schematic schem = new Schematic(collection, dimension, file);
final Dimension dimensions = new Dimension(width, height, length); final Dimension dimensions = new Dimension(width, height, length);
final Schematic schem = new Schematic(block, data, dimensions); final Schematic schem = new Schematic(block, data, dimensions, flags);
// Slow // Slow
try { try {
@ -632,8 +650,24 @@ public abstract class SchematicHandler {
public abstract void getCompoundTag(final String world, Set<RegionWrapper> regions, final RunnableVal<CompoundTag> whenDone); public abstract void getCompoundTag(final String world, Set<RegionWrapper> regions, final RunnableVal<CompoundTag> whenDone);
public void getCompoundTag(Plot plot, final RunnableVal<CompoundTag> whenDone) { public void getCompoundTag(final Plot plot, final RunnableVal<CompoundTag> whenDone) {
getCompoundTag(plot.getArea().worldname, plot.getRegions(), whenDone); getCompoundTag(plot.getArea().worldname, plot.getRegions(), new RunnableVal<CompoundTag>() {
@Override
public void run(CompoundTag value) {
if (plot.getFlags().size() > 0) {
HashMap<String, Tag> flagMap = new HashMap<>();
for (Map.Entry<String, Flag> entry : plot.getFlags().entrySet()) {
String key = entry.getKey();
flagMap.put(key, new StringTag(key, entry.getValue().getValueString()));
}
CompoundTag tag = new CompoundTag("Flags", flagMap);
HashMap<String, Tag> map = new HashMap<String, Tag>(value.getValue());
map.put("Flags", tag);
value.setValue(map);
whenDone.run(value);
}
}
});
} }
/** /**
@ -675,14 +709,24 @@ public abstract class SchematicHandler {
// Lossy but fast // Lossy but fast
private final short[] ids; private final short[] ids;
private final byte[] datas; private final byte[] datas;
private Map<String, Tag> flags;
private final Dimension schematicDimension; private final Dimension schematicDimension;
private HashSet<PlotItem> items; private HashSet<PlotItem> items;
public Schematic(final short[] i, final byte[] b, final Dimension d) { public Schematic(final short[] i, final byte[] b, final Dimension d, Map<String, Tag> flags) {
ids = i; ids = i;
datas = b; datas = b;
schematicDimension = d; schematicDimension = d;
setFlags(flags);
}
public Map<String, Tag> getFlags() {
return flags;
}
public void setFlags(Map<String, Tag> flags) {
this.flags = flags == null ? new HashMap<String, Tag>() : flags;
} }
/** /**
@ -767,7 +811,7 @@ public abstract class SchematicHandler {
} }
} }
} }
return new Schematic(ids2, datas2, new Dimension(width, height, length)); return new Schematic(ids2, datas2, new Dimension(width, height, length), null);
} }
public void save(final File file) { public void save(final File file) {