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;
|
|
|
|
|
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;
|
2016-11-16 16:59:00 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
|
2017-02-14 21:50:03 +01:00
|
|
|
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;
|
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;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
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;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.nbt.CompoundNBT;
|
2021-02-27 13:24:45 +01:00
|
|
|
import net.minecraft.util.Direction;
|
2021-02-27 16:33:00 +01:00
|
|
|
import net.minecraft.util.EnumActionResult;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.util.Hand;
|
2016-11-05 15:36:16 +01:00
|
|
|
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;
|
2021-02-26 22:15:48 +01:00
|
|
|
import org.apache.commons.lang3.tuple.Pair;
|
|
|
|
|
|
|
|
import java.util.List;
|
2016-11-05 15:36:16 +01:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
private static boolean removeFittingItem(BlockState state, PlayerEntity player) {
|
2016-11-16 20:31:16 +01:00
|
|
|
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++) {
|
2016-11-16 20:31:16 +01:00
|
|
|
ItemStack slot = player.inventory.getStackInSlot(i);
|
2018-03-23 04:49:50 +01:00
|
|
|
if (StackUtil.isValid(slot) && slot.isItemEqual(stack)) {
|
2018-06-23 01:39:30 +02:00
|
|
|
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());
|
2016-11-16 20:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
private static void saveData(ItemStack pickBlock, BlockState state, ItemStack wand) {
|
|
|
|
if (!wand.hasTagCompound()) {
|
|
|
|
wand.setTagCompound(new CompoundNBT());
|
|
|
|
}
|
2018-03-23 04:49:50 +01:00
|
|
|
wand.getTagCompound().setInteger("state", Block.getStateId(state));
|
|
|
|
wand.getTagCompound().setString("name", pickBlock.getDisplayName());
|
2016-11-16 20:31:16 +01:00
|
|
|
|
|
|
|
}
|
2018-03-23 04:49:50 +01:00
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
private static Pair<BlockState, String> loadData(ItemStack stack) {
|
|
|
|
if (stack.hasTagCompound()) {
|
|
|
|
return Pair.of(Block.getStateById(stack.getTagCompound().getInteger("state")), stack.getTagCompound().getString("name"));
|
|
|
|
}
|
2016-11-16 20:31:16 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-05 15:36:16 +01:00
|
|
|
@Override
|
2021-02-27 13:24:45 +01:00
|
|
|
public EnumActionResult onItemUse(PlayerEntity player, World world, BlockPos pos, Hand hand, Direction 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()) {
|
2021-02-26 22:15:48 +01:00
|
|
|
BlockState 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()) {
|
2021-02-26 22:15:48 +01:00
|
|
|
stack.setTagCompound(new CompoundNBT());
|
2016-11-05 15:36:16 +01:00
|
|
|
}
|
2021-02-26 22:15:48 +01:00
|
|
|
CompoundNBT compound = stack.getTagCompound();
|
2016-11-05 15:36:16 +01:00
|
|
|
|
2021-02-27 16:33:00 +01:00
|
|
|
if (compound.getInt("CurrX") == 0 && compound.getInt("CurrY") == 0 && compound.getInt("CurrZ") == 0) {
|
|
|
|
compound.putInt("FirstX", pos.getX());
|
|
|
|
compound.putInt("FirstY", pos.getY());
|
|
|
|
compound.putInt("FirstZ", pos.getZ());
|
2016-11-05 15:36:16 +01:00
|
|
|
|
|
|
|
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;
|
2021-02-26 22:15:48 +01:00
|
|
|
if (entity instanceof PlayerEntity) {
|
|
|
|
RayTraceResult result = WorldUtil.getNearestBlockWithDefaultReachDistance(world, (PlayerEntity) entity);
|
2018-03-23 04:49:50 +01:00
|
|
|
if (result != null && result.getBlockPos() != null) {
|
|
|
|
if (!stack.hasTagCompound()) {
|
2021-02-26 22:15:48 +01:00
|
|
|
stack.setTagCompound(new CompoundNBT());
|
2016-11-05 16:16:42 +01:00
|
|
|
}
|
2021-02-26 22:15:48 +01:00
|
|
|
CompoundNBT compound = stack.getTagCompound();
|
2016-11-05 15:36:16 +01:00
|
|
|
|
2016-11-05 16:16:42 +01:00
|
|
|
BlockPos pos = result.getBlockPos();
|
2021-02-27 16:33:00 +01:00
|
|
|
compound.putInt("SecondX", pos.getX());
|
|
|
|
compound.putInt("SecondY", pos.getY());
|
|
|
|
compound.putInt("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) {
|
2021-02-26 22:15:48 +01:00
|
|
|
if (entity instanceof PlayerEntity && stack.hasTagCompound()) {
|
|
|
|
PlayerEntity player = (PlayerEntity) entity;
|
2016-11-05 16:16:42 +01:00
|
|
|
boolean creative = player.capabilities.isCreativeMode;
|
2016-11-05 15:36:16 +01:00
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
CompoundNBT compound = stack.getTagCompound();
|
2016-11-05 15:36:16 +01:00
|
|
|
|
2021-02-27 16:33:00 +01:00
|
|
|
BlockPos firstPos = new BlockPos(compound.getInt("FirstX"), compound.getInt("FirstY"), compound.getInt("FirstZ"));
|
|
|
|
BlockPos secondPos = new BlockPos(compound.getInt("SecondX"), compound.getInt("SecondY"), compound.getInt("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
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
Pair<BlockState, String> data = loadData(stack);
|
2018-03-23 04:49:50 +01:00
|
|
|
if (data != null && (creative || this.getEnergyStored(stack) >= energyUse)) {
|
2021-02-26 22:15:48 +01:00
|
|
|
BlockState 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
|
|
|
|
2021-02-27 16:33:00 +01:00
|
|
|
int currX = compound.getInt("CurrX");
|
|
|
|
int currY = compound.getInt("CurrY");
|
|
|
|
int currZ = compound.getInt("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);
|
2021-02-26 22:15:48 +01:00
|
|
|
BlockState 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);
|
|
|
|
|
2016-11-07 19:58:34 +01:00
|
|
|
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) {
|
2016-11-23 18:10:55 +01:00
|
|
|
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) {
|
2021-02-27 16:33:00 +01:00
|
|
|
compound.putInt("CurrX", currX);
|
|
|
|
compound.putInt("CurrY", currY);
|
|
|
|
compound.putInt("CurrZ", currZ);
|
2016-11-05 16:16:42 +01:00
|
|
|
}
|
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
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
Pair<BlockState, String> data = loadData(stack);
|
2018-03-23 04:49:50 +01:00
|
|
|
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
|
|
|
}
|