优化network代码
This commit is contained in:
parent
e708b11b94
commit
a1954cf16a
6 changed files with 22 additions and 65 deletions
|
@ -85,5 +85,8 @@ public class TargetMod {
|
|||
addNetworkMessage(GunsDataMessage.class, GunsDataMessage::encode, GunsDataMessage::decode, GunsDataMessage::handler, Optional.of(NetworkDirection.PLAY_TO_CLIENT));
|
||||
addNetworkMessage(FireMessage.class, FireMessage::buffer, FireMessage::new, FireMessage::handler);
|
||||
addNetworkMessage(MortarGUIButtonMessage.class, MortarGUIButtonMessage::buffer, MortarGUIButtonMessage::new, MortarGUIButtonMessage::handler);
|
||||
addNetworkMessage(FireModeMessage.class, FireModeMessage::buffer, FireModeMessage::new, FireModeMessage::handler);
|
||||
addNetworkMessage(GunRecycleGuiButtonMessage.class, GunRecycleGuiButtonMessage::buffer, GunRecycleGuiButtonMessage::new, GunRecycleGuiButtonMessage::handler);
|
||||
addNetworkMessage(ReloadMessage.class, ReloadMessage::buffer, ReloadMessage::new, ReloadMessage::handler);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,3 @@
|
|||
|
||||
/*
|
||||
* MCreator note: This file will be REGENERATED on each build.
|
||||
*/
|
||||
package net.mcreator.target.init;
|
||||
|
||||
import com.mojang.blaze3d.platform.InputConstants;
|
||||
|
@ -27,9 +23,9 @@ public class TargetModKeyMappings {
|
|||
@Override
|
||||
public void setDown(boolean isDown) {
|
||||
super.setDown(isDown);
|
||||
if (isDownOld != isDown && isDown) {
|
||||
TargetMod.PACKET_HANDLER.sendToServer(new ReloadMessage(0, 0));
|
||||
ReloadMessage.pressAction(Minecraft.getInstance().player, 0, 0);
|
||||
if (isDownOld != isDown && isDown && Minecraft.getInstance().player != null) {
|
||||
TargetMod.PACKET_HANDLER.sendToServer(new ReloadMessage(0));
|
||||
ReloadMessage.pressAction(Minecraft.getInstance().player, 0);
|
||||
}
|
||||
isDownOld = isDown;
|
||||
}
|
||||
|
@ -56,7 +52,7 @@ public class TargetModKeyMappings {
|
|||
public void setDown(boolean isDown) {
|
||||
super.setDown(isDown);
|
||||
if (isDownOld != isDown && isDown) {
|
||||
TargetMod.PACKET_HANDLER.sendToServer(new FireModeMessage(0, 0));
|
||||
TargetMod.PACKET_HANDLER.sendToServer(new FireModeMessage(0));
|
||||
FireModeMessage.pressAction(Minecraft.getInstance().player, 0);
|
||||
}
|
||||
isDownOld = isDown;
|
||||
|
@ -68,11 +64,11 @@ public class TargetModKeyMappings {
|
|||
@Override
|
||||
public void setDown(boolean isDown) {
|
||||
super.setDown(isDown);
|
||||
if (isDownOld != isDown && isDown) {
|
||||
if (isDownOld != isDown && isDown && Minecraft.getInstance().player != null) {
|
||||
TargetMod.PACKET_HANDLER.sendToServer(new ZoomMessage(0, 0));
|
||||
ZoomMessage.pressAction(Minecraft.getInstance().player, 0);
|
||||
ZOOM_LASTPRESS = System.currentTimeMillis();
|
||||
} else if (isDownOld != isDown) {
|
||||
} else if (isDownOld != isDown && Minecraft.getInstance().player != null) {
|
||||
int dt = (int) (System.currentTimeMillis() - ZOOM_LASTPRESS);
|
||||
TargetMod.PACKET_HANDLER.sendToServer(new ZoomMessage(1, dt));
|
||||
ZoomMessage.pressAction(Minecraft.getInstance().player, 1);
|
||||
|
|
|
@ -1,14 +1,13 @@
|
|||
package net.mcreator.target.network;
|
||||
|
||||
import net.mcreator.target.init.TargetModSounds;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.sounds.SoundSource;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import net.minecraftforge.network.NetworkEvent;
|
||||
import net.minecraftforge.registries.ForgeRegistries;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
|
@ -51,9 +50,9 @@ public class DoubleJumpMessage {
|
|||
if ((entity.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new TargetModVariables.PlayerVariables())).playerDoubleJump) {
|
||||
entity.setDeltaMovement(new Vec3((1 * entity.getLookAngle().x), 0.8, (1 * entity.getLookAngle().z)));
|
||||
if (!level.isClientSide()) {
|
||||
level.playSound(null, BlockPos.containing(x, y, z), ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("target:doublejump")), SoundSource.BLOCKS, 1, 1);
|
||||
level.playSound(null, BlockPos.containing(x, y, z), TargetModSounds.DOUBLEJUMP.get(), SoundSource.BLOCKS, 1, 1);
|
||||
} else {
|
||||
level.playLocalSound(x, y, z, ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("target:doublejump")), SoundSource.BLOCKS, 1, 1, false);
|
||||
level.playLocalSound(x, y, z, TargetModSounds.DOUBLEJUMP.get(), SoundSource.BLOCKS, 1, 1, false);
|
||||
}
|
||||
entity.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
|
||||
capability.playerDoubleJump = false;
|
||||
|
|
|
@ -1,47 +1,35 @@
|
|||
package net.mcreator.target.network;
|
||||
|
||||
import net.mcreator.target.TargetMod;
|
||||
import net.mcreator.target.init.TargetModItems;
|
||||
import net.mcreator.target.init.TargetModSounds;
|
||||
import net.minecraft.core.Holder;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.network.protocol.game.ClientboundSoundPacket;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.sounds.SoundEvent;
|
||||
import net.minecraft.sounds.SoundSource;
|
||||
import net.minecraft.util.Mth;
|
||||
import net.minecraft.util.RandomSource;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
|
||||
import net.minecraftforge.network.NetworkEvent;
|
||||
import net.minecraftforge.registries.ForgeRegistries;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD)
|
||||
public class FireModeMessage {
|
||||
int type, pressedms;
|
||||
private final int type;
|
||||
|
||||
public FireModeMessage(int type, int pressedms) {
|
||||
public FireModeMessage(int type) {
|
||||
this.type = type;
|
||||
this.pressedms = pressedms;
|
||||
}
|
||||
|
||||
public FireModeMessage(FriendlyByteBuf buffer) {
|
||||
this.type = buffer.readInt();
|
||||
this.pressedms = buffer.readInt();
|
||||
}
|
||||
|
||||
public static void buffer(FireModeMessage message, FriendlyByteBuf buffer) {
|
||||
buffer.writeInt(message.type);
|
||||
buffer.writeInt(message.pressedms);
|
||||
}
|
||||
|
||||
public static void handler(FireModeMessage message, Supplier<NetworkEvent.Context> contextSupplier) {
|
||||
|
@ -70,7 +58,7 @@ public class FireModeMessage {
|
|||
};
|
||||
|
||||
if (player instanceof ServerPlayer serverPlayer) {
|
||||
serverPlayer.connection.send(new ClientboundSoundPacket(new Holder.Direct<>(ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("target:firerate"))),
|
||||
serverPlayer.connection.send(new ClientboundSoundPacket(new Holder.Direct<>(TargetModSounds.FIRERATE.get()),
|
||||
SoundSource.PLAYERS, serverPlayer.getX(), serverPlayer.getY(), serverPlayer.getZ(), 1f, 1f, serverPlayer.level().random.nextLong()));
|
||||
}
|
||||
|
||||
|
@ -101,9 +89,4 @@ public class FireModeMessage {
|
|||
tag.putDouble("chargingtime", 128);
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void registerMessage(FMLCommonSetupEvent event) {
|
||||
TargetMod.addNetworkMessage(FireModeMessage.class, FireModeMessage::buffer, FireModeMessage::new, FireModeMessage::handler);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package net.mcreator.target.network;
|
||||
|
||||
import net.mcreator.target.TargetMod;
|
||||
import net.mcreator.target.init.TargetModItems;
|
||||
import net.mcreator.target.init.TargetModTags;
|
||||
import net.mcreator.target.world.inventory.GunRecycleGuiMenu;
|
||||
|
@ -9,15 +8,11 @@ import net.minecraft.network.FriendlyByteBuf;
|
|||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.Items;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
|
||||
import net.minecraftforge.items.ItemHandlerHelper;
|
||||
import net.minecraftforge.network.NetworkEvent;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD)
|
||||
public class GunRecycleGuiButtonMessage {
|
||||
private final int buttonID, x, y, z;
|
||||
|
||||
|
@ -105,9 +100,4 @@ public class GunRecycleGuiButtonMessage {
|
|||
player.containerMenu.broadcastChanges();
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void registerMessage(FMLCommonSetupEvent event) {
|
||||
TargetMod.addNetworkMessage(GunRecycleGuiButtonMessage.class, GunRecycleGuiButtonMessage::buffer, GunRecycleGuiButtonMessage::new, GunRecycleGuiButtonMessage::handler);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,50 +1,41 @@
|
|||
package net.mcreator.target.network;
|
||||
|
||||
import net.mcreator.target.TargetMod;
|
||||
import net.mcreator.target.procedures.PlayerReloadProcedure;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
|
||||
import net.minecraftforge.network.NetworkEvent;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD)
|
||||
public class ReloadMessage {
|
||||
int type, pressedms;
|
||||
private final int type;
|
||||
|
||||
public ReloadMessage(int type, int pressedms) {
|
||||
public ReloadMessage(int type) {
|
||||
this.type = type;
|
||||
this.pressedms = pressedms;
|
||||
}
|
||||
|
||||
public ReloadMessage(FriendlyByteBuf buffer) {
|
||||
this.type = buffer.readInt();
|
||||
this.pressedms = buffer.readInt();
|
||||
}
|
||||
|
||||
public static void buffer(ReloadMessage message, FriendlyByteBuf buffer) {
|
||||
buffer.writeInt(message.type);
|
||||
buffer.writeInt(message.pressedms);
|
||||
}
|
||||
|
||||
public static void handler(ReloadMessage message, Supplier<NetworkEvent.Context> contextSupplier) {
|
||||
NetworkEvent.Context context = contextSupplier.get();
|
||||
context.enqueueWork(() -> {
|
||||
pressAction(context.getSender(), message.type, message.pressedms);
|
||||
if (context.getSender() != null) {
|
||||
pressAction(context.getSender(), message.type);
|
||||
}
|
||||
});
|
||||
context.setPacketHandled(true);
|
||||
}
|
||||
|
||||
public static void pressAction(Player entity, int type, int pressedms) {
|
||||
public static void pressAction(Player entity, int type) {
|
||||
Level world = entity.level();
|
||||
double x = entity.getX();
|
||||
double y = entity.getY();
|
||||
double z = entity.getZ();
|
||||
// security measure to prevent arbitrary chunk generation
|
||||
|
||||
if (!world.hasChunkAt(entity.blockPosition()))
|
||||
return;
|
||||
if (type == 0) {
|
||||
|
@ -52,9 +43,4 @@ public class ReloadMessage {
|
|||
PlayerReloadProcedure.execute(entity);
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void registerMessage(FMLCommonSetupEvent event) {
|
||||
TargetMod.addNetworkMessage(ReloadMessage.class, ReloadMessage::buffer, ReloadMessage::new, ReloadMessage::handler);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue