NaturesAura/src/main/java/de/ellpeck/naturesaura/items/ItemMultiblockMaker.java

91 lines
3.2 KiB
Java
Raw Normal View History

2018-11-07 13:33:49 +01:00
package de.ellpeck.naturesaura.items;
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import de.ellpeck.naturesaura.api.multiblock.IMultiblock;
import de.ellpeck.naturesaura.blocks.multi.Multiblock;
2021-12-15 16:24:53 +01:00
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
2018-11-07 13:33:49 +01:00
import java.util.ArrayList;
import java.util.List;
2020-01-26 01:41:49 +01:00
public class ItemMultiblockMaker extends ItemImpl {
2018-11-07 13:33:49 +01:00
private static List<IMultiblock> multiblocks;
2018-11-07 13:33:49 +01:00
2020-01-26 01:41:49 +01:00
public ItemMultiblockMaker() {
2020-02-07 23:50:16 +01:00
super("multiblock_maker");
2018-11-07 13:33:49 +01:00
}
@Override
2021-12-15 16:24:53 +01:00
public InteractionResultHolder<ItemStack> use(Level levelIn, Player playerIn, InteractionHand handIn) {
2021-12-15 16:30:22 +01:00
var stack = playerIn.getItemInHand(handIn);
2021-12-04 15:40:09 +01:00
if (!levelIn.isClientSide && playerIn.isCreative()) {
var curr = getMultiblockId(stack);
2021-12-15 16:30:22 +01:00
var next = (curr + 1) % multiblocks().size();
2019-11-04 19:08:49 +01:00
stack.getOrCreateTag().putInt("multiblock", next);
2018-11-07 13:33:49 +01:00
}
2021-12-15 16:24:53 +01:00
return new InteractionResultHolder<>(InteractionResult.SUCCESS, stack);
2018-11-07 13:33:49 +01:00
}
@Override
2021-12-15 16:24:53 +01:00
public InteractionResult useOn(UseOnContext context) {
2021-12-15 16:30:22 +01:00
var player = context.getPlayer();
2019-11-04 19:08:49 +01:00
if (player.isCreative()) {
var multi = getMultiblock(player.getItemInHand(context.getHand()));
2018-11-07 13:33:49 +01:00
if (multi == null)
2021-12-04 15:40:09 +01:00
return InteractionResult.PASS;
2018-11-07 13:33:49 +01:00
2021-12-04 15:40:09 +01:00
if (!context.getLevel().isClientSide)
2021-12-15 16:24:53 +01:00
multi.forEach(context.getClickedPos().above(), (char) 0, (blockPos, matcher) -> {
context.getLevel().setBlockAndUpdate(blockPos, matcher.defaultState());
return true;
});
2018-11-07 13:33:49 +01:00
2021-12-04 15:40:09 +01:00
return InteractionResult.SUCCESS;
2018-11-07 13:33:49 +01:00
}
2021-12-04 15:40:09 +01:00
return InteractionResult.PASS;
2018-11-07 13:33:49 +01:00
}
@Override
2021-12-15 16:24:53 +01:00
public Component getName(ItemStack stack) {
2021-12-15 16:30:22 +01:00
var name = (MutableComponent) super.getName(stack);
var multi = getMultiblock(stack);
2021-12-15 16:24:53 +01:00
return multi == null ? name : name.append(" (" + multi.getName() + ")");
2018-11-07 13:33:49 +01:00
}
2021-12-15 16:24:53 +01:00
private static List<IMultiblock> multiblocks() {
if (multiblocks == null) {
// some weird mixins call getName way too early, before multiblocks are initialized
if (NaturesAuraAPI.MULTIBLOCKS.isEmpty())
return null;
multiblocks = new ArrayList<>();
multiblocks.addAll(NaturesAuraAPI.MULTIBLOCKS.values());
}
return multiblocks;
}
private static int getMultiblockId(ItemStack stack) {
if (!stack.hasTag())
return -1;
return stack.getTag().getInt("multiblock");
}
private static IMultiblock getMultiblock(ItemStack stack) {
var multiblocks = multiblocks();
if (multiblocks == null)
return null;
var id = getMultiblockId(stack);
if (id < 0 || id >= multiblocks.size())
return null;
return multiblocks.get(id);
}
2018-11-07 13:33:49 +01:00
}