Skip to content

Commit

Permalink
Registry sync v2 - Item (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
thecatcore authored Jan 19, 2025
1 parent 0524871 commit 7a0c24b
Show file tree
Hide file tree
Showing 41 changed files with 788 additions and 34 deletions.
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ legacy-fabric-networking-api-v1.version = 2.0.2
legacy-fabric-permissions-api-v1.version = 1.1.1
legacy-fabric-registry-sync-api-v1.version = 2.2.0
legacy-fabric-registry-sync-api-v2.version = 1.0.0
legacy-fabric-item-api-v1.version = 1.0.0
legacy-fabric-rendering-api-v1.version = 1.0.0
legacy-fabric-resource-loader-v1.version = 2.2.2
Empty file.
2 changes: 2 additions & 0 deletions legacy-fabric-item-api-v1/1.12.2/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minVersionIncluded=1.8
maxVersionIncluded=1.12.2
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.impl.item.versioned;

import net.minecraft.item.Item;

import net.fabricmc.api.EnvType;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.entrypoint.PreLaunchEntrypoint;

import net.legacyfabric.fabric.api.registry.v2.RegistryIds;
import net.legacyfabric.fabric.api.registry.v2.event.RegistryInitializedEvent;
import net.legacyfabric.fabric.api.registry.v2.registry.holder.FabricRegistry;
import net.legacyfabric.fabric.api.registry.v2.registry.holder.SyncedFabricRegistry;

public class EarlyInitializer implements PreLaunchEntrypoint {
@Override
public void onPreLaunch() {
RegistryInitializedEvent.event(RegistryIds.ITEMS).register(EarlyInitializer::itemRegistryInit);
}

private static void itemRegistryInit(FabricRegistry<?> holder) {
SyncedFabricRegistry<Item> registry = (SyncedFabricRegistry<Item>) holder;

if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) registry.fabric$getRegistryRemapCallback().register(new ItemModelsRemapper());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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.impl.item.versioned;

import java.util.HashMap;
import java.util.Map;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.item.ItemModels;
import net.minecraft.client.util.ModelIdentifier;
import net.minecraft.item.Item;

import net.legacyfabric.fabric.api.registry.v2.RegistryHelper;
import net.legacyfabric.fabric.api.registry.v2.event.RegistryRemapCallback;
import net.legacyfabric.fabric.api.registry.v2.registry.holder.FabricRegistryEntry;
import net.legacyfabric.fabric.api.util.Identifier;
import net.legacyfabric.fabric.mixin.item.versioned.ItemModelsAccessor;

public class ItemModelsRemapper implements RegistryRemapCallback<Item> {
private static final Map<Identifier, Map<Integer, ModelIdentifier>> modelIds = new HashMap<>();

public static void registerModelId(Identifier id, int metadata, ModelIdentifier modelId) {
modelIds.computeIfAbsent(id, k -> new HashMap<>())
.put(metadata, modelId);
}

private int pack(int itemId, int metadata) {
return itemId << 16 | metadata;
}

private ItemModels getModelRegistry() {
return MinecraftClient.getInstance().getItemRenderer().getModels();
}

@Override
public void callback(Map<Integer, FabricRegistryEntry<Item>> changedIdsMap) {
((ItemModelsAccessor) getModelRegistry()).getModelIds().clear();

for (Map.Entry<Identifier, Map<Integer, ModelIdentifier>> entry : modelIds.entrySet()) {
Identifier itemId = entry.getKey();

Item item = RegistryHelper.<Item>getValue(Item.REGISTRY, itemId);
int itemIndex = RegistryHelper.getRawId(Item.REGISTRY, item);

if (changedIdsMap.containsKey(itemIndex)) {
itemIndex = changedIdsMap.get(itemIndex).getId();
}

for (Map.Entry<Integer, ModelIdentifier> modelIdEntry : entry.getValue().entrySet()) {
((ItemModelsAccessor) getModelRegistry()).getModelIds()
.put(pack(itemIndex, modelIdEntry.getKey()), modelIdEntry.getValue());
}
}

getModelRegistry().reloadModels();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.item.versioned;

import java.util.Map;

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

import net.minecraft.client.render.item.ItemModels;
import net.minecraft.client.util.ModelIdentifier;

@Mixin(ItemModels.class)
public interface ItemModelsAccessor {
@Accessor
Map<Integer, ModelIdentifier> getModelIds();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.item.versioned;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.minecraft.client.render.item.ItemModels;
import net.minecraft.client.util.ModelIdentifier;
import net.minecraft.item.Item;

import net.legacyfabric.fabric.api.registry.v2.RegistryHelper;
import net.legacyfabric.fabric.api.util.Identifier;
import net.legacyfabric.fabric.impl.item.versioned.ItemModelsRemapper;

@Mixin(ItemModels.class)
public class ItemModelsMixin {
@Inject(method = "putModel", at = @At("RETURN"))
private void lf$registerModelId(Item item, int metadata, ModelIdentifier id, CallbackInfo ci) {
Identifier identifier = RegistryHelper.getId(Item.REGISTRY, item);

if (identifier != null) ItemModelsRemapper.registerModelId(identifier, metadata, id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"schemaVersion": 1,
"id": "legacy-fabric-item-api-v1",
"name": "Legacy Fabric Item API (V1)",
"version": "${version}",
"environment": "*",
"license": "Apache-2.0",
"icon": "assets/legacy-fabric/icon.png",
"contact": {
"homepage": "https://legacyfabric.net/",
"irc": "irc://irc.esper.net:6667/legacyfabric",
"issues": "https://github.com/Legacy-Fabric/fabric/issues",
"sources": "https://github.com/Legacy-Fabric/fabric"
},
"authors": [
"Legacy-Fabric"
],
"depends": {
"fabricloader": ">=0.4.0",
"minecraft": "${minecraft_version}"
},
"description": "Item utils",
"entrypoints": {
"preLaunch": [
"net.legacyfabric.fabric.impl.item.versioned.EarlyInitializer"
]
},
"mixins": [
"legacy-fabric-item-api-v1.mixins.json"
],
"custom": {
"loom:injected_interfaces": {
},
"modmenu": {
"badges": [ "library" ],
"parent": {
"id": "legacy-fabric-api",
"name": "Legacy Fabric API",
"badges": [ "library" ],
"description": "Core API module providing key hooks and inter-compatibility features for Minecraft 1.7.10-1.12.2.",
"icon": "assets/legacy-fabric/icon.png"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"required": true,
"package": "net.legacyfabric.fabric.mixin.item",
"compatibilityLevel": "JAVA_8",
"injectors": {
"defaultRequire": 1
},
"mixins": [
],
"client": [
"versioned.ItemModelsAccessor",
"versioned.ItemModelsMixin"
]
}
3 changes: 3 additions & 0 deletions legacy-fabric-item-api-v1/1.7.10/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
loom {
accessWidenerPath = file("src/main/resources/legacy-fabric-item-api.accesswidener")
}
2 changes: 2 additions & 0 deletions legacy-fabric-item-api-v1/1.7.10/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minVersionIncluded=1.7
maxVersionIncluded=1.7.10
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.impl.item.versioned;

import net.minecraft.item.Item;

import net.fabricmc.loader.api.entrypoint.PreLaunchEntrypoint;

import net.legacyfabric.fabric.api.registry.v2.RegistryIds;
import net.legacyfabric.fabric.api.registry.v2.event.RegistryInitializedEvent;
import net.legacyfabric.fabric.api.registry.v2.registry.holder.FabricRegistry;

public class EarlyInitializer implements PreLaunchEntrypoint {
@Override
public void onPreLaunch() {
RegistryInitializedEvent.event(RegistryIds.ITEMS).register(EarlyInitializer::itemRegistryInit);
}

private static void itemRegistryInit(FabricRegistry<?> holder) {
FabricRegistry<Item> registry = (FabricRegistry<Item>) holder;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"schemaVersion": 1,
"id": "legacy-fabric-item-api-v1",
"name": "Legacy Fabric Item API (V1)",
"version": "${version}",
"environment": "*",
"license": "Apache-2.0",
"icon": "assets/legacy-fabric/icon.png",
"contact": {
"homepage": "https://legacyfabric.net/",
"irc": "irc://irc.esper.net:6667/legacyfabric",
"issues": "https://github.com/Legacy-Fabric/fabric/issues",
"sources": "https://github.com/Legacy-Fabric/fabric"
},
"authors": [
"Legacy-Fabric"
],
"depends": {
"fabricloader": ">=0.4.0",
"minecraft": "${minecraft_version}"
},
"description": "Item utils",
"entrypoints": {
"preLaunch": []
},
"mixins": [
"legacy-fabric-item-api-v1.mixins.json"
],
"accessWidener": "legacy-fabric-item-api.accesswidener",
"custom": {
"loom:injected_interfaces": {
},
"modmenu": {
"badges": [ "library" ],
"parent": {
"id": "legacy-fabric-api",
"name": "Legacy Fabric API",
"badges": [ "library" ],
"description": "Core API module providing key hooks and inter-compatibility features for Minecraft 1.7.10-1.12.2.",
"icon": "assets/legacy-fabric/icon.png"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"required": true,
"package": "net.legacyfabric.fabric.mixin.item",
"compatibilityLevel": "JAVA_8",
"injectors": {
"defaultRequire": 1
},
"mixins": [
],
"client": [
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
accessWidener v2 named

transitive-accessible method net/minecraft/item/Item getFromId (Ljava/lang/String;)Lnet/minecraft/item/Item;
6 changes: 6 additions & 0 deletions legacy-fabric-item-api-v1/common.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
moduleDependencies(project, [
"legacy-fabric-api-base",
"legacy-fabric-networking-api-v1",
"legacy-fabric-resource-loader-v1",
"legacy-fabric-registry-sync-api-v2"
])
Empty file.
2 changes: 2 additions & 0 deletions legacy-fabric-item-api-v1/common/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minVersionIncluded=1.7
maxVersionIncluded=1.12.2
Loading

0 comments on commit 7a0c24b

Please sign in to comment.