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

83 lines
2.8 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;
2019-10-20 22:30:49 +02:00
import net.minecraft.entity.player.PlayerEntity;
2018-11-07 13:33:49 +01:00
import net.minecraft.item.ItemStack;
2019-11-04 19:08:49 +01:00
import net.minecraft.item.ItemUseContext;
2018-11-07 13:33:49 +01:00
import net.minecraft.util.ActionResult;
2019-10-20 22:30:49 +02:00
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Hand;
2019-11-04 19:08:49 +01:00
import net.minecraft.util.text.ITextComponent;
2020-09-22 03:17:02 +02:00
import net.minecraft.util.text.TextComponent;
2018-11-07 13:33:49 +01:00
import net.minecraft.world.World;
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
}
2020-02-07 15:22:30 +01:00
private static int getMultiblock(ItemStack stack) {
if (!stack.hasTag())
return -1;
return stack.getTag().getInt("multiblock");
}
private static List<IMultiblock> multiblocks() {
if (multiblocks == null) {
multiblocks = new ArrayList<>();
multiblocks.addAll(NaturesAuraAPI.MULTIBLOCKS.values());
}
return multiblocks;
}
2018-11-07 13:33:49 +01:00
@Override
2019-10-20 22:30:49 +02:00
public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
2018-11-07 13:33:49 +01:00
ItemStack stack = playerIn.getHeldItem(handIn);
2019-11-04 19:08:49 +01:00
if (!worldIn.isRemote && playerIn.isCreative()) {
2018-11-07 13:33:49 +01:00
int curr = getMultiblock(stack);
int 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
}
2019-10-20 22:30:49 +02:00
return new ActionResult<>(ActionResultType.SUCCESS, stack);
2018-11-07 13:33:49 +01:00
}
@Override
2019-11-04 19:08:49 +01:00
public ActionResultType onItemUse(ItemUseContext context) {
PlayerEntity player = context.getPlayer();
if (player.isCreative()) {
2020-01-22 01:32:26 +01:00
int id = getMultiblock(player.getHeldItem(context.getHand()));
if (id < 0)
return ActionResultType.PASS;
IMultiblock multi = multiblocks().get(id);
2018-11-07 13:33:49 +01:00
if (multi == null)
2019-10-20 22:30:49 +02:00
return ActionResultType.PASS;
2018-11-07 13:33:49 +01:00
2019-11-04 19:08:49 +01:00
if (!context.getWorld().isRemote)
multi.forEach(context.getPos().up(), (char) 0, (blockPos, matcher) -> {
context.getWorld().setBlockState(blockPos, matcher.getDefaultState());
return true;
});
2018-11-07 13:33:49 +01:00
2019-10-20 22:30:49 +02:00
return ActionResultType.SUCCESS;
2018-11-07 13:33:49 +01:00
}
2019-10-20 22:30:49 +02:00
return ActionResultType.PASS;
2018-11-07 13:33:49 +01:00
}
@Override
2019-11-04 19:08:49 +01:00
public ITextComponent getDisplayName(ItemStack stack) {
2020-09-22 03:17:02 +02:00
TextComponent name = (TextComponent) super.getDisplayName(stack);
2020-01-22 01:32:26 +01:00
int id = getMultiblock(stack);
if (id < 0)
return name;
IMultiblock multi = multiblocks().get(id);
2020-09-22 03:17:02 +02:00
return multi == null ? name : name.appendString(" (" + multi.getName() + ")");
2018-11-07 13:33:49 +01:00
}
}