优化nbt相关方法

This commit is contained in:
17146 2024-11-02 23:05:04 +08:00
parent 29a367578d
commit c2429e4aae
7 changed files with 40 additions and 34 deletions

View file

@ -106,9 +106,9 @@ public class ModUtils {
addNetworkMessage(ZoomMessage.class, ZoomMessage::encode, ZoomMessage::decode, ZoomMessage::handler); addNetworkMessage(ZoomMessage.class, ZoomMessage::encode, ZoomMessage::decode, ZoomMessage::handler);
addNetworkMessage(DoubleJumpMessage.class, DoubleJumpMessage::encode, DoubleJumpMessage::decode, DoubleJumpMessage::handler); addNetworkMessage(DoubleJumpMessage.class, DoubleJumpMessage::encode, DoubleJumpMessage::decode, DoubleJumpMessage::handler);
addNetworkMessage(GunsDataMessage.class, GunsDataMessage::encode, GunsDataMessage::decode, GunsDataMessage::handler, Optional.of(NetworkDirection.PLAY_TO_CLIENT)); addNetworkMessage(GunsDataMessage.class, GunsDataMessage::encode, GunsDataMessage::decode, GunsDataMessage::handler, Optional.of(NetworkDirection.PLAY_TO_CLIENT));
addNetworkMessage(FireMessage.class, FireMessage::encode, FireMessage::new, FireMessage::handler); addNetworkMessage(FireMessage.class, FireMessage::encode, FireMessage::decode, FireMessage::handler);
addNetworkMessage(VehicleFireMessage.class, VehicleFireMessage::encode, VehicleFireMessage::new, VehicleFireMessage::handler); addNetworkMessage(VehicleFireMessage.class, VehicleFireMessage::encode, VehicleFireMessage::decode, VehicleFireMessage::handler);
addNetworkMessage(FireModeMessage.class, FireModeMessage::encode, FireModeMessage::new, FireModeMessage::handler); addNetworkMessage(FireModeMessage.class, FireModeMessage::encode, FireModeMessage::decode, FireModeMessage::handler);
addNetworkMessage(ReloadMessage.class, ReloadMessage::encode, ReloadMessage::decode, ReloadMessage::handler); addNetworkMessage(ReloadMessage.class, ReloadMessage::encode, ReloadMessage::decode, ReloadMessage::handler);
addNetworkMessage(PlayerGunKillMessage.class, PlayerGunKillMessage::encode, PlayerGunKillMessage::decode, PlayerGunKillMessage::handler, Optional.of(NetworkDirection.PLAY_TO_CLIENT)); addNetworkMessage(PlayerGunKillMessage.class, PlayerGunKillMessage::encode, PlayerGunKillMessage::decode, PlayerGunKillMessage::handler, Optional.of(NetworkDirection.PLAY_TO_CLIENT));
addNetworkMessage(ClientIndicatorMessage.class, ClientIndicatorMessage::encode, ClientIndicatorMessage::decode, ClientIndicatorMessage::handler, Optional.of(NetworkDirection.PLAY_TO_CLIENT)); addNetworkMessage(ClientIndicatorMessage.class, ClientIndicatorMessage::encode, ClientIndicatorMessage::decode, ClientIndicatorMessage::handler, Optional.of(NetworkDirection.PLAY_TO_CLIENT));
@ -117,7 +117,7 @@ public class ModUtils {
addNetworkMessage(AdjustMortarAngleMessage.class, AdjustMortarAngleMessage::encode, AdjustMortarAngleMessage::decode, AdjustMortarAngleMessage::handler); addNetworkMessage(AdjustMortarAngleMessage.class, AdjustMortarAngleMessage::encode, AdjustMortarAngleMessage::decode, AdjustMortarAngleMessage::handler);
addNetworkMessage(InteractMessage.class, InteractMessage::encode, InteractMessage::decode, InteractMessage::handler); addNetworkMessage(InteractMessage.class, InteractMessage::encode, InteractMessage::decode, InteractMessage::handler);
addNetworkMessage(DroneMovementMessage.class, DroneMovementMessage::encode, DroneMovementMessage::decode, DroneMovementMessage::handler); addNetworkMessage(DroneMovementMessage.class, DroneMovementMessage::encode, DroneMovementMessage::decode, DroneMovementMessage::handler);
addNetworkMessage(DroneFireMessage.class, DroneFireMessage::encode, DroneFireMessage::new, DroneFireMessage::handler); addNetworkMessage(DroneFireMessage.class, DroneFireMessage::encode, DroneFireMessage::decode, DroneFireMessage::handler);
addNetworkMessage(SimulationDistanceMessage.class, SimulationDistanceMessage::encode, SimulationDistanceMessage::decode, SimulationDistanceMessage::handle, Optional.of(NetworkDirection.PLAY_TO_CLIENT)); addNetworkMessage(SimulationDistanceMessage.class, SimulationDistanceMessage::encode, SimulationDistanceMessage::decode, SimulationDistanceMessage::handle, Optional.of(NetworkDirection.PLAY_TO_CLIENT));
addNetworkMessage(GunReforgeMessage.class, GunReforgeMessage::encode, GunReforgeMessage::decode, GunReforgeMessage::handler); addNetworkMessage(GunReforgeMessage.class, GunReforgeMessage::encode, GunReforgeMessage::decode, GunReforgeMessage::handler);
addNetworkMessage(SetPerkLevelMessage.class, SetPerkLevelMessage::encode, SetPerkLevelMessage::decode, SetPerkLevelMessage::handler); addNetworkMessage(SetPerkLevelMessage.class, SetPerkLevelMessage::encode, SetPerkLevelMessage::decode, SetPerkLevelMessage::handler);

View file

@ -268,9 +268,13 @@ public class LivingEventHandler {
if (oldTags.getInt("bolt_action_time") > 0) { if (oldTags.getInt("bolt_action_time") > 0) {
oldTags.putInt("bolt_action_anim", 0); oldTags.putInt("bolt_action_anim", 0);
} }
CompoundTag data = oldTags.getCompound("GunData");
data.putInt("ReloadTime", 0);
oldStack.addTagElement("GunData", data);
oldTags.putBoolean("is_normal_reloading", false); oldTags.putBoolean("is_normal_reloading", false);
oldTags.putBoolean("is_empty_reloading", false); oldTags.putBoolean("is_empty_reloading", false);
oldTags.putInt("gun_reloading_time", 0);
if (oldTags.getDouble("iterative_time") != 0) { if (oldTags.getDouble("iterative_time") != 0) {
oldTags.putBoolean("force_stop", false); oldTags.putBoolean("force_stop", false);
@ -292,7 +296,6 @@ public class LivingEventHandler {
capability.edit = false; capability.edit = false;
capability.syncPlayerVariables(player); capability.syncPlayerVariables(player);
}); });
} }
if (newStack.getItem() instanceof GunItem) { if (newStack.getItem() instanceof GunItem) {
@ -303,7 +306,10 @@ public class LivingEventHandler {
} }
newStack.getOrCreateTag().putBoolean("is_normal_reloading", false); newStack.getOrCreateTag().putBoolean("is_normal_reloading", false);
newStack.getOrCreateTag().putBoolean("is_empty_reloading", false); newStack.getOrCreateTag().putBoolean("is_empty_reloading", false);
newStack.getOrCreateTag().putInt("gun_reloading_time", 0);
CompoundTag data = newStack.getOrCreateTag().getCompound("GunData");
data.putInt("ReloadTime", 0);
newStack.addTagElement("GunData", data);
if (newStack.getOrCreateTag().getDouble("iterative_time") != 0) { if (newStack.getOrCreateTag().getDouble("iterative_time") != 0) {
newStack.getOrCreateTag().putBoolean("force_stop", false); newStack.getOrCreateTag().putBoolean("force_stop", false);
@ -323,7 +329,7 @@ public class LivingEventHandler {
int level = PerkHelper.getItemPerkLevel(ModPerks.KILLING_TALLY.get(), newStack); int level = PerkHelper.getItemPerkLevel(ModPerks.KILLING_TALLY.get(), newStack);
if (level != 0) { if (level != 0) {
newStack.getOrCreateTag().putInt("KillingTally", 0); GunsTool.setPerkIntTag(newStack, "KillingTally", 0);
} }
if (player.level() instanceof ServerLevel) { if (player.level() instanceof ServerLevel) {

View file

@ -10,14 +10,15 @@ import net.minecraftforge.network.NetworkEvent;
import java.util.function.Supplier; import java.util.function.Supplier;
public class DroneFireMessage { public class DroneFireMessage {
private final int type; private final int type;
public DroneFireMessage(int type) { public DroneFireMessage(int type) {
this.type = type; this.type = type;
} }
public DroneFireMessage(FriendlyByteBuf buffer) { public static DroneFireMessage decode(FriendlyByteBuf buffer) {
this.type = buffer.readInt(); return new DroneFireMessage(buffer.readInt());
} }
public static void encode(DroneFireMessage message, FriendlyByteBuf buffer) { public static void encode(DroneFireMessage message, FriendlyByteBuf buffer) {

View file

@ -45,8 +45,8 @@ public class FireMessage {
this.type = type; this.type = type;
} }
public FireMessage(FriendlyByteBuf buffer) { public static FireMessage decode(FriendlyByteBuf buffer) {
this.type = buffer.readInt(); return new FireMessage(buffer.readInt());
} }
public static void encode(FireMessage message, FriendlyByteBuf buffer) { public static void encode(FireMessage message, FriendlyByteBuf buffer) {
@ -260,7 +260,7 @@ public class FireMessage {
CompoundTag tag = heldItem.getOrCreateTag(); CompoundTag tag = heldItem.getOrCreateTag();
var perk = PerkHelper.getPerkByType(heldItem, Perk.Type.AMMO); var perk = PerkHelper.getPerkByType(heldItem, Perk.Type.AMMO);
float headshot = (float) tag.getDouble("headshot"); float headshot = (float) tag.getDouble("headshot");
float velocity = 2 * (float) tag.getDouble("speed") * (float)perkSpeed(heldItem); float velocity = 2 * (float) tag.getDouble("speed") * (float) perkSpeed(heldItem);
float bypassArmorRate = (float) heldItem.getOrCreateTag().getDouble("BypassesArmor"); float bypassArmorRate = (float) heldItem.getOrCreateTag().getDouble("BypassesArmor");
double damage; double damage;
boolean zoom = player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ModVariables.PlayerVariables()).zoom; boolean zoom = player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ModVariables.PlayerVariables()).zoom;
@ -329,7 +329,7 @@ public class FireMessage {
ItemStack heldItem = player.getMainHandItem(); ItemStack heldItem = player.getMainHandItem();
CompoundTag tag = heldItem.getOrCreateTag(); CompoundTag tag = heldItem.getOrCreateTag();
float velocity = 2 * (float) tag.getDouble("speed") * (float)perkSpeed(heldItem); float velocity = 2 * (float) tag.getDouble("speed") * (float) perkSpeed(heldItem);
BocekArrowEntity arrow = new BocekArrowEntity(player, player.level()); BocekArrowEntity arrow = new BocekArrowEntity(player, player.level());
arrow.setBaseDamage(0); arrow.setBaseDamage(0);

View file

@ -3,6 +3,7 @@ package net.mcreator.superbwarfare.network.message;
import net.mcreator.superbwarfare.init.ModItems; import net.mcreator.superbwarfare.init.ModItems;
import net.mcreator.superbwarfare.init.ModSounds; import net.mcreator.superbwarfare.init.ModSounds;
import net.mcreator.superbwarfare.init.ModTags; import net.mcreator.superbwarfare.init.ModTags;
import net.mcreator.superbwarfare.tools.GunsTool;
import net.minecraft.core.Holder; import net.minecraft.core.Holder;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.FriendlyByteBuf;
@ -22,8 +23,8 @@ public class FireModeMessage {
this.type = type; this.type = type;
} }
public FireModeMessage(FriendlyByteBuf buffer) { public static FireModeMessage decode(FriendlyByteBuf buffer) {
this.type = buffer.readInt(); return new FireModeMessage(buffer.readInt());
} }
public static void encode(FireModeMessage message, FriendlyByteBuf buffer) { public static void encode(FireModeMessage message, FriendlyByteBuf buffer) {
@ -95,11 +96,10 @@ public class FireModeMessage {
} }
} }
if (mainHandItem.getItem() == ModItems.SENTINEL.get() if (mainHandItem.getItem() == ModItems.SENTINEL.get()
&& !player.isSpectator() && !player.isSpectator()
&& !(player.getCooldowns().isOnCooldown(mainHandItem.getItem())) && !(player.getCooldowns().isOnCooldown(mainHandItem.getItem()))
&& mainHandItem.getOrCreateTag().getInt("gun_reloading_time") == 0 && GunsTool.getGunIntTag(mainHandItem, "ReloadTime") == 0
&& !mainHandItem.getOrCreateTag().getBoolean("sentinel_is_charging")) { && !mainHandItem.getOrCreateTag().getBoolean("sentinel_is_charging")) {
int count = 0; int count = 0;

View file

@ -3,6 +3,7 @@ package net.mcreator.superbwarfare.network.message;
import net.mcreator.superbwarfare.init.ModItems; import net.mcreator.superbwarfare.init.ModItems;
import net.mcreator.superbwarfare.init.ModTags; import net.mcreator.superbwarfare.init.ModTags;
import net.mcreator.superbwarfare.network.ModVariables; import net.mcreator.superbwarfare.network.ModVariables;
import net.mcreator.superbwarfare.tools.GunsTool;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
@ -57,7 +58,7 @@ public class ReloadMessage {
&& stack.is(ModTags.Items.GUN) && stack.is(ModTags.Items.GUN)
&& !stack.getOrCreateTag().getBoolean("sentinel_is_charging") && !stack.getOrCreateTag().getBoolean("sentinel_is_charging")
&& !(player.getCooldowns().isOnCooldown(stack.getItem())) && !(player.getCooldowns().isOnCooldown(stack.getItem()))
&& stack.getOrCreateTag().getInt("gun_reloading_time") == 0 && GunsTool.getGunIntTag(stack, "ReloadTime") == 0
) { ) {
CompoundTag tag = stack.getOrCreateTag(); CompoundTag tag = stack.getOrCreateTag();

View file

@ -2,21 +2,21 @@ package net.mcreator.superbwarfare.network.message;
import net.mcreator.superbwarfare.entity.ICannonEntity; import net.mcreator.superbwarfare.entity.ICannonEntity;
import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraftforge.network.NetworkEvent; import net.minecraftforge.network.NetworkEvent;
import java.util.function.Supplier; import java.util.function.Supplier;
public class VehicleFireMessage { public class VehicleFireMessage {
private final int type; private final int type;
public VehicleFireMessage(int type) { public VehicleFireMessage(int type) {
this.type = type; this.type = type;
} }
public VehicleFireMessage(FriendlyByteBuf buffer) { public static VehicleFireMessage decode(FriendlyByteBuf buffer) {
this.type = buffer.readInt(); return new VehicleFireMessage(buffer.readInt());
} }
public static void encode(VehicleFireMessage message, FriendlyByteBuf buffer) { public static void encode(VehicleFireMessage message, FriendlyByteBuf buffer) {
@ -27,21 +27,19 @@ public class VehicleFireMessage {
NetworkEvent.Context context = contextSupplier.get(); NetworkEvent.Context context = contextSupplier.get();
context.enqueueWork(() -> { context.enqueueWork(() -> {
if (context.getSender() != null) { if (context.getSender() != null) {
pressAction(context.getSender(), message.type); var player = context.getSender();
Level level = player.level();
if (!level.isLoaded(player.blockPosition())) {
return;
}
if (player.getVehicle() instanceof ICannonEntity entity) {
entity.cannonShoot(player);
}
} }
}); });
context.setPacketHandled(true); context.setPacketHandled(true);
} }
public static void pressAction(Player player, int type) {
Level level = player.level();
if (!level.isLoaded(player.blockPosition())) {
return;
}
if (player.getVehicle() instanceof ICannonEntity entity) {
entity.cannonShoot(player);
}
}
} }