Skip to content

Instantly share code, notes, and snippets.

@LatvianModder
Last active June 26, 2016 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LatvianModder/b179ddcac33f867edcd98b74222926cc to your computer and use it in GitHub Desktop.
Save LatvianModder/b179ddcac33f867edcd98b74222926cc to your computer and use it in GitHub Desktop.
PermissionAPI
import net.minecraft.entity.Entity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.HashMap;
import java.util.Map;
/**
* Created by LatvianModder on 26.05.2016.
*/
@ParametersAreNonnullByDefault
public class Context
{
/**
* Not recommended, only used when there really is no context available
*/
public static final Context EMPTY = new Context();
private IBlockAccess blockAccess;
private Entity entity;
private BlockPos blockPos;
private Map<String, Object> custom;
public Context()
{
}
public Context(IBlockAccess w)
{
blockAccess = w;
}
public Context(Entity e)
{
blockAccess = e.worldObj;
entity = e;
}
public Context(IBlockAccess w, BlockPos pos)
{
blockAccess = w;
blockPos = pos;
}
public Context(Entity e, BlockPos pos)
{
blockAccess = e.worldObj;
entity = e;
blockPos = pos;
}
// IBlockAccess //
public IBlockAccess getBlockAccess()
{
return blockAccess;
}
public Context setBlockAccess(IBlockAccess w)
{
blockAccess = w;
return this;
}
public boolean hasBlockAccess()
{
return blockAccess != null;
}
// Entity //
public Entity getEntity()
{
return entity;
}
public Context setEntity(Entity e)
{
blockAccess = e.worldObj;
entity = e;
return this;
}
public boolean hasEntity()
{
return entity != null;
}
// BlockPos //
public BlockPos getBlockPos()
{
return blockPos;
}
public Context setBlockPos(BlockPos pos)
{
blockPos = pos;
return this;
}
public boolean hasBlockPos()
{
return blockPos != null;
}
// Custom objects //
public Object getCustomObject(String id)
{
return (custom == null || id.isEmpty()) ? null : custom.get(id);
}
public boolean hasCustomObject(String id)
{
return custom != null && !id.isEmpty() && custom.containsKey(id);
}
public Context setCustomObject(String id, Object obj)
{
if(custom == null)
{
custom = new HashMap<>();
}
custom.put(id, obj);
return this;
}
}
import com.feed_the_beast.ftbl.FTBLibMod;
import com.mojang.authlib.GameProfile;
import net.minecraft.server.MinecraftServer;
import net.minecraftforge.fml.common.FMLCommonHandler;
import javax.annotation.ParametersAreNonnullByDefault;
/**
* Created by LatvianModder on 24.05.2016.
*/
@ParametersAreNonnullByDefault
public class PermissionAPI
{
private static PermissionHandler permissionHandler;
public static void setPermissionHandler(PermissionHandler handler)
{
if(permissionHandler != null)
{
FTBLibMod.logger.warn("Replacing " + permissionHandler.getClass().getName() + " with " + handler.getClass().getName());
}
permissionHandler = handler;
}
/**
* @param profile GameProfile of the player who is requesting permission
* @param permission Permission node, best if lowercase and contains '.'
* @param defaultForPlayer Default value for players
* @param context Context for this permission. Do not use null, when there is no context available, use Context.EMPTY!
* @return true, if player has permission, false if he does not.
*/
public static boolean hasPermission(GameProfile profile, String permission, boolean defaultForPlayer, Context context)
{
if(permission.isEmpty())
{
throw new NullPointerException("Permission string can't be empty!");
}
MinecraftServer server = FMLCommonHandler.instance().getMinecraftServerInstance();
if(server == null)
{
return true;
}
if(!server.isDedicatedServer() || server.getPlayerList().getOppedPlayers().getPermissionLevel(profile) > 0)
{
return true;
}
if(permissionHandler == null)
{
return defaultForPlayer;
}
switch(permissionHandler.hasPermission(profile, permission, context))
{
case ALLOW:
{
return true;
}
case DENY:
{
return false;
}
default:
{
return defaultForPlayer;
}
}
}
}
import com.mojang.authlib.GameProfile;
import net.minecraftforge.fml.common.eventhandler.Event;
import javax.annotation.ParametersAreNonnullByDefault;
/**
* Created by LatvianModder on 26.05.2016.
*/
@ParametersAreNonnullByDefault
public interface PermissionHandler
{
Event.Result hasPermission(GameProfile profile, String permission, Context context);
}
@yuuka-miya
Copy link

yuuka-miya commented Jun 26, 2016

LatvianModder, just give me easy-to-use wrappers like these: https://github.com/ForgeEssentials/ForgeEssentials/blob/189/develop/src/main/java/net/minecraftforge/permission/PermissionManager.java#L163


IMO the whole OpPermissionLevel system should be nuked and replaced with Permission API calls. (this is what sponge does)

So there shouldn't be a need to accommodate that.


Otherwise it looks good, let me call my permissions guy to give his input - @olee

@yuuka-miya
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment