From 984cb45b027abbe32adf960e554c0a8424afb0dc Mon Sep 17 00:00:00 2001 From: Aria Date: Tue, 28 Feb 2023 21:42:19 -0700 Subject: [PATCH] Add some events for warp access --- .../essentials/events/WarpAccessChanged.java | 30 ++++++++++++++++++ .../WarpAccessControlListUpdatedEvent.java | 26 ++++++++++++++++ .../essentials/warps/AccessControlList.java | 31 +++++++++++++------ .../dev/zontreck/essentials/warps/Warp.java | 9 ++++-- 4 files changed, 85 insertions(+), 11 deletions(-) create mode 100644 src/main/java/dev/zontreck/essentials/events/WarpAccessChanged.java create mode 100644 src/main/java/dev/zontreck/essentials/events/WarpAccessControlListUpdatedEvent.java diff --git a/src/main/java/dev/zontreck/essentials/events/WarpAccessChanged.java b/src/main/java/dev/zontreck/essentials/events/WarpAccessChanged.java new file mode 100644 index 0000000..4e20929 --- /dev/null +++ b/src/main/java/dev/zontreck/essentials/events/WarpAccessChanged.java @@ -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; + } +} diff --git a/src/main/java/dev/zontreck/essentials/events/WarpAccessControlListUpdatedEvent.java b/src/main/java/dev/zontreck/essentials/events/WarpAccessControlListUpdatedEvent.java new file mode 100644 index 0000000..e977f5e --- /dev/null +++ b/src/main/java/dev/zontreck/essentials/events/WarpAccessControlListUpdatedEvent.java @@ -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; + } +} diff --git a/src/main/java/dev/zontreck/essentials/warps/AccessControlList.java b/src/main/java/dev/zontreck/essentials/warps/AccessControlList.java index 61258a4..73cb327 100644 --- a/src/main/java/dev/zontreck/essentials/warps/AccessControlList.java +++ b/src/main/java/dev/zontreck/essentials/warps/AccessControlList.java @@ -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 entr = entries.iterator(); while(entr.hasNext()) @@ -90,9 +101,11 @@ public class AccessControlList { if(entry.id==ID) { entr.remove(); - return; + return entry; } } + + return null; } /** diff --git a/src/main/java/dev/zontreck/essentials/warps/Warp.java b/src/main/java/dev/zontreck/essentials/warps/Warp.java index a69cd2b..3bb74d0 100644 --- a/src/main/java/dev/zontreck/essentials/warps/Warp.java +++ b/src/main/java/dev/zontreck/essentials/warps/Warp.java @@ -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)); } }