Add proper ACL
This commit is contained in:
parent
91a4a7f7a8
commit
1542891f6d
2 changed files with 154 additions and 27 deletions
|
@ -0,0 +1,143 @@
|
|||
package dev.zontreck.essentials.warps;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.ListTag;
|
||||
import net.minecraft.nbt.NbtUtils;
|
||||
import net.minecraft.nbt.Tag;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
|
||||
public class AccessControlList {
|
||||
|
||||
/**
|
||||
* Warp ACLs do not need privilege level. It is simply a question of yes or no to if someone has access to the warp.
|
||||
*/
|
||||
public class ACLEntry
|
||||
{
|
||||
public String name;
|
||||
public UUID id;
|
||||
|
||||
public ACLEntry(String name, UUID id)
|
||||
{
|
||||
this.name=name;
|
||||
this.id=id;
|
||||
}
|
||||
|
||||
public CompoundTag serialize()
|
||||
{
|
||||
CompoundTag tag = new CompoundTag();
|
||||
tag.putString("name", name);
|
||||
tag.put("id", NbtUtils.createUUID(id));
|
||||
|
||||
return tag;
|
||||
}
|
||||
|
||||
public ACLEntry(CompoundTag tag)
|
||||
{
|
||||
name=tag.getString("name");
|
||||
id=NbtUtils.loadUUID(tag.get("id"));
|
||||
}
|
||||
}
|
||||
|
||||
public List<ACLEntry> entries = new ArrayList<>();
|
||||
|
||||
public AccessControlList()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public List<String> getNames()
|
||||
{
|
||||
List<String> names = new ArrayList<>();
|
||||
for (ACLEntry entry : entries) {
|
||||
names.add(entry.name);
|
||||
}
|
||||
|
||||
return names;
|
||||
}
|
||||
|
||||
public List<UUID> getIDs()
|
||||
{
|
||||
List<UUID> ids = new ArrayList<>();
|
||||
for (ACLEntry entry : entries) {
|
||||
ids.add(entry.id);
|
||||
}
|
||||
|
||||
return ids;
|
||||
}
|
||||
|
||||
public void add(ServerPlayer player)
|
||||
{
|
||||
ACLEntry entry = new ACLEntry(player.getName().getContents(), player.getUUID());
|
||||
entries.add(entry);
|
||||
}
|
||||
|
||||
public void addEntry(String name, UUID id)
|
||||
{
|
||||
entries.add(new ACLEntry(name, id));
|
||||
}
|
||||
|
||||
public void removeByID(UUID ID)
|
||||
{
|
||||
Iterator<ACLEntry> entr = entries.iterator();
|
||||
while(entr.hasNext())
|
||||
{
|
||||
ACLEntry entry = entr.next();
|
||||
if(entry.id==ID)
|
||||
{
|
||||
entr.remove();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the current instance as a NBT Tag
|
||||
* @return
|
||||
*/
|
||||
public CompoundTag serialize()
|
||||
{
|
||||
CompoundTag tag = new CompoundTag();
|
||||
ListTag lst = new ListTag();
|
||||
for (ACLEntry aclEntry : entries) {
|
||||
lst.add(aclEntry.serialize());
|
||||
}
|
||||
tag.put("entries", lst);
|
||||
|
||||
return tag;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Reads a NBT Tag back into an AccessControlList
|
||||
* @param tag
|
||||
* @return
|
||||
*/
|
||||
public static AccessControlList deserialize(CompoundTag tag)
|
||||
{
|
||||
AccessControlList acl = new AccessControlList();
|
||||
ListTag lst = tag.getList("entries", Tag.TAG_COMPOUND);
|
||||
for (Tag tag2 : lst) {
|
||||
CompoundTag entry = (CompoundTag)tag2;
|
||||
ACLEntry entryItem = acl.deserializeEntry(entry);
|
||||
acl.entries.add(entryItem);
|
||||
}
|
||||
|
||||
return acl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes an entry from the subclass
|
||||
* @see ACLEntry
|
||||
* @param tag
|
||||
* @return
|
||||
*/
|
||||
private ACLEntry deserializeEntry(CompoundTag tag)
|
||||
{
|
||||
return new ACLEntry(tag);
|
||||
}
|
||||
}
|
|
@ -19,7 +19,7 @@ public class Warp {
|
|||
public boolean RTP;
|
||||
public boolean isPublic;
|
||||
public TeleportDestination destination;
|
||||
private List<UUID> ACL;
|
||||
private AccessControlList ACL;
|
||||
|
||||
public Warp(UUID owner, String name, boolean rtp, boolean publicWarp, TeleportDestination destination)
|
||||
{
|
||||
|
@ -28,26 +28,16 @@ public class Warp {
|
|||
RTP=rtp;
|
||||
isPublic=publicWarp;
|
||||
this.destination=destination;
|
||||
this.ACL = new ArrayList<>();
|
||||
this.ACL = new AccessControlList();
|
||||
}
|
||||
|
||||
public static Warp deserialize(CompoundTag tag) throws InvalidDeserialization
|
||||
{
|
||||
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;
|
||||
}
|
||||
warp.ACL = AccessControlList.deserialize(tag.getCompound("acl"));
|
||||
}
|
||||
|
||||
return warp;
|
||||
|
@ -63,13 +53,7 @@ public class Warp {
|
|||
tag.put("destination", destination.serialize());
|
||||
if(!isPublic)
|
||||
{
|
||||
|
||||
ListTag lst = new ListTag();
|
||||
for(UUID id : ACL)
|
||||
{
|
||||
lst.add(NbtUtils.createUUID(id));
|
||||
}
|
||||
tag.put("acl", lst);
|
||||
tag.put("acl", ACL.serialize());
|
||||
}
|
||||
|
||||
return tag;
|
||||
|
@ -93,7 +77,7 @@ public class Warp {
|
|||
public boolean hasAccess(UUID ID)
|
||||
{
|
||||
if(isPublic)return true;
|
||||
return ACL.contains(ID);
|
||||
return ACL.getIDs().contains(ID);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -102,18 +86,18 @@ public class Warp {
|
|||
*/
|
||||
protected void giveAccess(ServerPlayer player)
|
||||
{
|
||||
giveAccess(player.getUUID());
|
||||
giveAccess(player.getName().getContents(), player.getUUID());
|
||||
}
|
||||
|
||||
/**
|
||||
* If the warp is not public, it gives an ID access to the warp
|
||||
* @param ID
|
||||
*/
|
||||
protected void giveAccess(UUID ID)
|
||||
protected void giveAccess(String name, UUID ID)
|
||||
{
|
||||
if(!isPublic)
|
||||
{
|
||||
ACL.add(ID);
|
||||
ACL.addEntry(name, ID);
|
||||
}else return;
|
||||
}
|
||||
|
||||
|
@ -131,9 +115,9 @@ public class Warp {
|
|||
*/
|
||||
protected void removeAccess(UUID id)
|
||||
{
|
||||
if(ACL.contains(id))
|
||||
if(ACL.getIDs().contains(id))
|
||||
{
|
||||
ACL.remove(id);
|
||||
ACL.removeByID(id);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -143,6 +127,6 @@ public class Warp {
|
|||
*/
|
||||
protected List<UUID> getWarpACL()
|
||||
{
|
||||
return new ArrayList<UUID>(ACL);
|
||||
return ACL.getIDs();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue