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

239 lines
9.9 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.StackUtil;
2024-03-09 01:24:19 +01:00
import de.ellpeck.actuallyadditions.mod.util.Util;
2024-03-02 21:23:08 +01:00
import net.minecraft.core.BlockPos;
2024-03-03 01:20:53 +01:00
import net.minecraft.core.registries.BuiltInRegistries;
2024-03-02 21:23:08 +01:00
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
2024-03-04 20:21:48 +01:00
import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;
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
public class Filler extends ItemEnergy {
2018-03-23 04:49:50 +01:00
public Filler() {
super(500000, 1000);
2016-11-05 15:36:16 +01:00
}
2024-03-02 21:23:08 +01:00
private static boolean removeFittingItem(BlockState state, Player player) {
Block block = state.getBlock();
ItemStack stack = new ItemStack(block, 1);
2018-03-23 04:49:50 +01:00
if (StackUtil.isValid(stack)) {
2024-03-02 21:23:08 +01:00
for (int i = 0; i < player.getInventory().getContainerSize(); i++) {
ItemStack slot = player.getInventory().getItem(i);
2024-03-03 01:20:53 +01:00
if (StackUtil.isValid(slot) && ItemStack.isSameItem(slot, stack)) {
slot.shrink(1);
2018-03-23 04:49:50 +01:00
if (!StackUtil.isValid(slot)) {
2024-03-02 21:23:08 +01:00
player.getInventory().setItem(i, ItemStack.EMPTY);
}
return true;
}
}
}
return false;
}
private static void saveData(BlockState state, ItemStack wand) {
2024-03-02 21:23:08 +01:00
wand.getOrCreateTag().put("state", NbtUtils.writeBlockState(state));
}
2018-03-23 04:49:50 +01:00
private static Optional<BlockState> loadData(ItemStack stack) {
if (stack.getOrCreateTag().contains("state")) {
2024-03-03 01:20:53 +01:00
return Optional.of(NbtUtils.readBlockState(BuiltInRegistries.BLOCK.asLookup(), stack.getOrCreateTag().getCompound("state")));
2021-02-26 22:15:48 +01:00
}
return Optional.empty();
}
2016-11-05 15:36:16 +01:00
@Override
2024-03-02 21:23:08 +01:00
public InteractionResult useOn(UseOnContext context) {
if (context.getPlayer() == null) {
2024-03-02 21:23:08 +01:00
return InteractionResult.PASS;
}
ItemStack stack = context.getPlayer().getItemInHand(context.getHand());
if (!context.getLevel().isClientSide && context.getPlayer().getUseItemRemainingTicks() <= 0) {
2021-12-30 18:30:01 +01:00
if (context.getPlayer().isCrouching()) {
BlockState state = context.getLevel().getBlockState(context.getClickedPos());
saveData(state, stack);
2024-03-02 21:23:08 +01:00
return InteractionResult.SUCCESS;
} else if (loadData(stack).isPresent()) {
2024-03-02 21:23:08 +01:00
CompoundTag 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.getClickedPos().getX());
compound.putInt("FirstY", context.getClickedPos().getY());
compound.putInt("FirstZ", context.getClickedPos().getZ());
2016-11-05 15:36:16 +01:00
context.getPlayer().startUsingItem(context.getHand());
2024-03-02 21:23:08 +01:00
return InteractionResult.SUCCESS;
2016-11-05 15:36:16 +01:00
}
}
}
return super.useOn(context);
2016-11-05 15:36:16 +01:00
}
@Override
2024-03-02 21:23:08 +01:00
public void releaseUsing(ItemStack stack, Level world, LivingEntity entity, int timeLeft) {
if (!world.isClientSide) {
2016-11-05 16:16:42 +01:00
boolean clear = true;
2024-03-09 01:13:09 +01:00
if (entity instanceof Player player) {
2024-03-09 01:24:19 +01:00
HitResult result = player.pick(Util.getReachDistance(player), 1f, false);
2024-03-02 21:23:08 +01:00
if (result instanceof BlockHitResult) {
CompoundTag compound = stack.getOrCreateTag();
2016-11-05 15:36:16 +01:00
2024-03-02 21:23:08 +01:00
BlockPos pos = ((BlockHitResult) 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.releaseUsing(stack, world, entity, timeLeft);
2016-11-05 15:36:16 +01:00
}
@Override
2024-03-02 21:23:08 +01:00
public void inventoryTick(ItemStack stack, Level world, Entity entity, int itemSlot, boolean isSelected) {
super.inventoryTick(stack, world, entity, itemSlot, isSelected);
2016-11-05 15:36:16 +01:00
if (!world.isClientSide) {
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) {
2024-03-03 01:20:53 +01:00
if (entity instanceof Player player && stack.hasTag()) {
boolean creative = player.isCreative();
2016-11-05 15:36:16 +01:00
2024-03-02 21:23:08 +01:00
CompoundTag 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
2024-03-03 01:20:53 +01:00
if (state.canBeReplaced() && replaceState.canSurvive(world, pos)) {
2018-03-23 04:49:50 +01:00
if (creative || removeFittingItem(replaceState, player)) {
world.setBlock(pos, replaceState, 2);
2016-11-05 16:16:42 +01:00
SoundType sound = replaceState.getBlock().getSoundType(replaceState, world, pos, player);
2024-03-02 21:23:08 +01:00
world.playSound(null, pos, sound.getPlaceSound(), SoundSource.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
}
2021-12-30 18:30:01 +01:00
@OnlyIn(Dist.CLIENT)
2016-11-05 16:16:42 +01:00
@Override
2024-03-02 21:23:08 +01:00
public void appendHoverText(ItemStack stack, @Nullable Level worldIn, List<Component> tooltip, TooltipFlag flagIn) {
super.appendHoverText(stack, worldIn, tooltip, flagIn);
2016-11-05 16:16:42 +01:00
2024-03-02 21:23:08 +01:00
MutableComponent display = loadData(stack)
.map(state -> state.getBlock().getName())
2024-03-03 01:20:53 +01:00
.orElse(Component.translatable("tooltip.actuallyadditions.item_filling_wand.selected_block.none"));
2016-11-05 15:36:16 +01:00
2024-03-03 01:20:53 +01:00
tooltip.add(Component.translatable("tooltip.actuallyadditions.item_filling_wand.selected_block", 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;
}
}