mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-12 21:17:32 +00:00
New Packet Things - WIP
This commit is contained in:
1 parent
17eac9a690
commit
be6c318619
9 files changed
+209
-68
No files matched your search
@@ -6,37 +6,43 @@ import cpw.mods.fml.common.network.FMLNetworkEvent;
|
||||
import cpw.mods.fml.common.network.NetworkRegistry;
|
||||
import cpw.mods.fml.common.network.internal.FMLProxyPacket;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import kamkeel.network.enums.EnumPacketType;
|
||||
import kamkeel.util.ByteBufUtils;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.EntityTracker;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.network.NetHandlerPlayServer;
|
||||
import net.minecraft.world.WorldServer;
|
||||
import noppes.npcs.CustomNpcs;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Hashtable;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
public final class PacketHandler {
|
||||
public static PacketHandler Instance;
|
||||
|
||||
public Map<String, AbstractPacket> packet_names = new Hashtable<>();
|
||||
public Map<String, FMLEventChannel> channels = new Hashtable<>();
|
||||
|
||||
public final static String INFO_PACKET = "CNPC+|Info";
|
||||
public final static String DATA_PACKET = "CNPC+|Data";
|
||||
public final static String CLIENT_PACKET = "CNPC+|Client";
|
||||
public Map<EnumPacketType, FMLEventChannel> channels = new Hashtable<>();
|
||||
|
||||
public final static List<String> channelNames = new ArrayList<>();
|
||||
public final static PacketChannel INFO_PACKET = new PacketChannel("CNPC+|Info", EnumPacketType.INFO);
|
||||
public final static PacketChannel DATA_PACKET = new PacketChannel("CNPC+|Data", EnumPacketType.DATA);
|
||||
public final static PacketChannel CLIENT_PACKET = new PacketChannel("CNPC+|Client", EnumPacketType.CLIENT);
|
||||
public final static PacketChannel LARGE_PACKET = new PacketChannel("CNPC+|Large", EnumPacketType.LARGE);
|
||||
|
||||
public final static List<PacketChannel> packetChannels = new ArrayList<>();
|
||||
|
||||
public PacketHandler() {
|
||||
// Register Channels
|
||||
channelNames.add(INFO_PACKET);
|
||||
channelNames.add(DATA_PACKET);
|
||||
channelNames.add(CLIENT_PACKET);
|
||||
packetChannels.add(INFO_PACKET);
|
||||
packetChannels.add(DATA_PACKET);
|
||||
packetChannels.add(CLIENT_PACKET);
|
||||
this.registerChannels();
|
||||
|
||||
// Register Info Packets
|
||||
|
||||
// Register Data Packets
|
||||
|
||||
// Register Client Packets
|
||||
|
||||
// Register Packets
|
||||
// map.put(PingPacket.packetName, new PingPacket());
|
||||
@@ -44,60 +50,29 @@ public final class PacketHandler {
|
||||
|
||||
public void registerChannels() {
|
||||
FMLEventChannel eventChannel;
|
||||
for (String channel : channelNames) {
|
||||
eventChannel = NetworkRegistry.INSTANCE.newEventDrivenChannel(channel);
|
||||
for (PacketChannel channel : packetChannels) {
|
||||
eventChannel = NetworkRegistry.INSTANCE.newEventDrivenChannel(channel.getChannelName());
|
||||
eventChannel.register(this);
|
||||
channels.put(channel, eventChannel);
|
||||
channels.put(channel.getChannelType(), eventChannel);
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void onServerPacket(FMLNetworkEvent.ServerCustomPacketEvent event) {
|
||||
try {
|
||||
packet_names.get(event.packet.channel()).receiveData(event.packet.payload(), ((NetHandlerPlayServer) event.handler).playerEntity);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void onClientPacket(FMLNetworkEvent.ClientCustomPacketEvent event) {
|
||||
try {
|
||||
packet_names.get(event.packet.channel()).receiveData(event.packet.payload(), CustomNpcs.proxy.getPlayer());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void sendToPlayer(FMLProxyPacket packet, EntityPlayerMP player) {
|
||||
if (packet != null && CustomNpcs.side()== Side.SERVER) {
|
||||
channels.get(packet.channel()).sendTo(packet, player);
|
||||
}
|
||||
}
|
||||
|
||||
public void sendToServer(FMLProxyPacket packet) {
|
||||
if (packet != null) {
|
||||
packet.setTarget(Side.SERVER);
|
||||
channels.get(packet.channel()).sendToServer(packet);
|
||||
}
|
||||
}
|
||||
|
||||
public void sendToTrackingPlayers(Entity entity, FMLProxyPacket packet) {
|
||||
if (packet != null && CustomNpcs.side() == Side.SERVER) {
|
||||
EntityTracker tracker = ((WorldServer) entity.worldObj).getEntityTracker();
|
||||
tracker.func_151248_b(entity, packet); // Send packet to tracking players
|
||||
}
|
||||
}
|
||||
|
||||
public void sendAround(Entity entity, double range, FMLProxyPacket packet) {
|
||||
if (packet != null && CustomNpcs.side() == Side.SERVER) {
|
||||
channels.get(packet.channel()).sendToAllAround(packet, new NetworkRegistry.TargetPoint(entity.dimension, entity.posX, entity.posY, entity.posZ, range));
|
||||
}
|
||||
}
|
||||
|
||||
public void sendToAll(FMLProxyPacket packet) {
|
||||
if (packet != null && CustomNpcs.side() == Side.SERVER) {
|
||||
channels.get(packet.channel()).sendToAll(packet);
|
||||
public PacketChannel getPacketChannel(EnumPacketType type){
|
||||
PacketChannel packetChannel;
|
||||
switch (type){
|
||||
case DATA:
|
||||
packetChannel = DATA_PACKET;
|
||||
break;
|
||||
case INFO:
|
||||
packetChannel = INFO_PACKET;
|
||||
break;
|
||||
case LARGE:
|
||||
packetChannel = LARGE_PACKET;
|
||||
break;
|
||||
default:
|
||||
packetChannel = CLIENT_PACKET;
|
||||
break;
|
||||
}
|
||||
return packetChannel;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user