完成射击诸元screen
This commit is contained in:
parent
41354c428a
commit
cfed9e6511
5 changed files with 127 additions and 20 deletions
|
@ -2,6 +2,7 @@ package com.atsuishio.superbwarfare.client.screens;
|
|||
|
||||
import com.atsuishio.superbwarfare.Mod;
|
||||
import com.atsuishio.superbwarfare.component.ModDataComponents;
|
||||
import com.atsuishio.superbwarfare.network.message.send.FiringParametersEditMessage;
|
||||
import com.mojang.math.Axis;
|
||||
import net.minecraft.ChatFormatting;
|
||||
import net.minecraft.client.GameNarrator;
|
||||
|
@ -16,15 +17,15 @@ import net.minecraft.world.InteractionHand;
|
|||
import net.minecraft.world.item.ItemStack;
|
||||
import net.neoforged.api.distmarker.Dist;
|
||||
import net.neoforged.api.distmarker.OnlyIn;
|
||||
import net.neoforged.neoforge.network.PacketDistributor;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
// TODO 完成这个screen
|
||||
@OnlyIn(Dist.CLIENT)
|
||||
public class FiringParametersScreen extends Screen {
|
||||
|
||||
private static final ResourceLocation TEXTURE = Mod.loc("textures/gui/firing_parameters.png");
|
||||
|
||||
private ItemStack stack;
|
||||
private final ItemStack stack;
|
||||
private final InteractionHand hand;
|
||||
|
||||
public EditBox posX;
|
||||
|
@ -34,6 +35,8 @@ public class FiringParametersScreen extends Screen {
|
|||
|
||||
public boolean isDepressed;
|
||||
|
||||
private boolean init = false;
|
||||
|
||||
protected int imageWidth = 94;
|
||||
protected int imageHeight = 126;
|
||||
|
||||
|
@ -47,11 +50,35 @@ public class FiringParametersScreen extends Screen {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tick() {
|
||||
super.tick();
|
||||
if (!this.init) {
|
||||
if (!this.stack.isEmpty()) {
|
||||
var parameters = stack.get(ModDataComponents.FIRING_PARAMETERS);
|
||||
if (parameters != null) {
|
||||
var pos = parameters.pos();
|
||||
|
||||
this.posX.setValue("" + pos.getX());
|
||||
this.posY.setValue("" + pos.getY());
|
||||
this.posZ.setValue("" + pos.getZ());
|
||||
this.radius.setValue("" + Math.max(0, parameters.radius()));
|
||||
} else {
|
||||
this.posX.setValue("0");
|
||||
this.posY.setValue("0");
|
||||
this.posZ.setValue("0");
|
||||
this.radius.setValue("0");
|
||||
}
|
||||
}
|
||||
this.init = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(@NotNull GuiGraphics pGuiGraphics, int pMouseX, int pMouseY, float pPartialTick) {
|
||||
this.renderBackground(pGuiGraphics, pMouseX, pMouseY, pPartialTick);
|
||||
this.renderBg(pGuiGraphics, pMouseX, pMouseY);
|
||||
// TODO 解决渲染问题
|
||||
super.render(pGuiGraphics, pMouseX, pMouseY, pPartialTick);
|
||||
this.renderBg(pGuiGraphics, pMouseX, pMouseY);
|
||||
this.renderPositions(pGuiGraphics, pMouseX, pMouseY, pPartialTick);
|
||||
}
|
||||
|
||||
|
@ -117,6 +144,7 @@ public class FiringParametersScreen extends Screen {
|
|||
|
||||
this.radius = new EditBox(this.font, i + 41, j + 71, 40, 12, Component.empty());
|
||||
this.initEditBox(this.radius);
|
||||
this.radius.setFilter(s -> s.matches("\\d*"));
|
||||
}
|
||||
|
||||
protected void initEditBox(EditBox editBox) {
|
||||
|
@ -155,7 +183,7 @@ public class FiringParametersScreen extends Screen {
|
|||
}
|
||||
|
||||
@OnlyIn(Dist.CLIENT)
|
||||
static class DoneButton extends AbstractButton {
|
||||
class DoneButton extends AbstractButton {
|
||||
|
||||
public DoneButton(int pX, int pY, int pWidth, int pHeight) {
|
||||
super(pX, pY, pWidth, pHeight, Component.empty());
|
||||
|
@ -163,7 +191,20 @@ public class FiringParametersScreen extends Screen {
|
|||
|
||||
@Override
|
||||
public void onPress() {
|
||||
|
||||
if (!FiringParametersScreen.this.init) return;
|
||||
if (FiringParametersScreen.this.minecraft != null) {
|
||||
FiringParametersScreen.this.minecraft.setScreen(null);
|
||||
}
|
||||
PacketDistributor.sendToServer(
|
||||
new FiringParametersEditMessage(
|
||||
getEditBoxValue(FiringParametersScreen.this.posX.getValue()),
|
||||
getEditBoxValue(FiringParametersScreen.this.posY.getValue()),
|
||||
getEditBoxValue(FiringParametersScreen.this.posZ.getValue()),
|
||||
Math.max(0, getEditBoxValue(FiringParametersScreen.this.radius.getValue())),
|
||||
FiringParametersScreen.this.isDepressed,
|
||||
FiringParametersScreen.this.hand == InteractionHand.MAIN_HAND
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -176,5 +217,14 @@ public class FiringParametersScreen extends Screen {
|
|||
@Override
|
||||
protected void updateWidgetNarration(@NotNull NarrationElementOutput pNarrationElementOutput) {
|
||||
}
|
||||
|
||||
public int getEditBoxValue(String value) {
|
||||
if (value.equals("-")) return 0;
|
||||
try {
|
||||
return Integer.parseInt(value);
|
||||
} catch (NumberFormatException e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,6 +26,7 @@ public class ModDataComponents {
|
|||
builder -> builder.persistent(RecordCodecBuilder.create(instance ->
|
||||
instance.group(
|
||||
BlockPos.CODEC.fieldOf("pos").forGetter(FiringParameters.Parameters::pos),
|
||||
Codec.INT.fieldOf("radius").forGetter(FiringParameters.Parameters::radius),
|
||||
Codec.BOOL.fieldOf("is_depressed").forGetter(FiringParameters.Parameters::isDepressed)
|
||||
).apply(instance, FiringParameters.Parameters::new)
|
||||
))
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.atsuishio.superbwarfare.item;
|
||||
|
||||
import com.atsuishio.superbwarfare.client.TooltipTool;
|
||||
import com.atsuishio.superbwarfare.client.screens.FiringParametersScreen;
|
||||
import com.atsuishio.superbwarfare.component.ModDataComponents;
|
||||
import net.minecraft.ChatFormatting;
|
||||
|
@ -25,7 +26,14 @@ import java.util.List;
|
|||
|
||||
public class FiringParameters extends Item implements ItemScreenProvider {
|
||||
|
||||
public record Parameters(BlockPos pos, boolean isDepressed) {
|
||||
public record Parameters(BlockPos pos, int radius, boolean isDepressed) {
|
||||
public Parameters(BlockPos pos, boolean isDepressed) {
|
||||
this(pos, 0, isDepressed);
|
||||
}
|
||||
|
||||
public Parameters(BlockPos pos) {
|
||||
this(pos, 0, false);
|
||||
}
|
||||
}
|
||||
|
||||
public FiringParameters() {
|
||||
|
@ -69,22 +77,23 @@ public class FiringParameters extends Item implements ItemScreenProvider {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void appendHoverText(ItemStack stack, @NotNull TooltipContext context, @NotNull List<Component> tooltipComponents, @NotNull TooltipFlag tooltipFlag) {
|
||||
public void appendHoverText(@NotNull ItemStack stack, @NotNull TooltipContext context, @NotNull List<Component> tooltipComponents, @NotNull TooltipFlag tooltipFlag) {
|
||||
TooltipTool.addScreenProviderText(tooltipComponents);
|
||||
|
||||
var parameters = stack.get(ModDataComponents.FIRING_PARAMETERS);
|
||||
if (parameters == null) return;
|
||||
|
||||
var pos = parameters.pos();
|
||||
tooltipComponents.add(Component.translatable("tips.superbwarfare.mortar.target_pos")
|
||||
.withStyle(ChatFormatting.GRAY)
|
||||
.append(Component.literal("["
|
||||
+ pos.getX() + ","
|
||||
+ pos.getY() + ","
|
||||
+ pos.getZ() + "]")
|
||||
)
|
||||
);
|
||||
var pos = new BlockPos(0, 0, 0);
|
||||
var isDepressed = false;
|
||||
if (parameters != null) {
|
||||
pos = parameters.pos;
|
||||
isDepressed = parameters.isDepressed;
|
||||
}
|
||||
|
||||
tooltipComponents.add(Component.translatable("tips.superbwarfare.mortar.target_pos").withStyle(ChatFormatting.GRAY)
|
||||
.append(Component.literal("[" + pos.getX()
|
||||
+ ", " + pos.getY()
|
||||
+ ", " + pos.getZ() + "]")));
|
||||
tooltipComponents.add(Component.translatable(
|
||||
parameters.isDepressed
|
||||
isDepressed
|
||||
? "tips.superbwarfare.mortar.target_pos.depressed_trajectory"
|
||||
: "tips.superbwarfare.mortar.target_pos.lofted_trajectory"
|
||||
).withStyle(ChatFormatting.GRAY));
|
||||
|
|
|
@ -61,6 +61,7 @@ public class NetworkRegistry {
|
|||
playToServer(TacticalSprintMessage.TYPE, TacticalSprintMessage.STREAM_CODEC, TacticalSprintMessage::handler);
|
||||
playToServer(DogTagFinishEditMessage.TYPE, DogTagFinishEditMessage.STREAM_CODEC, DogTagFinishEditMessage::handler);
|
||||
playToServer(MouseMoveMessage.TYPE, MouseMoveMessage.STREAM_CODEC, MouseMoveMessage::handler);
|
||||
playToServer(FiringParametersEditMessage.TYPE, FiringParametersEditMessage.STREAM_CODEC, FiringParametersEditMessage::handler);
|
||||
}
|
||||
|
||||
private static PayloadRegistrar registrar;
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
package com.atsuishio.superbwarfare.network.message.send;
|
||||
|
||||
import com.atsuishio.superbwarfare.Mod;
|
||||
import com.atsuishio.superbwarfare.component.ModDataComponents;
|
||||
import com.atsuishio.superbwarfare.init.ModItems;
|
||||
import com.atsuishio.superbwarfare.item.FiringParameters;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.network.codec.ByteBufCodecs;
|
||||
import net.minecraft.network.codec.StreamCodec;
|
||||
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.neoforged.neoforge.network.handling.IPayloadContext;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public record FiringParametersEditMessage(
|
||||
int x, int y, int z,
|
||||
int radius, boolean isDepressed, boolean mainHand
|
||||
) implements CustomPacketPayload {
|
||||
public static final Type<FiringParametersEditMessage> TYPE = new Type<>(Mod.loc("firing_parameters_edit"));
|
||||
|
||||
public static final StreamCodec<ByteBuf, FiringParametersEditMessage> STREAM_CODEC = StreamCodec.composite(
|
||||
ByteBufCodecs.INT, FiringParametersEditMessage::x,
|
||||
ByteBufCodecs.INT, FiringParametersEditMessage::y,
|
||||
ByteBufCodecs.INT, FiringParametersEditMessage::z,
|
||||
ByteBufCodecs.INT, FiringParametersEditMessage::radius,
|
||||
ByteBufCodecs.BOOL, FiringParametersEditMessage::isDepressed,
|
||||
ByteBufCodecs.BOOL, FiringParametersEditMessage::mainHand,
|
||||
FiringParametersEditMessage::new
|
||||
);
|
||||
|
||||
public static void handler(FiringParametersEditMessage message, final IPayloadContext context) {
|
||||
var player = context.player();
|
||||
|
||||
ItemStack stack = message.mainHand ? player.getMainHandItem() : player.getOffhandItem();
|
||||
if (!stack.is(ModItems.FIRING_PARAMETERS.get())) return;
|
||||
|
||||
var parameters = new FiringParameters.Parameters(new BlockPos(message.x, message.y, message.z), message.radius, message.isDepressed);
|
||||
stack.set(ModDataComponents.FIRING_PARAMETERS, parameters);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Type<? extends CustomPacketPayload> type() {
|
||||
return TYPE;
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue