From 5ae06d69a39218f3d5d16a8024912034cc212527 Mon Sep 17 00:00:00 2001 From: Adrian Siekierka Date: Sun, 2 Dec 2018 14:34:54 +0100 Subject: [PATCH] wrap ToolManager in FabricBlockBuilder --- .../fabric/helpers/FabricBlockBuilder.java | 22 +++++++++ .../fabric/helpers/FabricBuilderEvent.java | 33 +++++++++++++ .../fabric/mixin/helpers/MixinBlock.java | 37 +++++++++++++++ .../fabric/mixin/helpers/MixinItem.java | 37 +++++++++++++++ .../fabricmc/fabric/tools/ToolManager.java | 47 ++++++++++++++----- .../net.fabricmc.fabric.mixins.common.json | 2 + 6 files changed, 167 insertions(+), 11 deletions(-) create mode 100644 src/main/java/net/fabricmc/fabric/helpers/FabricBuilderEvent.java create mode 100644 src/main/java/net/fabricmc/fabric/mixin/helpers/MixinBlock.java create mode 100644 src/main/java/net/fabricmc/fabric/mixin/helpers/MixinItem.java diff --git a/src/main/java/net/fabricmc/fabric/helpers/FabricBlockBuilder.java b/src/main/java/net/fabricmc/fabric/helpers/FabricBlockBuilder.java index e6c8e4a03..fb81de369 100644 --- a/src/main/java/net/fabricmc/fabric/helpers/FabricBlockBuilder.java +++ b/src/main/java/net/fabricmc/fabric/helpers/FabricBlockBuilder.java @@ -19,7 +19,9 @@ package net.fabricmc.fabric.helpers; import net.fabricmc.fabric.tools.ToolManager; import net.minecraft.block.Block; import net.minecraft.block.Material; +import net.minecraft.item.Item; import net.minecraft.sound.BlockSoundGroup; +import net.minecraft.tag.Tag; import net.minecraft.util.DyeColor; import net.minecraft.util.Identifier; import net.minecraft.util.MapColor; @@ -61,6 +63,24 @@ public class FabricBlockBuilder { return new FabricBlockBuilder(base); } + /* FABRIC HELPERS */ + + public FabricBlockBuilder setBreakByHand(boolean value) { + ToolManager.get(delegate).breakByHand(value); + return this; + } + + public FabricBlockBuilder setBreakByTool(Tag tag) { + return setBreakByTool(tag, 0); + } + + public FabricBlockBuilder setBreakByTool(Tag tag, int miningLevel) { + ToolManager.get(delegate).putBreakByTool(tag, miningLevel); + return this; + } + + /* DELEGATE WRAPPERS */ + public FabricBlockBuilder setMapColor(MapColor color) { castDelegate.fabric_setMapColor(color); return this; @@ -128,6 +148,8 @@ public class FabricBlockBuilder { return this; } + /* BUILDING LOGIC */ + public Block.Builder build() { return delegate; } diff --git a/src/main/java/net/fabricmc/fabric/helpers/FabricBuilderEvent.java b/src/main/java/net/fabricmc/fabric/helpers/FabricBuilderEvent.java new file mode 100644 index 000000000..1c87de6cb --- /dev/null +++ b/src/main/java/net/fabricmc/fabric/helpers/FabricBuilderEvent.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2016, 2017, 2018 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.fabricmc.fabric.helpers; + +import net.fabricmc.fabric.util.HandlerList; +import net.fabricmc.fabric.util.HandlerRegistry; +import net.minecraft.block.Block; +import net.minecraft.item.Item; + +import java.util.function.BiConsumer; + +public final class FabricBuilderEvent { + public static final HandlerRegistry> BLOCK = new HandlerList<>(); + public static final HandlerRegistry> ITEM = new HandlerList<>(); + + private FabricBuilderEvent() { + + } +} diff --git a/src/main/java/net/fabricmc/fabric/mixin/helpers/MixinBlock.java b/src/main/java/net/fabricmc/fabric/mixin/helpers/MixinBlock.java new file mode 100644 index 000000000..d0a93b5b8 --- /dev/null +++ b/src/main/java/net/fabricmc/fabric/mixin/helpers/MixinBlock.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2016, 2017, 2018 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.fabricmc.fabric.mixin.helpers; + +import net.fabricmc.fabric.helpers.FabricBuilderEvent; +import net.fabricmc.fabric.util.HandlerList; +import net.minecraft.block.Block; +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 java.util.function.BiConsumer; + +@Mixin(Block.class) +public class MixinBlock { + @Inject(method = "(Lnet/minecraft/block/Block$Builder;)V", at = @At("RETURN")) + public void init(Block.Builder builder, CallbackInfo info) { + for (Object o : ((HandlerList>) FabricBuilderEvent.BLOCK).getBackingArray()) { + ((BiConsumer) o).accept(builder, (Block) (Object) this); + } + } +} diff --git a/src/main/java/net/fabricmc/fabric/mixin/helpers/MixinItem.java b/src/main/java/net/fabricmc/fabric/mixin/helpers/MixinItem.java new file mode 100644 index 000000000..76ddb4cb0 --- /dev/null +++ b/src/main/java/net/fabricmc/fabric/mixin/helpers/MixinItem.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2016, 2017, 2018 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.fabricmc.fabric.mixin.helpers; + +import net.fabricmc.fabric.helpers.FabricBuilderEvent; +import net.fabricmc.fabric.util.HandlerList; +import net.minecraft.item.Item; +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 java.util.function.BiConsumer; + +@Mixin(Item.class) +public class MixinItem { + @Inject(method = "(Lnet/minecraft/item/Item$Builder;)V", at = @At("RETURN")) + public void init(Item.Builder builder, CallbackInfo info) { + for (Object o : ((HandlerList>) FabricBuilderEvent.ITEM).getBackingArray()) { + ((BiConsumer) o).accept(builder, (Item) (Object) this); + } + } +} diff --git a/src/main/java/net/fabricmc/fabric/tools/ToolManager.java b/src/main/java/net/fabricmc/fabric/tools/ToolManager.java index 2751d1b99..5a99cc51d 100644 --- a/src/main/java/net/fabricmc/fabric/tools/ToolManager.java +++ b/src/main/java/net/fabricmc/fabric/tools/ToolManager.java @@ -16,6 +16,7 @@ package net.fabricmc.fabric.tools; +import net.fabricmc.fabric.helpers.FabricBuilderEvent; import net.fabricmc.fabric.util.TriState; import net.minecraft.block.Block; import net.minecraft.block.BlockState; @@ -28,17 +29,24 @@ import java.util.HashMap; import java.util.Map; public final class ToolManager { - private static class Entry { + public interface Entry { + void breakByHand(boolean value); + void putBreakByTool(Tag tag, int miningLevel); + } + + private static class EntryImpl implements Entry { @SuppressWarnings("unchecked") private Tag[] tags = (Tag[]) new Tag[0]; private int[] tagLevels = new int[0]; private TriState defaultValue = TriState.DEFAULT; - public void setDefaultValue(TriState defaultValue) { - this.defaultValue = defaultValue; + @Override + public void breakByHand(boolean value) { + this.defaultValue = TriState.of(value); } - public void addTag(Tag tag, int miningLevel) { + @Override + public void putBreakByTool(Tag tag, int miningLevel) { for (int i = 0; i < tags.length; i++) { if (tags[i] == tag) { tagLevels[i] = miningLevel; @@ -58,23 +66,40 @@ public final class ToolManager { } } - private static final Map entries = new HashMap<>(); + private static final Map entriesPre = new HashMap<>(); + private static final Map entries = new HashMap<>(); private ToolManager() { } - private static Entry computeEntry(Block b) { - return entries.computeIfAbsent(b, (bb) -> new Entry()); + static { + FabricBuilderEvent.BLOCK.register(ToolManager::onBlockRegistered); } + private static void onBlockRegistered(Block.Builder builder, Block block) { + EntryImpl entry = entriesPre.get(builder); + if (entry != null) { + entries.put(block, entry); + } + } + + public static Entry get(Block.Builder builder) { + return entriesPre.computeIfAbsent(builder, (bb) -> new EntryImpl()); + } + + private static Entry get(Block block) { + return entries.computeIfAbsent(block, (bb) -> new EntryImpl()); + } + + @Deprecated public static void registerBreakByHand(Block block, boolean value) { - computeEntry(block).defaultValue = TriState.of(value); + get(block).breakByHand(value); } + @Deprecated public static void registerBreakByTool(Block block, Tag tag, int miningLevel) { - computeEntry(block).defaultValue = TriState.FALSE; - computeEntry(block).addTag(tag, miningLevel); + get(block).putBreakByTool(tag, miningLevel); } public static int getMiningLevel(ItemStack stack) { @@ -86,7 +111,7 @@ public final class ToolManager { } public static TriState handleIsEffectiveOn(ItemStack stack, BlockState state) { - Entry entry = entries.get(state.getBlock()); + EntryImpl entry = entries.get(state.getBlock()); if (entry != null) { Item item = stack.getItem(); for (int i = 0; i < entry.tags.length; i++) { diff --git a/src/main/resources/net.fabricmc.fabric.mixins.common.json b/src/main/resources/net.fabricmc.fabric.mixins.common.json index de1f23eb8..e640dfe14 100644 --- a/src/main/resources/net.fabricmc.fabric.mixins.common.json +++ b/src/main/resources/net.fabricmc.fabric.mixins.common.json @@ -4,7 +4,9 @@ "compatibilityLevel": "JAVA_8", "mixins": [ "commands.MixinServerCommandManager", + "helpers.MixinBlock", "helpers.MixinBlockBuilder", + "helpers.MixinItem", "networking.MixinServerPlayNetworkHandler", "networking.MixinSPacketCustomPayload", "registry.MixinBootstrap",