Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Feature/protection groups #53

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.fabricservertools.htm;

import com.github.fabricservertools.htm.api.ProtectionGroup;
import com.github.fabricservertools.htm.api.Lock;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.nbt.NbtCompound;
Expand All @@ -11,20 +12,19 @@
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.UUID;
import java.util.*;

public class HTMContainerLock {
private Lock type;
private UUID owner;
private HashSet<UUID> groups;
private HashSet<UUID> trusted;
private Map<String, Boolean> flags;

public HTMContainerLock() {
type = null;
owner = null;
groups = new HashSet<>();
trusted = new HashSet<>();
initFlags();
}
Expand All @@ -48,9 +48,14 @@ public void toTag(NbtCompound tag, RegistryWrapper.WrapperLookup registryLookup)
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<String, Boolean> entry : flags.entrySet()) {
NbtCompound flagTag = new NbtCompound();
Expand All @@ -76,13 +81,17 @@ public void fromTag(NbtCompound tag, RegistryWrapper.WrapperLookup registryLooku
}
type.fromTag(tag.getCompound("TypeData"), registryLookup);
owner = tag.getUuid("Owner");

NbtList trustedTag = tag.getList("Trusted", NbtElement.INT_ARRAY_TYPE);

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", NbtElement.COMPOUND_TYPE);
for (NbtElement flagTag : flagTags) {
NbtCompound compoundTag = (NbtCompound) flagTag;
Expand Down Expand Up @@ -119,6 +128,10 @@ public HashSet<UUID> getTrusted() {
return trusted;
}

public Set<UUID> getGroups() {
return groups;
}

public void setType(Lock type, ServerPlayerEntity owner) {
this.type = type;
this.owner = owner.getUuid();
Expand All @@ -140,6 +153,14 @@ public boolean removeTrust(UUID id) {
return trusted.remove(id);
}

public boolean addGroup(ProtectionGroup protectionGroup) {
return groups.add(protectionGroup.getId());
}

public boolean removeGroup(ProtectionGroup protectionGroup) {
return groups.remove(protectionGroup.getId());
}

public boolean isTrusted(UUID id) {
return trusted.contains(id);
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/github/fabricservertools/htm/Utility.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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;
Expand All @@ -25,6 +26,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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 LockProtectionGroup implements ProtectionGroup {
private final UUID id;
private final UUID owner;
private final HashSet<UUID> players;
private final HashSet<UUID> managers;
private String name;


/**
* Creates a new instance of @see LockGroup.
* @param name The non-unique display name of the group.
*/
public LockProtectionGroup(String name, ServerPlayerEntity owner) {
this.id = UUID.randomUUID();
this.owner = owner.getUuid();
this.players = new HashSet<>();
this.managers = new HashSet<>();
this.name = name;
}

public LockProtectionGroup(UUID id, UUID owner, HashSet<UUID> players, HashSet<UUID> 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<UUID> 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<UUID> 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 LockProtectionGroup lockGroup)) {
return false;
}
return Objects.equals(id, lockGroup.id);
}

@Override
public int hashCode() {
return Objects.hashCode(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.github.fabricservertools.htm.api;

import java.util.Set;
import java.util.UUID;

/**
* Describes a group which is able to be applied to protection and holds users.
*/
public interface ProtectionGroup {
/**
* 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<UUID> 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<UUID> 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();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.github.fabricservertools.htm.command.arguments;

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<ProtectionGroup> {
@Override
public ProtectionGroup parse(StringReader reader) throws CommandSyntaxException {
return null;
}
}
Loading