(#53) Fuel/composter hooks

This commit is contained in:
asie 2019-01-25 23:44:56 +01:00
parent 8285a8c056
commit 3a17200c4d
9 changed files with 324 additions and 0 deletions

View file

@ -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<Float> {
public static final CompostingChanceRegistry INSTANCE = new CompostingChanceRegistryImpl();
}

View file

@ -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<Integer> {
public static final FuelRegistry INSTANCE = new FuelRegistryImpl();
}

View file

@ -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> {
V get(Block block);
void add(Block block, V value);
void add(Tag<Block> tag, V value);
void remove(Block block);
void remove(Tag<Block> tag);
}

View file

@ -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> {
V get(ItemProvider item);
void add(ItemProvider item, V value);
void add(Tag<Item> tag, V value);
void remove(ItemProvider item);
void remove(Tag<Item> tag);
}

View file

@ -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<Item> 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<Item> tag) {
throw new UnsupportedOperationException("Tags currently not supported!");
}
}

View file

@ -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<ItemProvider> itemCookTimes = new Object2IntLinkedOpenHashMap<>();
private final Object2IntMap<Tag<Item>> 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<Item> 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<Item> tag) {
add(tag, 0);
}
public void apply(Map<Item, Integer> 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<Item> 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);
}
}
}
}
}

View file

@ -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<Map<Item, Integer>> info) {
FuelRegistryImpl.INSTANCE.apply(info.getReturnValue());
}
}

View file

@ -17,6 +17,7 @@
"events.server.MixinMinecraftServer", "events.server.MixinMinecraftServer",
"events.tick.MixinMinecraftServer", "events.tick.MixinMinecraftServer",
"events.tick.MixinWorld", "events.tick.MixinWorld",
"item.MixinAbstractFurnaceBlockEntity",
"itemgroup.MixinItemGroup", "itemgroup.MixinItemGroup",
"misc.MixinCrashReport", "misc.MixinCrashReport",
"networking.MixinServerPlayNetworkHandler", "networking.MixinServerPlayNetworkHandler",

View file

@ -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);
}
}