Skip to content

Commit

Permalink
Allow Synchronized Registry to declare themselves not to be synchronized
Browse files Browse the repository at this point in the history
  • Loading branch information
thecatcore committed May 8, 2024
1 parent 4caee9f commit da6916f
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,20 @@
import org.spongepowered.asm.mixin.Unique;

import net.minecraft.class_2929;
import net.minecraft.util.registry.MutableRegistry;
import net.minecraft.util.registry.SimpleRegistry;

import net.legacyfabric.fabric.api.event.Event;
import net.legacyfabric.fabric.api.event.EventFactory;
import net.legacyfabric.fabric.api.registry.v2.event.RegistryRemapCallback;
import net.legacyfabric.fabric.api.registry.v2.registry.holder.SyncedRegistry;
import net.legacyfabric.fabric.api.registry.v2.registry.registrable.DesynchronizeableRegistrable;
import net.legacyfabric.fabric.api.registry.v2.registry.registrable.IdsHolder;
import net.legacyfabric.fabric.api.registry.v2.registry.registrable.SyncedRegistrable;
import net.legacyfabric.fabric.api.util.Identifier;

@Mixin(SimpleRegistry.class)
public abstract class SimpleRegistryMixin<K, V> implements SyncedRegistry<V>, SyncedRegistrable<V> {
public abstract class SimpleRegistryMixin<K, V> implements SyncedRegistry<V>, SyncedRegistrable<V>, DesynchronizeableRegistrable {
// 1.8+
@Shadow
public abstract void add(int id, K identifier, V object);
Expand All @@ -58,10 +60,29 @@ public abstract class SimpleRegistryMixin<K, V> implements SyncedRegistry<V>, Sy
@Shadow
public abstract Object getByRawId(int index);

@Unique
private boolean synchronize = true;

@Override
public void setSynchronize(boolean isSynchronize) {
this.synchronize = isSynchronize;
}

@Override
public boolean canSynchronize() {
return this.synchronize;
}

@Override
public void fabric$register(int rawId, Identifier identifier, V value) {
fabric$getBeforeAddedCallback().invoker().onEntryAdding(rawId, identifier, value);
add(rawId, fabric$toKeyType(identifier), value);

if (this.synchronize) {
add(rawId, fabric$toKeyType(identifier), value);
} else {
((MutableRegistry<K, V>) (Object) this).put(fabric$toKeyType(identifier), value);
}

fabric$getEntryAddedCallback().invoker().onEntryAdded(rawId, identifier, value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,20 @@
import org.spongepowered.asm.mixin.Unique;

import net.minecraft.util.collection.IdList;
import net.minecraft.util.registry.MutableRegistry;
import net.minecraft.util.registry.SimpleRegistry;

import net.legacyfabric.fabric.api.event.Event;
import net.legacyfabric.fabric.api.event.EventFactory;
import net.legacyfabric.fabric.api.registry.v2.event.RegistryRemapCallback;
import net.legacyfabric.fabric.api.registry.v2.registry.holder.SyncedRegistry;
import net.legacyfabric.fabric.api.registry.v2.registry.registrable.DesynchronizeableRegistrable;
import net.legacyfabric.fabric.api.registry.v2.registry.registrable.IdsHolder;
import net.legacyfabric.fabric.api.registry.v2.registry.registrable.SyncedRegistrable;
import net.legacyfabric.fabric.api.util.Identifier;

@Mixin(SimpleRegistry.class)
public abstract class SimpleRegistryMixinV2<V> implements SyncedRegistry<V>, SyncedRegistrable<V> {
public abstract class SimpleRegistryMixinV2<V> implements SyncedRegistry<V>, SyncedRegistrable<V>, DesynchronizeableRegistrable {
// 1.8+
@Shadow
public abstract void add(int id, String identifier, Object object);
Expand All @@ -56,10 +58,29 @@ public abstract class SimpleRegistryMixinV2<V> implements SyncedRegistry<V>, Syn
@Shadow
public abstract String getId(Object par1);

@Unique
private boolean synchronize = true;

@Override
public void setSynchronize(boolean isSynchronize) {
this.synchronize = isSynchronize;
}

@Override
public boolean canSynchronize() {
return this.synchronize;
}

@Override
public void fabric$register(int rawId, Identifier identifier, V value) {
fabric$getBeforeAddedCallback().invoker().onEntryAdding(rawId, identifier, value);
add(rawId, fabric$toKeyType(identifier), value);

if (this.synchronize) {
add(rawId, fabric$toKeyType(identifier), value);
} else {
((MutableRegistry) (Object) this).put(fabric$toKeyType(identifier), value);
}

fabric$getEntryAddedCallback().invoker().onEntryAdded(rawId, identifier, value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,20 @@
import org.spongepowered.asm.mixin.Unique;

import net.minecraft.util.collection.IdList;
import net.minecraft.util.registry.MutableRegistry;
import net.minecraft.util.registry.SimpleRegistry;

import net.legacyfabric.fabric.api.event.Event;
import net.legacyfabric.fabric.api.event.EventFactory;
import net.legacyfabric.fabric.api.registry.v2.event.RegistryRemapCallback;
import net.legacyfabric.fabric.api.registry.v2.registry.holder.SyncedRegistry;
import net.legacyfabric.fabric.api.registry.v2.registry.registrable.DesynchronizeableRegistrable;
import net.legacyfabric.fabric.api.registry.v2.registry.registrable.IdsHolder;
import net.legacyfabric.fabric.api.registry.v2.registry.registrable.SyncedRegistrable;
import net.legacyfabric.fabric.api.util.Identifier;

@Mixin(SimpleRegistry.class)
public abstract class SimpleRegistryMixinV2<K, V> implements SyncedRegistry<V>, SyncedRegistrable<V> {
public abstract class SimpleRegistryMixinV2<K, V> implements SyncedRegistry<V>, SyncedRegistrable<V>, DesynchronizeableRegistrable {
// 1.8+
@Shadow
public abstract void add(int id, K identifier, V object);
Expand All @@ -57,10 +59,29 @@ public abstract class SimpleRegistryMixinV2<K, V> implements SyncedRegistry<V>,
@Final
protected IdList<V> ids;

@Unique
private boolean synchronize = true;

@Override
public void setSynchronize(boolean isSynchronize) {
this.synchronize = isSynchronize;
}

@Override
public boolean canSynchronize() {
return this.synchronize;
}

@Override
public void fabric$register(int rawId, Identifier identifier, V value) {
fabric$getBeforeAddedCallback().invoker().onEntryAdding(rawId, identifier, value);
add(rawId, fabric$toKeyType(identifier), value);

if (this.synchronize) {
add(rawId, fabric$toKeyType(identifier), value);
} else {
((MutableRegistry<K, V>) (Object) this).put(fabric$toKeyType(identifier), value);
}

fabric$getEntryAddedCallback().invoker().onEntryAdded(rawId, identifier, value);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2020 - 2024 Legacy Fabric
* Copyright (c) 2016 - 2022 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.legacyfabric.fabric.api.registry.v2.registry.registrable;

public interface DesynchronizeableRegistrable {
default boolean canSynchronize() {
return true;
}

void setSynchronize(boolean synchronize);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import net.legacyfabric.fabric.api.registry.v2.event.RegistryRemapCallback;
import net.legacyfabric.fabric.api.registry.v2.registry.SyncedRegistrableRegistry;
import net.legacyfabric.fabric.api.registry.v2.registry.holder.Registry;
import net.legacyfabric.fabric.api.registry.v2.registry.registrable.DesynchronizeableRegistrable;
import net.legacyfabric.fabric.api.registry.v2.registry.registrable.Registrable;
import net.legacyfabric.fabric.api.registry.v2.registry.registrable.SyncedRegistrable;
import net.legacyfabric.fabric.api.util.Identifier;
Expand Down Expand Up @@ -88,7 +89,13 @@ public static <T> void registerRegistry(Identifier identifier, Registry<T> holde

if (holder instanceof RegistryIdSetter) ((RegistryIdSetter) holder).fabric$setId(identifier);

if (holder instanceof SyncedRegistrableRegistry) {
boolean remappable = true;

if (holder instanceof DesynchronizeableRegistrable) {
remappable = ((DesynchronizeableRegistrable) holder).canSynchronize();
}

if (holder instanceof SyncedRegistrableRegistry && remappable) {
REMAPPERS.put(identifier, new RegistryRemapper<>((SyncedRegistrableRegistry<?>) holder));
}

Expand All @@ -108,7 +115,7 @@ public static <T> void registerRegistry(Identifier identifier, Registry<T> holde
if (event != null) event.invoker().onEntryAdded(rawId, id, object);
});

if (holder instanceof SyncedRegistrableRegistry) {
if (holder instanceof SyncedRegistrableRegistry && remappable) {
((SyncedRegistrableRegistry<T>) holder).fabric$getRegistryRemapCallback().register(changedIdsMap -> {
Event<RegistryRemapCallback<T>> event = (Event<RegistryRemapCallback<T>>) (Object) RegistryEventHelper.IDENTIFIER_REMAP_MAP.get(identifier);

Expand All @@ -124,7 +131,13 @@ public static <T> void register(Registry<T> registry, Identifier identifier, T v
Registrable<T> registrable = (Registrable<T>) registry;
int computedId = -1;

if (registry instanceof SyncedRegistrable) {
boolean remappable = true;

if (registry instanceof DesynchronizeableRegistrable) {
remappable = ((DesynchronizeableRegistrable) registry).canSynchronize();
}

if (registry instanceof SyncedRegistrable && remappable) {
computedId = ((SyncedRegistrable<T>) registrable).fabric$nextId();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import net.minecraft.util.registry.SimpleRegistry;

import net.legacyfabric.fabric.api.registry.v2.registry.SyncedRegistrableRegistry;
import net.legacyfabric.fabric.api.registry.v2.registry.registrable.DesynchronizeableRegistrable;

@Mixin(SimpleRegistry.class)
public abstract class SimpleRegistryMixinV2<K, V> implements SyncedRegistrableRegistry<V> {
public abstract class SimpleRegistryMixinV2<K, V> implements SyncedRegistrableRegistry<V>, DesynchronizeableRegistrable {
}

0 comments on commit da6916f

Please sign in to comment.