From 3a17200c4dc499d1436577974f7bc43dc9ed62bf Mon Sep 17 00:00:00 2001 From: asie Date: Fri, 25 Jan 2019 23:44:56 +0100 Subject: [PATCH] (#53) Fuel/composter hooks --- .../registry/CompostingChanceRegistry.java | 28 ++++++ .../fabric/api/registry/FuelRegistry.java | 27 ++++++ .../fabric/api/util/Block2ObjectMap.java | 29 ++++++ .../fabric/api/util/Item2ObjectMap.java | 29 ++++++ .../item/CompostingChanceRegistryImpl.java | 50 ++++++++++ .../fabric/impl/item/FuelRegistryImpl.java | 95 +++++++++++++++++++ .../item/MixinAbstractFurnaceBlockEntity.java | 35 +++++++ .../net.fabricmc.fabric.mixins.common.json | 1 + .../net/fabricmc/fabric/item/FuelMod.java | 30 ++++++ 9 files changed, 324 insertions(+) create mode 100644 src/main/java/net/fabricmc/fabric/api/registry/CompostingChanceRegistry.java create mode 100644 src/main/java/net/fabricmc/fabric/api/registry/FuelRegistry.java create mode 100644 src/main/java/net/fabricmc/fabric/api/util/Block2ObjectMap.java create mode 100644 src/main/java/net/fabricmc/fabric/api/util/Item2ObjectMap.java create mode 100644 src/main/java/net/fabricmc/fabric/impl/item/CompostingChanceRegistryImpl.java create mode 100644 src/main/java/net/fabricmc/fabric/impl/item/FuelRegistryImpl.java create mode 100644 src/main/java/net/fabricmc/fabric/mixin/item/MixinAbstractFurnaceBlockEntity.java create mode 100644 src/test/java/net/fabricmc/fabric/item/FuelMod.java diff --git a/src/main/java/net/fabricmc/fabric/api/registry/CompostingChanceRegistry.java b/src/main/java/net/fabricmc/fabric/api/registry/CompostingChanceRegistry.java new file mode 100644 index 000000000..878b8813e --- /dev/null +++ b/src/main/java/net/fabricmc/fabric/api/registry/CompostingChanceRegistry.java @@ -0,0 +1,28 @@ +/* + * 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.api.registry; + +import net.fabricmc.fabric.api.util.Item2ObjectMap; +import net.fabricmc.fabric.impl.item.CompostingChanceRegistryImpl; + +/** + * Registry of items to 0.0-1.0 values, defining the chance of a given item + * increasing the Composter block's level + */ +public interface CompostingChanceRegistry extends Item2ObjectMap { + public static final CompostingChanceRegistry INSTANCE = new CompostingChanceRegistryImpl(); +} diff --git a/src/main/java/net/fabricmc/fabric/api/registry/FuelRegistry.java b/src/main/java/net/fabricmc/fabric/api/registry/FuelRegistry.java new file mode 100644 index 000000000..d99510510 --- /dev/null +++ b/src/main/java/net/fabricmc/fabric/api/registry/FuelRegistry.java @@ -0,0 +1,27 @@ +/* + * 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.api.registry; + +import net.fabricmc.fabric.api.util.Item2ObjectMap; +import net.fabricmc.fabric.impl.item.FuelRegistryImpl; + +/** + * Registry of items to 0-32767 fuel burn time values, in in-game ticks. + */ +public interface FuelRegistry extends Item2ObjectMap { + public static final FuelRegistry INSTANCE = new FuelRegistryImpl(); +} diff --git a/src/main/java/net/fabricmc/fabric/api/util/Block2ObjectMap.java b/src/main/java/net/fabricmc/fabric/api/util/Block2ObjectMap.java new file mode 100644 index 000000000..703dcfe36 --- /dev/null +++ b/src/main/java/net/fabricmc/fabric/api/util/Block2ObjectMap.java @@ -0,0 +1,29 @@ +/* + * 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.api.util; + +import net.minecraft.block.Block; +import net.minecraft.item.ItemProvider; +import net.minecraft.tag.Tag; + +public interface Block2ObjectMap { + V get(Block block); + void add(Block block, V value); + void add(Tag tag, V value); + void remove(Block block); + void remove(Tag tag); +} diff --git a/src/main/java/net/fabricmc/fabric/api/util/Item2ObjectMap.java b/src/main/java/net/fabricmc/fabric/api/util/Item2ObjectMap.java new file mode 100644 index 000000000..25b507940 --- /dev/null +++ b/src/main/java/net/fabricmc/fabric/api/util/Item2ObjectMap.java @@ -0,0 +1,29 @@ +/* + * 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.api.util; + +import net.minecraft.item.Item; +import net.minecraft.item.ItemProvider; +import net.minecraft.tag.Tag; + +public interface Item2ObjectMap { + V get(ItemProvider item); + void add(ItemProvider item, V value); + void add(Tag tag, V value); + void remove(ItemProvider item); + void remove(Tag tag); +} diff --git a/src/main/java/net/fabricmc/fabric/impl/item/CompostingChanceRegistryImpl.java b/src/main/java/net/fabricmc/fabric/impl/item/CompostingChanceRegistryImpl.java new file mode 100644 index 000000000..b308d3c68 --- /dev/null +++ b/src/main/java/net/fabricmc/fabric/impl/item/CompostingChanceRegistryImpl.java @@ -0,0 +1,50 @@ +/* + * 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.impl.item; + +import net.fabricmc.fabric.api.registry.CompostingChanceRegistry; +import net.minecraft.block.ComposterBlock; +import net.minecraft.item.Item; +import net.minecraft.item.ItemProvider; +import net.minecraft.tag.Tag; + +public class CompostingChanceRegistryImpl implements CompostingChanceRegistry { + @Override + public Float get(ItemProvider item) { + return ComposterBlock.ITEM_TO_LEVEL_INCREASE_CHANCE.getOrDefault(item.getItem(), 0.0F); + } + + @Override + public void add(ItemProvider item, Float value) { + ComposterBlock.ITEM_TO_LEVEL_INCREASE_CHANCE.put(item.getItem(), value.floatValue()); + } + + @Override + public void add(Tag tag, Float value) { + throw new UnsupportedOperationException("Tags currently not supported!"); + } + + @Override + public void remove(ItemProvider item) { + ComposterBlock.ITEM_TO_LEVEL_INCREASE_CHANCE.removeFloat(item.getItem()); + } + + @Override + public void remove(Tag tag) { + throw new UnsupportedOperationException("Tags currently not supported!"); + } +} diff --git a/src/main/java/net/fabricmc/fabric/impl/item/FuelRegistryImpl.java b/src/main/java/net/fabricmc/fabric/impl/item/FuelRegistryImpl.java new file mode 100644 index 000000000..be813cb4c --- /dev/null +++ b/src/main/java/net/fabricmc/fabric/impl/item/FuelRegistryImpl.java @@ -0,0 +1,95 @@ +/* + * 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.impl.item; + +import it.unimi.dsi.fastutil.objects.Object2IntLinkedOpenHashMap; +import it.unimi.dsi.fastutil.objects.Object2IntMap; +import net.fabricmc.fabric.api.registry.FuelRegistry; +import net.minecraft.block.entity.AbstractFurnaceBlockEntity; +import net.minecraft.item.Item; +import net.minecraft.item.ItemProvider; +import net.minecraft.tag.Tag; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.Map; + +// TODO: Clamp values to 32767 (+ add hook for mods which extend the limit to disable the check?) +public class FuelRegistryImpl implements FuelRegistry { + private static final Logger LOGGER = LogManager.getLogger(); + private final Object2IntMap itemCookTimes = new Object2IntLinkedOpenHashMap<>(); + private final Object2IntMap> tagCookTimes = new Object2IntLinkedOpenHashMap<>(); + + public FuelRegistryImpl() { + + } + + @Override + public Integer get(ItemProvider item) { + return AbstractFurnaceBlockEntity.createBurnableMap().get(item.getItem()); + } + + @Override + public void add(ItemProvider item, Integer cookTime) { + if (cookTime > 32767) { + LOGGER.warn("Tried to register an overly high cookTime: " + cookTime + " > 32767! (" + item + ")"); + } + itemCookTimes.put(item, cookTime); + } + + @Override + public void add(Tag tag, Integer cookTime) { + if (cookTime > 32767) { + LOGGER.warn("Tried to register an overly high cookTime: " + cookTime + " > 32767! (" + tag.getId() + ")"); + } + tagCookTimes.put(tag, cookTime); + } + + @Override + public void remove(ItemProvider item) { + add(item, 0); + } + + @Override + public void remove(Tag tag) { + add(tag, 0); + } + + public void apply(Map map) { + for (ItemProvider item : itemCookTimes.keySet()) { + int time = itemCookTimes.getInt(item); + if (time <= 0) { + map.remove(item.getItem()); + } else { + map.put(item.getItem(), time); + } + } + + for (Tag tag : tagCookTimes.keySet()) { + int time = tagCookTimes.getInt(tag); + if (time <= 0) { + for (Item i : tag.values()) { + map.remove(i); + } + } else { + for (Item i : tag.values()) { + map.put(i, time); + } + } + } + } +} \ No newline at end of file diff --git a/src/main/java/net/fabricmc/fabric/mixin/item/MixinAbstractFurnaceBlockEntity.java b/src/main/java/net/fabricmc/fabric/mixin/item/MixinAbstractFurnaceBlockEntity.java new file mode 100644 index 000000000..0d6c21941 --- /dev/null +++ b/src/main/java/net/fabricmc/fabric/mixin/item/MixinAbstractFurnaceBlockEntity.java @@ -0,0 +1,35 @@ +/* + * 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.item; + +import net.fabricmc.fabric.impl.item.FuelRegistryImpl; +import net.minecraft.block.entity.AbstractFurnaceBlockEntity; +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.CallbackInfoReturnable; + +import java.util.Map; + +@Mixin(AbstractFurnaceBlockEntity.class) +public class MixinAbstractFurnaceBlockEntity { + @Inject(at = @At("RETURN"), method = "getBurnTimeMap") + private static void burnTimeMapHook(CallbackInfoReturnable> info) { + FuelRegistryImpl.INSTANCE.apply(info.getReturnValue()); + } +} \ No newline at end of file diff --git a/src/main/resources/net.fabricmc.fabric.mixins.common.json b/src/main/resources/net.fabricmc.fabric.mixins.common.json index e503d7084..a3f34a861 100644 --- a/src/main/resources/net.fabricmc.fabric.mixins.common.json +++ b/src/main/resources/net.fabricmc.fabric.mixins.common.json @@ -17,6 +17,7 @@ "events.server.MixinMinecraftServer", "events.tick.MixinMinecraftServer", "events.tick.MixinWorld", + "item.MixinAbstractFurnaceBlockEntity", "itemgroup.MixinItemGroup", "misc.MixinCrashReport", "networking.MixinServerPlayNetworkHandler", diff --git a/src/test/java/net/fabricmc/fabric/item/FuelMod.java b/src/test/java/net/fabricmc/fabric/item/FuelMod.java new file mode 100644 index 000000000..d03da73ba --- /dev/null +++ b/src/test/java/net/fabricmc/fabric/item/FuelMod.java @@ -0,0 +1,30 @@ +/* + * 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.item; + +import net.fabricmc.api.ModInitializer; +import net.fabricmc.fabric.api.registry.FuelRegistry; +import net.minecraft.block.Blocks; +import net.minecraft.item.Items; + +public class FuelMod implements ModInitializer { + @Override + public void onInitialize() { + FuelRegistry.INSTANCE.add(Items.APPLE, 200); + FuelRegistry.INSTANCE.remove(Blocks.OAK_PLANKS); + } +}