Check for f perm in building

This commit is contained in:
Trent Hensler 2018-02-03 12:49:04 -08:00
parent 9384b0faa6
commit 182022fe5f
10 changed files with 88 additions and 62 deletions

View File

@ -439,7 +439,7 @@ run a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However, to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do modify any covered work. These permissableActions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so. covered work, you indicate your acceptance of this License to do so.

View File

@ -6,7 +6,7 @@ import com.massivecraft.factions.struct.Relation;
import com.massivecraft.factions.struct.Role; import com.massivecraft.factions.struct.Role;
import com.massivecraft.factions.util.LazyLocation; import com.massivecraft.factions.util.LazyLocation;
import com.massivecraft.factions.zcore.fperms.Access; import com.massivecraft.factions.zcore.fperms.Access;
import com.massivecraft.factions.zcore.fperms.Action; import com.massivecraft.factions.zcore.fperms.PermissableAction;
import com.massivecraft.factions.zcore.fperms.Permissable; import com.massivecraft.factions.zcore.fperms.Permissable;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
@ -139,11 +139,11 @@ public interface Faction extends EconomyParticipator {
public int getDeaths(); public int getDeaths();
public Access getAccess(Permissable permissable, Action action); public Access getAccess(Permissable permissable, PermissableAction permissableAction);
public Access getAccess(FPlayer player, Action action); public Access getAccess(FPlayer player, PermissableAction permissableAction);
public void setPermission(Permissable permissable, Action action, Access access); public void setPermission(Permissable permissable, PermissableAction permissableAction, Access access);
public void resetPerms(); public void resetPerms();

View File

@ -14,7 +14,7 @@ import com.massivecraft.factions.struct.Relation;
import com.massivecraft.factions.util.*; import com.massivecraft.factions.util.*;
import com.massivecraft.factions.zcore.MPlugin; import com.massivecraft.factions.zcore.MPlugin;
import com.massivecraft.factions.zcore.fperms.Access; import com.massivecraft.factions.zcore.fperms.Access;
import com.massivecraft.factions.zcore.fperms.Action; import com.massivecraft.factions.zcore.fperms.PermissableAction;
import com.massivecraft.factions.zcore.util.TextUtil; import com.massivecraft.factions.zcore.util.TextUtil;
import net.milkbowl.vault.permission.Permission; import net.milkbowl.vault.permission.Permission;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -171,7 +171,7 @@ public class P extends MPlugin {
Type mapFLocToStringSetType = new TypeToken<Map<FLocation, Set<String>>>() { Type mapFLocToStringSetType = new TypeToken<Map<FLocation, Set<String>>>() {
}.getType(); }.getType();
Type accessTypeAdatper = new TypeToken<Map<Relation, Map<Action, Access>>>() { Type accessTypeAdatper = new TypeToken<Map<Relation, Map<PermissableAction, Access>>>() {
}.getType(); }.getType();
return new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().excludeFieldsWithModifiers(Modifier.TRANSIENT, Modifier.VOLATILE).registerTypeAdapter(accessTypeAdatper, new PermissionsMapTypeAdapter()).registerTypeAdapter(LazyLocation.class, new MyLocationTypeAdapter()).registerTypeAdapter(mapFLocToStringSetType, new MapFLocToStringSetTypeAdapter()).registerTypeAdapterFactory(EnumTypeAdapter.ENUM_FACTORY); return new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().excludeFieldsWithModifiers(Modifier.TRANSIENT, Modifier.VOLATILE).registerTypeAdapter(accessTypeAdatper, new PermissionsMapTypeAdapter()).registerTypeAdapter(LazyLocation.class, new MyLocationTypeAdapter()).registerTypeAdapter(mapFLocToStringSetType, new MapFLocToStringSetTypeAdapter()).registerTypeAdapterFactory(EnumTypeAdapter.ENUM_FACTORY);

View File

@ -4,7 +4,7 @@ import com.massivecraft.factions.P;
import com.massivecraft.factions.struct.Permission; import com.massivecraft.factions.struct.Permission;
import com.massivecraft.factions.struct.Relation; import com.massivecraft.factions.struct.Relation;
import com.massivecraft.factions.zcore.fperms.Access; import com.massivecraft.factions.zcore.fperms.Access;
import com.massivecraft.factions.zcore.fperms.Action; import com.massivecraft.factions.zcore.fperms.PermissableAction;
import com.massivecraft.factions.zcore.util.TL; import com.massivecraft.factions.zcore.util.TL;
import java.util.Arrays; import java.util.Arrays;
@ -47,7 +47,7 @@ public class CmdPerm extends FCommand {
} }
Set<Relation> relations = new HashSet<>(); Set<Relation> relations = new HashSet<>();
Set<Action> actions = new HashSet<>(); Set<PermissableAction> permissableActions = new HashSet<>();
boolean allRelations = argAsString(0).equalsIgnoreCase("all"); boolean allRelations = argAsString(0).equalsIgnoreCase("all");
boolean allActions = argAsString(1).equalsIgnoreCase("all"); boolean allActions = argAsString(1).equalsIgnoreCase("all");
@ -65,15 +65,15 @@ public class CmdPerm extends FCommand {
} }
if (allActions) { if (allActions) {
actions.addAll(Arrays.asList(Action.values())); permissableActions.addAll(Arrays.asList(PermissableAction.values()));
} else { } else {
Action action = Action.fromString(argAsString(1)); PermissableAction permissableAction = PermissableAction.fromString(argAsString(1));
if (action == null) { if (permissableAction == null) {
fme.msg(TL.COMMAND_PERM_INVALID_ACTION); fme.msg(TL.COMMAND_PERM_INVALID_ACTION);
return; return;
} }
actions.add(action); permissableActions.add(permissableAction);
} }
Access access = Access.fromString(argAsString(2)); Access access = Access.fromString(argAsString(2));
@ -84,8 +84,8 @@ public class CmdPerm extends FCommand {
} }
for (Relation relation : relations) { for (Relation relation : relations) {
for (Action action : actions) { for (PermissableAction permissableAction : permissableActions) {
fme.getFaction().setPermission(relation, action, access); fme.getFaction().setPermission(relation, permissableAction, access);
} }
} }

View File

@ -4,7 +4,7 @@ import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.struct.Permission; import com.massivecraft.factions.struct.Permission;
import com.massivecraft.factions.struct.Role; import com.massivecraft.factions.struct.Role;
import com.massivecraft.factions.zcore.fperms.Access; import com.massivecraft.factions.zcore.fperms.Access;
import com.massivecraft.factions.zcore.fperms.Action; import com.massivecraft.factions.zcore.fperms.PermissableAction;
import com.massivecraft.factions.zcore.util.TL; import com.massivecraft.factions.zcore.util.TL;
public class FPromoteCommand extends FCommand { public class FPromoteCommand extends FCommand {
@ -38,7 +38,7 @@ public class FPromoteCommand extends FCommand {
return; return;
} }
Access access = myFaction.getAccess(fme.getRole(), Action.PROMOTE); Access access = myFaction.getAccess(fme.getRole(), PermissableAction.PROMOTE);
// Well this is messy. // Well this is messy.
if (access == null || access == Access.UNDEFINED) { if (access == null || access == Access.UNDEFINED) {

View File

@ -5,7 +5,7 @@ import com.massivecraft.factions.integration.Worldguard;
import com.massivecraft.factions.struct.Permission; import com.massivecraft.factions.struct.Permission;
import com.massivecraft.factions.struct.Relation; import com.massivecraft.factions.struct.Relation;
import com.massivecraft.factions.zcore.fperms.Access; import com.massivecraft.factions.zcore.fperms.Access;
import com.massivecraft.factions.zcore.fperms.Action; import com.massivecraft.factions.zcore.fperms.PermissableAction;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
@ -252,7 +252,7 @@ public class FactionsBlockListener implements Listener {
} }
} }
Access access = otherFaction.getAccess(me, Action.fromString(action)); Access access = otherFaction.getAccess(me, PermissableAction.fromString(action));
if (access != null && access != Access.UNDEFINED) { if (access != null && access != Access.UNDEFINED) {
// TODO: Update this once new access values are added other than just allow / deny. // TODO: Update this once new access values are added other than just allow / deny.
return access == Access.ALLOW; return access == Access.ALLOW;

View File

@ -11,6 +11,7 @@ import com.massivecraft.factions.struct.Relation;
import com.massivecraft.factions.struct.Role; import com.massivecraft.factions.struct.Role;
import com.massivecraft.factions.util.VisualizeUtil; import com.massivecraft.factions.util.VisualizeUtil;
import com.massivecraft.factions.zcore.fperms.Access; import com.massivecraft.factions.zcore.fperms.Access;
import com.massivecraft.factions.zcore.fperms.PermissableAction;
import com.massivecraft.factions.zcore.persist.MemoryFPlayer; import com.massivecraft.factions.zcore.persist.MemoryFPlayer;
import com.massivecraft.factions.zcore.util.TL; import com.massivecraft.factions.zcore.util.TL;
import com.massivecraft.factions.zcore.util.TextUtil; import com.massivecraft.factions.zcore.util.TextUtil;
@ -32,10 +33,9 @@ import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Level; import java.util.logging.Level;
public class FactionsPlayerListener implements Listener { public class FactionsPlayerListener implements Listener {
public P p; private P p;
public FactionsPlayerListener(P p) { public FactionsPlayerListener(P p) {
this.p = p; this.p = p;
@ -355,7 +355,7 @@ public class FactionsPlayerListener implements Listener {
Relation rel = myFaction.getRelationTo(otherFaction); Relation rel = myFaction.getRelationTo(otherFaction);
Access access = otherFaction.getAccess(me, com.massivecraft.factions.zcore.fperms.Action.ITEM); Access access = otherFaction.getAccess(me, PermissableAction.ITEM);
if (access != null && access != Access.UNDEFINED) { if (access != null && access != Access.UNDEFINED) {
// TODO: Update this once new access values are added other than just allow / deny. // TODO: Update this once new access values are added other than just allow / deny.
return access == Access.ALLOW; return access == Access.ALLOW;
@ -422,10 +422,36 @@ public class FactionsPlayerListener implements Listener {
} }
} }
Access access = otherFaction.getAccess(me, com.massivecraft.factions.zcore.fperms.Action.BUILD); PermissableAction action = null;
if (access != null && access != Access.UNDEFINED) {
// TODO: Update this once new access values are added other than just allow / deny. switch (block.getType()) {
return access == Access.ALLOW; case LEVER:
action = PermissableAction.LEVER;
break;
case STONE_BUTTON:
case WOOD_BUTTON:
action = PermissableAction.BUTTON;
break;
case DARK_OAK_DOOR:
case ACACIA_DOOR:
case BIRCH_DOOR:
case IRON_DOOR:
case JUNGLE_DOOR:
case SPRUCE_DOOR:
case TRAP_DOOR:
case WOOD_DOOR:
case WOODEN_DOOR:
action = PermissableAction.DOOR;
break;
default:
break;
}
// F PERM check runs through before other checks.
Access access = otherFaction.getAccess(me, action);
if (access == null || access == Access.DENY) {
me.msg(TL.GENERIC_NOPERMISSION, action);
return false;
} }
// We only care about some material types. // We only care about some material types.

View File

@ -4,7 +4,7 @@ import com.google.gson.*;
import com.massivecraft.factions.P; import com.massivecraft.factions.P;
import com.massivecraft.factions.struct.Relation; import com.massivecraft.factions.struct.Relation;
import com.massivecraft.factions.zcore.fperms.Access; import com.massivecraft.factions.zcore.fperms.Access;
import com.massivecraft.factions.zcore.fperms.Action; import com.massivecraft.factions.zcore.fperms.PermissableAction;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.HashMap; import java.util.HashMap;
@ -12,10 +12,10 @@ import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level; import java.util.logging.Level;
public class PermissionsMapTypeAdapter implements JsonDeserializer<Map<Relation, Map<Action, Access>>> { public class PermissionsMapTypeAdapter implements JsonDeserializer<Map<Relation, Map<PermissableAction, Access>>> {
@Override @Override
public Map<Relation, Map<Action, Access>> deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { public Map<Relation, Map<PermissableAction, Access>> deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
try { try {
JsonObject obj = json.getAsJsonObject(); JsonObject obj = json.getAsJsonObject();
@ -23,7 +23,7 @@ public class PermissionsMapTypeAdapter implements JsonDeserializer<Map<Relation,
return null; return null;
} }
Map<Relation, Map<Action, Access>> permissionsMap = new ConcurrentHashMap<>(); Map<Relation, Map<PermissableAction, Access>> permissionsMap = new ConcurrentHashMap<>();
// Top level is Relation // Top level is Relation
for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { for (Map.Entry<String, JsonElement> entry : obj.entrySet()) {
@ -31,10 +31,10 @@ public class PermissionsMapTypeAdapter implements JsonDeserializer<Map<Relation,
// Second level is the map between action -> access // Second level is the map between action -> access
for (Map.Entry<String, JsonElement> entry2 : entry.getValue().getAsJsonObject().entrySet()) { for (Map.Entry<String, JsonElement> entry2 : entry.getValue().getAsJsonObject().entrySet()) {
Map<Action, Access> accessMap = new HashMap<>(); Map<PermissableAction, Access> accessMap = new HashMap<>();
Action action = Action.fromString(entry2.getKey()); PermissableAction permissableAction = PermissableAction.fromString(entry2.getKey());
Access access = Access.fromString(entry2.getValue().getAsString()); Access access = Access.fromString(entry2.getValue().getAsString());
accessMap.put(action, access); accessMap.put(permissableAction, access);
permissionsMap.put(relation, accessMap); permissionsMap.put(relation, accessMap);
} }

View File

@ -1,6 +1,6 @@
package com.massivecraft.factions.zcore.fperms; package com.massivecraft.factions.zcore.fperms;
public enum Action { public enum PermissableAction {
BUILD("build"), BUILD("build"),
DESTROY("destroy"), DESTROY("destroy"),
FROST_WALK("frostwalk"), FROST_WALK("frostwalk"),
@ -22,7 +22,7 @@ public enum Action {
private String name; private String name;
Action(String name) { PermissableAction(String name) {
this.name = name; this.name = name;
} }
@ -41,10 +41,10 @@ public enum Action {
* @param check * @param check
* @return * @return
*/ */
public static Action fromString(String check) { public static PermissableAction fromString(String check) {
for (Action action : values()) { for (PermissableAction permissableAction : values()) {
if (action.name().equalsIgnoreCase(check)) { if (permissableAction.name().equalsIgnoreCase(check)) {
return action; return permissableAction;
} }
} }

View File

@ -11,7 +11,7 @@ import com.massivecraft.factions.util.LazyLocation;
import com.massivecraft.factions.util.MiscUtil; import com.massivecraft.factions.util.MiscUtil;
import com.massivecraft.factions.util.RelationUtil; import com.massivecraft.factions.util.RelationUtil;
import com.massivecraft.factions.zcore.fperms.Access; import com.massivecraft.factions.zcore.fperms.Access;
import com.massivecraft.factions.zcore.fperms.Action; import com.massivecraft.factions.zcore.fperms.PermissableAction;
import com.massivecraft.factions.zcore.fperms.Permissable; import com.massivecraft.factions.zcore.fperms.Permissable;
import com.massivecraft.factions.zcore.util.TL; import com.massivecraft.factions.zcore.util.TL;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -49,7 +49,7 @@ public abstract class MemoryFaction implements Faction, EconomyParticipator {
private long lastDeath; private long lastDeath;
protected int maxVaults; protected int maxVaults;
protected Role defaultRole; protected Role defaultRole;
protected Map<Permissable, Map<Action, Access>> permissions = new HashMap<>(); protected Map<Permissable, Map<PermissableAction, Access>> permissions = new HashMap<>();
public HashMap<String, List<String>> getAnnouncements() { public HashMap<String, List<String>> getAnnouncements() {
return this.announcements; return this.announcements;
@ -325,29 +325,29 @@ public abstract class MemoryFaction implements Faction, EconomyParticipator {
// -------------------------------------------- // // -------------------------------------------- //
public Access getAccess(Permissable permissable, Action action) { public Access getAccess(Permissable permissable, PermissableAction permissableAction) {
if (permissable == null || action == null) { if (permissable == null || permissableAction == null) {
return null; return Access.UNDEFINED;
} }
Map<Action, Access> accessMap = permissions.get(permissable); Map<PermissableAction, Access> accessMap = permissions.get(permissable);
if (accessMap != null && accessMap.containsKey(action)) { if (accessMap != null && accessMap.containsKey(permissableAction)) {
return accessMap.get(action); return accessMap.get(permissableAction);
} }
return null; return Access.UNDEFINED;
} }
/** /**
* Get the Access of a player. Will use player's Role if they are a faction member. Otherwise, uses their Relation. * Get the Access of a player. Will use player's Role if they are a faction member. Otherwise, uses their Relation.
* *
* @param player * @param player
* @param action * @param permissableAction
* @return * @return
*/ */
public Access getAccess(FPlayer player, Action action) { public Access getAccess(FPlayer player, PermissableAction permissableAction) {
if (player == null || action == null) { if (player == null || permissableAction == null) {
return null; return Access.UNDEFINED;
} }
Permissable perm; Permissable perm;
@ -358,21 +358,21 @@ public abstract class MemoryFaction implements Faction, EconomyParticipator {
perm = player.getFaction().getRelationTo(this); perm = player.getFaction().getRelationTo(this);
} }
Map<Action, Access> accessMap = permissions.get(perm); Map<PermissableAction, Access> accessMap = permissions.get(perm);
if (accessMap != null && accessMap.containsKey(action)) { if (accessMap != null && accessMap.containsKey(permissableAction)) {
return accessMap.get(action); return accessMap.get(permissableAction);
} }
return null; return Access.UNDEFINED;
} }
public void setPermission(Permissable permissable, Action action, Access access) { public void setPermission(Permissable permissable, PermissableAction permissableAction, Access access) {
Map<Action, Access> accessMap = permissions.get(permissable); Map<PermissableAction, Access> accessMap = permissions.get(permissable);
if (accessMap == null) { if (accessMap == null) {
accessMap = new HashMap<>(); accessMap = new HashMap<>();
} }
accessMap.put(action, access); accessMap.put(permissableAction, access);
} }
public void resetPerms() { public void resetPerms() {
@ -381,9 +381,9 @@ public abstract class MemoryFaction implements Faction, EconomyParticipator {
permissions.clear(); permissions.clear();
// First populate a map with undefined as the permission for each action. // First populate a map with undefined as the permission for each action.
Map<Action, Access> freshMap = new HashMap<>(); Map<PermissableAction, Access> freshMap = new HashMap<>();
for (Action action : Action.values()) { for (PermissableAction permissableAction : PermissableAction.values()) {
freshMap.put(action, Access.UNDEFINED); freshMap.put(permissableAction, Access.UNDEFINED);
} }
// Put the map in there for each relation. // Put the map in there for each relation.