PrettyPipes/src/main/java/de/ellpeck/prettypipes/items/PipeFrameItem.java

72 lines
2.8 KiB
Java
Raw Normal View History

2020-04-20 03:37:23 +02:00
package de.ellpeck.prettypipes.items;
import de.ellpeck.prettypipes.Registry;
2020-04-20 13:12:26 +02:00
import de.ellpeck.prettypipes.Utility;
2020-04-20 03:37:23 +02:00
import de.ellpeck.prettypipes.entities.PipeFrameEntity;
2021-12-02 15:25:46 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.decoration.HangingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
2021-12-02 12:31:04 +01:00
import net.minecraft.world.item.ItemStack;
2021-12-02 15:25:46 +01:00
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
2024-02-03 15:17:58 +01:00
import net.neoforged.neoforge.registries.ForgeRegistries;
2020-04-20 03:37:23 +02:00
2020-04-20 13:12:26 +02:00
import javax.annotation.Nullable;
import java.util.List;
2020-04-20 03:37:23 +02:00
public class PipeFrameItem extends Item {
2021-12-02 15:25:46 +01:00
2020-04-20 03:37:23 +02:00
public PipeFrameItem() {
2023-07-07 19:54:52 +02:00
super(new Properties());
2020-04-20 03:37:23 +02:00
}
// HangingEntityItem copypasta mostly, since it hardcodes the entities bleh
@Override
2021-12-02 15:25:46 +01:00
public InteractionResult useOn(UseOnContext context) {
var blockpos = context.getClickedPos();
var direction = context.getClickedFace();
var blockpos1 = blockpos.relative(direction);
var playerentity = context.getPlayer();
var itemstack = context.getItemInHand();
2020-04-20 03:37:23 +02:00
if (playerentity != null && !this.canPlace(playerentity, direction, itemstack, blockpos1)) {
2021-12-02 15:25:46 +01:00
return InteractionResult.FAIL;
2020-04-20 03:37:23 +02:00
} else {
2021-12-02 15:25:46 +01:00
var world = context.getLevel();
2020-04-20 03:37:23 +02:00
HangingEntity hangingentity = new PipeFrameEntity(Registry.pipeFrameEntity, world, blockpos1, direction);
2021-12-02 15:25:46 +01:00
var compoundTag = itemstack.getTag();
if (compoundTag != null) {
EntityType.updateCustomEntityTag(world, playerentity, hangingentity, compoundTag);
2020-04-20 03:37:23 +02:00
}
2021-12-02 15:25:46 +01:00
if (hangingentity.survives()) {
if (!world.isClientSide) {
hangingentity.playPlacementSound();
world.addFreshEntity(hangingentity);
2020-04-20 03:37:23 +02:00
}
itemstack.shrink(1);
2021-12-02 15:25:46 +01:00
return InteractionResult.SUCCESS;
2020-04-20 03:37:23 +02:00
} else {
2021-12-02 15:25:46 +01:00
return InteractionResult.CONSUME;
2020-04-20 03:37:23 +02:00
}
}
}
2021-12-02 15:25:46 +01:00
protected boolean canPlace(Player playerIn, Direction directionIn, ItemStack itemStackIn, BlockPos posIn) {
2023-07-07 19:54:52 +02:00
return !directionIn.getAxis().isVertical() && playerIn.mayUseItemAt(posIn, directionIn, itemStackIn) && PipeFrameEntity.canPlace(playerIn.level(), posIn, directionIn);
2020-04-20 13:12:26 +02:00
}
@Override
2021-12-02 15:25:46 +01:00
public void appendHoverText(ItemStack stack, @Nullable Level worldIn, List<Component> tooltip, TooltipFlag flagIn) {
super.appendHoverText(stack, worldIn, tooltip, flagIn);
2022-06-27 13:57:06 +02:00
Utility.addTooltip(ForgeRegistries.ITEMS.getKey(this).getPath(), tooltip);
2020-04-20 03:37:23 +02:00
}
}