wrap ToolManager in FabricBlockBuilder

This commit is contained in:
Adrian Siekierka 2018-12-02 14:34:54 +01:00
parent 606ebeb524
commit 5ae06d69a3
6 changed files with 167 additions and 11 deletions

View file

@ -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<Item> tag) {
return setBreakByTool(tag, 0);
}
public FabricBlockBuilder setBreakByTool(Tag<Item> 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;
}

View file

@ -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<BiConsumer<Block.Builder, Block>> BLOCK = new HandlerList<>();
public static final HandlerRegistry<BiConsumer<Item.Builder, Item>> ITEM = new HandlerList<>();
private FabricBuilderEvent() {
}
}

View file

@ -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 = "<init>(Lnet/minecraft/block/Block$Builder;)V", at = @At("RETURN"))
public void init(Block.Builder builder, CallbackInfo info) {
for (Object o : ((HandlerList<BiConsumer<Block.Builder, Block>>) FabricBuilderEvent.BLOCK).getBackingArray()) {
((BiConsumer<Block.Builder, Block>) o).accept(builder, (Block) (Object) this);
}
}
}

View file

@ -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 = "<init>(Lnet/minecraft/item/Item$Builder;)V", at = @At("RETURN"))
public void init(Item.Builder builder, CallbackInfo info) {
for (Object o : ((HandlerList<BiConsumer<Item.Builder, Item>>) FabricBuilderEvent.ITEM).getBackingArray()) {
((BiConsumer<Item.Builder, Item>) o).accept(builder, (Item) (Object) this);
}
}
}

View file

@ -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<Item> tag, int miningLevel);
}
private static class EntryImpl implements Entry {
@SuppressWarnings("unchecked")
private Tag<Item>[] tags = (Tag<Item>[]) 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<Item> tag, int miningLevel) {
@Override
public void putBreakByTool(Tag<Item> 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<Block, Entry> entries = new HashMap<>();
private static final Map<Block.Builder, EntryImpl> entriesPre = new HashMap<>();
private static final Map<Block, EntryImpl> 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<Item> 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++) {

View file

@ -4,7 +4,9 @@
"compatibilityLevel": "JAVA_8",
"mixins": [
"commands.MixinServerCommandManager",
"helpers.MixinBlock",
"helpers.MixinBlockBuilder",
"helpers.MixinItem",
"networking.MixinServerPlayNetworkHandler",
"networking.MixinSPacketCustomPayload",
"registry.MixinBootstrap",