Add initial ACL implementation

This commit is contained in:
Aria 2023-02-28 18:50:58 -07:00
parent f492ca9acd
commit a55a04f620
3 changed files with 116 additions and 3 deletions

View file

@ -17,6 +17,8 @@ public class Messages {
public static final String WARP_STANDARD;
public static final String WARP_ACCESS_FORMAT;
public static final String PUBLIC;
public static final String PRIVATE;
static{
ESSENTIALS_PREFIX = "!Gray![!Dark_Green!AE!Gray!] ";
@ -33,6 +35,8 @@ public class Messages {
WARP_OWNER = "!Dark_Purple!The warp's owner is [0][1]";
COUNT = ESSENTIALS_PREFIX + "!Dark_Purple!There are [0] [1](s) available";
WARP_ACCESS_FORMAT = "This warp is [0]";
WARP_ACCESS_FORMAT = "!Dark_Purple!This warp is [0]";
PUBLIC = "!Dark_Green!Public";
PRIVATE = "!Dark_Red!Private";
}
}

View file

@ -79,7 +79,9 @@ public class WarpsCommand {
HoverEvent h2 = HoverTip.get(
ChatHelpers.macroize(Messages.WARP_HOVER_FORMAT,
ChatHelpers.macroize(Messages.WARP_OWNER, prof.name_color, prof.nickname),
ChatHelpers.macroize(Messages.WARP_ACCESS_FORMAT, )
ChatHelpers.macroize(Messages.WARP_ACCESS_FORMAT,
(warp.isPublic ? ChatHelpers.macroize(Messages.PUBLIC) : ChatHelpers.macroize(Messages.PRIVATE))
)
)
);

View file

@ -1,10 +1,17 @@
package dev.zontreck.essentials.warps;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import dev.zontreck.essentials.commands.teleport.TeleportDestination;
import dev.zontreck.libzontreck.exceptions.InvalidDeserialization;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.IntArrayTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.nbt.Tag;
import net.minecraft.server.level.ServerPlayer;
public class Warp {
public UUID owner;
@ -12,6 +19,7 @@ public class Warp {
public boolean RTP;
public boolean isPublic;
public TeleportDestination destination;
private List<UUID> ACL;
public Warp(UUID owner, String name, boolean rtp, boolean publicWarp, TeleportDestination destination)
{
@ -20,11 +28,29 @@ public class Warp {
RTP=rtp;
isPublic=publicWarp;
this.destination=destination;
this.ACL = new ArrayList<>();
}
public static Warp deserialize(CompoundTag tag) throws InvalidDeserialization
{
return new Warp(tag.getUUID("owner"), tag.getString("name"), tag.getBoolean("rtp"), tag.getBoolean("public"), new TeleportDestination(tag.getCompound("destination")));
Warp warp = new Warp(tag.getUUID("owner"), tag.getString("name"), tag.getBoolean("rtp"), tag.getBoolean("public"), new TeleportDestination(tag.getCompound("destination")));
List<UUID> acl = new ArrayList<>();
if(!warp.isPublic)
{
ListTag lst = tag.getList("acl", Tag.TAG_INT_ARRAY);
if(lst!=null){
for (Tag tag2 : lst) {
IntArrayTag tag3 = (IntArrayTag)tag2;
acl.add(NbtUtils.loadUUID(tag3));
}
warp.ACL=acl;
}
}
return warp;
}
public CompoundTag serialize()
@ -35,7 +61,88 @@ public class Warp {
tag.putBoolean("rtp", RTP);
tag.putBoolean("public", isPublic);
tag.put("destination", destination.serialize());
if(!isPublic)
{
ListTag lst = new ListTag();
for(UUID id : ACL)
{
lst.add(NbtUtils.createUUID(id));
}
tag.put("acl", lst);
}
return tag;
}
/**
* Checks if a player has access to a warp
* @param player
* @return
*/
public boolean hasAccess(ServerPlayer player)
{
return hasAccess(player.getUUID());
}
/**
* Checks if an ID has access to the warp
* @param ID
* @return
*/
public boolean hasAccess(UUID ID)
{
if(isPublic)return true;
return ACL.contains(ID);
}
/**
* Alias for Warp#giveAccess(UUID)
* @param player
*/
protected void giveAccess(ServerPlayer player)
{
giveAccess(player.getUUID());
}
/**
* If the warp is not public, it gives an ID access to the warp
* @param ID
*/
protected void giveAccess(UUID ID)
{
if(!isPublic)
{
ACL.add(ID);
}else return;
}
/**
* Takes away access to the warp
* @param player
*/
protected void removeAccess(ServerPlayer player)
{
removeAccess(player.getUUID());
}
/**
* Takes away access to the warp
* @param id
*/
protected void removeAccess(UUID id)
{
if(ACL.contains(id))
{
ACL.remove(id);
}
}
/**
* Returns a copy of the ACL List as it was when the request was made.
* @return
*/
protected List<UUID> getWarpACL()
{
return new ArrayList<UUID>(ACL);
}
}