2018-11-07 13:33:49 +01:00
|
|
|
package de.ellpeck.naturesaura.items;
|
|
|
|
|
2018-11-23 19:21:52 +01:00
|
|
|
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
|
|
|
|
import de.ellpeck.naturesaura.api.multiblock.IMultiblock;
|
2018-11-07 13:33:49 +01:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.util.ActionResult;
|
|
|
|
import net.minecraft.util.EnumActionResult;
|
|
|
|
import net.minecraft.util.EnumFacing;
|
|
|
|
import net.minecraft.util.EnumHand;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class ItemMultiblockMaker extends ItemImpl {
|
|
|
|
|
2018-11-23 19:21:52 +01:00
|
|
|
private static List<IMultiblock> multiblocks;
|
2018-11-07 13:33:49 +01:00
|
|
|
|
|
|
|
public ItemMultiblockMaker() {
|
|
|
|
super("multiblock_maker");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
|
|
|
|
ItemStack stack = playerIn.getHeldItem(handIn);
|
|
|
|
if (!worldIn.isRemote && playerIn.capabilities.isCreativeMode) {
|
|
|
|
int curr = getMultiblock(stack);
|
|
|
|
int next = (curr + 1) % multiblocks().size();
|
|
|
|
|
|
|
|
if (!stack.hasTagCompound())
|
|
|
|
stack.setTagCompound(new NBTTagCompound());
|
|
|
|
stack.getTagCompound().setInteger("multiblock", next);
|
|
|
|
}
|
|
|
|
return new ActionResult<>(EnumActionResult.SUCCESS, stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
|
|
|
|
if (player.capabilities.isCreativeMode) {
|
2018-11-23 19:21:52 +01:00
|
|
|
IMultiblock multi = multiblocks().get(getMultiblock(player.getHeldItem(hand)));
|
2018-11-07 13:33:49 +01:00
|
|
|
if (multi == null)
|
|
|
|
return EnumActionResult.PASS;
|
|
|
|
|
|
|
|
if (!worldIn.isRemote)
|
2018-11-07 13:38:10 +01:00
|
|
|
multi.forEach(pos.up(), (char) 0, (blockPos, matcher) -> {
|
2018-11-23 19:21:52 +01:00
|
|
|
worldIn.setBlockState(blockPos, matcher.getDefaultState());
|
2018-11-07 13:38:10 +01:00
|
|
|
return true;
|
|
|
|
});
|
2018-11-07 13:33:49 +01:00
|
|
|
|
|
|
|
return EnumActionResult.SUCCESS;
|
|
|
|
}
|
|
|
|
return EnumActionResult.PASS;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getItemStackDisplayName(ItemStack stack) {
|
|
|
|
String name = super.getItemStackDisplayName(stack);
|
2018-11-23 19:21:52 +01:00
|
|
|
IMultiblock multi = multiblocks().get(getMultiblock(stack));
|
|
|
|
return multi == null ? name : name + " (" + multi.getName() + ")";
|
2018-11-07 13:33:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static int getMultiblock(ItemStack stack) {
|
|
|
|
if (!stack.hasTagCompound())
|
|
|
|
return 0;
|
|
|
|
return stack.getTagCompound().getInteger("multiblock");
|
|
|
|
}
|
|
|
|
|
2018-11-23 19:21:52 +01:00
|
|
|
private static List<IMultiblock> multiblocks() {
|
2018-11-07 13:33:49 +01:00
|
|
|
if (multiblocks == null) {
|
|
|
|
multiblocks = new ArrayList<>();
|
2018-11-23 19:21:52 +01:00
|
|
|
multiblocks.addAll(NaturesAuraAPI.MULTIBLOCKS.values());
|
2018-11-07 13:33:49 +01:00
|
|
|
}
|
|
|
|
return multiblocks;
|
|
|
|
}
|
|
|
|
}
|