Skip to content

Commit

Permalink
More progress on registry api v2
Browse files Browse the repository at this point in the history
- differentiate "synced" Index-Key-Value registries from simple Key-Value registries
- start preparing registry remapping
  • Loading branch information
thecatcore committed Mar 23, 2024
1 parent 6ade139 commit f482232
Show file tree
Hide file tree
Showing 24 changed files with 605 additions and 107 deletions.
7 changes: 6 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ allprojects {
useLegacyMixinAp = true
}

interfaceInjection {
enableDependencyInterfaceInjection = true
}

runs {
"testModClient$mcVersion" {
client()
Expand Down Expand Up @@ -436,7 +440,8 @@ def addPomMetadataInformation(Project project, MavenPom pom) {
def modJsonFile = project.file("src/main/resources/fabric.mod.json")

if (!modJsonFile.exists()) {
modJsonFile = project.file("src/client/resources/fabric.mod.json")
System.out.println("Can't find fabric.mod.json at ${modJsonFile.toString()}!")
return;
}

def modJson = new JsonSlurper().parse(modJsonFile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public String toString() {
return this.namespace + ':' + this.path;
}

public String toTranslationKey() {
return this.namespace + "." + this.path;
}

public boolean equals(Object object) {
if (this == object) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,13 @@ public static Block getBlock(Identifier id) {
* @return The item registered
*/
public static Item registerItem(Item item, Identifier id) {
return RegistryHelperImpl.registerItem(item, id);
net.legacyfabric.fabric.api.registry.v2.RegistryHelper.register(net.legacyfabric.fabric.api.registry.v2.RegistryIds.ITEMS, id, item);

return item;
}

public static Item getItem(Identifier id) {
return RegistryHelperImpl.getValue(id, RegistryIds.ITEMS);
return net.legacyfabric.fabric.api.registry.v2.RegistryHelper.getValue(RegistryIds.ITEMS, id);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.mixin.registry.sync;

import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

import net.minecraft.class_2929;

import net.legacyfabric.fabric.api.registry.v2.registry.registrable.IdsHolder;

@Mixin(class_2929.class)
public abstract class class_2929Mixin<T> implements IdsHolder<T> {
@Shadow
@Nullable
public abstract T getById(int id);

@Override
public IdsHolder<T> fabric$new() {
return (IdsHolder<T>) new class_2929<>(256);
}

@Override
public int fabric$nextId() {
int id = 0;

while (this.getById(id) != null) id++;

return id;
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,52 @@
package net.legacyfabric.fabric.mixin.registry.sync.versioned;
/*
* 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.
*/

import net.legacyfabric.fabric.api.registry.v2.RegistryHolder;
package net.legacyfabric.fabric.mixin.registry.sync.versioned;

import net.legacyfabric.fabric.api.util.Identifier;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

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

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import net.legacyfabric.fabric.api.registry.v2.registry.holder.SyncedRegistryHolder;
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 RegistryHolder<V> {
public abstract class SimpleRegistryMixin<K, V> implements SyncedRegistryHolder<V>, SyncedRegistrable<V> {
@Shadow
public abstract void add(int id, K identifier, V object);

@Shadow
@Final
protected class_2929<V> field_13718;

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

@Override
public void register(int rawId, Identifier identifier, V value) {
getBeforeAddedCallback().invoker().onEntryAdding(rawId, identifier, value);
add(rawId, toKeyType(identifier), value);
getEntryAddedCallback().invoker().onEntryAdded(rawId, identifier, value);
public IdsHolder<V> fabric$getIdsHolder() {
return (IdsHolder<V>) field_13718;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"defaultRequire": 1
},
"mixins": [
"class_2929Mixin",
"versioned.SimpleRegistryMixin"
],
"client": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,59 @@
/*
* 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;

import java.util.function.Function;

import net.legacyfabric.fabric.api.registry.v2.registry.holder.RegistryHolder;
import net.legacyfabric.fabric.api.util.Identifier;
import net.legacyfabric.fabric.impl.registry.RegistryHelperImplementation;

import java.util.function.Function;

public class RegistryHelper {
public static <T> void register(RegistryHolder<T> registry, Identifier identifier, T value) {
RegistryHelperImplementation.register(registry, identifier, value);
}

public static <T> void registry(Identifier registryId, Identifier identifier, T value) {
public static <T> void register(Identifier registryId, Identifier identifier, T value) {
register(RegistryHelperImplementation.getRegistry(registryId), identifier, value);
}

public static <T> T register(RegistryHolder<T> registry, Identifier identifier, Function<Integer, T> valueConstructor) {
return RegistryHelperImplementation.register(registry, identifier, valueConstructor);
}

public static <T> T registry(Identifier registryId, Identifier identifier, Function<Integer, T> valueConstructor) {
public static <T> T register(Identifier registryId, Identifier identifier, Function<Integer, T> valueConstructor) {
return register(RegistryHelperImplementation.<T>getRegistry(registryId), identifier, valueConstructor);
}

public static void addRegistry(Identifier identifier, RegistryHolder<?> registryHolder) {
RegistryHelperImplementation.registerRegistry(identifier, registryHolder);
}

public static <T> RegistryHolder<T> getRegistry(Identifier identifier) {
return RegistryHelperImplementation.getRegistry(identifier);
}

public static <T> T getValue(Identifier registryId, Identifier identifier) {
return RegistryHelperImplementation.<T>getRegistry(registryId)
.fabric$getValue(identifier);
}

public static <T> T getValue(RegistryHolder<T> registry, Identifier identifier) {
return registry.fabric$getValue(identifier);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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;

import net.legacyfabric.fabric.api.util.Identifier;

public class RegistryIds {
public static final Identifier ITEMS = new Identifier("items");
public static final Identifier BLOCKS = new Identifier("blocks");
public static final Identifier BLOCK_ENTITY_TYPES = new Identifier("block_entity_types");
public static final Identifier STATUS_EFFECTS = new Identifier("status_effects");
public static final Identifier ENCHANTMENTS = new Identifier("enchantments");
public static final Identifier BIOMES = new Identifier("biomes");
public static final Identifier ENTITY_TYPES = new Identifier("entity_types");
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
/*
* 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.event;

import net.legacyfabric.fabric.api.event.Event;
import net.legacyfabric.fabric.api.registry.v2.RegistryHolder;
import net.legacyfabric.fabric.api.registry.v2.registry.holder.RegistryHolder;
import net.legacyfabric.fabric.api.util.Identifier;
import net.legacyfabric.fabric.impl.registry.RegistryHelperImplementation;

Expand All @@ -14,6 +31,6 @@ static <T> Event<RegistryBeforeAddCallback<T>> event(Identifier registryId) {
}

static <T> Event<RegistryBeforeAddCallback<T>> event(RegistryHolder<T> registry) {
return registry.getBeforeAddedCallback();
return registry.fabric$getBeforeAddedCallback();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
/*
* 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.event;

import net.legacyfabric.fabric.api.event.Event;
import net.legacyfabric.fabric.api.registry.v2.RegistryHolder;
import net.legacyfabric.fabric.api.registry.v2.registry.holder.RegistryHolder;
import net.legacyfabric.fabric.api.util.Identifier;
import net.legacyfabric.fabric.impl.registry.RegistryHelperImplementation;

Expand All @@ -14,6 +31,6 @@ static <T> Event<RegistryEntryAddedCallback<T>> event(Identifier registryId) {
}

static <T> Event<RegistryEntryAddedCallback<T>> event(RegistryHolder<T> registry) {
return registry.getEntryAddedCallback();
return registry.fabric$getEntryAddedCallback();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
/*
* 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.event;

import net.legacyfabric.fabric.api.event.Event;
import net.legacyfabric.fabric.api.registry.v2.RegistryHolder;
import net.legacyfabric.fabric.api.registry.v2.registry.holder.RegistryHolder;
import net.legacyfabric.fabric.api.util.Identifier;
import net.legacyfabric.fabric.impl.registry.RegistryHelperImplementation;

Expand Down
Loading

0 comments on commit f482232

Please sign in to comment.