ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/items/ItemFillingWand.java

247 lines
11 KiB
Java
Raw Normal View History

2016-11-05 15:36:16 +01:00
/*
* This file ("ItemFillingWand.java") is part of the Actually Additions mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://ellpeck.de/actaddlicense
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
2016-11-05 15:36:16 +01:00
*/
package de.ellpeck.actuallyadditions.mod.items;
2019-05-02 09:10:29 +02:00
import java.util.List;
import org.apache.commons.lang3.tuple.Pair;
2018-05-10 11:38:58 +02:00
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
2016-11-05 15:36:16 +01:00
import de.ellpeck.actuallyadditions.mod.items.base.ItemEnergy;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
import de.ellpeck.actuallyadditions.mod.util.StringUtil;
2016-11-05 15:36:16 +01:00
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
import net.minecraft.block.Block;
import net.minecraft.block.SoundType;
import net.minecraft.block.state.IBlockState;
2017-06-17 00:48:49 +02:00
import net.minecraft.client.util.ITooltipFlag;
2016-11-05 15:36:16 +01:00
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
2016-11-05 16:16:42 +01:00
import net.minecraft.item.EnumRarity;
2016-11-05 15:36:16 +01:00
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
2018-03-23 04:49:50 +01:00
import net.minecraft.util.math.Vec3d;
2016-11-05 15:36:16 +01:00
import net.minecraft.world.World;
2018-03-23 04:49:50 +01:00
public class ItemFillingWand extends ItemEnergy {
public ItemFillingWand(String name) {
2016-11-21 13:38:43 +01:00
super(500000, 1000, name);
2016-11-05 15:36:16 +01:00
}
2018-03-23 04:49:50 +01:00
private static boolean removeFittingItem(IBlockState state, EntityPlayer player) {
Block block = state.getBlock();
ItemStack stack = new ItemStack(block, 1, block.damageDropped(state));
2018-03-23 04:49:50 +01:00
if (StackUtil.isValid(stack)) {
for (int i = 0; i < player.inventory.getSizeInventory(); i++) {
ItemStack slot = player.inventory.getStackInSlot(i);
2018-03-23 04:49:50 +01:00
if (StackUtil.isValid(slot) && slot.isItemEqual(stack)) {
slot.shrink(1);
2018-03-23 04:49:50 +01:00
if (!StackUtil.isValid(slot)) {
2017-11-02 22:49:53 +01:00
player.inventory.setInventorySlotContents(i, StackUtil.getEmpty());
}
return true;
}
}
}
return false;
}
2018-03-23 04:49:50 +01:00
private static void saveData(ItemStack pickBlock, IBlockState state, ItemStack wand) {
if (!wand.hasTagCompound()) wand.setTagCompound(new NBTTagCompound());
wand.getTagCompound().setInteger("state", Block.getStateId(state));
wand.getTagCompound().setString("name", pickBlock.getDisplayName());
}
2018-03-23 04:49:50 +01:00
private static Pair<IBlockState, String> loadData(ItemStack stack) {
if (stack.hasTagCompound()) return Pair.of(Block.getStateById(stack.getTagCompound().getInteger("state")), stack.getTagCompound().getString("name"));
return null;
}
2016-11-05 15:36:16 +01:00
@Override
2018-03-23 04:49:50 +01:00
public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
2016-11-19 21:11:17 +01:00
ItemStack stack = player.getHeldItem(hand);
2018-03-23 04:49:50 +01:00
if (!world.isRemote && player.getItemInUseCount() <= 0) {
if (player.isSneaking()) {
2016-11-05 15:36:16 +01:00
IBlockState state = world.getBlockState(pos);
2018-03-23 04:49:50 +01:00
ItemStack pick = state.getBlock().getPickBlock(state, world.rayTraceBlocks(player.getPositionVector(), new Vec3d(pos.getX() + hitX, pos.getY() + hitY, pos.getZ() + hitZ)), world, pos, player);
saveData(pick, state, stack);
2016-11-05 15:36:16 +01:00
return EnumActionResult.SUCCESS;
2018-03-23 04:49:50 +01:00
} else if (loadData(stack) != null) {
if (!stack.hasTagCompound()) {
2016-11-05 15:36:16 +01:00
stack.setTagCompound(new NBTTagCompound());
}
NBTTagCompound compound = stack.getTagCompound();
2018-03-23 04:49:50 +01:00
if (compound.getInteger("CurrX") == 0 && compound.getInteger("CurrY") == 0 && compound.getInteger("CurrZ") == 0) {
2016-11-05 15:36:16 +01:00
compound.setInteger("FirstX", pos.getX());
compound.setInteger("FirstY", pos.getY());
compound.setInteger("FirstZ", pos.getZ());
player.setActiveHand(hand);
return EnumActionResult.SUCCESS;
}
}
}
return EnumActionResult.PASS;
}
@Override
2018-03-23 04:49:50 +01:00
public void onPlayerStoppedUsing(ItemStack stack, World world, EntityLivingBase entity, int timeLeft) {
if (!world.isRemote) {
2016-11-05 16:16:42 +01:00
boolean clear = true;
2018-03-23 04:49:50 +01:00
if (entity instanceof EntityPlayer) {
RayTraceResult result = WorldUtil.getNearestBlockWithDefaultReachDistance(world, (EntityPlayer) entity);
if (result != null && result.getBlockPos() != null) {
if (!stack.hasTagCompound()) {
2016-11-05 16:16:42 +01:00
stack.setTagCompound(new NBTTagCompound());
}
NBTTagCompound compound = stack.getTagCompound();
2016-11-05 15:36:16 +01:00
2016-11-05 16:16:42 +01:00
BlockPos pos = result.getBlockPos();
compound.setInteger("SecondX", pos.getX());
compound.setInteger("SecondY", pos.getY());
compound.setInteger("SecondZ", pos.getZ());
2016-11-05 15:36:16 +01:00
2016-11-05 16:16:42 +01:00
clear = false;
}
2016-11-05 15:36:16 +01:00
}
2018-03-23 04:49:50 +01:00
if (clear) {
2016-11-05 16:16:42 +01:00
ItemPhantomConnector.clearStorage(stack, "FirstX", "FirstY", "FirstZ");
}
2016-11-05 15:36:16 +01:00
}
super.onPlayerStoppedUsing(stack, world, entity, timeLeft);
}
@Override
2018-03-23 04:49:50 +01:00
public void onUpdate(ItemStack stack, World world, Entity entity, int itemSlot, boolean isSelected) {
2016-11-05 15:36:16 +01:00
super.onUpdate(stack, world, entity, itemSlot, isSelected);
2018-03-23 04:49:50 +01:00
if (!world.isRemote) {
2016-11-05 16:16:42 +01:00
boolean shouldClear = false;
2016-11-05 15:36:16 +01:00
2018-03-23 04:49:50 +01:00
if (isSelected) {
if (entity instanceof EntityPlayer && stack.hasTagCompound()) {
EntityPlayer player = (EntityPlayer) entity;
2016-11-05 16:16:42 +01:00
boolean creative = player.capabilities.isCreativeMode;
2016-11-05 15:36:16 +01:00
2016-11-05 16:16:42 +01:00
NBTTagCompound compound = stack.getTagCompound();
2016-11-05 15:36:16 +01:00
2016-11-05 16:16:42 +01:00
BlockPos firstPos = new BlockPos(compound.getInteger("FirstX"), compound.getInteger("FirstY"), compound.getInteger("FirstZ"));
BlockPos secondPos = new BlockPos(compound.getInteger("SecondX"), compound.getInteger("SecondY"), compound.getInteger("SecondZ"));
2016-11-05 15:36:16 +01:00
2018-03-23 04:49:50 +01:00
if (!BlockPos.ORIGIN.equals(firstPos) && !BlockPos.ORIGIN.equals(secondPos)) {
2016-11-05 16:16:42 +01:00
int energyUse = 1500;
2016-11-05 15:36:16 +01:00
2018-03-23 04:49:50 +01:00
Pair<IBlockState, String> data = loadData(stack);
if (data != null && (creative || this.getEnergyStored(stack) >= energyUse)) {
IBlockState replaceState = data.getLeft();
2016-11-05 16:16:42 +01:00
int lowestX = Math.min(firstPos.getX(), secondPos.getX());
int lowestY = Math.min(firstPos.getY(), secondPos.getY());
int lowestZ = Math.min(firstPos.getZ(), secondPos.getZ());
2016-11-05 15:36:16 +01:00
2016-11-05 16:16:42 +01:00
int currX = compound.getInteger("CurrX");
int currY = compound.getInteger("CurrY");
int currZ = compound.getInteger("CurrZ");
2016-11-05 15:36:16 +01:00
2018-03-23 04:49:50 +01:00
BlockPos pos = new BlockPos(lowestX + currX, lowestY + currY, lowestZ + currZ);
2016-11-05 16:16:42 +01:00
IBlockState state = world.getBlockState(pos);
2016-11-05 15:36:16 +01:00
2018-03-23 04:49:50 +01:00
if (state.getBlock().isReplaceable(world, pos) && replaceState.getBlock().canPlaceBlockAt(world, pos)) {
if (creative || removeFittingItem(replaceState, player)) {
2016-11-05 16:16:42 +01:00
world.setBlockState(pos, replaceState, 2);
SoundType sound = replaceState.getBlock().getSoundType(replaceState, world, pos, player);
2018-03-23 04:49:50 +01:00
world.playSound(null, pos, sound.getPlaceSound(), SoundCategory.BLOCKS, sound.getVolume() / 2F + .5F, sound.getPitch() * 0.8F);
2016-11-05 16:16:42 +01:00
2018-03-23 04:49:50 +01:00
if (!creative) {
this.extractEnergyInternal(stack, energyUse, false);
2016-11-05 16:16:42 +01:00
}
2018-03-23 04:49:50 +01:00
} else {
2016-11-05 15:36:16 +01:00
shouldClear = true;
}
}
2018-03-23 04:49:50 +01:00
int distX = Math.abs(secondPos.getX() - firstPos.getX());
int distY = Math.abs(secondPos.getY() - firstPos.getY());
int distZ = Math.abs(secondPos.getZ() - firstPos.getZ());
2016-11-05 16:16:42 +01:00
currX++;
2018-03-23 04:49:50 +01:00
if (currX > distX) {
2016-11-05 16:16:42 +01:00
currX = 0;
currY++;
2018-03-23 04:49:50 +01:00
if (currY > distY) {
2016-11-05 16:16:42 +01:00
currY = 0;
currZ++;
2018-03-23 04:49:50 +01:00
if (currZ > distZ) {
2016-11-05 16:16:42 +01:00
shouldClear = true;
}
}
}
2018-03-23 04:49:50 +01:00
if (!shouldClear) {
2016-11-05 16:16:42 +01:00
compound.setInteger("CurrX", currX);
compound.setInteger("CurrY", currY);
compound.setInteger("CurrZ", currZ);
}
2018-03-23 04:49:50 +01:00
} else {
2016-11-05 16:16:42 +01:00
shouldClear = true;
}
2016-11-05 15:36:16 +01:00
}
}
2018-03-23 04:49:50 +01:00
} else {
2016-11-05 16:16:42 +01:00
shouldClear = true;
}
2018-03-23 04:49:50 +01:00
if (shouldClear) {
2016-11-05 16:16:42 +01:00
ItemPhantomConnector.clearStorage(stack, "FirstX", "FirstY", "FirstZ", "SecondX", "SecondY", "SecondZ", "CurrX", "CurrY", "CurrZ");
}
2016-11-05 15:36:16 +01:00
}
2016-11-05 16:16:42 +01:00
}
@Override
2018-03-23 04:49:50 +01:00
public void addInformation(ItemStack stack, World playerIn, List<String> tooltip, ITooltipFlag advanced) {
2017-06-17 00:48:49 +02:00
super.addInformation(stack, playerIn, tooltip, advanced);
2016-11-05 16:16:42 +01:00
2018-05-10 11:38:58 +02:00
String display = StringUtil.localize("tooltip." + ActuallyAdditions.MODID + ".item_filling_wand.selectedBlock.none");
2016-11-05 16:16:42 +01:00
2018-03-23 04:49:50 +01:00
Pair<IBlockState, String> data = loadData(stack);
if (data != null) {
display = data.getRight();
2016-11-05 15:36:16 +01:00
}
2018-05-10 11:38:58 +02:00
tooltip.add(String.format("%s: %s", StringUtil.localize("tooltip." + ActuallyAdditions.MODID + ".item_filling_wand.selectedBlock"), display));
2016-11-05 16:16:42 +01:00
}
2016-11-05 15:36:16 +01:00
@Override
2018-03-23 04:49:50 +01:00
public int getMaxItemUseDuration(ItemStack stack) {
2016-11-05 15:36:16 +01:00
return Integer.MAX_VALUE;
}
2016-11-05 16:16:42 +01:00
@Override
2018-03-23 04:49:50 +01:00
public EnumRarity getRarity(ItemStack stack) {
2016-11-05 16:16:42 +01:00
return EnumRarity.EPIC;
}
2016-11-05 15:36:16 +01:00
}