Skip to content

Commit

Permalink
Registry sync v2 - Block (#172)
Browse files Browse the repository at this point in the history
* Block module rebase fixes

* Fix whether to use neighbour light for Modded blocks

* Clean up and fix style

* Update test mods

* Fix compiling errors

* Spotless

* Fix compiling errors

* PR Feedback
  • Loading branch information
thecatcore authored Feb 11, 2025
1 parent 7a0c24b commit c89b8a8
Show file tree
Hide file tree
Showing 38 changed files with 893 additions and 1 deletion.
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ 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-block-api-v1.version = 1.0.0
legacy-fabric-rendering-api-v1.version = 1.0.0
legacy-fabric-resource-loader-v1.version = 2.2.2
3 changes: 3 additions & 0 deletions legacy-fabric-block-api-v1/1.12.2/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
loom {
accessWidenerPath = file("src/main/resources/legacy-fabric-block-api-v1.accesswidener")
}
2 changes: 2 additions & 0 deletions legacy-fabric-block-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,69 @@
/*
* 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.block.versioned;

import java.util.Map;

import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.util.collection.IdList;

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.registry.v2.registry.registrable.IdsHolder;
import net.legacyfabric.fabric.api.util.Identifier;
import net.legacyfabric.fabric.api.util.VersionUtils;
import net.legacyfabric.fabric.mixin.block.versioned.BlockAccessor;

public class BlockStateRemapper implements RegistryRemapCallback<Block> {
private static final boolean hasSpecialCase = VersionUtils.matches(">1.8.9");
private static final Identifier specialCaseId = new Identifier("tripwire");

@Override
public void callback(Map<Integer, FabricRegistryEntry<Block>> changedIdsMap) {
IdsHolder<BlockState> newList = Block.BLOCK_STATES.fabric$new();

for (Block block : Block.REGISTRY) {
int blockRawId = RegistryHelper.getRawId(Block.REGISTRY, block);

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

Identifier blockId = RegistryHelper.getId(Block.REGISTRY, block);

if (blockId.equals(specialCaseId) && hasSpecialCase) {
for (int i = 0; i < 15; ++i) {
int blockStateId = blockRawId << 4 | i;
BlockState state = block.stateFromData(i);

newList.fabric$setValue(state, blockStateId);
}
} else {
for (BlockState state : block.getStateManager().getBlockStates()) {
int blockStateId = blockRawId << 4 | block.getData(state);

newList.fabric$setValue(state, blockStateId);
}
}
}

BlockAccessor.setBlockStateList((IdList<BlockState>) newList);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* 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.block.versioned;

import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.SlabBlock;
import net.minecraft.block.StairsBlock;
import net.minecraft.block.material.Material;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;

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

import net.legacyfabric.fabric.api.registry.v2.RegistryHelper;
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;
import net.legacyfabric.fabric.api.util.Identifier;
import net.legacyfabric.fabric.api.util.VersionUtils;
import net.legacyfabric.fabric.mixin.block.versioned.ItemAccessor;

public class EarlyInitializer implements PreLaunchEntrypoint {
private static final boolean checkGrass = VersionUtils.matches(">1.8.9");
@Override
public void onPreLaunch() {
RegistryInitializedEvent.event(RegistryIds.BLOCKS).register(EarlyInitializer::blockRegistryInit);
RegistryInitializedEvent.event(RegistryIds.ITEMS).register(EarlyInitializer::itemRegistryInit);
}

private static void blockRegistryInit(FabricRegistry<?> holder) {
SyncedFabricRegistry<Block> registry = (SyncedFabricRegistry<Block>) holder;

registry.fabric$getEntryAddedCallback().register((rawId, id, block) -> {
for (BlockState blockState : block.getStateManager().getBlockStates()) {
int i = rawId << 4 | block.getData(blockState);
Block.BLOCK_STATES.set(blockState, i);
}
});

registry.fabric$getRegistryRemapCallback().register(new BlockStateRemapper());

registry.fabric$getEntryAddedCallback().register((rawId, id, block) -> {
if (block.material == Material.AIR) {
block.useNeighbourLight = false;
} else {
boolean useNeighbourLight = false;
boolean isStairs = block instanceof StairsBlock;
boolean isSlab = block instanceof SlabBlock;
boolean isMissingTop = block == RegistryHelper.getValue(Item.REGISTRY, new Identifier("farmland"))
|| (checkGrass && block == RegistryHelper.getValue(Item.REGISTRY, new Identifier("grass_path")));
boolean isTranslucent = block.transluscent;
boolean isNotOpaque = block.opacity == 0;

if (isStairs || isSlab || isMissingTop || isTranslucent || isNotOpaque) {
useNeighbourLight = true;
}

block.useNeighbourLight = useNeighbourLight;
}
});
}

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

registry.fabric$getEntryAddedCallback().register((rawId, id, item) -> {
if (item instanceof BlockItem) {
ItemAccessor.getBlockItemsMap().put(((BlockItem) item).getBlock(), item);
}
});
}
}
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.block.versioned;

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

import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.util.collection.IdList;

@Mixin(Block.class)
public interface BlockAccessor {
@Accessor("BLOCK_STATES")
static void setBlockStateList(IdList<BlockState> blockStates) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.versioned;

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.Block;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.BiDefaultedRegistry;

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

@Mixin(Block.class)
public class BlockMixin {
@Shadow
@Final
public static BiDefaultedRegistry<Identifier, Block> REGISTRY;

@Inject(method = "setup", at = @At("RETURN"))
private static void registerRegistry(CallbackInfo ci) {
RegistryHelper.addRegistry(RegistryIds.BLOCKS, REGISTRY);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.versioned;

import java.util.Map;

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

import net.minecraft.block.Block;
import net.minecraft.item.Item;

@Mixin(Item.class)
public interface ItemAccessor {
@Accessor("BLOCK_ITEMS")
static Map<Block, Item> getBlockItemsMap() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"schemaVersion": 1,
"id": "legacy-fabric-block-api-v1",
"name": "Legacy Fabric Block 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 utils",
"entrypoints": {
"preLaunch": [
"net.legacyfabric.fabric.impl.block.versioned.EarlyInitializer"
]
},
"mixins": [
"legacy-fabric-block-api-v1.mixins.json"
],
"accessWidener": "legacy-fabric-block-api-v1.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,6 @@
accessWidener v2 named

accessible field net/minecraft/block/Block useNeighbourLight Z
accessible field net/minecraft/block/Block transluscent Z
accessible field net/minecraft/block/Block material Lnet/minecraft/block/material/Material;
accessible field net/minecraft/block/Block opacity I
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"required": true,
"package": "net.legacyfabric.fabric.mixin.block.versioned",
"compatibilityLevel": "JAVA_8",
"injectors": {
"defaultRequire": 1
},
"mixins": [
"BlockAccessor",
"BlockMixin",
"ItemAccessor"
],
"client": [
]
}
3 changes: 3 additions & 0 deletions legacy-fabric-block-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-block-api-v1.accesswidener")
}
2 changes: 2 additions & 0 deletions legacy-fabric-block-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
Loading

0 comments on commit c89b8a8

Please sign in to comment.