Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Registry sync v2 - Block #172

Draft
wants to merge 24 commits into
base: feature/registry-sync-v2/main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
628b580
Initial work on item-api
thecatcore Mar 23, 2024
dd00b5e
spotless apply
thecatcore Mar 24, 2024
41ae62b
Rename some classes
thecatcore Apr 24, 2024
b666926
Move registry related test mods to per version global test mods
thecatcore May 5, 2024
ca468c0
Make it easier to set item texture in 1.7.10
thecatcore May 7, 2024
02f4a01
Fix item models being wrong after registry is remapped for 1.8+
thecatcore May 7, 2024
94892d9
Disable Block testmods as Block registry is not ported yet
thecatcore May 7, 2024
a278d25
Fix style
thecatcore May 7, 2024
dcb8aa9
Fix Item registry being registered before vanilla items initialisation
thecatcore May 8, 2024
dcf5ee3
Remove block related fixes from item modules
thecatcore May 8, 2024
c250ca1
Clean up and fix style
thecatcore May 8, 2024
58dff99
Fix crash on server
thecatcore Jul 28, 2024
18b5aaa
Woops
thecatcore Aug 29, 2024
ed2566e
Spotless
thecatcore Aug 29, 2024
87d4444
Fix compiling errors and apply spotless
thecatcore Dec 22, 2024
2e23a85
Some clean up
thecatcore Dec 22, 2024
f1c1497
Fully fledged test item + more clean up
thecatcore Dec 22, 2024
8e32026
Block module rebase fixes
thecatcore Dec 22, 2024
f983330
Fix whether to use neighbour light for Modded blocks
thecatcore May 8, 2024
655d1e3
Clean up and fix style
thecatcore May 8, 2024
68cd062
Update test mods
thecatcore Dec 22, 2024
d66cda2
Fix weird rebase conflicts
thecatcore Dec 22, 2024
33d53ca
Fix compiling errors
thecatcore Dec 22, 2024
9da83b0
Spotless
thecatcore Dec 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ 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-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.setBLOCK_STATES((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 var12 = false;
boolean var13 = block instanceof StairsBlock;
boolean var14 = block instanceof SlabBlock;
boolean var15 = block == RegistryHelper.getValue(Item.REGISTRY, new Identifier("farmland"))
|| (checkGrass && block == RegistryHelper.getValue(Item.REGISTRY, new Identifier("grass_path")));
boolean var16 = block.transluscent;
boolean var17 = block.opacity == 0;

if (var13 || var14 || var15 || var16 || var17) {
var12 = true;
}

block.useNeighbourLight = var12;
}
});
}

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

registry.fabric$getEntryAddedCallback().register((rawId, id, item) -> {
if (item instanceof BlockItem) {
ItemAccessor.getBLOCK_ITEMS().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
static void setBLOCK_STATES(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,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 java.util.Map;

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


@Mixin(Item.class)
public interface ItemAccessor {
@Accessor
static Map<Block, Item> getBLOCK_ITEMS() {
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
Loading