Skip to content
Merged
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
17 changes: 3 additions & 14 deletions src/main/java/cc/aabss/eventutils/EventUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import cc.aabss.eventutils.commands.CommandRegister;
import cc.aabss.eventutils.config.EUConfig;
import cc.aabss.eventutils.config.EventType;
import cc.aabss.eventutils.config.Group;
import cc.aabss.eventutils.config.migrations.*;
import cc.aabss.eventutils.config.serdes.EntityTypeSerializer;
import cc.aabss.eventutils.config.serdes.EventSettingsSerializer;
import cc.aabss.eventutils.config.serdes.GroupSerializer;
import cc.aabss.eventutils.config.serdes.GroupsSerializer;
import cc.aabss.eventutils.discordrpc.DiscordRPC;
import cc.aabss.eventutils.manager.AuthManager;
import cc.aabss.eventutils.manager.DiscordLinkManager;
Expand Down Expand Up @@ -120,7 +120,8 @@ public EventUtils() {
// Custom serdes
new EntityTypeSerializer(),
new EventSettingsSerializer(),
new GroupSerializer());
new GroupSerializer(),
new GroupsSerializer());

opt.bindFile(new File(FabricLoader.getInstance().getConfigDir().toFile(), "eventutils.json"));
opt.removeOrphans(true);
Expand All @@ -137,18 +138,6 @@ public EventUtils() {

// Clamp event_server_display_minutes
config.setEventServerDisplayMinutes(config.event_server_display_minutes);
// Add UUIDs to Groups
for (final Map.Entry<UUID, Group> entry : config.groups.entrySet()) entry.getValue().setUuid(entry.getKey());
// Validate groups (unique names)
boolean updated = false;
final Set<String> groupNames = new HashSet<>();
for (final Group group : new HashSet<>(config.groups.values())) {
if (groupNames.add(group.getName().toLowerCase())) continue;
EventUtils.LOGGER.error("Removing duplicate group: {}", group);
config.groups.remove(group.getUuid());
updated = true;
}
if (updated) config.save(); // Save if a group was removed

// Need to wait for config to be loaded
updateLogLevel();
Expand Down
32 changes: 4 additions & 28 deletions src/main/java/cc/aabss/eventutils/config/EUConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.spi.StandardLevel;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Range;

import java.util.*;
Expand Down Expand Up @@ -42,7 +41,7 @@ public class EUConfig extends OkaeriConfig {
public int event_server_display_minutes = Defaults.EVENT_SERVER_DISPLAY_MINUTES;

@Stat
@NotNull public Map<UUID, Group> groups = Defaults.groups();
@NotNull public Groups groups = Defaults.groups();

@Stat
@NotNull public Map<EventType, EventSettings> event_settings = Defaults.eventSettings();
Expand All @@ -54,29 +53,6 @@ public class EUConfig extends OkaeriConfig {
@NotNull public StandardLevel log_level = Defaults.LOG_LEVEL;


@NotNull
public List<String> getGroupNames() {
return groups.values().stream()
.map(Group::getName)
.toList();
}

@Nullable
public Group getGroupByName(@NotNull String name) {
return groups.values().stream()
.filter(group -> group.getName().equalsIgnoreCase(name))
.findFirst()
.orElse(null);
}

/**
* If a group with the same UUID already exists, it will be replaced
*/
public void upsertGroup(@NotNull Group group) {
groups.put(group.getUuid(), group);
save();
}

@NotNull
public EventSettings getEventSettings(@NotNull EventType type) {
return event_settings.getOrDefault(type, new EventSettings());
Expand Down Expand Up @@ -106,7 +82,7 @@ public static class Defaults {
@NotNull public static final String DEFAULT_FAMOUS_IP = "play.invadedlands.net";
public static final boolean BEE_ICONS = true;
public static final int EVENT_SERVER_DISPLAY_MINUTES = 5;
@NotNull private static final Map<UUID, Group> GROUPS = Map.of(UUID.randomUUID(), new Group().setName("Hide All Players"));
@NotNull private static final Groups GROUPS = new Groups(new Group().setName("Hide All Players"));

@NotNull private static final Map<EventType, EventSettings> EVENT_SETTINGS = Arrays.stream(EventType.values())
.collect(Collectors.toMap(type -> type, type -> new EventSettings(), (a, b) -> a, () -> new EnumMap<>(EventType.class)));
Expand All @@ -115,8 +91,8 @@ public static class Defaults {
@NotNull public static final StandardLevel LOG_LEVEL = Level.INFO.getStandardLevel();

@NotNull
public static Map<UUID, Group> groups() {
return new HashMap<>(GROUPS);
public static Groups groups() {
return new Groups(GROUPS);
}
@NotNull
public static Map<EventType, EventSettings> eventSettings() {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/cc/aabss/eventutils/config/Group.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ public Group() {
this.uuid = UUID.randomUUID();
}

@Override
public int hashCode() {
return uuid.hashCode();
}

@Override
public boolean equals(@Nullable Object obj) {
if (this == obj) return true;
if (!(obj instanceof Group group)) return false;
return uuid.equals(group.uuid);
}

@NotNull
public UUID getUuid() {
return uuid;
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/cc/aabss/eventutils/config/Groups.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package cc.aabss.eventutils.config;

import cc.aabss.eventutils.stats.Statable;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;


public class Groups implements Statable {
@NotNull public final List<Group> groups;

public Groups(@NotNull Map<UUID, Group> groups) {
this.groups = new ArrayList<>(groups.values());
}

public Groups(@NotNull Group @NotNull ... groups) {
this.groups = new ArrayList<>(List.of(groups));
}

public Groups(@NotNull Groups groups) {
this.groups = new ArrayList<>(groups.groups);
}

@NotNull
public Map<UUID, Group> toMap() {
return groups.stream().collect(Collectors.toMap(Group::getUuid, group -> group));
}

@Nullable
public Group getByName(@NotNull String name) {
return groups.stream()
.filter(group -> group.getName().equalsIgnoreCase(name))
.findFirst()
.orElse(null);
}

public void remove(@NotNull UUID uuid) {
groups.removeIf(group -> group.getUuid().equals(uuid));
}

@Override @NotNull
public JsonElement toStat() {
final JsonArray array = new JsonArray();
for (final Group group : groups) array.add(group.toStat());
return array;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cc.aabss.eventutils.config.serdes;

import cc.aabss.eventutils.EventUtils;
import cc.aabss.eventutils.config.Group;
import cc.aabss.eventutils.config.Groups;
import eu.okaeri.configs.schema.GenericsDeclaration;
import eu.okaeri.configs.serdes.DeserializationData;
import eu.okaeri.configs.serdes.ObjectSerializer;
import eu.okaeri.configs.serdes.SerializationData;
import org.jetbrains.annotations.NotNull;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;


public class GroupsSerializer implements ObjectSerializer<Groups> {
@Override
public boolean supports(@NotNull Class<?> type) {
return Groups.class.isAssignableFrom(type);
}

@Override
public void serialize(@NotNull Groups object, @NotNull SerializationData data, @NotNull GenericsDeclaration generics) {
data.setValue(object.toMap(), GenericsDeclaration.of(Map.class, List.of(UUID.class, Group.class)));
}

@Override @NotNull
public Groups deserialize(@NotNull DeserializationData data, @NotNull GenericsDeclaration generics) {
final Map<UUID, Group> map = data.getValueDirect(GenericsDeclaration.of(Map.class, List.of(UUID.class, Group.class)));

// Add UUIDs to Groups
for (final Map.Entry<UUID, Group> entry : map.entrySet()) entry.getValue().setUuid(entry.getKey());

// Validate (unique names)
final Set<String> groupNames = new HashSet<>();
for (final Group group : map.values()) {
if (groupNames.add(group.getName().toLowerCase())) continue;
EventUtils.LOGGER.error("Removing duplicate group: {}", group);
map.remove(group.getUuid());
}

return new Groups(map);
}
}
48 changes: 11 additions & 37 deletions src/main/java/cc/aabss/eventutils/manager/GroupManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,22 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Iterator;
import java.util.UUID;


public class GroupManager {
@NotNull private final EventUtils mod;

@Nullable public UUID selectedGroup;
@NotNull public Iterator<UUID> groupIterator;
@Nullable public Group selectedGroup;

public GroupManager(@NotNull EventUtils mod) {
this.mod = mod;
resetIterator();
}

public boolean isAllPlayersRevealed() {
return selectedGroup == null;
}

@Nullable
public Group getSelectedGroup() {
return selectedGroup != null ? mod.config.groups.get(selectedGroup) : null;
}

private void resetIterator() {
groupIterator = mod.config.groups.keySet().iterator();
}

/**
* Revealed -> group 1 -> group 2 -> revealed -> repeat
*/
public void cycle() {
if (selectedGroup == null) resetIterator();

if (groupIterator.hasNext()) {
selectedGroup = groupIterator.next();
} else {
selectedGroup = null;
}
final int nextIndex = selectedGroup == null ? 0 : mod.config.groups.groups.indexOf(selectedGroup) + 1;
selectedGroup = nextIndex < mod.config.groups.groups.size() ? mod.config.groups.groups.get(nextIndex) : null;
}

private boolean isSelf(@NotNull String name) {
Expand All @@ -58,41 +35,38 @@ private boolean isSelf(@NotNull String name) {

public boolean isPlayerVisible(@NotNull GameProfile profile, @Nullable Vec3 position) {
// All visible
final Group group = getSelectedGroup();
if (group == null) return true;
if (selectedGroup == null) return true;

// Always show self
final VersionedGameProfile versionedProfile = new VersionedGameProfile(profile);
if (isSelf(versionedProfile.getName())) return true;

// NPC
if (EventUtils.isNpc(versionedProfile.getId())) return group.isNpcVisible(position);
if (EventUtils.isNpc(versionedProfile.getId())) return selectedGroup.isNpcVisible(position);

// Delegate to Group
return group.isPlayerVisible(versionedProfile.getName(), position);
return selectedGroup.isPlayerVisible(versionedProfile.getName(), position);
}

public boolean isPlayerVisible(@Nullable String name, @Nullable Vec3 position) {
// All visible
final Group group = getSelectedGroup();
if (group == null) return true;
if (selectedGroup == null) return true;

// Always show self
if (name != null && isSelf(name)) return true;

// NPC
if (EventUtils.isNpc(name)) return group.isNpcVisible(position);
if (EventUtils.isNpc(name)) return selectedGroup.isNpcVisible(position);

// Delegate to Group
return group.isPlayerVisible(name, position);
return selectedGroup.isPlayerVisible(name, position);
}

public boolean isEntityVisible(@NotNull EntityType<?> entityType, @Nullable Vec3 position) {
// All visible
final Group group = getSelectedGroup();
if (group == null) return true;
if (selectedGroup == null) return true;

// Delegate to Group
return group.isEntityVisible(entityType, position);
return selectedGroup.isEntityVisible(entityType, position);
}
}
6 changes: 2 additions & 4 deletions src/main/java/cc/aabss/eventutils/manager/KeybindManager.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cc.aabss.eventutils.manager;

import cc.aabss.eventutils.EventUtils;
import cc.aabss.eventutils.config.Group;
import cc.aabss.eventutils.mixin.KeyMappingAccessor;
import cc.aabss.eventutils.screen.EventInfoScreen;
import cc.aabss.eventutils.sdk.EventWrapper;
Expand Down Expand Up @@ -135,9 +134,8 @@ public KeybindManager(@NotNull EventUtils mod) {

// Action bar
final MutableComponent message;
final Group group = mod.groupManager.getSelectedGroup();
if (group != null) {
message = literal(group.getName());
if (mod.groupManager.selectedGroup != null) {
message = literal(mod.groupManager.selectedGroup.getName());
} else {
message = translatable("eventutils.hideplayers.view_revealed");
}
Expand Down
Loading