Skip to content

Commit

Permalink
Rename HolderSet to GeyserHolderSet and address reviews by camotoy
Browse files Browse the repository at this point in the history
  • Loading branch information
eclipseisoffline committed Oct 22, 2024
1 parent b5be37c commit 3e6bae4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import org.geysermc.geyser.registry.Registries;
import org.geysermc.geyser.session.cache.registry.JavaRegistries;
import org.geysermc.geyser.session.cache.registry.RegistryEntryContext;
import org.geysermc.geyser.session.cache.tags.HolderSet;
import org.geysermc.geyser.session.cache.tags.GeyserHolderSet;
import org.geysermc.geyser.translator.text.MessageTranslator;

import java.util.HashSet;
Expand All @@ -46,23 +46,23 @@
*/
public record Enchantment(String identifier,
Set<EnchantmentComponent> effects,
HolderSet<Item> supportedItems,
GeyserHolderSet<Item> supportedItems,
int maxLevel,
String description,
int anvilCost,
HolderSet<Enchantment> exclusiveSet,
GeyserHolderSet<Enchantment> exclusiveSet,
@Nullable BedrockEnchantment bedrockEnchantment) {

public static Enchantment read(RegistryEntryContext context) {
NbtMap data = context.data();
Set<EnchantmentComponent> effects = readEnchantmentComponents(data.getCompound("effects"));

HolderSet<Item> supportedItems = HolderSet.readHolderSet(JavaRegistries.ITEM, data.get("supported_items"), itemId -> Registries.JAVA_ITEM_IDENTIFIERS.getOrDefault(itemId.asString(), Items.AIR).javaId());
GeyserHolderSet<Item> supportedItems = GeyserHolderSet.readHolderSet(JavaRegistries.ITEM, data.get("supported_items"), itemId -> Registries.JAVA_ITEM_IDENTIFIERS.getOrDefault(itemId.asString(), Items.AIR).javaId());

int maxLevel = data.getInt("max_level");
int anvilCost = data.getInt("anvil_cost");

HolderSet<Enchantment> exclusiveSet = HolderSet.readHolderSet(JavaRegistries.ENCHANTMENT, data.get("exclusive_set"), context::getNetworkId);
GeyserHolderSet<Enchantment> exclusiveSet = GeyserHolderSet.readHolderSet(JavaRegistries.ENCHANTMENT, data.get("exclusive_set"), context::getNetworkId);

BedrockEnchantment bedrockEnchantment = BedrockEnchantment.getByJavaIdentifier(context.id().asString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

package org.geysermc.geyser.session.cache;

import it.unimi.dsi.fastutil.ints.IntArrays;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.kyori.adventure.key.Key;
import org.geysermc.geyser.GeyserLogger;
Expand All @@ -33,7 +34,7 @@
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.session.cache.registry.JavaRegistries;
import org.geysermc.geyser.session.cache.registry.JavaRegistryKey;
import org.geysermc.geyser.session.cache.tags.HolderSet;
import org.geysermc.geyser.session.cache.tags.GeyserHolderSet;
import org.geysermc.geyser.session.cache.tags.Tag;
import org.geysermc.geyser.util.MinecraftKey;
import org.geysermc.mcprotocollib.protocol.packet.common.clientbound.ClientboundUpdateTagsPacket;
Expand Down Expand Up @@ -113,7 +114,7 @@ public boolean is(Tag<Item> tag, GeyserItemStack itemStack) {
/**
* @return true if the specified network ID is in the given holder set.
*/
public <T> boolean is(HolderSet<T> holderSet, T object) {
public <T> boolean is(GeyserHolderSet<T> holderSet, T object) {
return contains(holderSet.resolveRaw(this), holderSet.getRegistry().toNetworkId(session, object));
}

Expand All @@ -125,7 +126,7 @@ public <T> List<T> get(Tag<T> tag) {
* @return the network IDs in the given tag. This can be an empty list.
*/
public int[] getRaw(Tag<?> tag) {
return this.tags.getOrDefault(tag, new int[]{});
return this.tags.getOrDefault(tag, IntArrays.EMPTY_ARRAY);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
package org.geysermc.geyser.session.cache.tags;

import java.util.List;
import java.util.Objects;
import java.util.function.Function;

import it.unimi.dsi.fastutil.ints.IntArrays;
import lombok.Data;
import net.kyori.adventure.key.Key;
import org.checkerframework.checker.nullness.qual.NonNull;
Expand All @@ -40,23 +43,23 @@
*
* <p>Because HolderSets utilise tags, when loading a HolderSet, Geyser must store tags for the registry the HolderSet is for (see {@link JavaRegistryKey}).</p>
*
* <p>Use the {@link HolderSet#readHolderSet} method to easily read a HolderSet from NBT sent by a server. To turn the HolderSet into a list of network IDs, use the {@link HolderSet#resolveRaw} method.
* To turn the HolderSet into a list of objects, use the {@link HolderSet#resolve} method.</p>
* <p>Use the {@link GeyserHolderSet#readHolderSet} method to easily read a HolderSet from NBT sent by a server. To turn the HolderSet into a list of network IDs, use the {@link GeyserHolderSet#resolveRaw} method.
* To turn the HolderSet into a list of objects, use the {@link GeyserHolderSet#resolve} method.</p>
*/
@Data
public final class HolderSet<T> {
public final class GeyserHolderSet<T> {

private final JavaRegistryKey<T> registry;
private final @Nullable Tag<T> tag;
private final int @Nullable [] holders;

public HolderSet(JavaRegistryKey<T> registry, int @NonNull [] holders) {
public GeyserHolderSet(JavaRegistryKey<T> registry, int @NonNull [] holders) {
this.registry = registry;
this.tag = null;
this.holders = holders;
}

public HolderSet(JavaRegistryKey<T> registry, @NonNull Tag<T> tagId) {
public GeyserHolderSet(JavaRegistryKey<T> registry, @NonNull Tag<T> tagId) {
this.registry = registry;
this.tag = tagId;
this.holders = null;
Expand All @@ -81,8 +84,7 @@ public int[] resolveRaw(TagCache tagCache) {
return holders;
}

assert tag != null;
return tagCache.getRaw(tag);
return tagCache.getRaw(Objects.requireNonNull(tag, "HolderSet must have a tag if it doesn't have a list of IDs"));
}

/**
Expand All @@ -92,22 +94,22 @@ public int[] resolveRaw(TagCache tagCache) {
* @param holderSet the HolderSet as an object from NBT.
* @param keyIdMapping a function that maps resource location IDs in the HolderSet's registry to their network IDs.
*/
public static <T> HolderSet<T> readHolderSet(JavaRegistryKey<T> registry, @Nullable Object holderSet, Function<Key, Integer> keyIdMapping) {
public static <T> GeyserHolderSet<T> readHolderSet(JavaRegistryKey<T> registry, @Nullable Object holderSet, Function<Key, Integer> keyIdMapping) {
if (holderSet == null) {
return new HolderSet<>(registry, new int[]{});
return new GeyserHolderSet<>(registry, new int[]{});
}

if (holderSet instanceof String stringTag) {
if (stringTag.startsWith("#")) {
// Tag
return new HolderSet<>(registry, new Tag<>(registry, Key.key(stringTag.substring(1)))); // Remove '#' at beginning that indicates tag
return new GeyserHolderSet<>(registry, new Tag<>(registry, Key.key(stringTag.substring(1)))); // Remove '#' at beginning that indicates tag
} else if (stringTag.isEmpty()) {
return new HolderSet<>(registry, new int[]{});
return new GeyserHolderSet<>(registry, IntArrays.EMPTY_ARRAY);
}
return new HolderSet<>(registry, new int[]{keyIdMapping.apply(Key.key(stringTag))});
return new GeyserHolderSet<>(registry, new int[]{keyIdMapping.apply(Key.key(stringTag))});
} else if (holderSet instanceof List<?> list) {
// Assume the list is a list of strings
return new HolderSet<>(registry, list.stream().map(o -> (String) o).map(Key::key).map(keyIdMapping).mapToInt(Integer::intValue).toArray());
return new GeyserHolderSet<>(registry, list.stream().map(o -> (String) o).map(Key::key).map(keyIdMapping).mapToInt(Integer::intValue).toArray());
}
throw new IllegalArgumentException("Holder set must either be a tag, a string ID or a list of string IDs");
}
Expand Down

0 comments on commit 3e6bae4

Please sign in to comment.