Add some events for warp access

This commit is contained in:
Aria 2023-02-28 21:42:19 -07:00
parent 1542891f6d
commit 984cb45b02
4 changed files with 85 additions and 11 deletions

View file

@ -0,0 +1,30 @@
package dev.zontreck.essentials.events;
import dev.zontreck.essentials.warps.Warp;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.eventbus.api.Cancelable;
import net.minecraftforge.eventbus.api.Event;
/**
* Dispatched only when a warp's public status is toggled on or off.
* This event may be cancelled to prevent toggling. This could happen if the person is not the owner of the warp, or due to other permissions issues, such as from claims.
*/
@Cancelable
public class WarpAccessChanged extends Event
{
/**
* This is the warp the event is triggered for
*/
public Warp warp;
/**
* This is the player initiating the change
*/
public ServerPlayer player;
public WarpAccessChanged(Warp warp, ServerPlayer initiator)
{
this.warp=warp;
player=initiator;
}
}

View file

@ -0,0 +1,26 @@
package dev.zontreck.essentials.events;
import dev.zontreck.essentials.warps.Warp;
import dev.zontreck.essentials.warps.AccessControlList.ACLEntry;
import dev.zontreck.libzontreck.profiles.Profile;
import net.minecraftforge.eventbus.api.Event;
/**
* Dispatched when a warp's access control list gets updated.
*/
public class WarpAccessControlListUpdatedEvent extends Event
{
public Warp warp;
public ACLEntry entry;
/**
* If false, then the entry was deleted from the warp ACL
*/
public boolean added;
public WarpAccessControlListUpdatedEvent(Warp warp, ACLEntry entry, boolean added)
{
this.warp=warp;
this.entry=entry;
this.added=added;
}
}

View file

@ -70,18 +70,29 @@ public class AccessControlList {
return ids;
}
public void add(ServerPlayer player)
/**
* Adds a ACL Entry for a name and ID
* @param name
* @param id
* @return null if the entry was already in the ACL
* @return Entry if the entry was successfully added to the ACL
*/
public ACLEntry addEntry(String name, UUID id)
{
ACLEntry entry = new ACLEntry(player.getName().getContents(), player.getUUID());
ACLEntry entry = new ACLEntry(name, id);
if(getIDs().contains(id)) return null;
entries.add(entry);
return entry;
}
public void addEntry(String name, UUID id)
{
entries.add(new ACLEntry(name, id));
}
public void removeByID(UUID ID)
/**
* Removes a ACLEntry by UUID
* @param ID
* @return null if no such entry
* @return Entry that was removed from the list
*/
public ACLEntry removeByID(UUID ID)
{
Iterator<ACLEntry> entr = entries.iterator();
while(entr.hasNext())
@ -90,9 +101,11 @@ public class AccessControlList {
if(entry.id==ID)
{
entr.remove();
return;
return entry;
}
}
return null;
}
/**

View file

@ -5,6 +5,8 @@ import java.util.List;
import java.util.UUID;
import dev.zontreck.essentials.commands.teleport.TeleportDestination;
import dev.zontreck.essentials.events.WarpAccessControlListUpdatedEvent;
import dev.zontreck.essentials.warps.AccessControlList.ACLEntry;
import dev.zontreck.libzontreck.exceptions.InvalidDeserialization;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.IntArrayTag;
@ -12,6 +14,7 @@ import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.nbt.Tag;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.common.MinecraftForge;
public class Warp {
public UUID owner;
@ -97,7 +100,8 @@ public class Warp {
{
if(!isPublic)
{
ACL.addEntry(name, ID);
ACLEntry entry = ACL.addEntry(name, ID);
MinecraftForge.EVENT_BUS.post(new WarpAccessControlListUpdatedEvent(this, entry, true));
}else return;
}
@ -117,7 +121,8 @@ public class Warp {
{
if(ACL.getIDs().contains(id))
{
ACL.removeByID(id);
ACLEntry entry = ACL.removeByID(id);
MinecraftForge.EVENT_BUS.post(new WarpAccessControlListUpdatedEvent(this, entry, false));
}
}