Skip to content

Commit

Permalink
Allow duplicate entries after being frozen on the client.
Browse files Browse the repository at this point in the history
  • Loading branch information
modmuss50 committed Aug 4, 2024
1 parent eccb723 commit 78a12d5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.mixin.registry.sync;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

import net.minecraft.registry.SimpleRegistry;

@Mixin(SimpleRegistry.class)
public interface SimpleRegistryAccessor {
@Accessor
boolean isFrozen();
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import net.minecraft.registry.MutableRegistry;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
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 net.fabricmc.api.EnvType;
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.fabricmc.fabric.api.event.registry.RegistryAttribute;
Expand All @@ -66,6 +68,7 @@
import net.fabricmc.fabric.impl.registry.sync.RemapException;
import net.fabricmc.fabric.impl.registry.sync.RemapStateImpl;
import net.fabricmc.fabric.impl.registry.sync.RemappableRegistry;
import net.fabricmc.loader.api.FabricLoader;

@Mixin(SimpleRegistry.class)
public abstract class SimpleRegistryMixin<T> implements MutableRegistry<T>, RemappableRegistry, ListenableRegistry<T> {
Expand All @@ -74,6 +77,9 @@ public abstract class SimpleRegistryMixin<T> implements MutableRegistry<T>, Rema
@Unique
private static final Set<String> VANILLA_NAMESPACES = Set.of("minecraft", "brigadier");

@Unique
private static final Logger LOGGER = LoggerFactory.getLogger("FabricRegistrySync");

@Shadow
@Final
private ObjectList<RegistryEntry.Reference<T>> rawIdToEntry;
Expand Down Expand Up @@ -381,9 +387,16 @@ public void unmap(String name) throws RemapException {
}
}

// Actually throw the exception when a duplicate is found.
@ModifyExpressionValue(method = "add", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/Util;throwOrPause(Ljava/lang/Throwable;)Ljava/lang/Throwable;"))
private <E extends Throwable> E throwOnDuplicate(E t) throws E {
// I hate this as much as you do, blame Hypixel for sending duplicate entries to the client via dynamic registries.
if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT
&& ((SimpleRegistryAccessor) Registries.REGISTRIES).isFrozen()) {
LOGGER.error("Exception caught when adding entry to registry. This is likely a server or mod issue.", t);
return t;
}

// Actually throw the exception when a duplicate is found, before the registries are frozen
throw t;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"RegistryLoaderMixin",
"SaveLoadingMixin",
"SerializableRegistriesMixin",
"SimpleRegistryAccessor",
"SimpleRegistryMixin"
],
"injectors": {
Expand Down

0 comments on commit 78a12d5

Please sign in to comment.