mirror of
https://github.com/Ellpeck/PrettyPipes.git
synced 2024-09-24 15:11:04 +02:00
75 lines
3.3 KiB
Java
75 lines
3.3 KiB
Java
package de.ellpeck.prettypipes.items;
|
|
|
|
import de.ellpeck.prettypipes.Registry;
|
|
import de.ellpeck.prettypipes.blocks.pipe.PipeBlock;
|
|
import de.ellpeck.prettypipes.blocks.pipe.ConnectionType;
|
|
import net.minecraft.block.Block;
|
|
import net.minecraft.block.BlockState;
|
|
import net.minecraft.item.Item;
|
|
import net.minecraft.item.ItemStack;
|
|
import net.minecraft.item.ItemUseContext;
|
|
import net.minecraft.state.EnumProperty;
|
|
import net.minecraft.util.ActionResultType;
|
|
import net.minecraft.util.Direction;
|
|
import net.minecraft.util.SoundCategory;
|
|
import net.minecraft.util.SoundEvents;
|
|
import net.minecraft.util.math.AxisAlignedBB;
|
|
import net.minecraft.util.math.BlockPos;
|
|
import net.minecraft.util.math.shapes.VoxelShape;
|
|
import net.minecraft.world.World;
|
|
|
|
import java.util.Map;
|
|
|
|
public class WrenchItem extends Item {
|
|
public WrenchItem() {
|
|
super(new Item.Properties().maxStackSize(1).group(Registry.GROUP));
|
|
}
|
|
|
|
@Override
|
|
public ActionResultType onItemUse(ItemUseContext context) {
|
|
World world = context.getWorld();
|
|
BlockPos pos = context.getPos();
|
|
BlockState state = world.getBlockState(pos);
|
|
if (!(state.getBlock() instanceof PipeBlock))
|
|
return ActionResultType.PASS;
|
|
|
|
if (context.getPlayer().isShiftKeyDown()) {
|
|
if (!world.isRemote) {
|
|
Block.spawnDrops(state, world, pos, world.getTileEntity(pos), null, ItemStack.EMPTY);
|
|
world.playSound(null, pos, SoundEvents.ENTITY_ITEM_FRAME_REMOVE_ITEM, SoundCategory.PLAYERS, 1, 1);
|
|
world.removeBlock(pos, false);
|
|
}
|
|
return ActionResultType.SUCCESS;
|
|
}
|
|
|
|
// Blocking
|
|
for (Map.Entry<Direction, VoxelShape> entry : PipeBlock.DIR_SHAPES.entrySet()) {
|
|
AxisAlignedBB box = entry.getValue().getBoundingBox().offset(pos).grow(0.001F);
|
|
if (!box.contains(context.getHitVec()))
|
|
continue;
|
|
EnumProperty<ConnectionType> prop = PipeBlock.DIRECTIONS.get(entry.getKey());
|
|
ConnectionType curr = state.get(prop);
|
|
if (curr == ConnectionType.DISCONNECTED)
|
|
continue;
|
|
|
|
if (!world.isRemote) {
|
|
ConnectionType newType = curr == ConnectionType.BLOCKED ? ConnectionType.CONNECTED_PIPE : ConnectionType.BLOCKED;
|
|
BlockPos otherPos = pos.offset(entry.getKey());
|
|
BlockState otherState = world.getBlockState(otherPos);
|
|
if (otherState.getBlock() instanceof PipeBlock) {
|
|
otherState = otherState.with(PipeBlock.DIRECTIONS.get(entry.getKey().getOpposite()), newType);
|
|
world.setBlockState(otherPos, otherState);
|
|
PipeBlock.onStateChanged(world, otherPos, otherState);
|
|
} else if (newType == ConnectionType.CONNECTED_PIPE) {
|
|
newType = ConnectionType.CONNECTED_INVENTORY;
|
|
}
|
|
BlockState newState = state.with(prop, newType);
|
|
world.setBlockState(pos, newState);
|
|
PipeBlock.onStateChanged(world, pos, newState);
|
|
world.playSound(null, pos, SoundEvents.ENTITY_ITEM_FRAME_ROTATE_ITEM, SoundCategory.PLAYERS, 1, 1);
|
|
}
|
|
return ActionResultType.SUCCESS;
|
|
}
|
|
return ActionResultType.PASS;
|
|
}
|
|
}
|