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

237 lines
9.8 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;
import de.ellpeck.actuallyadditions.mod.items.base.ItemEnergy;
import de.ellpeck.actuallyadditions.mod.util.Help;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
2016-11-05 15:36:16 +01:00
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
2016-11-05 15:36:16 +01:00
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.LivingEntity;
2021-02-26 22:15:48 +01:00
import net.minecraft.entity.player.PlayerEntity;
2016-11-05 15:36:16 +01:00
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUseContext;
2021-02-26 22:15:48 +01:00
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.NBTUtil;
import net.minecraft.util.ActionResultType;
2016-11-05 15:36:16 +01:00
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
2016-11-05 15:36:16 +01:00
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.text.IFormattableTextComponent;
import net.minecraft.util.text.ITextComponent;
2016-11-05 15:36:16 +01:00
import net.minecraft.world.World;
2021-02-26 22:15:48 +01:00
import javax.annotation.Nullable;
2021-02-26 22:15:48 +01:00
import java.util.List;
import java.util.Optional;
2016-11-05 15:36:16 +01:00
2018-03-23 04:49:50 +01:00
public class ItemFillingWand extends ItemEnergy {
public ItemFillingWand() {
super(500000, 1000);
2016-11-05 15:36:16 +01:00
}
2021-02-26 22:15:48 +01:00
private static boolean removeFittingItem(BlockState state, PlayerEntity player) {
Block block = state.getBlock();
ItemStack stack = new ItemStack(block, 1);
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;
}
private static void saveData(BlockState state, ItemStack wand) {
wand.getOrCreateTag().put("state", NBTUtil.writeBlockState(state));
}
2018-03-23 04:49:50 +01:00
private static Optional<BlockState> loadData(ItemStack stack) {
if (stack.getOrCreateTag().contains("state")) {
return Optional.of(NBTUtil.readBlockState(stack.getOrCreateTag().getCompound("state")));
2021-02-26 22:15:48 +01:00
}
return Optional.empty();
}
2016-11-05 15:36:16 +01:00
@Override
public ActionResultType onItemUse(ItemUseContext context) {
if (context.getPlayer() == null) {
return ActionResultType.PASS;
}
ItemStack stack = context.getPlayer().getHeldItem(context.getHand());
if (!context.getWorld().isRemote && context.getPlayer().getItemInUseCount() <= 0) {
if (context.getPlayer().isSneaking()) {
BlockState state = context.getWorld().getBlockState(context.getPos());
saveData(state, stack);
return ActionResultType.SUCCESS;
} else if (loadData(stack).isPresent()) {
CompoundNBT compound = stack.getOrCreateTag();
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", context.getPos().getX());
compound.putInt("FirstY", context.getPos().getY());
compound.putInt("FirstZ", context.getPos().getZ());
2016-11-05 15:36:16 +01:00
context.getPlayer().setActiveHand(context.getHand());
return ActionResultType.SUCCESS;
2016-11-05 15:36:16 +01:00
}
}
}
return super.onItemUse(context);
2016-11-05 15:36:16 +01:00
}
@Override
public void onPlayerStoppedUsing(ItemStack stack, World world, LivingEntity entity, int timeLeft) {
2018-03-23 04:49:50 +01:00
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);
if (result instanceof BlockRayTraceResult) {
CompoundNBT compound = stack.getOrCreateTag();
2016-11-05 15:36:16 +01:00
BlockPos pos = ((BlockRayTraceResult) result).getPos();
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
public void inventoryTick(ItemStack stack, World world, Entity entity, int itemSlot, boolean isSelected) {
super.inventoryTick(stack, world, entity, itemSlot, isSelected);
2016-11-05 15:36:16 +01:00
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 PlayerEntity && stack.hasTag()) {
2021-02-26 22:15:48 +01:00
PlayerEntity player = (PlayerEntity) entity;
boolean creative = player.isCreative();
2016-11-05 15:36:16 +01:00
CompoundNBT compound = stack.getOrCreateTag();
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
if (!BlockPos.ZERO.equals(firstPos) && !BlockPos.ZERO.equals(secondPos)) {
2016-11-05 16:16:42 +01:00
int energyUse = 1500;
2016-11-05 15:36:16 +01:00
Optional<BlockState> data = loadData(stack);
if (data.isPresent() && (creative || this.getEnergyStored(stack) >= energyUse)) {
BlockState replaceState = data.get(); // not the best way to do this.
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
if (state.getMaterial().isReplaceable() && replaceState.isValidPosition(world, pos)) {
2018-03-23 04:49:50 +01:00
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) {
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
public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
super.addInformation(stack, worldIn, tooltip, flagIn);
2016-11-05 16:16:42 +01:00
IFormattableTextComponent display = loadData(stack)
.map(state -> state.getBlock().getTranslatedName())
.orElse(Help.Trans("tooltip", "item_filling_wand.selectedBlock.none"));
2016-11-05 15:36:16 +01:00
tooltip.add(Help.Trans("tooltip", "item_filling_wand.selectedBlock", display.getString()));
2016-11-05 16:16:42 +01:00
}
2016-11-05 15:36:16 +01:00
@Override
public int getUseDuration(ItemStack stack) {
2016-11-05 15:36:16 +01:00
return Integer.MAX_VALUE;
}
}