优化部分代码

This commit is contained in:
17146 2024-09-21 21:32:41 +08:00
parent ae317597cf
commit 5d1f788d73
3 changed files with 30 additions and 45 deletions

View file

@ -106,14 +106,13 @@ public class ClientEventHandler {
return !mc.isWindowActive();
}
private static boolean isMove() {
private static boolean isMoving() {
Player player = Minecraft.getInstance().player;
return Minecraft.getInstance().options.keyLeft.isDown()
|| Minecraft.getInstance().options.keyRight.isDown()
|| Minecraft.getInstance().options.keyUp.isDown()
|| Minecraft.getInstance().options.keyDown.isDown()
|| (player != null && player.isSprinting());
}
@SubscribeEvent
@ -130,17 +129,11 @@ public class ClientEventHandler {
float times = Minecraft.getInstance().getDeltaFrameTime();
double basicDev = stack.getOrCreateTag().getDouble("spread");
double walk = isMove() ? 0.15 * basicDev : 0;
double walk = isMoving() ? 0.15 * basicDev : 0;
double sprint = player.isSprinting() ? 0.25 * basicDev : 0;
double crouching = player.isCrouching() ? -0.15 * basicDev : 0;
double prone = isProne(player) ? -0.3 * basicDev : 0;
double jump = player.onGround() ? 0 * basicDev : 0.35 * basicDev;
double ride = player.onGround() ? -0.25 * basicDev : 0;
double zoomSpread;
@ -149,8 +142,7 @@ public class ClientEventHandler {
zoomSpread = 1 - (0.995 * zoomTime);
} else if (stack.is(ModTags.Items.SHOTGUN) || stack.is(ModItems.MINIGUN.get())) {
zoomSpread = 1 - (0.25 * zoomTime);
}
else {
} else {
zoomSpread = 1 - (0.9 * zoomTime);
}
@ -162,8 +154,6 @@ public class ClientEventHandler {
gunSpread -= 0.07 * Math.pow(spread - gunSpread, 2) * times;
}
// player.displayClientMessage(Component.literal(new java.text.DecimalFormat("####").format(gunSpread)), true);
// 开火部分
if (GLFW.glfwGetMouseButton(Minecraft.getInstance().getWindow().getWindow(), GLFW.GLFW_MOUSE_BUTTON_LEFT) == GLFW.GLFW_PRESS && player.getMainHandItem().is(ModTags.Items.NORMAL_GUN)) {
@ -193,7 +183,6 @@ public class ClientEventHandler {
clientTimer.setProgress((long) (clientTimer.getProgress() - cooldown));
}
// player.displayClientMessage(Component.literal(new java.text.DecimalFormat("####").format(clientTimer.getProgress())), true);
} else {
clientTimer.stop();
}
@ -235,7 +224,6 @@ public class ClientEventHandler {
.stream().filter(e -> e.getStringUUID().equals(stack.getOrCreateTag().getString("LinkedDrone"))).findFirst().orElse(null);
if (drone != null) {
if (droneRotZ > drone.getEntityData().get(ROT_Z)) {
droneRotZ = Mth.clamp(droneRotZ - 0.3 * Math.pow(drone.getEntityData().get(ROT_Z) - droneRotZ, 2), drone.getEntityData().get(ROT_Z), Double.POSITIVE_INFINITY);
} else {
@ -283,14 +271,14 @@ public class ClientEventHandler {
}
private static void handleWeaponSway(LivingEntity entity) {
if (entity.getMainHandItem().is(ModTags.Items.GUN)) {
if (entity.getMainHandItem().is(ModTags.Items.GUN) && entity instanceof Player player) {
float times = 2 * Minecraft.getInstance().getDeltaFrameTime();
double pose;
if (entity.isShiftKeyDown() && entity.getBbHeight() >= 1 && isProne((Player) entity)) {
if (player.isShiftKeyDown() && player.getBbHeight() >= 1 && isProne(player)) {
pose = 0.85;
} else if (isProne((Player) entity)) {
pose = entity.getMainHandItem().getOrCreateTag().getDouble("bipod") == 1 ? 0 : 0.25f;
} else if (isProne(player)) {
pose = player.getMainHandItem().getOrCreateTag().getDouble("bipod") == 1 ? 0 : 0.25f;
} else {
pose = 1;
}
@ -304,16 +292,16 @@ public class ClientEventHandler {
private static void handleWeaponMove(LivingEntity entity) {
if (entity.getMainHandItem().is(ModTags.Items.GUN)) {
float times = 4.5f * Minecraft.getInstance().getDeltaFrameTime();
double move_speed = (float) Mth.clamp(entity.getDeltaMovement().horizontalDistanceSqr(), 0, 0.02);
double on_ground;
double moveSpeed = (float) Mth.clamp(entity.getDeltaMovement().horizontalDistanceSqr(), 0, 0.02);
double onGround;
if (entity.onGround()) {
if (entity.isSprinting()) {
on_ground = 1.35;
onGround = 1.35;
} else {
on_ground = 2.0;
onGround = 2.0;
}
} else {
on_ground = 0.001;
onGround = 0.001;
}
if (Minecraft.getInstance().options.keyUp.isDown() && firePosTimer == 0) {
@ -322,15 +310,15 @@ public class ClientEventHandler {
moveRotZ = Mth.clamp(moveRotZ - 0.007 * times, 0, 0.14) * (1 - zoomTime);
}
if (isMove() && firePosTimer == 0) {
if (isMoving() && firePosTimer == 0) {
if (moveYTime < 1.25) {
moveYTime += 1.2 * on_ground * times * move_speed;
moveYTime += 1.2 * onGround * times * moveSpeed;
} else {
moveYTime = 0.25;
}
if (moveXTime < 2) {
moveXTime += 1.2 * on_ground * times * move_speed;
moveXTime += 1.2 * onGround * times * moveSpeed;
} else {
moveXTime = 0;
}
@ -454,8 +442,6 @@ public class ClientEventHandler {
fireRot = 0;
}
}
private static void handlePlayerBreath(LivingEntity entity) {

View file

@ -97,16 +97,15 @@ public class PlayerEventHandler {
public static boolean isProne(Player player) {
Level level = player.level();
if (player.getBbHeight() <= 1) return true;
return player.isCrouching() && level.getBlockState(BlockPos.containing(player.getX() + 0.7 * player.getLookAngle().x, player.getY() + 0.5, player.getZ() + 0.7 * player.getLookAngle().z)).canOcclude() && !level.getBlockState(BlockPos.containing(player.getX() + 0.7 * player.getLookAngle().x, player.getY() + 1.5, player.getZ() + 0.7 * player.getLookAngle().z)).canOcclude();
return player.isCrouching() && level.getBlockState(BlockPos.containing(player.getX() + 0.7 * player.getLookAngle().x, player.getY() + 0.5, player.getZ() + 0.7 * player.getLookAngle().z)).canOcclude()
&& !level.getBlockState(BlockPos.containing(player.getX() + 0.7 * player.getLookAngle().x, player.getY() + 1.5, player.getZ() + 0.7 * player.getLookAngle().z)).canOcclude();
}
private static void handleWeaponSway(Player player) {
if (player.getMainHandItem().is(ModTags.Items.GUN)) {
float pose;
var data = player.getPersistentData();
if (player.isCrouching() && player.getBbHeight() >= 1 && !isProne(player)) {
pose = 0.85f;

View file

@ -22,10 +22,10 @@ import static net.mcreator.superbwarfare.event.GunEventHandler.gunShoot;
import static net.mcreator.superbwarfare.event.GunEventHandler.playGunSounds;
public class ShootMessage {
private final double spared;
private final double spread;
public ShootMessage(double spared) {
this.spared = spared;
public ShootMessage(double spread) {
this.spread = spread;
}
public static ShootMessage decode(FriendlyByteBuf buffer) {
@ -33,14 +33,14 @@ public class ShootMessage {
}
public static void encode(ShootMessage message, FriendlyByteBuf buffer) {
buffer.writeDouble(message.spared);
buffer.writeDouble(message.spread);
}
public static void handler(ShootMessage message, Supplier<NetworkEvent.Context> contextSupplier) {
NetworkEvent.Context context = contextSupplier.get();
context.enqueueWork(() -> {
if (context.getSender() != null) {
pressAction(context.getSender(), message.spared);
pressAction(context.getSender(), message.spread);
}
});
context.setPacketHandled(true);