Skip to content

Commit

Permalink
Merge pull request #256 from Dragon-Seeker/1.21-RegistryFixes
Browse files Browse the repository at this point in the history
[1.21] Reimplement the ability to wrap ItemGroups and adjust RegistryAccess to fix #252
  • Loading branch information
gliscowo authored Jul 21, 2024
2 parents b8ba7f1 + 3f562f6 commit 01a30ec
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 92 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package io.wispforest.owo.itemgroup.json;

import com.mojang.serialization.Lifecycle;
import io.wispforest.owo.itemgroup.Icon;
import io.wispforest.owo.itemgroup.OwoItemGroup;
import io.wispforest.owo.itemgroup.gui.ItemGroupButton;
import io.wispforest.owo.itemgroup.gui.ItemGroupTab;
import io.wispforest.owo.mixin.itemgroup.ItemGroupAccessor;
import io.wispforest.owo.mixin.ui.SimpleRegistryAccessor;
import io.wispforest.owo.util.pond.OwoSimpleRegistryExtensions;
import net.minecraft.item.ItemGroup;
import net.minecraft.registry.Registries;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.SimpleRegistry;
import net.minecraft.registry.entry.RegistryEntryInfo;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.ApiStatus;

Expand All @@ -34,9 +33,7 @@ public WrapperGroup(ItemGroup parent, Identifier parentId, List<ItemGroupTab> ta

int parentRawId = Registries.ITEM_GROUP.getRawId(parent);

// TODO: set doesn't exist anymore. figure out what to do.
// ((SimpleRegistryAccessor<ItemGroup>) Registries.ITEM_GROUP).owo$getValueToEntry().remove(parent);
// ((SimpleRegistry<ItemGroup>) Registries.ITEM_GROUP).set(parentRawId, RegistryKey.of(RegistryKeys.ITEM_GROUP, parentId), this, Lifecycle.stable());
((OwoSimpleRegistryExtensions<ItemGroup>) Registries.ITEM_GROUP).owo$set(parentRawId, RegistryKey.of(RegistryKeys.ITEM_GROUP, parentId), this, RegistryEntryInfo.DEFAULT);

((ItemGroupAccessor) this).owo$setDisplayName(parent.getDisplayName());
((ItemGroupAccessor) this).owo$setColumn(parent.getColumn());
Expand Down
34 changes: 0 additions & 34 deletions src/main/java/io/wispforest/owo/mixin/SimpleRegistryMixin.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.wispforest.owo.mixin.registry;

import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.entry.RegistryEntry;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;

@Mixin(RegistryEntry.Reference.class)
public interface ReferenceAccessor<T> {
@Invoker("setRegistryKey")
void owo$setRegistryKey(RegistryKey<T> registryKey);

@Invoker("setValue")
void owo$setValue(T value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package io.wispforest.owo.mixin.registry;

import com.mojang.serialization.Lifecycle;
import io.wispforest.owo.util.OwoFreezer;
import io.wispforest.owo.util.pond.OwoSimpleRegistryExtensions;
import it.unimi.dsi.fastutil.objects.ObjectList;
import it.unimi.dsi.fastutil.objects.Reference2IntMap;
import net.fabricmc.fabric.api.event.registry.RegistryEntryAddedCallback;
import net.minecraft.registry.MutableRegistry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.SimpleRegistry;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.registry.entry.RegistryEntryInfo;
import net.minecraft.util.Identifier;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Mixin(SimpleRegistry.class)
public abstract class SimpleRegistryMixin<T> implements MutableRegistry<T>, OwoSimpleRegistryExtensions<T> {

@Shadow private Map<T, RegistryEntry.Reference<T>> intrusiveValueToEntry;
@Shadow @Final private Map<RegistryKey<T>, RegistryEntry.Reference<T>> keyToEntry;
@Shadow @Final private Map<Identifier, RegistryEntry.Reference<T>> idToEntry;
@Shadow @Final private Map<T, RegistryEntry.Reference<T>> valueToEntry;
@Shadow @Final private ObjectList<RegistryEntry.Reference<T>> rawIdToEntry;
@Shadow @Final private Reference2IntMap<T> entryToRawId;
@Shadow @Final private Map<RegistryKey<T>, RegistryEntryInfo> keyToEntryInfo;
@Shadow private Lifecycle lifecycle;

//--

/**
* Copy of the {@link SimpleRegistry#add} function but uses {@link List#set} instead of {@link List#add} for {@link SimpleRegistry#rawIdToEntry}
*/
public RegistryEntry.Reference<T> owo$set(int id, RegistryKey<T> arg, T object, RegistryEntryInfo arg2) {
this.valueToEntry.remove(object);

OwoFreezer.checkRegister("Registry Set Calls"); //this.assertNotFrozen(arg);

Objects.requireNonNull(arg);
Objects.requireNonNull(object);

RegistryEntry.Reference<T> reference;

if (this.intrusiveValueToEntry != null) {
reference = this.intrusiveValueToEntry.remove(object);

if (reference == null) {
throw new AssertionError("Missing intrusive holder for " + arg + ":" + object);
}

((ReferenceAccessor<T>) reference).owo$setRegistryKey(arg);
} else {
reference = this.keyToEntry.computeIfAbsent(arg, k -> RegistryEntry.Reference.standAlone(this.getEntryOwner(), k));
((ReferenceAccessor<T>) reference).owo$setValue((T)object);
}

this.keyToEntry.put(arg, reference);
this.idToEntry.put(arg.getValue(), reference);
this.valueToEntry.put(object, reference);
this.rawIdToEntry.set(id, reference);
this.entryToRawId.put(object, id);
this.keyToEntryInfo.put(arg, arg2);
this.lifecycle = this.lifecycle.add(arg2.lifecycle());

// TODO: SHOULD WE BE REFIREING THE EVENT?
RegistryEntryAddedCallback.event(this).invoker().onEntryAdded(id, arg.getValue(), (T)object);

return reference;
}
}

This file was deleted.

39 changes: 6 additions & 33 deletions src/main/java/io/wispforest/owo/util/RegistryAccess.java
Original file line number Diff line number Diff line change
@@ -1,56 +1,29 @@
package io.wispforest.owo.util;

import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.util.Identifier;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.registry.SimpleRegistry;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

@Deprecated(forRemoval = true)
public final class RegistryAccess {

private RegistryAccess() {}

/**
* Gets a {@link RegistryEntry} from its id
*
* @param registry The registry to operate on. Must be a {@link SimpleRegistry} at some point in the hierarchy
* @param id The id to use
* @param <T> The type of the registry and returned entry
* @return The entry, or {@code null} if it's not present
* @deprecated Use {@link Registry#getEntry(Identifier)}
*/
@Nullable
@SuppressWarnings("unchecked")
public static <T> RegistryEntry<T> getEntry(Registry<T> registry, Identifier id) {
checkSimple(registry);
return ((AccessibleRegistry<T>) registry).getEntry(id);
return registry.getEntry(id).orElse(null);
}

/**
* Gets a {@link RegistryEntry} from its value
*
* @param registry The registry to operate on. Must be a {@link SimpleRegistry} at some point in the hierarchy
* @param value The value to use
* @param <T> The type of the registry and returned entry
* @return The entry, or {@code null} if it's not present
* @deprecated Use {@link Registry#getEntry(T)}
*/
@Nullable
@SuppressWarnings("unchecked")
public static <T> RegistryEntry<T> getEntry(Registry<T> registry, T value) {
checkSimple(registry);
return ((AccessibleRegistry<T>) registry).getEntry(value);
return registry.getEntry(value);
}

private static void checkSimple(Registry<?> registry) {
if (registry instanceof SimpleRegistry<?>) return;
throw new IllegalArgumentException("[RegistryAccess] Tried to operate on Registry of class '"
+ registry.getClass() + "', but only 'SimpleRegistry' and descendants are supported");
}

public interface AccessibleRegistry<T> {
@Nullable RegistryEntry<T> getEntry(Identifier id);

@Nullable RegistryEntry<T> getEntry(T value);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.wispforest.owo.util.pond;

import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.registry.entry.RegistryEntryInfo;
import org.jetbrains.annotations.ApiStatus;

public interface OwoSimpleRegistryExtensions<T> {

@ApiStatus.Internal
RegistryEntry.Reference<T> owo$set(int id, RegistryKey<T> arg, T object, RegistryEntryInfo arg2);
}
4 changes: 2 additions & 2 deletions src/main/resources/owo.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"ServerPlayerEntityMixin",
"ServerPlayerInteractionManagerMixin",
"SetComponentsLootFunctionAccessor",
"SimpleRegistryMixin",
"TagGroupLoaderMixin",
"itemgroup.ItemGroupAccessor",
"itemgroup.ItemSettingsMixin",
Expand All @@ -31,13 +30,14 @@
"offline.WorldSaveHandlerMixin",
"recipe_remainders.CraftingResultSlotMixin",
"recipe_remainders.RecipeManagerMixin",
"registry.SimpleRegistryMixin",
"registry.ReferenceAccessor",
"text.LanguageMixin",
"text.TextCodecsMixin",
"text.TranslatableTextContentMixin",
"text.stapi.SystemDelegatedLanguageFixin",
"tweaks.EulaReaderMixin",
"tweaks.LevelInfoMixin",
"ui.SimpleRegistryAccessor",
"ui.SlotAccessor",
"ui.SlotMixin",
"ui.access.BlockEntityAccessor"
Expand Down

0 comments on commit 01a30ec

Please sign in to comment.