From 3a4f6c05aa9d011ced65ab728f0c60896df2eee9 Mon Sep 17 00:00:00 2001 From: Foxtrek_64 Date: Tue, 28 May 2024 08:37:23 -0500 Subject: [PATCH 1/2] Begin changes --- .../htm/HTMContainerLock.java | 37 +++- .../github/fabricservertools/htm/Utility.java | 9 + .../fabricservertools/htm/api/Group.java | 87 +++++++++ .../fabricservertools/htm/api/LockGroup.java | 175 ++++++++++++++++++ .../htm/command/arguments/GroupArgument.java | 13 ++ .../command/subcommands/GroupCommands.java | 150 +++++++++++++++ .../LockGroupSuggestionProvider.java | 82 ++++++++ .../fabricservertools/htm/locks/KeyLock.java | 7 +- .../htm/locks/PrivateLock.java | 5 + .../htm/world/data/LockGroupState.java | 153 +++++++++++++++ 10 files changed, 711 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/github/fabricservertools/htm/api/Group.java create mode 100644 src/main/java/com/github/fabricservertools/htm/api/LockGroup.java create mode 100644 src/main/java/com/github/fabricservertools/htm/command/arguments/GroupArgument.java create mode 100644 src/main/java/com/github/fabricservertools/htm/command/subcommands/GroupCommands.java create mode 100644 src/main/java/com/github/fabricservertools/htm/command/suggestors/LockGroupSuggestionProvider.java create mode 100644 src/main/java/com/github/fabricservertools/htm/world/data/LockGroupState.java diff --git a/src/main/java/com/github/fabricservertools/htm/HTMContainerLock.java b/src/main/java/com/github/fabricservertools/htm/HTMContainerLock.java index f015fc8..a218e0b 100644 --- a/src/main/java/com/github/fabricservertools/htm/HTMContainerLock.java +++ b/src/main/java/com/github/fabricservertools/htm/HTMContainerLock.java @@ -1,5 +1,6 @@ package com.github.fabricservertools.htm; +import com.github.fabricservertools.htm.api.Group; import com.github.fabricservertools.htm.api.Lock; import me.lucko.fabric.api.permissions.v0.Permissions; import net.fabricmc.fabric.api.util.NbtType; @@ -11,19 +12,20 @@ import net.minecraft.sound.SoundCategory; import net.minecraft.sound.SoundEvents; import net.minecraft.text.Text; +import org.apache.commons.lang3.NotImplementedException; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.UUID; +import java.util.*; public class HTMContainerLock { + private final HashSet groups; + private Lock type; private UUID owner; private HashSet trusted; private Map flags; public HTMContainerLock() { + groups = new HashSet<>(); type = null; owner = null; trusted = new HashSet<>(); @@ -49,9 +51,14 @@ public void toTag(NbtCompound tag) { for (UUID uuid : trusted) { trustedTag.add(NbtHelper.fromUuid(uuid)); } - tag.put("Trusted", trustedTag); + NbtList groupsTag = new NbtList(); + for (UUID groupId : groups) { + groupsTag.add(NbtHelper.fromUuid(groupId)); + } + tag.put("Groups", groupsTag); + NbtList flagsTag = new NbtList(); for (Map.Entry entry : flags.entrySet()) { NbtCompound flagTag = new NbtCompound(); @@ -79,11 +86,15 @@ public void fromTag(NbtCompound tag) { owner = tag.getUuid("Owner"); NbtList trustedTag = tag.getList("Trusted", NbtType.INT_ARRAY); - for (NbtElement value : trustedTag) { trusted.add(NbtHelper.toUuid(value)); } + NbtList groupsTag = tag.getList("Groups", NbtElement.INT_ARRAY_TYPE); + for (NbtElement value : groupsTag) { + groups.add(NbtHelper.toUuid(value)); + } + NbtList flagTags = tag.getList("Flags", NbtType.COMPOUND); for (NbtElement flagTag : flagTags) { NbtCompound compoundTag = (NbtCompound) flagTag; @@ -120,6 +131,10 @@ public HashSet getTrusted() { return trusted; } + public Set getGroups() { + return groups; + } + public void setType(Lock type, ServerPlayerEntity owner) { this.type = type; this.owner = owner.getUuid(); @@ -141,6 +156,16 @@ public boolean removeTrust(UUID id) { return trusted.remove(id); } + public boolean addGroup(Group group) { + // Todo: Check if group exists in group state first!!! + return groups.add(group.getId()); + } + + public boolean removeGroup(Group group) { + // Todo: Check if group exists in group state first!!! + return groups.remove(group.getId()); + } + public boolean isTrusted(UUID id) { return trusted.contains(id); } diff --git a/src/main/java/com/github/fabricservertools/htm/Utility.java b/src/main/java/com/github/fabricservertools/htm/Utility.java index 66517ee..e5c65d3 100644 --- a/src/main/java/com/github/fabricservertools/htm/Utility.java +++ b/src/main/java/com/github/fabricservertools/htm/Utility.java @@ -1,7 +1,9 @@ package com.github.fabricservertools.htm; +import com.github.fabricservertools.htm.api.LockGroup; import com.github.fabricservertools.htm.interactions.InteractionManager; import com.github.fabricservertools.htm.world.data.GlobalTrustState; +import com.github.fabricservertools.htm.world.data.LockGroupState; import com.mojang.authlib.GameProfile; import net.minecraft.datafixer.DataFixTypes; import net.minecraft.entity.player.PlayerEntity; @@ -25,6 +27,13 @@ public static GlobalTrustState getGlobalTrustState(MinecraftServer server) { "globalTrust"); } + public static LockGroupState getLockGroupState(MinecraftServer server) { + return server.getOverworld().getPersistentStateManager().getOrCreate( + new PersistentState.Type<>(LockGroupState::new, LockGroupState::fromNbt, DataFixTypes.PLAYER), + "lockGroups" + ); + } + public static void sendMessage(PlayerEntity player, Text message) { sendMessage(player, message, false); } diff --git a/src/main/java/com/github/fabricservertools/htm/api/Group.java b/src/main/java/com/github/fabricservertools/htm/api/Group.java new file mode 100644 index 0000000..273ece7 --- /dev/null +++ b/src/main/java/com/github/fabricservertools/htm/api/Group.java @@ -0,0 +1,87 @@ +package com.github.fabricservertools.htm.api; + +import net.minecraft.server.network.ServerPlayerEntity; + +import java.util.Set; +import java.util.UUID; + +/** + * Describes a group which is able to be applied to protection and holds users. + */ +public interface Group { + /** + * Gets the unique id of this group. + * @return The group id. + */ + UUID getId(); + + /** + * Gets the display name of the collection. + * @return The display name. + */ + String getName(); + + /** + * Sets the name of the collection. + * @param Name The new display name. + */ + void setName(String Name); + + /** + * Gets the id of the user who created this group. + * @return The {@see java.util.UUID} of the user who created this group. + */ + UUID getOwner(); + + /** + * Gets a readonly list of all group managers. + * @return A readonly list of group managers. + */ + Set getManagers(); + + /** + * Makes the specified player a manager. + * @param player The unique id of the player to add. + * @return true if the player was added to the collection; otherwise, + * false if the player was already a member of the collection. + */ + boolean addManager(UUID player); + + /** + * Removes the specified player being a group manager. + * @param player The unique id of the player to remove. + * @return true if the player was removed; otherwise, false. + */ + boolean removeManager(UUID player); + + /** + * Removes all players from the collection. + */ + void clearManagers(); + + /** + * Gets a readonly list of all members in this collection. + * @return A readonly list of all members in this collection. + */ + Set getMembers(); + + /** + * Adds the specified player to the collection. + * @param player The unique id of the player to add. + * @return true if the player was added to the collection; otherwise, + * false if the player was already a member of the collection. + */ + boolean addMember(UUID player); + + /** + * Removes the specified player from the collection. + * @param player The unique id of the player to remove. + * @return true if the player was removed; otherwise, false. + */ + boolean removeMember(UUID player); + + /** + * Removes all players from the collection. + */ + void clearMembers(); +} diff --git a/src/main/java/com/github/fabricservertools/htm/api/LockGroup.java b/src/main/java/com/github/fabricservertools/htm/api/LockGroup.java new file mode 100644 index 0000000..f151f30 --- /dev/null +++ b/src/main/java/com/github/fabricservertools/htm/api/LockGroup.java @@ -0,0 +1,175 @@ +package com.github.fabricservertools.htm.api; + +import net.minecraft.server.network.ServerPlayerEntity; + +import java.util.*; + +/** + * Describes a group which provides permissions globally. + */ +public class LockGroup implements Group { + private final UUID id; + private final UUID owner; + private final HashSet players; + private final HashSet managers; + private String name; + + + /** + * Creates a new instance of @see LockGroup. + * @param name The non-unique display name of the group. + */ + public LockGroup(String name, ServerPlayerEntity owner) { + this.id = UUID.randomUUID(); + this.owner = owner.getUuid(); + this.players = new HashSet<>(); + this.managers = new HashSet<>(); + this.name = name; + } + + public LockGroup(UUID id, UUID owner, HashSet players, HashSet managers, String name) { + this.id = id; + this.owner = owner; + this.players = players; + this.managers = managers; + this.name = name; + } + + /** + * Gets the unique id of this group. + * + * @return The group id. + */ + @Override + public UUID getId() { + return this.id; + } + + /** + * Gets the display name of the collection. + * + * @return The display name. + */ + @Override + public String getName() { + return name; + } + + /** + * Sets the name of the collection. + * + * @param name The new display name. + */ + @Override + public void setName(String name) { + this.name = name; + } + + /** + * Gets the id of the user who created this group. + * + * @return The {@see java.util.UUID} of the user who created this group. + */ + @Override + public UUID getOwner() { + return owner; + } + + /** + * Gets a readonly list of all group managers. + * + * @return A readonly list of group managers. + */ + @Override + public Set getManagers() { + return Collections.unmodifiableSet(managers); +} + + /** + * Makes the specified player a manager. + * + * @param player The id of the player to add. + * @return true if the player was added to the collection; otherwise, + * false if the player was already a member of the collection. + */ + @Override + public boolean addManager(UUID player) { + return managers.add(player); + } + + /** + * Removes the specified player being a group manager. + * + * @param player The unique id of the player to remove. + * @return true if the player was removed; otherwise, false. + */ + @Override + public boolean removeManager(UUID player) { + return managers.remove(player); + } + + /** + * Removes all players from the collection. + */ + @Override + public void clearManagers() { + managers.clear(); + } + + /** + * Gets a readonly list of all members in this collection. + * + * @return A readonly list of all members in this collection. + */ + @Override + public Set getMembers() { + return Collections.unmodifiableSet(players); + } + + /** + * Adds the specified player to the collection. + * + * @param player The unique id of the player to add. + * @return true if the player was added to the collection; otherwise, + * false if the player was already a member of the collection. + */ + @Override + public boolean addMember(UUID player) { + return players.add(player); + } + + /** + * Removes the specified player from the collection. + * + * @param player The unique id of the player to remove. + * @return true if the player was removed; otherwise, false. + */ + @Override + public boolean removeMember(UUID player) { + return players.remove(player); + } + + /** + * Removes all players from the collection. + */ + @Override + public void clearMembers() { + players.clear(); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof LockGroup lockGroup)) { + return false; + } + return Objects.equals(id, lockGroup.id); + } + + @Override + public int hashCode() { + return Objects.hashCode(id); + } +} diff --git a/src/main/java/com/github/fabricservertools/htm/command/arguments/GroupArgument.java b/src/main/java/com/github/fabricservertools/htm/command/arguments/GroupArgument.java new file mode 100644 index 0000000..2e4e16a --- /dev/null +++ b/src/main/java/com/github/fabricservertools/htm/command/arguments/GroupArgument.java @@ -0,0 +1,13 @@ +package com.github.fabricservertools.htm.command.arguments; + +import com.github.fabricservertools.htm.api.Group; +import com.mojang.brigadier.StringReader; +import com.mojang.brigadier.arguments.ArgumentType; +import com.mojang.brigadier.exceptions.CommandSyntaxException; + +public class GroupArgument implements ArgumentType { + @Override + public Group parse(StringReader reader) throws CommandSyntaxException { + return null; + } +} diff --git a/src/main/java/com/github/fabricservertools/htm/command/subcommands/GroupCommands.java b/src/main/java/com/github/fabricservertools/htm/command/subcommands/GroupCommands.java new file mode 100644 index 0000000..decce5b --- /dev/null +++ b/src/main/java/com/github/fabricservertools/htm/command/subcommands/GroupCommands.java @@ -0,0 +1,150 @@ +package com.github.fabricservertools.htm.command.subcommands; + +import com.github.fabricservertools.htm.Utility; +import com.github.fabricservertools.htm.command.SubCommand; +import com.github.fabricservertools.htm.command.suggestors.LockGroupSuggestionProvider; +import com.github.fabricservertools.htm.interactions.InteractionManager; +import com.github.fabricservertools.htm.interactions.TrustAction; +import com.github.fabricservertools.htm.world.data.GlobalTrustState; +import com.mojang.authlib.GameProfile; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import com.mojang.brigadier.tree.LiteralCommandNode; +import me.lucko.fabric.api.permissions.v0.Permissions; +import net.fabricmc.fabric.api.command.v2.ArgumentTypeRegistry; +import net.fabricmc.fabric.mixin.command.ArgumentTypesAccessor; +import net.minecraft.command.argument.ArgumentTypes; +import net.minecraft.command.argument.GameProfileArgumentType; +import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.text.Text; +import org.apache.commons.lang3.NotImplementedException; + +import java.util.Collection; +import java.util.UUID; +import java.util.stream.Collectors; + +import static net.minecraft.server.command.CommandManager.argument; +import static net.minecraft.server.command.CommandManager.literal; + +/** + * Defines commands which exist under the group node + */ +public class GroupCommands implements SubCommand { + + @Override + public LiteralCommandNode build() { + return literal("group") + .requires(Permissions.require("htm.command.group", true)) + .executes(ctx -> groupList(ctx, true)) + .then(literal("list") + .executes(ctx -> groupList(ctx, true)) + .then( + argument("owner", GameProfileArgumentType.gameProfile()) + .requires(Permissions.require("htm.command.group.listOther", false)) + .executes(ctx -> groupList(ctx, false)) + ) + ) + .then(literal("create") + .requires(Permissions.require("htm.command.group.create", true)) + .then( + argument("name", StringArgumentType.string()) + .executes(ctx -> createGroup(ctx.getSource(), StringArgumentType.getString(ctx, "name"))) + .then( + argument("owner", GameProfileArgumentType.gameProfile()) + .requires(Permissions.require("htm.command.group.create.other")) + .executes(ctx -> createGroupFor( + ctx.getSource(), + StringArgumentType.getString(ctx, "name"), + GameProfileArgumentType.getProfileArgument(ctx, "owner") + .stream() + .findFirst() + .orElseThrow() + .getId() + ) + ) + ) + ) + ) + .then(literal("delete") + .requires(Permissions.require("htm.command.group.delete")) + ) + .then(literal("search") + .requires(Permissions.require("htm.command.group.search")) + // TODO: By name + // TODO: By owner + // TODO: By UUID + ) + .then(literal("edit") + .requires(Permissions.require("htm.command.group.edit", true)) + .then(literal("name").then( + argument("group", StringArgumentType.string()) + .suggests(new LockGroupSuggestionProvider(false)) + .then( + argument("newName", StringArgumentType.string()) + .executes(ctx -> editGroupName( + ctx.getSource(), + StringArgumentType.getString(ctx, "group"), + StringArgumentType.getString(ctx, "newName") + ) + ) + ) + + ) + ) + .then(literal("trusted").then( + argument("group", StringArgumentType.string()) + .suggests(new LockGroupSuggestionProvider(true)) + .then(literal("add") + .requires() + .then( + argument("target", GameProfileArgumentType.gameProfile()) + )) + .then(literal("remove")) + ) + ) + .then(literal("managers")) + .then(literal("transfer")) + .then( + literal("other") + .requires(Permissions.require("htm.command.group.edit.others")) + .then( + argument("owner", GameProfileArgumentType.gameProfile()) + .then(literal("name").then( + argument("group", StringArgumentType.string() + //.suggests(new LockGroupSuggestionProvider(false, GameProfileArgumentType.getProfileArgument())) + ) + ) + .then(literal("trusted")) + .then(literal("managers")) + .then(literal("transfer")) + ) + ) + ) + .build(); + + + + } + + private int groupList(CommandContext context, boolean showForSelf) throws CommandSyntaxException { + return 1; + } + + private int createGroup(ServerCommandSource source, String groupName) throws CommandSyntaxException { + return createGroupFor(source, groupName, source.getPlayerOrThrow().getUuid()); + } + + private int createGroupFor(ServerCommandSource source, String groupName, UUID owner) { + return 1; + } + + private int editGroupName(ServerCommandSource source, String groupName, String newName) { + return 1; + } + + private void hasAddRemoveTrustPermissions(ServerCommandSource source, String groupName) { + + } +} diff --git a/src/main/java/com/github/fabricservertools/htm/command/suggestors/LockGroupSuggestionProvider.java b/src/main/java/com/github/fabricservertools/htm/command/suggestors/LockGroupSuggestionProvider.java new file mode 100644 index 0000000..8bbcd71 --- /dev/null +++ b/src/main/java/com/github/fabricservertools/htm/command/suggestors/LockGroupSuggestionProvider.java @@ -0,0 +1,82 @@ +package com.github.fabricservertools.htm.command.suggestors; + +import com.github.fabricservertools.htm.Utility; +import com.github.fabricservertools.htm.api.Group; +import com.github.fabricservertools.htm.world.data.LockGroupState; +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import com.mojang.brigadier.suggestion.SuggestionProvider; +import com.mojang.brigadier.suggestion.Suggestions; +import com.mojang.brigadier.suggestion.SuggestionsBuilder; +import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.server.network.ServerPlayerEntity; +import org.apache.logging.log4j.core.jmx.Server; + +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.CompletableFuture; +import java.util.stream.Collectors; + +/** + * Provides suggestions for the names of Lock Groups. + */ +public class LockGroupSuggestionProvider implements SuggestionProvider { + + private final boolean suggestManagedGroups; + private Set groupsForOwner; + private boolean initialized = false; + private Optional suggestedOwner; + + public LockGroupSuggestionProvider(boolean suggestManagedGroups) + { + this(suggestManagedGroups, null); + } + + public LockGroupSuggestionProvider( + boolean suggestManagedGroups, + Optional owner + ) { + this.suggestManagedGroups = suggestManagedGroups; + this.suggestedOwner = owner; + } + + @Override + public CompletableFuture getSuggestions( + CommandContext context, + SuggestionsBuilder builder + ) throws CommandSyntaxException { + init(context.getSource()); + String current = builder.getRemaining(); + + for (Group group : groupsForOwner) { + if (group.getName().contains(current)) { + builder.suggest(group.getName()); + } + } + + return builder.buildFuture(); + } + + private void init(ServerCommandSource source) throws CommandSyntaxException { + if (initialized) { + return; + } + LockGroupState lockGroupState = Utility.getLockGroupState(source.getServer()); + ServerPlayerEntity owner = suggestedOwner.orElse(source.getPlayerOrThrow()); + if (suggestManagedGroups) { + groupsForOwner = lockGroupState.getGroups() + .values() + .stream() + .filter(group -> group.getOwner() == owner.getUuid() || group.getManagers().contains(owner.getUuid())) + .collect(Collectors.toSet()); + } else { + groupsForOwner = lockGroupState.getGroups() + .values() + .stream() + .filter(group -> group.getOwner() == owner.getUuid()) + .collect(Collectors.toSet()); + } + + initialized = true; + } +} diff --git a/src/main/java/com/github/fabricservertools/htm/locks/KeyLock.java b/src/main/java/com/github/fabricservertools/htm/locks/KeyLock.java index 16bb623..4929880 100644 --- a/src/main/java/com/github/fabricservertools/htm/locks/KeyLock.java +++ b/src/main/java/com/github/fabricservertools/htm/locks/KeyLock.java @@ -15,8 +15,13 @@ public class KeyLock implements Lock { @Override public boolean canOpen(ServerPlayerEntity player, HTMContainerLock lock) { if (lock.isTrusted(player.getUuid())) return true; - if (Utility.getGlobalTrustState(player.server).isTrusted(lock.getOwner(), player.getUuid())) + + if (Utility.getLockGroupState(player.server).isTrusted(lock.getGroups(), player.getUuid())) { + return true; + } + if (Utility.getGlobalTrustState(player.server).isTrusted(lock.getOwner(), player.getUuid())) { return true; + } ItemStack itemStack = player.getMainHandStack(); return ItemStack.canCombine(itemStack, key); diff --git a/src/main/java/com/github/fabricservertools/htm/locks/PrivateLock.java b/src/main/java/com/github/fabricservertools/htm/locks/PrivateLock.java index 259e1d0..c9874d8 100644 --- a/src/main/java/com/github/fabricservertools/htm/locks/PrivateLock.java +++ b/src/main/java/com/github/fabricservertools/htm/locks/PrivateLock.java @@ -11,6 +11,11 @@ public class PrivateLock implements Lock { @Override public boolean canOpen(ServerPlayerEntity player, HTMContainerLock lock) { if (lock.isTrusted(player.getUuid())) return true; + + if (Utility.getLockGroupState(player.server).isTrusted(lock.getGroups(), player.getUuid())) { + return true; + } + return Utility.getGlobalTrustState(player.server).isTrusted(lock.getOwner(), player.getUuid()); } diff --git a/src/main/java/com/github/fabricservertools/htm/world/data/LockGroupState.java b/src/main/java/com/github/fabricservertools/htm/world/data/LockGroupState.java new file mode 100644 index 0000000..79069da --- /dev/null +++ b/src/main/java/com/github/fabricservertools/htm/world/data/LockGroupState.java @@ -0,0 +1,153 @@ +package com.github.fabricservertools.htm.world.data; + +import com.github.fabricservertools.htm.api.Group; +import com.github.fabricservertools.htm.api.LockGroup; +import com.google.common.collect.*; +import net.minecraft.nbt.*; +import net.minecraft.world.PersistentState; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; + +/** + * Describes a persistent state which stores groups. + */ +public class LockGroupState extends PersistentState { + private final HashMap groups; + + private static final String LockGroups = "LockGroups"; + private static final String LockGroupId = "LockGroupId"; + private static final String LockGroupOwner = "LockGroupOwner"; + private static final String LockGroupName = "LockGroupName"; + private static final String LockGroupList = "LockGroupList"; + private static final String LockGroupManagers = "LockGroupManagers"; + + public LockGroupState() { + groups = new HashMap<>(); + } + + @Override + public NbtCompound writeNbt(NbtCompound nbt) { + NbtList trustList = new NbtList(); + + for (Group group : groups.values()) { + NbtCompound groupTag = new NbtCompound(); + groupTag.putUuid(LockGroupId, group.getId()); + groupTag.putUuid(LockGroupOwner, group.getOwner()); + groupTag.putString(LockGroupName, group.getName()); + + NbtList trustedUsers = new NbtList(); + for (UUID userId : group.getMembers()) { + trustedUsers.add(NbtHelper.fromUuid(userId)); + } + groupTag.put(LockGroupList, trustedUsers); + + NbtList managers = new NbtList(); + for (UUID userId : group.getManagers()) { + managers.add(NbtHelper.fromUuid(userId)); + } + groupTag.put(LockGroupManagers, managers); + + trustList.add(groupTag); + } + + nbt.put(LockGroups, trustList); + return nbt; + } + + public static LockGroupState fromNbt(NbtCompound tag) { + LockGroupState lockGroupState = new LockGroupState(); + NbtList lockGroupList = tag.getList(LockGroups, NbtElement.COMPOUND_TYPE); + + lockGroupList.forEach(it -> { + NbtCompound compoundTag = (NbtCompound)it; + UUID groupId = compoundTag.getUuid(LockGroupId); + UUID ownerId = compoundTag.getUuid(LockGroupOwner); + String groupName = compoundTag.getString(LockGroupName); + HashSet trusted = new HashSet<>(); + HashSet managers = new HashSet<>(); + + NbtList trustedList = compoundTag.getList(LockGroupList, NbtElement.INT_ARRAY_TYPE); + for (NbtElement value : trustedList) { + trusted.add(NbtHelper.toUuid(value)); + } + + NbtList managerList = compoundTag.getList(LockGroupManagers, NbtElement.INT_ARRAY_TYPE); + for (NbtElement value : managerList) { + managers.add(NbtHelper.toUuid(value)); + } + + lockGroupState.groups.put(groupId, new LockGroup(groupId, ownerId, trusted, managers, groupName)); + }); + + return lockGroupState; + } + + public boolean isTrusted(Set groupIds, UUID playerId) { + return groupIds.stream().anyMatch(it -> { + Group group = groups.get(it); + if (group == null) { + return false; + } + + if (group.getMembers().contains(playerId)) return true; + if (group.getManagers().contains(playerId)) return true; + return false; + }); + } + + public boolean isMember(UUID groupId, UUID trusted) { + return groups.get(groupId).getMembers().contains(trusted); + } + + public boolean isManager(UUID groupId, UUID manager) { + return groups.get(groupId).getManagers().contains(manager); + } + + public boolean addToGroup(UUID groupId, UUID trusted) { + Group group = groups.get(groupId); + if (group == null) { + return false; + } + + if (group.addMember(trusted)) { + markDirty(); + return true; + } + + return false; + } + + public boolean removeFromGroup(UUID group, UUID trusted) { + if(groups.remove(group, trusted)) { + markDirty(); + return true; + } + return false; + } + + public boolean addGroup(Group group) { + if (groups.containsKey(group.getId())) { + return false; + } + + groups.put(group.getId(), group); + markDirty(); + return true; + } + + public boolean removeGroup(Group group) { + if (groups.containsKey(group.getId())) { + groups.remove(group.getId()); + markDirty(); + return true; + } + return false; + } + + public ImmutableMap getGroups() { + return ImmutableMap.copyOf(groups); + } +} From 34f74223cfc2ccd6ceae50e03dffb725498bfa21 Mon Sep 17 00:00:00 2001 From: Foxtrek_64 Date: Wed, 17 Jul 2024 16:45:41 -0500 Subject: [PATCH 2/2] Add protection groups --- .../htm/HTMContainerLock.java | 18 +++---- .../github/fabricservertools/htm/Utility.java | 1 - ...ockGroup.java => LockProtectionGroup.java} | 8 ++-- .../api/{Group.java => ProtectionGroup.java} | 4 +- .../htm/command/arguments/GroupArgument.java | 6 +-- .../LockGroupSuggestionProvider.java | 11 ++--- .../htm/listeners/PlayerEventListener.java | 8 +++- .../htm/locks/PrivateLock.java | 2 +- .../htm/world/data/LockGroupState.java | 48 +++++++++---------- 9 files changed, 52 insertions(+), 54 deletions(-) rename src/main/java/com/github/fabricservertools/htm/api/{LockGroup.java => LockProtectionGroup.java} (92%) rename src/main/java/com/github/fabricservertools/htm/api/{Group.java => ProtectionGroup.java} (96%) diff --git a/src/main/java/com/github/fabricservertools/htm/HTMContainerLock.java b/src/main/java/com/github/fabricservertools/htm/HTMContainerLock.java index a218e0b..286f3c4 100644 --- a/src/main/java/com/github/fabricservertools/htm/HTMContainerLock.java +++ b/src/main/java/com/github/fabricservertools/htm/HTMContainerLock.java @@ -1,6 +1,6 @@ package com.github.fabricservertools.htm; -import com.github.fabricservertools.htm.api.Group; +import com.github.fabricservertools.htm.api.ProtectionGroup; import com.github.fabricservertools.htm.api.Lock; import me.lucko.fabric.api.permissions.v0.Permissions; import net.fabricmc.fabric.api.util.NbtType; @@ -12,22 +12,20 @@ import net.minecraft.sound.SoundCategory; import net.minecraft.sound.SoundEvents; import net.minecraft.text.Text; -import org.apache.commons.lang3.NotImplementedException; import java.util.*; public class HTMContainerLock { - private final HashSet groups; - private Lock type; private UUID owner; + private HashSet groups; private HashSet trusted; private Map flags; public HTMContainerLock() { - groups = new HashSet<>(); type = null; owner = null; + groups = new HashSet<>(); trusted = new HashSet<>(); initFlags(); } @@ -156,14 +154,12 @@ public boolean removeTrust(UUID id) { return trusted.remove(id); } - public boolean addGroup(Group group) { - // Todo: Check if group exists in group state first!!! - return groups.add(group.getId()); + public boolean addGroup(ProtectionGroup protectionGroup) { + return groups.add(protectionGroup.getId()); } - public boolean removeGroup(Group group) { - // Todo: Check if group exists in group state first!!! - return groups.remove(group.getId()); + public boolean removeGroup(ProtectionGroup protectionGroup) { + return groups.remove(protectionGroup.getId()); } public boolean isTrusted(UUID id) { diff --git a/src/main/java/com/github/fabricservertools/htm/Utility.java b/src/main/java/com/github/fabricservertools/htm/Utility.java index e5c65d3..f9261e7 100644 --- a/src/main/java/com/github/fabricservertools/htm/Utility.java +++ b/src/main/java/com/github/fabricservertools/htm/Utility.java @@ -1,6 +1,5 @@ package com.github.fabricservertools.htm; -import com.github.fabricservertools.htm.api.LockGroup; import com.github.fabricservertools.htm.interactions.InteractionManager; import com.github.fabricservertools.htm.world.data.GlobalTrustState; import com.github.fabricservertools.htm.world.data.LockGroupState; diff --git a/src/main/java/com/github/fabricservertools/htm/api/LockGroup.java b/src/main/java/com/github/fabricservertools/htm/api/LockProtectionGroup.java similarity index 92% rename from src/main/java/com/github/fabricservertools/htm/api/LockGroup.java rename to src/main/java/com/github/fabricservertools/htm/api/LockProtectionGroup.java index f151f30..bf97967 100644 --- a/src/main/java/com/github/fabricservertools/htm/api/LockGroup.java +++ b/src/main/java/com/github/fabricservertools/htm/api/LockProtectionGroup.java @@ -7,7 +7,7 @@ /** * Describes a group which provides permissions globally. */ -public class LockGroup implements Group { +public class LockProtectionGroup implements ProtectionGroup { private final UUID id; private final UUID owner; private final HashSet players; @@ -19,7 +19,7 @@ public class LockGroup implements Group { * Creates a new instance of @see LockGroup. * @param name The non-unique display name of the group. */ - public LockGroup(String name, ServerPlayerEntity owner) { + public LockProtectionGroup(String name, ServerPlayerEntity owner) { this.id = UUID.randomUUID(); this.owner = owner.getUuid(); this.players = new HashSet<>(); @@ -27,7 +27,7 @@ public LockGroup(String name, ServerPlayerEntity owner) { this.name = name; } - public LockGroup(UUID id, UUID owner, HashSet players, HashSet managers, String name) { + public LockProtectionGroup(UUID id, UUID owner, HashSet players, HashSet managers, String name) { this.id = id; this.owner = owner; this.players = players; @@ -162,7 +162,7 @@ public boolean equals(Object o) { if (this == o) { return true; } - if (!(o instanceof LockGroup lockGroup)) { + if (!(o instanceof LockProtectionGroup lockGroup)) { return false; } return Objects.equals(id, lockGroup.id); diff --git a/src/main/java/com/github/fabricservertools/htm/api/Group.java b/src/main/java/com/github/fabricservertools/htm/api/ProtectionGroup.java similarity index 96% rename from src/main/java/com/github/fabricservertools/htm/api/Group.java rename to src/main/java/com/github/fabricservertools/htm/api/ProtectionGroup.java index 273ece7..0d5e20d 100644 --- a/src/main/java/com/github/fabricservertools/htm/api/Group.java +++ b/src/main/java/com/github/fabricservertools/htm/api/ProtectionGroup.java @@ -1,14 +1,12 @@ package com.github.fabricservertools.htm.api; -import net.minecraft.server.network.ServerPlayerEntity; - import java.util.Set; import java.util.UUID; /** * Describes a group which is able to be applied to protection and holds users. */ -public interface Group { +public interface ProtectionGroup { /** * Gets the unique id of this group. * @return The group id. diff --git a/src/main/java/com/github/fabricservertools/htm/command/arguments/GroupArgument.java b/src/main/java/com/github/fabricservertools/htm/command/arguments/GroupArgument.java index 2e4e16a..e8615e0 100644 --- a/src/main/java/com/github/fabricservertools/htm/command/arguments/GroupArgument.java +++ b/src/main/java/com/github/fabricservertools/htm/command/arguments/GroupArgument.java @@ -1,13 +1,13 @@ package com.github.fabricservertools.htm.command.arguments; -import com.github.fabricservertools.htm.api.Group; +import com.github.fabricservertools.htm.api.ProtectionGroup; import com.mojang.brigadier.StringReader; import com.mojang.brigadier.arguments.ArgumentType; import com.mojang.brigadier.exceptions.CommandSyntaxException; -public class GroupArgument implements ArgumentType { +public class GroupArgument implements ArgumentType { @Override - public Group parse(StringReader reader) throws CommandSyntaxException { + public ProtectionGroup parse(StringReader reader) throws CommandSyntaxException { return null; } } diff --git a/src/main/java/com/github/fabricservertools/htm/command/suggestors/LockGroupSuggestionProvider.java b/src/main/java/com/github/fabricservertools/htm/command/suggestors/LockGroupSuggestionProvider.java index 8bbcd71..15e9640 100644 --- a/src/main/java/com/github/fabricservertools/htm/command/suggestors/LockGroupSuggestionProvider.java +++ b/src/main/java/com/github/fabricservertools/htm/command/suggestors/LockGroupSuggestionProvider.java @@ -1,7 +1,7 @@ package com.github.fabricservertools.htm.command.suggestors; import com.github.fabricservertools.htm.Utility; -import com.github.fabricservertools.htm.api.Group; +import com.github.fabricservertools.htm.api.ProtectionGroup; import com.github.fabricservertools.htm.world.data.LockGroupState; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; @@ -10,7 +10,6 @@ import com.mojang.brigadier.suggestion.SuggestionsBuilder; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; -import org.apache.logging.log4j.core.jmx.Server; import java.util.Optional; import java.util.Set; @@ -23,7 +22,7 @@ public class LockGroupSuggestionProvider implements SuggestionProvider { private final boolean suggestManagedGroups; - private Set groupsForOwner; + private Set groupsForOwner; private boolean initialized = false; private Optional suggestedOwner; @@ -48,9 +47,9 @@ public CompletableFuture getSuggestions( init(context.getSource()); String current = builder.getRemaining(); - for (Group group : groupsForOwner) { - if (group.getName().contains(current)) { - builder.suggest(group.getName()); + for (ProtectionGroup protectionGroup : groupsForOwner) { + if (protectionGroup.getName().contains(current)) { + builder.suggest(protectionGroup.getName()); } } diff --git a/src/main/java/com/github/fabricservertools/htm/listeners/PlayerEventListener.java b/src/main/java/com/github/fabricservertools/htm/listeners/PlayerEventListener.java index 76da56c..c667e09 100644 --- a/src/main/java/com/github/fabricservertools/htm/listeners/PlayerEventListener.java +++ b/src/main/java/com/github/fabricservertools/htm/listeners/PlayerEventListener.java @@ -10,9 +10,15 @@ import com.github.fabricservertools.htm.interactions.InteractionManager; import net.fabricmc.fabric.api.event.player.AttackBlockCallback; import net.fabricmc.fabric.api.event.player.PlayerBlockBreakEvents; +import net.fabricmc.fabric.api.transfer.v1.item.InventoryStorage; +import net.fabricmc.fabric.impl.transfer.item.InventoryStorageImpl; import net.minecraft.block.BlockState; +import net.minecraft.block.ComparatorBlock; +import net.minecraft.block.InventoryProvider; import net.minecraft.block.entity.BlockEntity; import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.inventory.Inventory; +import net.minecraft.inventory.InventoryChangedListener; import net.minecraft.item.ItemPlacementContext; import net.minecraft.registry.Registries; import net.minecraft.server.network.ServerPlayerEntity; @@ -23,7 +29,6 @@ import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; import net.minecraft.world.World; - import java.util.Optional; public class PlayerEventListener { @@ -31,6 +36,7 @@ public static void init() { PlayerPlaceBlockCallback.EVENT.register(PlayerEventListener::onPlace); PlayerBlockBreakEvents.BEFORE.register(PlayerEventListener::onBeforeBreak); AttackBlockCallback.EVENT.register(PlayerEventListener::onAttackBlock); + } private static ActionResult onAttackBlock(PlayerEntity player, World world, Hand hand, BlockPos pos, Direction direction) { diff --git a/src/main/java/com/github/fabricservertools/htm/locks/PrivateLock.java b/src/main/java/com/github/fabricservertools/htm/locks/PrivateLock.java index c9874d8..c6af1f0 100644 --- a/src/main/java/com/github/fabricservertools/htm/locks/PrivateLock.java +++ b/src/main/java/com/github/fabricservertools/htm/locks/PrivateLock.java @@ -12,7 +12,7 @@ public class PrivateLock implements Lock { public boolean canOpen(ServerPlayerEntity player, HTMContainerLock lock) { if (lock.isTrusted(player.getUuid())) return true; - if (Utility.getLockGroupState(player.server).isTrusted(lock.getGroups(), player.getUuid())) { + if (Utility.getLockGroupState(player.server).isTrusted(lock.getGroupIds(), player.getUuid())) { return true; } diff --git a/src/main/java/com/github/fabricservertools/htm/world/data/LockGroupState.java b/src/main/java/com/github/fabricservertools/htm/world/data/LockGroupState.java index 79069da..a1787de 100644 --- a/src/main/java/com/github/fabricservertools/htm/world/data/LockGroupState.java +++ b/src/main/java/com/github/fabricservertools/htm/world/data/LockGroupState.java @@ -1,7 +1,7 @@ package com.github.fabricservertools.htm.world.data; -import com.github.fabricservertools.htm.api.Group; -import com.github.fabricservertools.htm.api.LockGroup; +import com.github.fabricservertools.htm.api.ProtectionGroup; +import com.github.fabricservertools.htm.api.LockProtectionGroup; import com.google.common.collect.*; import net.minecraft.nbt.*; import net.minecraft.world.PersistentState; @@ -15,7 +15,7 @@ * Describes a persistent state which stores groups. */ public class LockGroupState extends PersistentState { - private final HashMap groups; + private final HashMap groups; private static final String LockGroups = "LockGroups"; private static final String LockGroupId = "LockGroupId"; @@ -32,20 +32,20 @@ public LockGroupState() { public NbtCompound writeNbt(NbtCompound nbt) { NbtList trustList = new NbtList(); - for (Group group : groups.values()) { + for (ProtectionGroup protectionGroup : groups.values()) { NbtCompound groupTag = new NbtCompound(); - groupTag.putUuid(LockGroupId, group.getId()); - groupTag.putUuid(LockGroupOwner, group.getOwner()); - groupTag.putString(LockGroupName, group.getName()); + groupTag.putUuid(LockGroupId, protectionGroup.getId()); + groupTag.putUuid(LockGroupOwner, protectionGroup.getOwner()); + groupTag.putString(LockGroupName, protectionGroup.getName()); NbtList trustedUsers = new NbtList(); - for (UUID userId : group.getMembers()) { + for (UUID userId : protectionGroup.getMembers()) { trustedUsers.add(NbtHelper.fromUuid(userId)); } groupTag.put(LockGroupList, trustedUsers); NbtList managers = new NbtList(); - for (UUID userId : group.getManagers()) { + for (UUID userId : protectionGroup.getManagers()) { managers.add(NbtHelper.fromUuid(userId)); } groupTag.put(LockGroupManagers, managers); @@ -79,7 +79,7 @@ public static LockGroupState fromNbt(NbtCompound tag) { managers.add(NbtHelper.toUuid(value)); } - lockGroupState.groups.put(groupId, new LockGroup(groupId, ownerId, trusted, managers, groupName)); + lockGroupState.groups.put(groupId, new LockProtectionGroup(groupId, ownerId, trusted, managers, groupName)); }); return lockGroupState; @@ -87,13 +87,13 @@ public static LockGroupState fromNbt(NbtCompound tag) { public boolean isTrusted(Set groupIds, UUID playerId) { return groupIds.stream().anyMatch(it -> { - Group group = groups.get(it); - if (group == null) { + ProtectionGroup protectionGroup = groups.get(it); + if (protectionGroup == null) { return false; } - if (group.getMembers().contains(playerId)) return true; - if (group.getManagers().contains(playerId)) return true; + if (protectionGroup.getMembers().contains(playerId)) return true; + if (protectionGroup.getManagers().contains(playerId)) return true; return false; }); } @@ -107,12 +107,12 @@ public boolean isManager(UUID groupId, UUID manager) { } public boolean addToGroup(UUID groupId, UUID trusted) { - Group group = groups.get(groupId); - if (group == null) { + ProtectionGroup protectionGroup = groups.get(groupId); + if (protectionGroup == null) { return false; } - if (group.addMember(trusted)) { + if (protectionGroup.addMember(trusted)) { markDirty(); return true; } @@ -128,26 +128,26 @@ public boolean removeFromGroup(UUID group, UUID trusted) { return false; } - public boolean addGroup(Group group) { - if (groups.containsKey(group.getId())) { + public boolean addGroup(ProtectionGroup protectionGroup) { + if (groups.containsKey(protectionGroup.getId())) { return false; } - groups.put(group.getId(), group); + groups.put(protectionGroup.getId(), protectionGroup); markDirty(); return true; } - public boolean removeGroup(Group group) { - if (groups.containsKey(group.getId())) { - groups.remove(group.getId()); + public boolean removeGroup(ProtectionGroup protectionGroup) { + if (groups.containsKey(protectionGroup.getId())) { + groups.remove(protectionGroup.getId()); markDirty(); return true; } return false; } - public ImmutableMap getGroups() { + public ImmutableMap getGroups() { return ImmutableMap.copyOf(groups); } }