mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-09-02 10:37:54 +00:00
Clean up disk <-> drive right clicking
Oh dear. I'd originally set out to *remove* logic from DiskItem — we're so close to being able to remove this item in 1.21! However, while looking at this code, I realised I could remove the whole Forge-specific doesSneakBypassUse. We now remove the use hook on the block, and override useOn on the item. Obvious in retrospect!
This commit is contained in:
@@ -4,19 +4,18 @@
|
||||
|
||||
package dan200.computercraft.shared.media.items;
|
||||
|
||||
import dan200.computercraft.annotations.ForgeOverride;
|
||||
import dan200.computercraft.core.util.Colour;
|
||||
import dan200.computercraft.shared.ModRegistry;
|
||||
import dan200.computercraft.shared.common.IColouredItem;
|
||||
import dan200.computercraft.shared.peripheral.diskdrive.DiskDriveBlock;
|
||||
import net.minecraft.ChatFormatting;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.InteractionResult;
|
||||
import net.minecraft.world.item.Item;
|
||||
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.LevelReader;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
@@ -47,9 +46,9 @@ public class DiskItem extends Item implements IColouredItem {
|
||||
}
|
||||
}
|
||||
|
||||
@ForgeOverride
|
||||
public boolean doesSneakBypassUse(ItemStack stack, LevelReader world, BlockPos pos, Player player) {
|
||||
return true;
|
||||
@Override
|
||||
public InteractionResult useOn(UseOnContext context) {
|
||||
return DiskDriveBlock.defaultUseItemOn(context);
|
||||
}
|
||||
|
||||
public static int getDiskID(ItemStack stack) {
|
||||
|
@@ -4,17 +4,16 @@
|
||||
|
||||
package dan200.computercraft.shared.media.items;
|
||||
|
||||
import dan200.computercraft.annotations.ForgeOverride;
|
||||
import dan200.computercraft.core.util.Colour;
|
||||
import dan200.computercraft.shared.ModRegistry;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import dan200.computercraft.shared.peripheral.diskdrive.DiskDriveBlock;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.InteractionResult;
|
||||
import net.minecraft.world.item.Item;
|
||||
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.LevelReader;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
@@ -34,9 +33,9 @@ public class TreasureDiskItem extends Item {
|
||||
if (!label.isEmpty()) list.add(Component.literal(label));
|
||||
}
|
||||
|
||||
@ForgeOverride
|
||||
public boolean doesSneakBypassUse(ItemStack stack, LevelReader world, BlockPos pos, Player player) {
|
||||
return true;
|
||||
@Override
|
||||
public InteractionResult useOn(UseOnContext context) {
|
||||
return DiskDriveBlock.defaultUseItemOn(context);
|
||||
}
|
||||
|
||||
public static ItemStack create(String subPath, int colourIndex) {
|
||||
|
@@ -6,12 +6,11 @@ package dan200.computercraft.shared.peripheral.diskdrive;
|
||||
|
||||
import dan200.computercraft.shared.ModRegistry;
|
||||
import dan200.computercraft.shared.common.HorizontalContainerBlock;
|
||||
import dan200.computercraft.shared.platform.PlatformHelper;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.world.InteractionHand;
|
||||
import net.minecraft.world.InteractionResult;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.item.context.UseOnContext;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.block.BaseEntityBlock;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
@@ -21,7 +20,6 @@ import net.minecraft.world.level.block.entity.BlockEntityType;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.level.block.state.StateDefinition;
|
||||
import net.minecraft.world.level.block.state.properties.EnumProperty;
|
||||
import net.minecraft.world.phys.BlockHitResult;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
public class DiskDriveBlock extends HorizontalContainerBlock {
|
||||
@@ -42,21 +40,26 @@ public class DiskDriveBlock extends HorizontalContainerBlock {
|
||||
properties.add(FACING, STATE);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
|
||||
if (player.isCrouching() && level.getBlockEntity(pos) instanceof DiskDriveBlockEntity drive) {
|
||||
// Try to put a disk into the drive
|
||||
var disk = player.getItemInHand(hand);
|
||||
if (disk.isEmpty()) return InteractionResult.PASS;
|
||||
/**
|
||||
* A default implementation of {@link Item#useOn(UseOnContext)} for items that can be placed into a drive.
|
||||
*
|
||||
* @param context The context of this item usage action.
|
||||
* @return Whether the item was placed or not.
|
||||
*/
|
||||
public static InteractionResult defaultUseItemOn(UseOnContext context) {
|
||||
if (context.getPlayer() == null || !context.getPlayer().isSecondaryUseActive()) return InteractionResult.PASS;
|
||||
|
||||
if (!level.isClientSide && drive.getDiskStack().isEmpty() && PlatformHelper.get().getMedia(disk) != null) {
|
||||
drive.setDiskStack(disk.split(1));
|
||||
var level = context.getLevel();
|
||||
var blockPos = context.getClickedPos();
|
||||
var blockState = level.getBlockState(blockPos);
|
||||
if (blockState.is(ModRegistry.Blocks.DISK_DRIVE.get()) && blockState.getValue(STATE) == DiskDriveState.EMPTY) {
|
||||
if (!level.isClientSide && level.getBlockEntity(blockPos) instanceof DiskDriveBlockEntity drive && drive.getDiskStack().isEmpty()) {
|
||||
drive.setDiskStack(context.getItemInHand().split(1));
|
||||
}
|
||||
return InteractionResult.sidedSuccess(level.isClientSide);
|
||||
}
|
||||
|
||||
return super.use(state, level, pos, player, hand, hit);
|
||||
return InteractionResult.PASS;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
Reference in New Issue
Block a user