Skip to content

Commit

Permalink
Registry sync v2 - Block Entity (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
thecatcore committed Feb 27, 2025
1 parent 1b14d7e commit deb6536
Show file tree
Hide file tree
Showing 40 changed files with 955 additions and 13 deletions.
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ 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-block-api-v1.version = 1.0.0
legacy-fabric-block-entity-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-block-entity-api-v1/1.10.2/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minVersionIncluded=1.10.2
maxVersionIncluded=1.10.2
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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.block.entity;

import java.util.Map;

import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.minecraft.block.entity.BlockEntity;

import net.legacyfabric.fabric.api.registry.v2.RegistryHelper;
import net.legacyfabric.fabric.api.registry.v2.RegistryIds;
import net.legacyfabric.fabric.api.registry.v2.registry.holder.FabricRegistry;
import net.legacyfabric.fabric.api.util.Identifier;
import net.legacyfabric.fabric.impl.block.entity.BlockEntityUtils;
import net.legacyfabric.fabric.impl.registry.wrapper.MapFabricRegistryWrapper;

@Mixin(BlockEntity.class)
public class BlockEntityMixin {
@Shadow
@Final
private static Map<String, Class<? extends BlockEntity>> stringClassMap;
@Shadow
@Final
private static Map<Class<? extends BlockEntity>, String> classStringMap;
@Unique
private static FabricRegistry<Class<? extends BlockEntity>> BLOCK_ENTITY_TYPE_REGISTRY;

@Inject(method = "<clinit>", at = @At("RETURN"))
private static void registerRegistry(CallbackInfo ci) {
BLOCK_ENTITY_TYPE_REGISTRY = new MapFabricRegistryWrapper<>(
RegistryIds.BLOCK_ENTITY_TYPES, stringClassMap, classStringMap,
identifier -> BlockEntityUtils.ID_TO_OLD.getOrDefault(identifier, identifier.toString()),
string -> BlockEntityUtils.OLD_TO_ID.getOrDefault(string, new Identifier(string))
);

RegistryHelper.addRegistry(RegistryIds.BLOCK_ENTITY_TYPES, BLOCK_ENTITY_TYPE_REGISTRY);
}

/*
Previous version of LFAPI used to transform vanilla names to id.
We don't do that anymore, so we still check for id versions to not break old saves.
*/
@ModifyArg(method = "create", at = @At(value = "INVOKE", remap = false, target = "Ljava/util/Map;get(Ljava/lang/Object;)Ljava/lang/Object;"))
private static Object fixOldSaves(Object oldKey) {
Identifier asId = new Identifier(oldKey);

if (BlockEntityUtils.ID_TO_OLD.containsKey(asId)) {
return BlockEntityUtils.ID_TO_OLD.get(asId);
}

return oldKey;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"schemaVersion": 1,
"id": "legacy-fabric-block-entity-api-v1",
"name": "Legacy Fabric Block Entity 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": "Block Entity utils",
"entrypoints": {
"preLaunch": [
]
},
"mixins": [
"legacy-fabric-block-entity-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,13 @@
{
"required": true,
"package": "net.legacyfabric.fabric.mixin.block.entity",
"compatibilityLevel": "JAVA_8",
"injectors": {
"defaultRequire": 1
},
"mixins": [
"BlockEntityMixin"
],
"client": [
]
}
Empty file.
2 changes: 2 additions & 0 deletions legacy-fabric-block-entity-api-v1/1.12.2/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minVersionIncluded=1.11
maxVersionIncluded=1.12.2
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.block.entity;

import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
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.block.entity.BlockEntity;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.SimpleRegistry;

import net.legacyfabric.fabric.api.registry.v2.RegistryHelper;
import net.legacyfabric.fabric.api.registry.v2.RegistryIds;
import net.legacyfabric.fabric.api.registry.v2.registry.registrable.DesynchronizeableRegistrable;

@Mixin(BlockEntity.class)
public class BlockEntityMixin {
@Shadow
@Final
private static SimpleRegistry<Identifier, Class<? extends BlockEntity>> BLOCK_ENTITY;

@Inject(method = "<clinit>", at = @At("RETURN"))
private static void registerRegistry(CallbackInfo ci) {
((DesynchronizeableRegistrable) BLOCK_ENTITY).fabric$setSynchronize(false);
RegistryHelper.addRegistry(RegistryIds.BLOCK_ENTITY_TYPES, BLOCK_ENTITY);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"schemaVersion": 1,
"id": "legacy-fabric-block-entity-api-v1",
"name": "Legacy Fabric Block Entity 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": "Block Entity utils",
"entrypoints": {
"preLaunch": [
]
},
"mixins": [
"legacy-fabric-block-entity-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,13 @@
{
"required": true,
"package": "net.legacyfabric.fabric.mixin.block.entity",
"compatibilityLevel": "JAVA_8",
"injectors": {
"defaultRequire": 1
},
"mixins": [
"BlockEntityMixin"
],
"client": [
]
}
Empty file.
2 changes: 2 additions & 0 deletions legacy-fabric-block-entity-api-v1/1.8.9/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minVersionIncluded=1.7
maxVersionIncluded=1.9.4
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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.block.entity;

import java.util.Map;

import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.minecraft.block.entity.BlockEntity;

import net.legacyfabric.fabric.api.registry.v2.RegistryHelper;
import net.legacyfabric.fabric.api.registry.v2.RegistryIds;
import net.legacyfabric.fabric.api.registry.v2.registry.holder.FabricRegistry;
import net.legacyfabric.fabric.api.util.Identifier;
import net.legacyfabric.fabric.impl.block.entity.BlockEntityUtils;
import net.legacyfabric.fabric.impl.registry.wrapper.MapFabricRegistryWrapper;

@Mixin(BlockEntity.class)
public class BlockEntityMixin {
@Shadow
@Final
private static Map<String, Class<? extends BlockEntity>> stringClassMap;
@Shadow
@Final
private static Map<Class<? extends BlockEntity>, String> classStringMap;
@Unique
private static FabricRegistry<Class<? extends BlockEntity>> BLOCK_ENTITY_TYPE_REGISTRY;

@Inject(method = "<clinit>", at = @At("RETURN"))
private static void registerRegistry(CallbackInfo ci) {
BLOCK_ENTITY_TYPE_REGISTRY = new MapFabricRegistryWrapper<>(
RegistryIds.BLOCK_ENTITY_TYPES, stringClassMap, classStringMap,
identifier -> BlockEntityUtils.ID_TO_OLD.getOrDefault(identifier, identifier.toString()),
string -> BlockEntityUtils.OLD_TO_ID.getOrDefault(string, new Identifier(string))
);

RegistryHelper.addRegistry(RegistryIds.BLOCK_ENTITY_TYPES, BLOCK_ENTITY_TYPE_REGISTRY);
}

/*
Previous version of LFAPI used to transform vanilla names to id.
We don't do that anymore, so we still check for id versions to not break old saves.
*/
@ModifyArg(method = "createFromNbt", at = @At(value = "INVOKE", remap = false, target = "Ljava/util/Map;get(Ljava/lang/Object;)Ljava/lang/Object;"))
private static Object fixOldSaves(Object oldKey) {
Identifier asId = new Identifier(oldKey);

if (BlockEntityUtils.ID_TO_OLD.containsKey(asId)) {
return BlockEntityUtils.ID_TO_OLD.get(asId);
}

return oldKey;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"schemaVersion": 1,
"id": "legacy-fabric-block-entity-api-v1",
"name": "Legacy Fabric Block Entity 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": "Block Entity utils",
"entrypoints": {
"preLaunch": [
]
},
"mixins": [
"legacy-fabric-block-entity-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,13 @@
{
"required": true,
"package": "net.legacyfabric.fabric.mixin.block.entity",
"compatibilityLevel": "JAVA_8",
"injectors": {
"defaultRequire": 1
},
"mixins": [
"BlockEntityMixin"
],
"client": [
]
}
Loading

0 comments on commit deb6536

Please sign in to comment.