content包
This commit is contained in:
parent
f83c6ff51e
commit
e04a18b6cc
|
|
@ -4,6 +4,7 @@ import net.minecraft.core.BlockPos;
|
|||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.world.InteractionHand;
|
||||
import net.minecraft.world.InteractionResult;
|
||||
import net.minecraft.world.ItemInteractionResult;
|
||||
import net.minecraft.world.MenuProvider;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.level.Level;
|
||||
|
|
@ -25,11 +26,22 @@ public class NetworkPatternControllerBlock extends Block implements EntityBlock
|
|||
}
|
||||
|
||||
@Override
|
||||
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
|
||||
if (!level.isClientSide) {
|
||||
protected ItemInteractionResult useItemOn(net.minecraft.world.item.ItemStack heldItem, BlockState state, Level level,
|
||||
BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
|
||||
if (!level.isClientSide && player instanceof ServerPlayer sp) {
|
||||
BlockEntity be = level.getBlockEntity(pos);
|
||||
if (be instanceof MenuProvider provider && player instanceof ServerPlayer sp) {
|
||||
// 使用原生 API 打开界面;如需自定义数据传输,请在 Menu 构造中使用 BlockPos/Access 读取
|
||||
if (be instanceof MenuProvider provider) {
|
||||
sp.openMenu(provider);
|
||||
}
|
||||
}
|
||||
return ItemInteractionResult.sidedSuccess(level.isClientSide);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hit) {
|
||||
if (!level.isClientSide && player instanceof ServerPlayer sp) {
|
||||
BlockEntity be = level.getBlockEntity(pos);
|
||||
if (be instanceof MenuProvider provider) {
|
||||
sp.openMenu(provider);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ import net.minecraft.core.Direction;
|
|||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.core.HolderLookup;
|
||||
import net.minecraft.world.MenuProvider;
|
||||
import net.minecraft.world.entity.player.Inventory;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
|
|
@ -49,14 +50,14 @@ public class NetworkPatternControllerBlockEntity extends BlockEntity implements
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void saveAdditional(CompoundTag tag) {
|
||||
super.saveAdditional(tag);
|
||||
protected void saveAdditional(CompoundTag tag, HolderLookup.Provider registries) {
|
||||
super.saveAdditional(tag, registries);
|
||||
this.managedNode.saveToNBT(tag);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load(CompoundTag tag) {
|
||||
super.load(tag);
|
||||
public void loadAdditional(CompoundTag tag, HolderLookup.Provider registries) {
|
||||
super.loadAdditional(tag, registries);
|
||||
this.managedNode.loadFromNBT(tag);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import net.minecraft.core.BlockPos;
|
|||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.world.InteractionHand;
|
||||
import net.minecraft.world.InteractionResult;
|
||||
import net.minecraft.world.ItemInteractionResult;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.item.Items;
|
||||
import net.minecraft.world.level.Level;
|
||||
|
|
@ -50,24 +51,25 @@ public class WirelessTransceiverBlock extends Block implements EntityBlock {
|
|||
super.attack(state, level, pos, player);
|
||||
}
|
||||
|
||||
// 1.21+: 拆分为 useItemOn 与 useWithoutItem
|
||||
@Override
|
||||
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
|
||||
if (level.isClientSide) {
|
||||
return InteractionResult.SUCCESS;
|
||||
}
|
||||
protected ItemInteractionResult useItemOn(net.minecraft.world.item.ItemStack heldItem, BlockState state, Level level, BlockPos pos,
|
||||
Player player, InteractionHand hand, BlockHitResult hit) {
|
||||
BlockEntity be = level.getBlockEntity(pos);
|
||||
if (be instanceof WirelessTransceiverBlockEntity te) {
|
||||
boolean sneaking = player.isShiftKeyDown();
|
||||
if (sneaking) {
|
||||
if (te.isLocked()) {
|
||||
if (!(be instanceof WirelessTransceiverBlockEntity te)) {
|
||||
return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION;
|
||||
}
|
||||
|
||||
boolean sneaking = player.isShiftKeyDown();
|
||||
if (sneaking) {
|
||||
if (te.isLocked()) {
|
||||
if (!level.isClientSide) {
|
||||
player.displayClientMessage(Component.literal("收发器已锁定,无法修改频道"), true);
|
||||
return InteractionResult.CONSUME;
|
||||
}
|
||||
// 频率调节:主手 +1(或 +10),副手 -1(或 -10)
|
||||
} else {
|
||||
int step = 1;
|
||||
// 手持红石火把:加10;手持木棍:减10(仅改变步长,不改变加/减方向)
|
||||
if (player.getItemInHand(hand).is(Items.REDSTONE_TORCH)) step = 10;
|
||||
if (player.getItemInHand(hand).is(Items.STICK)) step = 10;
|
||||
if (heldItem.is(Items.REDSTONE_TORCH)) step = 10;
|
||||
if (heldItem.is(Items.STICK)) step = 10;
|
||||
|
||||
long f = te.getFrequency();
|
||||
if (hand == InteractionHand.MAIN_HAND) {
|
||||
|
|
@ -77,18 +79,61 @@ public class WirelessTransceiverBlock extends Block implements EntityBlock {
|
|||
if (f < 0) f = 0;
|
||||
}
|
||||
te.setFrequency(f);
|
||||
player.displayClientMessage(Component.literal("频道:" + te.getFrequency()), true);
|
||||
} else {
|
||||
if (te.isLocked()) {
|
||||
player.displayClientMessage(Component.literal("收发器已锁定,无法切换模式"), true);
|
||||
return InteractionResult.CONSUME;
|
||||
if (!level.isClientSide) {
|
||||
player.displayClientMessage(Component.literal("频道:" + te.getFrequency()), true);
|
||||
}
|
||||
te.setMasterMode(!te.isMasterMode());
|
||||
}
|
||||
return ItemInteractionResult.sidedSuccess(level.isClientSide);
|
||||
} else {
|
||||
if (te.isLocked()) {
|
||||
if (!level.isClientSide) {
|
||||
player.displayClientMessage(Component.literal("收发器已锁定,无法切换模式"), true);
|
||||
}
|
||||
return ItemInteractionResult.sidedSuccess(level.isClientSide);
|
||||
}
|
||||
te.setMasterMode(!te.isMasterMode());
|
||||
if (!level.isClientSide) {
|
||||
player.displayClientMessage(Component.literal(te.isMasterMode() ? "模式:主端" : "模式:从端"), true);
|
||||
}
|
||||
return InteractionResult.CONSUME;
|
||||
return ItemInteractionResult.sidedSuccess(level.isClientSide);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hit) {
|
||||
BlockEntity be = level.getBlockEntity(pos);
|
||||
if (!(be instanceof WirelessTransceiverBlockEntity te)) {
|
||||
return InteractionResult.PASS;
|
||||
}
|
||||
boolean sneaking = player.isShiftKeyDown();
|
||||
if (sneaking) {
|
||||
if (te.isLocked()) {
|
||||
if (!level.isClientSide) {
|
||||
player.displayClientMessage(Component.literal("收发器已锁定,无法修改频道"), true);
|
||||
}
|
||||
} else {
|
||||
long f = te.getFrequency();
|
||||
// 空手交互:按主手逻辑 +1
|
||||
f += 1;
|
||||
te.setFrequency(f);
|
||||
if (!level.isClientSide) {
|
||||
player.displayClientMessage(Component.literal("频道:" + te.getFrequency()), true);
|
||||
}
|
||||
}
|
||||
return InteractionResult.sidedSuccess(level.isClientSide);
|
||||
} else {
|
||||
if (te.isLocked()) {
|
||||
if (!level.isClientSide) {
|
||||
player.displayClientMessage(Component.literal("收发器已锁定,无法切换模式"), true);
|
||||
}
|
||||
return InteractionResult.sidedSuccess(level.isClientSide);
|
||||
}
|
||||
te.setMasterMode(!te.isMasterMode());
|
||||
if (!level.isClientSide) {
|
||||
player.displayClientMessage(Component.literal(te.isMasterMode() ? "模式:主端" : "模式:从端"), true);
|
||||
}
|
||||
return InteractionResult.sidedSuccess(level.isClientSide);
|
||||
}
|
||||
return InteractionResult.PASS;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import net.minecraft.core.BlockPos;
|
|||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.core.HolderLookup;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
|
|
@ -170,8 +171,8 @@ public class WirelessTransceiverBlockEntity extends BlockEntity implements IWire
|
|||
|
||||
/* ===================== NBT ===================== */
|
||||
@Override
|
||||
protected void saveAdditional(CompoundTag tag) {
|
||||
super.saveAdditional(tag);
|
||||
protected void saveAdditional(CompoundTag tag, HolderLookup.Provider registries) {
|
||||
super.saveAdditional(tag, registries);
|
||||
tag.putLong("frequency", frequency);
|
||||
tag.putBoolean("master", masterMode);
|
||||
tag.putBoolean("locked", locked);
|
||||
|
|
@ -181,8 +182,8 @@ public class WirelessTransceiverBlockEntity extends BlockEntity implements IWire
|
|||
}
|
||||
|
||||
@Override
|
||||
public void load(CompoundTag tag) {
|
||||
super.load(tag);
|
||||
public void loadAdditional(CompoundTag tag, HolderLookup.Provider registries) {
|
||||
super.loadAdditional(tag, registries);
|
||||
this.frequency = tag.getLong("frequency");
|
||||
this.masterMode = tag.getBoolean("master");
|
||||
this.locked = tag.getBoolean("locked");
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user