Skip to content

Commit

Permalink
Merge branch 'refs/heads/feature/registry-sync-v2/block' into feature…
Browse files Browse the repository at this point in the history
…/registry-sync-v2/block-entity
  • Loading branch information
thecatcore committed May 8, 2024
2 parents bca9170 + 5398991 commit c7de1c8
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 13 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ def addPomMetadataInformation(Project project, MavenPom pom) {
def modJsonFile = project.file("src/main/resources/fabric.mod.json")

if (!modJsonFile.exists()) {
System.out.println("Can't find fabric.mod.json at ${modJsonFile.toString()}!")
logger.error("Can't find fabric.mod.json at ${modJsonFile.toString()}!")
return;
}

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.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 @@ -24,6 +24,7 @@
import org.jetbrains.annotations.NotNull;

import net.legacyfabric.fabric.api.event.Event;
import net.legacyfabric.fabric.api.event.EventFactory;
import net.legacyfabric.fabric.api.registry.v2.event.RegistryBeforeAddCallback;
import net.legacyfabric.fabric.api.registry.v2.event.RegistryEntryAddedCallback;
import net.legacyfabric.fabric.api.registry.v2.registry.RegistrableRegistry;
Expand All @@ -36,6 +37,21 @@ public class MapRegistryWrapper<K, V> implements RegistrableRegistry<V> {
private final Function<Identifier, K> toMapKey;
private final Function<K, Identifier> fromMapKey;

private final Event<RegistryEntryAddedCallback<V>> addObjectEvent = EventFactory.createArrayBacked(RegistryEntryAddedCallback.class,
(callbacks) -> (rawId, id, object) -> {
for (RegistryEntryAddedCallback<V> callback : callbacks) {
callback.onEntryAdded(rawId, id, object);
}
}
);
private final Event<RegistryBeforeAddCallback<V>> beforeAddObjectEvent = EventFactory.createArrayBacked(RegistryBeforeAddCallback.class,
(callbacks) -> (rawId, id, object) -> {
for (RegistryBeforeAddCallback<V> callback : callbacks) {
callback.onEntryAdding(rawId, id, object);
}
}
);

public MapRegistryWrapper(Identifier id, Map<K, V> idToValue, Map<V, K> valueToId, Function<Identifier, K> toMapKey, Function<K, Identifier> fromMapKey) {
this.id = id;
this.idToValue = idToValue;
Expand All @@ -51,12 +67,12 @@ public MapRegistryWrapper(Identifier id, Map<K, V> idToValue, Map<V, K> valueToI

@Override
public Event<RegistryEntryAddedCallback<V>> fabric$getEntryAddedCallback() {
return null;
return this.addObjectEvent;
}

@Override
public Event<RegistryBeforeAddCallback<V>> fabric$getBeforeAddedCallback() {
return null;
return this.beforeAddObjectEvent;
}

@Override
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 c7de1c8

Please sign in to comment.