Redo of the whole PermissionAPI (#7780)

Co-authored-by: LexManos <[email protected]>
This commit is contained in:
JTK222andLexManos authored and GitHub committed 2021-12-20 18:34:57 +00:00
1 parent d33e9c8251
commit 2f5f696b68
27 files changed
+938 -750

No files matched your search

@@ -28,6 +28,8 @@ import org.apache.logging.log4j.LogManager;
import net.minecraftforge.common.ForgeConfigSpec.BooleanValue;
import net.minecraftforge.common.ForgeConfigSpec.DoubleValue;
import net.minecraftforge.common.ForgeConfigSpec.ConfigValue;
public class ForgeConfig {
public static class Server {
@@ -42,6 +44,8 @@ public class ForgeConfig {
public final BooleanValue fixAdvancementLoading;
public final ConfigValue<String> permissionHandler;
Server(ForgeConfigSpec.Builder builder) {
builder.comment("Server configuration settings")
.push("server");
@@ -80,6 +84,11 @@ public class ForgeConfig {
.translation("forge.configgui.fixAdvancementLoading")
.define("fixAdvancementLoading", true);
permissionHandler = builder
.comment("")
.translation("forge.configgui.permissionHandler")
.define("permissionHandler", "forge:default_handler");
builder.pop();
}
}
@@ -44,6 +44,7 @@ import net.minecraftforge.network.NetworkHooks;
import net.minecraftforge.network.NetworkRegistry;
import net.minecraftforge.forgespi.locating.IModFile;
import net.minecraftforge.resource.PathResourcePack;
import net.minecraftforge.server.permission.PermissionAPI;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
@@ -97,6 +98,7 @@ public class ServerLifecycleHooks
public static boolean handleServerStarting(final MinecraftServer server)
{
DistExecutor.runWhenOn(Dist.DEDICATED_SERVER, ()->()->LanguageHook.loadLanguagesOnServer(server));
PermissionAPI.initializePermissionAPI();
return !MinecraftForge.EVENT_BUS.post(new ServerStartingEvent(server));
}
@@ -109,6 +111,7 @@ public class ServerLifecycleHooks
public static void handleServerStopping(final MinecraftServer server)
{
allowLogins.set(false);
PermissionAPI.resetPermissionAPI();
MinecraftForge.EVENT_BUS.post(new ServerStoppingEvent(server));
}
@@ -1,94 +0,0 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2021.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.server.permission;
import com.mojang.authlib.GameProfile;
import net.minecraft.server.MinecraftServer;
import net.minecraftforge.server.ServerLifecycleHooks;
import net.minecraftforge.server.permission.context.IContext;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
/**
* Default implementation of PermissionAPI.
* {@link #hasPermission(GameProfile, String, IContext)} is based on DefaultPermissionLevel
*
* @see IPermissionHandler
*/
public enum DefaultPermissionHandler implements IPermissionHandler
{
INSTANCE;
private static final HashMap<String, DefaultPermissionLevel> PERMISSION_LEVEL_MAP = new HashMap<String, DefaultPermissionLevel>();
private static final HashMap<String, String> DESCRIPTION_MAP = new HashMap<String, String>();
@Override
public void registerNode(String node, DefaultPermissionLevel level, String desc)
{
PERMISSION_LEVEL_MAP.put(node, level);
if(!desc.isEmpty())
{
DESCRIPTION_MAP.put(node, desc);
}
}
@Override
public Collection<String> getRegisteredNodes()
{
return Collections.unmodifiableSet(PERMISSION_LEVEL_MAP.keySet());
}
@Override
public boolean hasPermission(GameProfile profile, String node, @Nullable IContext context)
{
DefaultPermissionLevel level = getDefaultPermissionLevel(node);
if(level == DefaultPermissionLevel.NONE)
{
return false;
}
else if(level == DefaultPermissionLevel.ALL)
{
return true;
}
MinecraftServer server = ServerLifecycleHooks.getCurrentServer();
return server != null && server.getPlayerList().isOp(profile);
}
@Override
public String getNodeDescription(String node)
{
String desc = DESCRIPTION_MAP.get(node);
return desc == null ? "" : desc;
}
/**
* @return The default permission level of a node. If the permission isn't registred, it will return NONE
*/
public DefaultPermissionLevel getDefaultPermissionLevel(String node)
{
DefaultPermissionLevel level = PERMISSION_LEVEL_MAP.get(node);
return level == null ? DefaultPermissionLevel.NONE : level;
}
}
@@ -1,51 +0,0 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2021.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.server.permission;
import com.mojang.authlib.GameProfile;
import net.minecraftforge.server.permission.context.IContext;
import javax.annotation.Nullable;
import java.util.Collection;
public interface IPermissionHandler
{
/**
* Use {@link PermissionAPI#registerNode(String, DefaultPermissionLevel, String)}
*/
void registerNode(String node, DefaultPermissionLevel level, String desc);
/**
* @return Immutable collection of all registered nodes
*/
Collection<String> getRegisteredNodes();
/**
* Use {@link PermissionAPI#hasPermission(GameProfile, String, IContext)}
*/
boolean hasPermission(GameProfile profile, String node, @Nullable IContext context);
/**
* @param node Permission node
* @return Description of the node. "" in case this node doesn't have a decription
* @see #registerNode(String, DefaultPermissionLevel, String)
*/
String getNodeDescription(String node);
}
@@ -19,80 +19,135 @@
package net.minecraftforge.server.permission;
import com.google.common.base.Preconditions;
import com.mojang.authlib.GameProfile;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.server.permission.context.IContext;
import net.minecraftforge.server.permission.context.PlayerContext;
import javax.annotation.Nullable;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import net.minecraft.ResourceLocationException;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.StringRepresentable;
import net.minecraftforge.common.ForgeConfig;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.server.permission.events.PermissionGatherEvent;
import net.minecraftforge.server.permission.exceptions.UnregisteredPermissionException;
import net.minecraftforge.server.permission.handler.DefaultPermissionHandler;
import net.minecraftforge.server.permission.handler.IPermissionHandler;
import net.minecraftforge.server.permission.handler.IPermissionHandlerFactory;
import net.minecraftforge.server.permission.nodes.PermissionDynamicContext;
import net.minecraftforge.server.permission.nodes.PermissionNode;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class PermissionAPI
import javax.annotation.Nullable;
import java.util.*;
public final class PermissionAPI
{
private static final Logger LOGGER = LogManager.getLogger();
private static IPermissionHandler permissionHandler = DefaultPermissionHandler.INSTANCE;
private static IPermissionHandler activeHandler = null;
public static Collection<PermissionNode<?>> getRegisteredNodes()
{
return activeHandler == null ? Collections.emptySet() : activeHandler.getRegisteredNodes();
}
private PermissionAPI()
{
}
/**
* <b>Only use this in PreInit state!</b>
* @return the Identifier of the currently active permission handler
*/
public static void setPermissionHandler(IPermissionHandler handler)
@Nullable
public static ResourceLocation getActivePermissionHandler()
{
Preconditions.checkNotNull(handler, "Permission handler can't be null!");
// TODO Loader states Preconditions.checkState(Loader.instance().getLoaderState().ordinal() <= LoaderState.PREINITIALIZATION.ordinal(), "Can't register after IPermissionHandler PreInit!");
LOGGER.warn("Replacing {} with {}", permissionHandler.getClass().getName(), handler.getClass().getName());
permissionHandler = handler;
}
public static IPermissionHandler getPermissionHandler()
{
return permissionHandler;
return activeHandler == null ? null : activeHandler.getIdentifier();
}
/**
* <b>Only use this after PreInit state!</b>
* <p>Queries a player's permission for a given node and contexts</p>
* <p><strong>Warning:</strong> PermissionNodes <strong>must</strong> be registered using the
* {@link PermissionGatherEvent.Nodes} event before querying.</p>
*
* @param node Permission node, best if it's lowercase and contains '.' (e.g. <code>"modid.subgroup.permission_id"</code>)
* @param level Default permission level for this node. If not isn't registered, it's level is going to be 'NONE'
* @param desc Optional description of the node
* @param player player for which you want to check permissions
* @param node the PermissionNode for which you want to query
* @param context optional array of PermissionDynamicContext, single entries will be ignored if they weren't
* registered to the node
* @param <T> type of the queried PermissionNode
* @return a value of type {@code <T>}, that the combination of Player and PermissionNode map to, defaults to the
* PermissionNodes default handler.
* @throws UnregisteredPermissionException when the PermissionNode wasn't registered properly
*/
public static String registerNode(String node, DefaultPermissionLevel level, String desc)
public static <T> T getPermission(ServerPlayer player, PermissionNode<T> node, PermissionDynamicContext<? extends StringRepresentable>... context)
{
Preconditions.checkNotNull(node, "Permission node can't be null!");
Preconditions.checkNotNull(level, "Permission level can't be null!");
Preconditions.checkNotNull(desc, "Permission description can't be null!");
Preconditions.checkArgument(!node.isEmpty(), "Permission node can't be empty!");
// TODO Loader states Preconditions.checkState(Loader.instance().getLoaderState().ordinal() > LoaderState.PREINITIALIZATION.ordinal(), "Can't register permission nodes before Init!");
permissionHandler.registerNode(node, level, desc);
return node;
if (!activeHandler.getRegisteredNodes().contains(node)) throw new UnregisteredPermissionException(node);
return activeHandler.getPermission(player, node, context);
}
/**
* @param profile GameProfile of the player who is requesting permission. The player doesn't have to be online
* @param node Permission node. See {@link #registerNode(String, DefaultPermissionLevel, String)}
* @param context Context for this permission. Highly recommended to not be null. See {@link IContext}
* @return true, if player has permission, false if he does not.
* @see DefaultPermissionHandler
*/
public static boolean hasPermission(GameProfile profile, String node, @Nullable IContext context)
{
Preconditions.checkNotNull(profile, "GameProfile can't be null!");
Preconditions.checkNotNull(node, "Permission node can't be null!");
Preconditions.checkArgument(!node.isEmpty(), "Permission node can't be empty!");
return permissionHandler.hasPermission(profile, node, context);
}
/**
* Shortcut method using EntityPlayer and creating PlayerContext
* See {@link PermissionAPI#getPermission(ServerPlayer, PermissionNode, PermissionDynamicContext[])}
*
* @see PermissionAPI#hasPermission(GameProfile, String, IContext)
* @param player offline player for which you want to check permissions
* @param node the PermissionNode for which you want to query
* @param context optional array of PermissionDynamicContext, single entries will be ignored if they weren't
* registered to the node
* @param <T> type of the queried PermissionNode
* @return a value of type {@code <T>}, that the combination of Player and PermissionNode map to, defaults to the
* PermissionNodes default handler.
* @throws UnregisteredPermissionException when the PermissionNode wasn't registered properly
*/
public static boolean hasPermission(Player player, String node)
public static <T> T getOfflinePermission(UUID player, PermissionNode<T> node, PermissionDynamicContext<? extends StringRepresentable>... context)
{
Preconditions.checkNotNull(player, "Player can't be null!");
return hasPermission(player.getGameProfile(), node, new PlayerContext(player));
if (!activeHandler.getRegisteredNodes().contains(node)) throw new UnregisteredPermissionException(node);
return activeHandler.getOfflinePermission(player, node, context);
}
/**
* <p>Helper method for internal use only!</p>
* <p>Initializes the active permission handler based on the users config.</p>
*/
public static void initializePermissionAPI()
{
if (PermissionAPI.activeHandler != null) throw new IllegalStateException("Tried to initialize PermissionAPI multiple times!");
PermissionGatherEvent.Handler handlerEvent = new PermissionGatherEvent.Handler();
MinecraftForge.EVENT_BUS.post(handlerEvent);
Map<ResourceLocation, IPermissionHandlerFactory> availableHandlers = handlerEvent.getAvailablePermissionHandlerFactories();
try
{
ResourceLocation selectedPermissionHandler = new ResourceLocation(ForgeConfig.SERVER.permissionHandler.get());
if (!availableHandlers.containsKey(selectedPermissionHandler))
{
LOGGER.error("Unable to find configured permission handler {}, will use {}", selectedPermissionHandler, DefaultPermissionHandler.IDENTIFIER);
selectedPermissionHandler = DefaultPermissionHandler.IDENTIFIER;
}
IPermissionHandlerFactory factory = availableHandlers.get(selectedPermissionHandler);
PermissionGatherEvent.Nodes nodesEvent = new PermissionGatherEvent.Nodes();
MinecraftForge.EVENT_BUS.post(nodesEvent);
PermissionAPI.activeHandler = factory.create(nodesEvent.getNodes());
if(!selectedPermissionHandler.equals(activeHandler.getIdentifier()))
LOGGER.warn("Identifier for permission handler {} does not match registered one {}", activeHandler.getIdentifier(), selectedPermissionHandler);
LOGGER.info("Successfully initialized permission handler {}", PermissionAPI.activeHandler.getIdentifier());
}
catch (ResourceLocationException e)
{
LOGGER.error("Error parsing config value 'permissionHandler'", e);
}
}
/**
* <p>Helper method for internal use only!</p>
* <p>Resets the active permission handler.</p>
*/
public static void resetPermissionAPI()
{
PermissionAPI.activeHandler = null;
LOGGER.debug("Reset PermissionAPI");
}
}
@@ -1,51 +0,0 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2021.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.server.permission.context;
import com.google.common.base.Preconditions;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.phys.AABB;
import javax.annotation.Nullable;
public class AreaContext extends PlayerContext
{
private final AABB area;
public AreaContext(Player ep, AABB aabb)
{
super(ep);
area = Preconditions.checkNotNull(aabb, "AxisAlignedBB can't be null in AreaContext!");
}
@SuppressWarnings("unchecked")
@Override
@Nullable
public <T> T get(ContextKey<T> key)
{
return key.equals(ContextKeys.AREA) ? (T) area : super.get(key);
}
@Override
protected boolean covers(ContextKey<?> key)
{
return key.equals(ContextKeys.AREA);
}
}
@@ -1,80 +0,0 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2021.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.server.permission.context;
import com.google.common.base.Preconditions;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.entity.player.Player;
import net.minecraft.core.Direction;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.ChunkPos;
import javax.annotation.Nullable;
public class BlockPosContext extends PlayerContext
{
private final BlockPos blockPos;
private BlockState blockState;
private Direction facing;
public BlockPosContext(Player ep, BlockPos pos, @Nullable BlockState state, @Nullable Direction f)
{
super(ep);
blockPos = Preconditions.checkNotNull(pos, "BlockPos can't be null in BlockPosContext!");
blockState = state;
facing = f;
}
public BlockPosContext(Player ep, ChunkPos pos)
{
this(ep, new BlockPos(pos.getMinBlockX() + 8, 0, pos.getMinBlockZ() + 8), null, null);
}
@Override
@Nullable
public <T> T get(ContextKey<T> key)
{
if(key.equals(ContextKeys.POS))
{
return (T) blockPos;
}
else if(key.equals(ContextKeys.BLOCK_STATE))
{
if(blockState == null)
{
blockState = getWorld().getBlockState(blockPos);
}
return (T) blockState;
}
else if(key.equals(ContextKeys.FACING))
{
return (T) facing;
}
return super.get(key);
}
@Override
protected boolean covers(ContextKey<?> key)
{
return key.equals(ContextKeys.POS) || key.equals(ContextKeys.BLOCK_STATE) || (facing != null && key.equals(ContextKeys.FACING));
}
}
@@ -1,87 +0,0 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2021.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.server.permission.context;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;
public class Context implements IContext
{
private Map<ContextKey<?>, Object> map;
@Override
@Nullable
public Level getWorld()
{
return null;
}
@Override
@Nullable
public Player getPlayer()
{
return null;
}
@Override
@Nullable
public <T> T get(ContextKey<T> key)
{
return map == null || map.isEmpty() ? null : (T) map.get(key);
}
@Override
public boolean has(ContextKey<?> key)
{
return covers(key) || (map != null && !map.isEmpty() && map.containsKey(key));
}
/**
* Sets Context object
*
* @param key Context key
* @param obj Context object. Can be null
* @return itself, for easy context chaining
*/
public <T> Context set(ContextKey<T> key, @Nullable T obj)
{
if(covers(key))
{
return this;
}
if(map == null)
{
map = new HashMap<ContextKey<?>, Object>();
}
map.put(key, obj);
return this;
}
protected boolean covers(ContextKey<?> key)
{
return false;
}
}
@@ -1,67 +0,0 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2021.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.server.permission.context;
import com.google.common.base.Preconditions;
public final class ContextKey<T>
{
private final String ID;
private final Class<T> typeClass;
public static <E> ContextKey<E> create(String id, Class<E> c)
{
Preconditions.checkNotNull(id, "ContextKey's ID can't be null!");
Preconditions.checkNotNull(c, "ContextKey's Type can't be null!");
if(id.isEmpty())
{
throw new IllegalArgumentException("ContextKey's ID can't be blank!");
}
return new ContextKey<E>(id, c);
}
private ContextKey(String id, Class<T> c)
{
ID = id;
typeClass = c;
}
public String toString()
{
return ID;
}
public int hashCode()
{
return ID.hashCode();
}
public boolean equals(Object o)
{
return o == this || (o != null && o.toString().equals(ID));
}
public Class<T> getTypeClass()
{
return typeClass;
}
}
@@ -1,46 +0,0 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2021.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.server.permission.context;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.entity.Entity;
import net.minecraft.core.Direction;
import net.minecraft.world.phys.AABB;
import net.minecraft.core.BlockPos;
/**
* Some default context keys, for easier compatibility
*/
public class ContextKeys
{
/**
* BlockPos for interacting, breaking and other permissions
*/
public static final ContextKey<BlockPos> POS = ContextKey.create("pos", BlockPos.class);
/**
* The entity can be anything that gets interacted with - a sheep when you try to dye it, skeleton that you attack, etc.
*/
public static final ContextKey<Entity> TARGET = ContextKey.create("target", Entity.class);
public static final ContextKey<Direction> FACING = ContextKey.create("facing", Direction.class);
public static final ContextKey<AABB> AREA = ContextKey.create("area", AABB.class);
public static final ContextKey<BlockState> BLOCK_STATE = ContextKey.create("blockstate", BlockState.class);
}
@@ -1,56 +0,0 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2021.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.server.permission.context;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import javax.annotation.Nullable;
/**
* Use {@link BlockPosContext} or {@link PlayerContext} when possible
*/
public interface IContext
{
/**
* World from where permission is requested. Can be null
*/
@Nullable
Level getWorld();
/**
* @return Player requesting permission. Can be null
*/
@Nullable
Player getPlayer();
/**
* @param key Context key
* @return Context object
*/
@Nullable
<T> T get(ContextKey<T> key);
/**
* @param key Context key
* @return true if context contains this key
*/
boolean has(ContextKey<?> key);
}
@@ -1,46 +0,0 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2021.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.server.permission.context;
import com.google.common.base.Preconditions;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
public class PlayerContext extends Context
{
private final Player player;
public PlayerContext(Player ep)
{
player = Preconditions.checkNotNull(ep, "Player can't be null in PlayerContext!");
}
@Override
public Level getWorld()
{
return player.getCommandSenderWorld();
}
@Override
public Player getPlayer()
{
return player;
}
}
@@ -1,49 +0,0 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2021.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.server.permission.context;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import javax.annotation.Nullable;
public class TargetContext extends PlayerContext
{
private final Entity target;
public TargetContext(Player ep, @Nullable Entity entity)
{
super(ep);
target = entity;
}
@Override
@Nullable
public <T> T get(ContextKey<T> key)
{
return key.equals(ContextKeys.TARGET) ? (T) target : super.get(key);
}
@Override
protected boolean covers(ContextKey<?> key)
{
return target != null && key.equals(ContextKeys.TARGET);
}
}
@@ -1,49 +0,0 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2021.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.server.permission.context;
import com.google.common.base.Preconditions;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import javax.annotation.Nullable;
public class WorldContext extends Context
{
private final Level world;
public WorldContext(Level w)
{
world = Preconditions.checkNotNull(w, "World can't be null in WorldContext!");
}
@Override
public Level getWorld()
{
return world;
}
@Override
@Nullable
public Player getPlayer()
{
return null;
}
}
@@ -0,0 +1,109 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2021.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.server.permission.events;
import com.google.common.base.Preconditions;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.server.permission.handler.DefaultPermissionHandler;
import net.minecraftforge.server.permission.handler.IPermissionHandler;
import net.minecraftforge.server.permission.handler.IPermissionHandlerFactory;
import net.minecraftforge.server.permission.nodes.PermissionNode;
import java.util.*;
import java.util.function.Function;
/**
* Fired to gather information for the permissions API, such as the {@link IPermissionHandler} and {@link PermissionNode}s.
*
* <p>
* {@link Handler} allows to set a new PermissionHandler<br/>
* {@link Nodes} allows you to register new PermissionNodes</p>
*
* <p><strong>Note:</strong> All PermissionNodes that you want to use, <strong>must</strong> be registered!</p>
*/
public class PermissionGatherEvent extends Event
{
/**
* Used to register a new PermissionHandler, a server config value exists to choose which one to use.
* <p>Note: Create a new instance when registering a PermissionHandler.
* If you cache it, make sure that your PermissionHandler is actually used after this event.</p>
*/
public static class Handler extends PermissionGatherEvent
{
private Map<ResourceLocation, IPermissionHandlerFactory> availableHandlers = new HashMap<>();
public Handler()
{
availableHandlers.put(DefaultPermissionHandler.IDENTIFIER, DefaultPermissionHandler::new);
}
public Map<ResourceLocation, IPermissionHandlerFactory> getAvailablePermissionHandlerFactories()
{
return Collections.unmodifiableMap(availableHandlers);
}
public void addPermissionHandler(ResourceLocation identifier, IPermissionHandlerFactory handlerFactory)
{
Preconditions.checkNotNull(identifier, "Permission handler identifier cannot be null!");
Preconditions.checkNotNull(handlerFactory, "Permission handler cannot be null!");
if(this.availableHandlers.containsKey(identifier))
throw new IllegalArgumentException("Attempted to overwrite permission handler " + identifier + ", this is not allowed.");
this.availableHandlers.put(identifier, handlerFactory);
}
}
/**
* Used to register your PermissionNodes, <strong>every node that you want to use, must be registered!</strong>
*/
public static class Nodes extends PermissionGatherEvent
{
private final Set<PermissionNode<?>> nodes = new HashSet<>();
public Nodes()
{
}
public Collection<PermissionNode<?>> getNodes()
{
return Collections.unmodifiableCollection(this.nodes);
}
public void addNodes(PermissionNode<?>... nodes)
{
for (PermissionNode<?> node : nodes)
{
if (!this.nodes.add(node))
throw new IllegalArgumentException("Tried to register duplicate PermissionNode '" + node.getNodeName() + "'");
}
}
public void addNodes(Iterable<PermissionNode<?>> nodes)
{
for (PermissionNode<?> node : nodes)
{
if (!this.nodes.add(node))
throw new IllegalArgumentException("Tried to register duplicate PermissionNode '" + node.getNodeName() + "'");
}
}
}
}
@@ -17,20 +17,22 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.server.permission;
package net.minecraftforge.server.permission.exceptions;
/**
* <table><caption>Default Permission Levels</caption>
* <thead><tr><th>Level</th><th>Player</th><th>OP</th></tr>
* </thead><tbody>
* <tr><td>ALL</td><td>true</td><td>true</td></tr>
* <tr><td>OP</td><td>false</td><td>true</td></tr>
* <tr><td>NONE</td><td>false</td><td>false</td></tr>
* </tbody></table>
*/
public enum DefaultPermissionLevel
import net.minecraftforge.server.permission.nodes.PermissionNode;
public class UnregisteredPermissionException extends RuntimeException
{
ALL,
OP,
NONE
private PermissionNode node;
public UnregisteredPermissionException(PermissionNode node)
{
super(String.format("Tried to query PermissionNode '%s' although it has not been Registered", node.getNodeName()));
this.node = node;
}
public PermissionNode getNode()
{
return node;
}
}
@@ -0,0 +1,64 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2021.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.server.permission.handler;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.server.permission.nodes.PermissionDynamicContext;
import net.minecraftforge.server.permission.nodes.PermissionNode;
import java.util.*;
public final class DefaultPermissionHandler implements IPermissionHandler
{
public static final ResourceLocation IDENTIFIER = new ResourceLocation("forge", "default_handler");;
private final Set<PermissionNode<?>> registeredNodes = new HashSet<>();
private Set<PermissionNode<?>> immutableRegisteredNodes = Collections.unmodifiableSet(this.registeredNodes);
public DefaultPermissionHandler(Collection<PermissionNode<?>> permissions)
{
this.registeredNodes.addAll(permissions);
}
@Override
public ResourceLocation getIdentifier()
{
return IDENTIFIER;
}
@Override
public Set<PermissionNode<?>> getRegisteredNodes()
{
return immutableRegisteredNodes;
}
@Override
public <T> T getPermission(ServerPlayer player, PermissionNode<T> node, PermissionDynamicContext<?>... context)
{
return node.getDefaultResolver().resolve(player, player.getUUID(), context);
}
@Override
public <T> T getOfflinePermission(UUID player, PermissionNode<T> node, PermissionDynamicContext<?>... context)
{
return node.getDefaultResolver().resolve(null, player, context);
}
}
@@ -0,0 +1,81 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2021.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.server.permission.handler;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.StringRepresentable;
import net.minecraftforge.server.permission.PermissionAPI;
import net.minecraftforge.server.permission.events.PermissionGatherEvent;
import net.minecraftforge.server.permission.nodes.PermissionDynamicContext;
import net.minecraftforge.server.permission.nodes.PermissionNode;
import java.util.Set;
import java.util.UUID;
/**
* This is the Heart of the PermissionAPI, it manages {@link PermissionNode}s
* as well as it handles all permission queries.
*
* <p>Note: You do not need to implement a PermissionHandler to query for permissions.</p>
*
* @implNote The {@link DefaultPermissionHandler} does forward all permission queries to the PermissionNodes default resolver.
* @apiNote You can implement your own PermissionHandler using the {@link PermissionGatherEvent.Handler} event.
*/
public interface IPermissionHandler
{
/**
* {@return an identifier for the PermissionHandler}
*/
ResourceLocation getIdentifier();
/**
* {@return an unmodifiable view of the collection of registered permission nodes}
*/
Set<PermissionNode<?>> getRegisteredNodes();
/**
* <strong>Mods must use {@link PermissionAPI#getPermission(ServerPlayer, PermissionNode, PermissionDynamicContext[])}</strong>
*
* <p>Queries a player's permission for a given node and contexts</p>
* <p><strong>Warning:</strong> PermissionNodes <strong>must</strong> be registered using the
* {@link PermissionGatherEvent.Nodes} event before querying.</p>
*
* @param player player for which you want to check permissions
* @param node the PermissionNode for which you want to query
* @param context optional array of PermissionDynamicContext, single entries will be ignored if they weren't
* registered to the node
* @param <T> type of the queried PermissionNode
* @return a value of type {@code <T>}, that the combination of Player and PermissionNode map to.
*/
<T> T getPermission(ServerPlayer player, PermissionNode<T> node, PermissionDynamicContext<?>... context);
/**
* See {@link IPermissionHandler#getPermission(ServerPlayer, PermissionNode, PermissionDynamicContext[])}
*
* @param player offline player for which you want to check permissions
* @param node the PermissionNode for which you want to query
* @param context optional array of PermissionDynamicContext, single entries will be ignored if they weren't
* registered to the node
* @param <T> type of the queried PermissionNode
* @return a value of type {@code <T>}, that the combination of Player and PermissionNode map to.
*/
<T> T getOfflinePermission(UUID player, PermissionNode<T> node, PermissionDynamicContext<?>... context);
}
@@ -17,10 +17,14 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
package net.minecraftforge.server.permission.context;
package net.minecraftforge.server.permission.handler;
import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraftforge.server.permission.nodes.PermissionNode;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.Collection;
@FunctionalInterface
public interface IPermissionHandlerFactory
{
IPermissionHandler create(Collection<PermissionNode<?>> permissions);
}
@@ -0,0 +1,71 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2021.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.server.permission.nodes;
import java.util.Objects;
/**
* Pair of a PermissionDynamicContextKey and a value of the corresponding type.
* Use {@link PermissionDynamicContextKey#createContext(Object)} )} for constructing.
*
* <p>Note: While the DynamicContext behaves similar to BlockStates, it does not oblige to the same limitations.
* There is no string representation that you have to follow, nor is there a limit on how many unique value a DynamicContext may have</p>
*
* @implNote this class could be a record but is not in favor of a package private constructor
*/
public final class PermissionDynamicContext<T>
{
private PermissionDynamicContextKey<T> dynamic;
private T value;
PermissionDynamicContext(PermissionDynamicContextKey<T> dynamic, T value)
{
this.dynamic = dynamic;
this.value = value;
}
public PermissionDynamicContextKey<T> getDynamic()
{
return dynamic;
}
public T getValue()
{
return value;
}
public String getSerializedValue(){
return this.dynamic.serializer().apply(this.value);
}
@Override
public boolean equals(Object o)
{
if (this == o) return true;
if (!(o instanceof PermissionDynamicContext otherContext)) return false;
return dynamic.equals(otherContext.dynamic) && value.equals(otherContext.value);
}
@Override
public int hashCode()
{
return Objects.hash(dynamic, value);
}
}
@@ -0,0 +1,39 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2021.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.server.permission.nodes;
import java.util.function.Function;
/**
* Represents a key that can be used to build a {@link PermissionDynamicContext}.
*
* <p>Keys, along with their associated values, can be used to provide additional context for a permission handler
* in determining whether to grant permission for an actor and a specific node.</p>
*
* <p>As an example usage, a dimension context key could be used inside a building permission
* check to ensure that the actor can build given those constraints.</p>
*/
public record PermissionDynamicContextKey<T>(Class<T> typeToken, String name, Function<T, String> serializer)
{
public PermissionDynamicContext<T> createContext(T value)
{
return new PermissionDynamicContext<>(this, value);
}
}
@@ -0,0 +1,192 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2021.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.server.permission.nodes;
import com.google.common.base.Preconditions;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.server.permission.events.PermissionGatherEvent;
import net.minecraftforge.server.permission.handler.IPermissionHandler;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Objects;
import java.util.UUID;
/**
* Represents the basic unit at the heart of the permission system.
*
* <p>A permission indicates the ability for an actor to perform an action,
* in its most general sense. In the permission system, all permissions are encoded as instances of this class,
* optionally integrated by a {@link PermissionDynamicContext}.</p>
*
* <p>A node is uniquely identified by its `nodeName`,
* which is a dot-separated string providing meaning to the node itself.
* The suggested structure of the name is `modid.name`, where `modid` is the ID of the owner of the node.
* This API <strong>does not require</strong> any implicit hierarchy,
* so `modid.name` is not considered a parent of `modid.name.sub`. Such decisions are left to the {@link IPermissionHandler}.</p>
*
* <p>Each node also has an associated {@link PermissionType}, detailing its exact type,
* along with a {@link PermissionResolver} that is used to obtain the default value of the permission.
* More information can be found on their documentation.</p>
*
* <p>A node can also be bound to additional {@link PermissionDynamicContextKey}s,
* which are used when querying the availability of the permission.
* There are no restrictions on their amount. It is also not mandatory to provide a value for all dynamics in a permission query.
* See the above link for more information.</p>
*
* <p>Each node should be registered via the {@link PermissionGatherEvent.Nodes} and stored statically in a field.
* That instance should then be reused every-time a permission check needs to be performed via {@link net.minecraftforge.server.permission.PermissionAPI#getPermissions(ServerPlayer, PermissionNode, PermissionDynamicContext[])}.</p>
*/
public final class PermissionNode<T>
{
private final String nodeName;
private final PermissionType<T> type;
private final PermissionResolver<T> defaultResolver;
private final PermissionDynamicContextKey<?>[] dynamics;
@Nullable
private Component readableName;
@Nullable
private Component description;
/**
* Calls {@link PermissionNode#PermissionNode(String, PermissionType, PermissionResolver, PermissionDynamicContextKey[])}
* with "namespace.path" as the first parameter
*/
public PermissionNode(ResourceLocation nodeName, PermissionType<T> type, PermissionResolver<T> defaultResolver, PermissionDynamicContextKey... dynamics)
{
this(nodeName.getNamespace(), nodeName.getPath(), type, defaultResolver, dynamics);
}
/**
* Calls {@link PermissionNode#PermissionNode(String, PermissionType, PermissionResolver, PermissionDynamicContextKey[])}
* with "modid.nodename" as the first parameter
*/
public PermissionNode(String modID, String nodeName, PermissionType<T> type, PermissionResolver<T> defaultResolver, PermissionDynamicContextKey... dynamics)
{
this(modID + "." + nodeName, type, defaultResolver, dynamics);
}
/**
* @param nodeName The identifier of a node, recommended identifier structure: "modid.path.for.node"
* @param type type object for the PermissionNode, only use types in {@link PermissionTypes}
* @param defaultResolver Default resolver for the permission, can but doesn't have to be used to by PermissionHandlers
* @param dynamics PermissionDynamicContextKey is a dynamic component for permission nodes, similar to BlockState Properties.
* They <strong>must</strong> be passed into the constructor if you want to use them.
*/
private PermissionNode(String nodeName, PermissionType<T> type, PermissionResolver<T> defaultResolver, PermissionDynamicContextKey... dynamics)
{
this.nodeName = nodeName;
this.type = type;
this.dynamics = dynamics;
this.defaultResolver = defaultResolver;
}
/**
* Allows you to set a human-readable name & description for your Permission.
*
* <p>Note: Even though not used by Default, PermissionHandlers may display this information in game,
* or provide it to the user by other means.</br>
* You may use {@link net.minecraft.network.chat.TranslatableComponent}, but you'll need 2 language files.
* One inside the data directory for the server and one inside assets for the client.</p>
*
* @param readableName an easier to read name for the PermissionNode,
* when using TranslatableComponent, key should be of format "permission.name.<nodename>"
* @param description description for the PermissionNode
* when using TranslatableComponent, key should be of format "permission.desc.<nodename>"
* @return itself with the new information set.
*/
public PermissionNode setInformation(@Nonnull Component readableName, @Nonnull Component description)
{
Preconditions.checkNotNull(readableName, "Readable name for PermissionNodes must not be null %s", this.nodeName);
Preconditions.checkNotNull(description, "Description for PermissionNodes must not be null %s", this.nodeName);
this.readableName = readableName;
this.description = description;
return this;
}
public String getNodeName()
{
return nodeName;
}
public PermissionType<T> getType()
{
return type;
}
public PermissionDynamicContextKey<?>[] getDynamics()
{
return dynamics;
}
public PermissionResolver<T> getDefaultResolver()
{
return defaultResolver;
}
@Nullable
public Component getReadableName()
{
return readableName;
}
@Nullable
public Component getDescription()
{
return description;
}
/**
* Utility Interface used for resolving the default value of PermissionNodes
*
* @param <T> generic value of the PermissionType of a PermissionNode
*/
@FunctionalInterface
public interface PermissionResolver<T>
{
/**
* @param player an online player
* @param playerUUID if the player is null, this UUID belongs to an offline player,
* otherwise it must match the UUID of the passed in player.
* @param context may contain DynamicContext if it was provided
* @return according Permission Value
*/
T resolve(@Nullable ServerPlayer player, UUID playerUUID, PermissionDynamicContext<?>... context);
}
@Override
public boolean equals(Object o)
{
if (this == o) return true;
if (!(o instanceof PermissionNode otherNode)) return false;
return nodeName.equals(otherNode.nodeName) && type.equals(otherNode.type);
}
@Override
public int hashCode()
{
return Objects.hash(nodeName, type);
}
}
@@ -0,0 +1,73 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2021.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.server.permission.nodes;
import java.util.Objects;
/**
* <p>Type of a Permission, use the existing Types in {@link PermissionTypes}</p>
*
* @implNote this class could be a record but is not in favor of a package private constructor
*/
public final class PermissionType<T>
{
private final Class<T> typeToken;
private final String typeName;
PermissionType(Class<T> typeToken, String typeName)
{
this.typeToken = typeToken;
this.typeName = typeName;
}
public Class<T> typeToken()
{
return typeToken;
}
public String typeName()
{
return typeName;
}
@Override
public boolean equals(Object obj)
{
if (obj == this) return true;
if(!(obj instanceof PermissionType otherType)) return false;
return Objects.equals(this.typeToken, otherType.typeToken) &&
Objects.equals(this.typeName, otherType.typeName);
}
@Override
public int hashCode()
{
return Objects.hash(typeToken, typeName);
}
@Override
public String toString()
{
return "PermissionType[" +
"typeToken=" + typeToken + ", " +
"typeName=" + typeName + ']';
}
}
@@ -0,0 +1,52 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2021.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.server.permission.nodes;
import net.minecraft.network.chat.Component;
import javax.annotation.Nullable;
/**
* Default PermissionTypes, if you need additional ones, please PR it.
*/
public final class PermissionTypes
{
public static final PermissionType<Boolean> BOOLEAN = new PermissionType<>(Boolean.class, "boolean");
public static final PermissionType<Integer> INTEGER = new PermissionType<>(Integer.class, "integer");
public static final PermissionType<String> STRING = new PermissionType<>(String.class, "string");
public static final PermissionType<Component> COMPONENT = new PermissionType<>(Component.class, "component");
private PermissionTypes()
{
}
@Nullable
public static PermissionType<?> getTypeByName(String name)
{
return switch (name)
{
case "boolean" -> BOOLEAN;
case "integer" -> INTEGER;
case "string" -> STRING;
case "component" -> COMPONENT;
default -> null;
};
}
}
@@ -23,4 +23,4 @@ package net.minecraftforge.server.permission;
import net.minecraft.MethodsReturnNonnullByDefault;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.ParametersAreNonnullByDefault;