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

129 lines
5.2 KiB
Java
Raw Normal View History

2020-04-13 22:54:18 +02:00
package de.ellpeck.prettypipes.items;
2020-10-17 00:58:31 +02:00
import de.ellpeck.prettypipes.Utility;
2020-04-16 04:42:42 +02:00
import de.ellpeck.prettypipes.pipe.ConnectionType;
import de.ellpeck.prettypipes.pipe.PipeBlock;
2021-12-02 14:44:26 +01:00
import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
2021-12-02 15:25:46 +01:00
import net.minecraft.network.chat.Component;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.EntityBlock;
2022-06-27 13:57:06 +02:00
import net.minecraftforge.registries.ForgeRegistries;
2020-04-13 22:54:18 +02:00
2020-10-17 00:58:31 +02:00
import java.util.List;
2020-04-13 22:54:18 +02:00
public class WrenchItem extends Item {
2020-10-17 00:58:31 +02:00
2020-04-13 22:54:18 +02:00
public WrenchItem() {
2023-07-07 19:54:52 +02:00
super(new Item.Properties().stacksTo(1));
2020-04-13 22:54:18 +02:00
}
@Override
2021-12-02 15:25:46 +01:00
public InteractionResult useOn(UseOnContext context) {
var world = context.getLevel();
var pos = context.getClickedPos();
var player = context.getPlayer();
var state = world.getBlockState(pos);
2020-04-13 22:54:18 +02:00
if (!(state.getBlock() instanceof PipeBlock))
2021-12-02 15:25:46 +01:00
return InteractionResult.PASS;
var tile = Utility.getBlockEntity(PipeBlockEntity.class, world, pos);
2020-10-17 00:58:31 +02:00
if (tile == null)
2021-12-02 15:25:46 +01:00
return InteractionResult.FAIL;
2020-04-13 22:54:18 +02:00
2021-12-02 15:25:46 +01:00
if (player.isCrouching()) {
if (!world.isClientSide) {
2020-10-17 00:58:31 +02:00
if (tile.cover != null) {
// remove the cover
tile.removeCover(player, context.getHand());
2021-12-02 12:31:04 +01:00
Utility.sendBlockEntityToClients(tile);
2020-10-17 00:58:31 +02:00
} else {
// remove the pipe
PipeBlock.dropItems(world, pos, player);
2021-12-02 15:25:46 +01:00
Block.dropResources(state, world, pos, tile, null, ItemStack.EMPTY);
2020-10-17 00:58:31 +02:00
world.removeBlock(pos, false);
}
2021-12-02 15:25:46 +01:00
world.playSound(null, pos, SoundEvents.ITEM_FRAME_REMOVE_ITEM, SoundSource.PLAYERS, 1, 1);
2020-04-13 22:54:18 +02:00
}
2021-12-02 15:25:46 +01:00
return InteractionResult.sidedSuccess(world.isClientSide);
2020-10-17 00:58:31 +02:00
}
// placing covers
if (tile.cover == null) {
2021-12-02 15:25:46 +01:00
var offhand = player.getOffhandItem();
2020-10-17 00:58:31 +02:00
if (offhand.getItem() instanceof BlockItem) {
2021-12-02 15:25:46 +01:00
if (!world.isClientSide) {
var blockContext = new BlockPlaceContext(context);
var block = ((BlockItem) offhand.getItem()).getBlock();
var cover = block.getStateForPlacement(blockContext);
if (cover != null && !(block instanceof EntityBlock)) {
2020-10-17 00:58:31 +02:00
tile.cover = cover;
2021-12-02 12:31:04 +01:00
Utility.sendBlockEntityToClients(tile);
2020-10-17 00:58:31 +02:00
offhand.shrink(1);
2021-12-02 15:25:46 +01:00
world.playSound(null, pos, SoundEvents.ITEM_FRAME_ADD_ITEM, SoundSource.PLAYERS, 1, 1);
2020-10-17 00:58:31 +02:00
}
}
2021-12-02 15:25:46 +01:00
return InteractionResult.sidedSuccess(world.isClientSide);
2020-10-17 00:58:31 +02:00
}
2020-04-13 22:54:18 +02:00
}
2020-10-17 00:58:31 +02:00
// disabling directions
2021-12-02 15:25:46 +01:00
for (var entry : PipeBlock.DIR_SHAPES.entrySet()) {
var box = entry.getValue().bounds().move(pos).inflate(0.001F);
if (!box.contains(context.getClickLocation()))
2020-04-13 22:54:18 +02:00
continue;
2021-12-02 15:25:46 +01:00
var prop = PipeBlock.DIRECTIONS.get(entry.getKey());
var curr = state.getValue(prop);
2020-04-13 22:54:18 +02:00
if (curr == ConnectionType.DISCONNECTED)
continue;
2021-12-02 15:25:46 +01:00
if (!world.isClientSide) {
var newType = curr == ConnectionType.BLOCKED ? ConnectionType.CONNECTED : ConnectionType.BLOCKED;
var otherPos = pos.relative(entry.getKey());
var otherState = world.getBlockState(otherPos);
2020-04-14 01:38:48 +02:00
if (otherState.getBlock() instanceof PipeBlock) {
2021-12-02 15:25:46 +01:00
otherState = otherState.setValue(PipeBlock.DIRECTIONS.get(entry.getKey().getOpposite()), newType);
world.setBlockAndUpdate(otherPos, otherState);
2020-04-14 01:38:48 +02:00
PipeBlock.onStateChanged(world, otherPos, otherState);
}
2021-12-02 15:25:46 +01:00
var newState = state.setValue(prop, newType);
world.setBlockAndUpdate(pos, newState);
2020-04-14 01:38:48 +02:00
PipeBlock.onStateChanged(world, pos, newState);
2021-12-02 15:25:46 +01:00
world.playSound(null, pos, SoundEvents.ITEM_FRAME_ROTATE_ITEM, SoundSource.PLAYERS, 1, 1);
2020-04-13 22:54:18 +02:00
}
2021-12-02 15:25:46 +01:00
return InteractionResult.sidedSuccess(world.isClientSide);
2020-04-13 22:54:18 +02:00
}
2021-12-02 15:25:46 +01:00
return InteractionResult.PASS;
2020-04-13 22:54:18 +02:00
}
2020-10-17 00:58:31 +02:00
@Override
2021-12-02 15:25:46 +01:00
public void appendHoverText(ItemStack stack, Level worldIn, List<Component> tooltip, TooltipFlag flagIn) {
2022-06-27 13:57:06 +02:00
Utility.addTooltip(ForgeRegistries.ITEMS.getKey(this).getPath(), tooltip);
2020-10-17 00:58:31 +02:00
}
@Override
public boolean isEnchantable(ItemStack stack) {
return true;
}
@Override
public int getEnchantmentValue(ItemStack stack) {
2020-10-17 00:58:31 +02:00
return 1;
}
@Override
public boolean canApplyAtEnchantingTable(ItemStack stack, Enchantment enchantment) {
return enchantment == Enchantments.SILK_TOUCH;
}
2020-04-13 22:54:18 +02:00
}