Port UntrustedVisitFlag

This commit is contained in:
Alexander Söderberg 2020-02-13 13:42:06 +01:00
parent 30d2785ef2
commit 0b1d4d4d13
9 changed files with 43 additions and 91 deletions

View File

@ -9,6 +9,7 @@ import com.github.intellectualsites.plotsquared.plot.config.Captions;
import com.github.intellectualsites.plotsquared.plot.config.Settings;
import com.github.intellectualsites.plotsquared.plot.flag.Flags;
import com.github.intellectualsites.plotsquared.plot.flags.implementations.ExplosionFlag;
import com.github.intellectualsites.plotsquared.plot.flags.implementations.UntrustedVisitFlag;
import com.github.intellectualsites.plotsquared.plot.listener.PlayerBlockEventType;
import com.github.intellectualsites.plotsquared.plot.listener.PlotListener;
import com.github.intellectualsites.plotsquared.plot.object.Location;
@ -790,7 +791,7 @@ import java.util.regex.Pattern;
// to is identical to the plot's home location, and untrusted-visit is true
// i.e. untrusted-visit can override deny-teleport
// this is acceptable, because otherwise it wouldn't make sense to have both flags set
if (!result && !(Flags.UNTRUSTED_VISIT.isTrue(plot) && plot.getHome().equals(BukkitUtil.getLocationFull(to)))) {
if (!result && !(plot.getFlag(UntrustedVisitFlag.class) && plot.getHome().equals(BukkitUtil.getLocationFull(to)))) {
MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT,
Captions.PERMISSION_ADMIN_ENTRY_DENIED);
event.setCancelled(true);

View File

@ -5,7 +5,7 @@ import com.github.intellectualsites.plotsquared.commands.CommandDeclaration;
import com.github.intellectualsites.plotsquared.plot.PlotSquared;
import com.github.intellectualsites.plotsquared.plot.config.Captions;
import com.github.intellectualsites.plotsquared.plot.config.Settings;
import com.github.intellectualsites.plotsquared.plot.flag.Flags;
import com.github.intellectualsites.plotsquared.plot.flags.implementations.UntrustedVisitFlag;
import com.github.intellectualsites.plotsquared.plot.object.Plot;
import com.github.intellectualsites.plotsquared.plot.object.PlotArea;
import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
@ -140,7 +140,7 @@ import java.util.concurrent.CompletableFuture;
}
} else {
if (!Permissions.hasPermission(player, Captions.PERMISSION_VISIT_OTHER) &&
!Flags.UNTRUSTED_VISIT.isTrue(plot)) {
!plot.getFlag(UntrustedVisitFlag.class)) {
Captions.NO_PERMISSION.send(player, Captions.PERMISSION_VISIT_OTHER);
return CompletableFuture.completedFuture(false);
}

View File

@ -550,6 +550,7 @@ public enum Captions implements Caption {
FLAG_DESCRIPTION_EXPLOSION("Set to 'true' to enable explosions in the plot, and 'false' to disable them", "Flags"),
FLAG_DESCRIPTION_MUSIC("Set to a music disk ID (item name) to play the music disc inside of the plot", "Flags"),
FLAG_DESCRIPTION_FLIGHT("Set to 'true' to enable flight within the plot when in survival or adventure mode", "Flags"),
FLAG_DESCRIPTION_UNTRUSTED("Set to 'false' to disallow untrusted players from visiting the plot", "Flags"),
//</editor-fold>
//<editor-fold desc="Flag category errors">
FLAG_ERROR_BOOLEAN("Flag value must be a boolean (true|false)", "Flags"),

View File

@ -3,6 +3,7 @@ package com.github.intellectualsites.plotsquared.plot.flag;
import com.github.intellectualsites.plotsquared.plot.PlotSquared;
import com.github.intellectualsites.plotsquared.plot.database.DBFunc;
import com.github.intellectualsites.plotsquared.plot.flags.FlagContainer;
import com.github.intellectualsites.plotsquared.plot.flags.PlotFlag;
import com.github.intellectualsites.plotsquared.plot.object.Plot;
import com.github.intellectualsites.plotsquared.plot.object.PlotArea;
import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
@ -131,6 +132,11 @@ public class FlagManager {
return getSettingFlag(plot.getArea(), plot.getSettings(), flag);
}
public static <V> boolean addPlotFlag(Plot origin, PlotFlag<V, ?> flag, Object value) {
// TODO: Implement this
return true;
}
/**
* Add a flag to a plot.
*
@ -194,6 +200,11 @@ public class FlagManager {
return getPlotFlags(area, settings, false);
}
public static boolean removePlotFlag(Plot origin, PlotFlag<?, ?> flag) {
// TODO: Implement
return true;
}
/**
* Removes a flag from a certain plot.
*

View File

@ -1,15 +1,8 @@
package com.github.intellectualsites.plotsquared.plot.flag;
import com.github.intellectualsites.plotsquared.plot.PlotSquared;
import com.github.intellectualsites.plotsquared.plot.config.Captions;
import com.github.intellectualsites.plotsquared.plot.util.MainUtil;
import com.github.intellectualsites.plotsquared.plot.util.MathMan;
import com.github.intellectualsites.plotsquared.plot.util.StringMan;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
public final class Flags {
@ -114,85 +107,8 @@ public final class Flags {
return Captions.FLAG_ERROR_KEEP.getTranslated();
}
};
public static final TeleportDenyFlag DENY_TELEPORT = new TeleportDenyFlag("deny-teleport");
public static final BooleanFlag DENY_EXIT = new BooleanFlag("deny-exit");
public static final BooleanFlag UNTRUSTED_VISIT = new BooleanFlag("untrusted-visit");
private static final HashMap<String, Flag<?>> flags;
static {
flags = new HashMap<>();
try {
for (Field field : Flags.class.getFields()) {
String fieldName = field.getName().replace("_", "-").toLowerCase();
Object fieldValue = field.get(null);
if (!(fieldValue instanceof Flag)) {
continue;
}
Flag flag = (Flag) fieldValue;
if (!flag.getName().equals(fieldName)) {
PlotSquared.debug(
Flags.class + "Field doesn't match: " + fieldName + " != " + flag
.getName());
}
flags.put(flag.getName(), flag);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Get an immutable collection of registered flags.
*
* @return a collection of registered flags.
*/
public static Collection<Flag<?>> getFlags() {
return Collections.unmodifiableCollection(flags.values());
}
public static Flag<?> getFlag(String flag) {
return flags.get(flag);
}
public static void registerFlag(final Flag<?> flag) {
final Flag<?> duplicate = flags.put(flag.getName(), flag);
if (duplicate != null) {
PlotSquared.get().forEachPlotArea(value -> {
if (value.DEFAULT_FLAGS.containsKey(duplicate)) {
Object remove = value.DEFAULT_FLAGS.remove(duplicate);
try {
if (remove instanceof Collection
&& remove.getClass().getMethod("toString").getDeclaringClass()
== Object.class) {
value.DEFAULT_FLAGS.put(flag,
flag.parseValue(StringMan.join((Collection) remove, ',')));
} else {
value.DEFAULT_FLAGS.put(flag, flag.parseValue("" + remove));
}
} catch (NoSuchMethodException neverHappens) {
neverHappens.printStackTrace();
}
}
});
PlotSquared.get().forEachPlotRaw(value -> {
if (value.hasFlag(duplicate)) {
Object remove = value.getFlags().remove(duplicate);
try {
if (remove instanceof Collection
&& remove.getClass().getMethod("toString").getDeclaringClass()
== Object.class) {
value.getFlags().put(flag,
flag.parseValue(StringMan.join((Collection) remove, ',')));
} else {
value.getFlags().put(flag, flag.parseValue("" + remove));
}
} catch (NoSuchMethodException neverHappens) {
neverHappens.printStackTrace();
}
}
});
}
}
}

View File

@ -3,6 +3,7 @@ package com.github.intellectualsites.plotsquared.plot.flags;
import com.github.intellectualsites.plotsquared.plot.flags.implementations.ExplosionFlag;
import com.github.intellectualsites.plotsquared.plot.flags.implementations.FlightFlag;
import com.github.intellectualsites.plotsquared.plot.flags.implementations.MusicFlag;
import com.github.intellectualsites.plotsquared.plot.flags.implementations.UntrustedVisitFlag;
import lombok.Getter;
import javax.annotation.Nonnull;
@ -42,6 +43,7 @@ public final class GlobalFlagContainer extends FlagContainer {
this.addFlag(ExplosionFlag.EXPLOSION_FALSE);
this.addFlag(MusicFlag.MUSIC_FLAG_NONE);
this.addFlag(FlightFlag.FLIGHT_FLAG_FALSE);
this.addFlag(UntrustedVisitFlag.UNTRUSTED_VISIT_FLAG_TRUE);
}
@Override public void addFlag(PlotFlag<?, ?> flag) {

View File

@ -0,0 +1,22 @@
package com.github.intellectualsites.plotsquared.plot.flags.implementations;
import com.github.intellectualsites.plotsquared.plot.config.Captions;
import com.github.intellectualsites.plotsquared.plot.flags.types.BooleanFlag;
import org.jetbrains.annotations.NotNull;
public class UntrustedVisitFlag extends BooleanFlag<UntrustedVisitFlag> {
public static UntrustedVisitFlag UNTRUSTED_VISIT_FLAG_TRUE = new UntrustedVisitFlag(true);
protected UntrustedVisitFlag(boolean value) {
super(value, Captions.FLAG_DESCRIPTION_UNTRUSTED);
}
@Override public String getExample() {
return "";
}
@Override protected UntrustedVisitFlag flagOf(@NotNull Boolean value) {
return new UntrustedVisitFlag(value);
}
}

View File

@ -1089,7 +1089,7 @@ public class Plot {
* @param flag Flag to set
* @param value Flag value
*/
public <V> boolean setFlag(Flag<V> flag, Object value) {
public <V> boolean setFlag(PlotFlag<V, ?> flag, Object value) {
if (flag == Flags.KEEP && ExpireManager.IMP != null) {
ExpireManager.IMP.updateExpired(this);
}
@ -1102,7 +1102,7 @@ public class Plot {
* @param flag the flag to remove
* @return success
*/
public boolean removeFlag(Flag<?> flag) {
public boolean removeFlag(PlotFlag<?, ?> flag) {
return FlagManager.removePlotFlag(this, flag);
}

View File

@ -72,7 +72,6 @@ public abstract class PlotArea {
public boolean SCHEMATIC_ON_CLAIM = false;
public String SCHEMATIC_FILE = "null";
public List<String> SCHEMATICS = null;
public Map<Flag<?>, Object> DEFAULT_FLAGS;
public boolean USE_ECONOMY = false;
public Map<String, Expression<Double>> PRICES = new HashMap<>();
public boolean SPAWN_EGGS = false;