Syncing and Large Packet Completion

This commit is contained in:
Kam committed 2025-01-29 18:25:34 -05:00
1 parent dcf3a0d8b0
commit de56032118
39 files changed
+648 -862

No files matched your search

+2 -1
View File
@@ -5,6 +5,7 @@ import foxz.commandhelper.annotations.Command;
import foxz.commandhelper.annotations.SubCommand;
import foxz.commandhelper.permissions.OpOnly;
import foxz.commandhelper.permissions.ParamCheck;
import kamkeel.npcs.controllers.SyncMaster;
import net.minecraft.command.CommandBase;
import net.minecraft.entity.player.EntityPlayer;
import noppes.npcs.NoppesUtilServer;
@@ -89,7 +90,7 @@ public class CmdDialog extends ChMcLogger {
)
public boolean reload(String args[]){
new DialogController().load();
SyncController.syncAllDialogs();
SyncMaster.syncAllDialogs();
return true;
}
+2 -1
View File
@@ -5,6 +5,7 @@ import foxz.commandhelper.annotations.Command;
import foxz.commandhelper.annotations.SubCommand;
import foxz.commandhelper.permissions.OpOnly;
import foxz.commandhelper.permissions.ParamCheck;
import kamkeel.npcs.controllers.SyncMaster;
import net.minecraft.entity.player.EntityPlayerMP;
import noppes.npcs.Server;
import noppes.npcs.constants.EnumPacketClient;
@@ -170,7 +171,7 @@ public class CmdQuest extends ChMcLogger{
)
public boolean reload(String args[]){
new QuestController().load();
SyncController.syncAllQuests();
SyncMaster.syncAllQuests();
return true;
}
}
@@ -1,5 +1,6 @@
package kamkeel.npcs.command;
import kamkeel.npcs.controllers.SyncMaster;
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
@@ -89,7 +90,7 @@ public class DialogCommand extends CommandKamkeelBase {
)
public void reload(ICommandSender sender, String args[]){
new DialogController().load();
SyncController.syncAllDialogs();
SyncMaster.syncAllDialogs();
sendResult(sender, "Dialogs Reloaded");
}
@@ -1,5 +1,6 @@
package kamkeel.npcs.command;
import kamkeel.npcs.controllers.SyncMaster;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayer;
@@ -382,7 +383,7 @@ public class QuestCommand extends CommandKamkeelBase {
)
public void reload(ICommandSender sender, String args[]){
new QuestController().load();
SyncController.syncAllQuests();
SyncMaster.syncAllQuests();
sendResult(sender, "Quests Reloaded");
}
}
@@ -0,0 +1,383 @@
package kamkeel.npcs.controllers;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import kamkeel.npcs.addon.DBCAddon;
import kamkeel.npcs.network.PacketHandler;
import kamkeel.npcs.network.packets.client.large.sync.LargeSyncPacket;
import kamkeel.npcs.network.enums.EnumSyncAction;
import kamkeel.npcs.network.enums.EnumSyncType;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import noppes.npcs.client.ClientCacheHandler;
import noppes.npcs.controllers.DialogController;
import noppes.npcs.controllers.FactionController;
import noppes.npcs.controllers.QuestController;
import noppes.npcs.controllers.data.*;
import java.util.HashMap;
public class SyncMaster {
public static void syncPlayer(EntityPlayerMP player){
// 1) Factions
PacketHandler.Instance.sendToPlayer(new LargeSyncPacket(
EnumSyncType.FACTION,
EnumSyncAction.RELOAD,
-1,
factionsNBT()
), player);
// 2) Dialog Categories
PacketHandler.Instance.sendToPlayer(new LargeSyncPacket(
EnumSyncType.DIALOG_CATEGORY,
EnumSyncAction.RELOAD,
-1,
dialogCategoriesNBT()
), player);
// 3) Quest Categories
PacketHandler.Instance.sendToPlayer(new LargeSyncPacket(
EnumSyncType.QUEST_CATEGORY,
EnumSyncAction.RELOAD,
-1,
questCategoriesNBT()
), player);
DBCAddon.instance.syncPlayer(player);
syncPlayerData(player, false);
}
public static NBTTagCompound factionsNBT(){
NBTTagList list = new NBTTagList();
NBTTagCompound compound = new NBTTagCompound();
for(Faction faction : FactionController.getInstance().factions.values()){
NBTTagCompound factioNBT = new NBTTagCompound();
faction.writeNBT(factioNBT);
list.appendTag(factioNBT);
}
compound.setTag("Factions", list);
return compound;
}
public static NBTTagCompound dialogCategoriesNBT(){
NBTTagCompound compound = new NBTTagCompound();
NBTTagList categoryList = new NBTTagList();
for(DialogCategory category : DialogController.Instance.categories.values()){
NBTTagCompound questCompound = new NBTTagCompound();;
NBTTagList dialogList = new NBTTagList();
for(int dialogID : category.dialogs.keySet()){
Dialog quest = category.dialogs.get(dialogID);
dialogList.appendTag(quest.writeToNBT(new NBTTagCompound()));
}
questCompound.setTag("Data", dialogList);
questCompound.setTag("CatNBT", category.writeSmallNBT(new NBTTagCompound()));
}
compound.setTag("DialogCategories", categoryList);
return compound;
}
public static void syncPlayerData(EntityPlayerMP player, boolean update){
PlayerData data = PlayerData.get(player);
if(data != null){
if(update){
PacketHandler.Instance.sendToPlayer(new LargeSyncPacket(
EnumSyncType.PLAYERDATA,
EnumSyncAction.UPDATE,
-1,
data.getSyncNBT())
, player);
} else {
PacketHandler.Instance.sendToPlayer(new LargeSyncPacket(
EnumSyncType.PLAYERDATA,
EnumSyncAction.RELOAD,
-1,
data.getSyncNBTFull()), player);
}
}
}
public static void syncRemove(EnumSyncType enumSyncType, int id){
PacketHandler.Instance.sendToAll(new LargeSyncPacket(
enumSyncType,
EnumSyncAction.REMOVE,
id,
new NBTTagCompound()
));
}
public static void syncAllDialogs() {
PacketHandler.Instance.sendToAll(new LargeSyncPacket(
EnumSyncType.DIALOG_CATEGORY,
EnumSyncAction.RELOAD,
-1,
dialogCategoriesNBT()
));
}
public static void syncAllQuests() {
PacketHandler.Instance.sendToAll(new LargeSyncPacket(
EnumSyncType.QUEST_CATEGORY,
EnumSyncAction.RELOAD,
-1,
questCategoriesNBT()
));
}
public static NBTTagCompound questCategoriesNBT(){
NBTTagCompound compound = new NBTTagCompound();
NBTTagList categoryList = new NBTTagList();
for(QuestCategory category : QuestController.Instance.categories.values()){
NBTTagCompound questCompound = new NBTTagCompound();;
NBTTagList questList = new NBTTagList();
for(int questID : category.quests.keySet()){
Quest quest = category.quests.get(questID);
questList.appendTag(quest.writeToNBT(new NBTTagCompound()));
}
questCompound.setTag("Data", questList);
questCompound.setTag("CatNBT", category.writeSmallNBT(new NBTTagCompound()));
}
compound.setTag("QuestCategories", categoryList);
return compound;
}
@SideOnly(Side.CLIENT)
public static void clientSync(EnumSyncType enumSyncType, NBTTagCompound fullCompound) {
switch (enumSyncType){
case FACTION: {
NBTTagList list = fullCompound.getTagList("Factions", 10);
for (int i = 0; i < list.tagCount(); i++) {
Faction faction = new Faction();
faction.readNBT(list.getCompoundTagAt(i));
FactionController.getInstance().factionsSync.put(faction.id, faction);
}
FactionController.getInstance().factions = FactionController.getInstance().factionsSync;
FactionController.getInstance().factionsSync = new HashMap<Integer, Faction>();
break;
}
case DIALOG_CATEGORY: {
if(!fullCompound.hasNoTags()){
NBTTagList categories = fullCompound.getTagList("DialogCategories", 10);
for(int j = 0; j < fullCompound.getTagList("DialogCategories", 10).tagCount(); j++) {
NBTTagCompound categoryCompound = categories.getCompoundTagAt(j);
if(categoryCompound.hasNoTags())
continue;
DialogCategory category = new DialogCategory();
category.readSmallNBT(categoryCompound.getCompoundTag("CatNBT"));
NBTTagList dialogList = categoryCompound.getTagList("Data", 10);
if(DialogController.Instance.categoriesSync.containsKey(category.id)){
category = DialogController.Instance.categoriesSync.get(category.id);
category.readSmallNBT(categoryCompound.getCompoundTag("CatNBT"));
}
for(int i = 0; i < dialogList.tagCount(); i++)
{
Dialog dialog = new Dialog();
dialog.readNBT(dialogList.getCompoundTagAt(i));
dialog.category = category;
category.dialogs.put(dialog.id, dialog);
}
DialogController.Instance.categoriesSync.put(category.id, category);
}
}
HashMap<Integer, Dialog> dialogs = new HashMap<Integer, Dialog>();
for(DialogCategory category : DialogController.Instance.categoriesSync.values()){
for(Dialog dialog : category.dialogs.values()){
dialogs.put(dialog.id, dialog);
}
}
DialogController.Instance.categories = DialogController.Instance.categoriesSync;
DialogController.Instance.dialogs = dialogs;
DialogController.Instance.categoriesSync = new HashMap<Integer, DialogCategory>();
break;
}
case QUEST_CATEGORY: {
if(!fullCompound.hasNoTags()){
NBTTagList categories = fullCompound.getTagList("QuestCategories", 10);
for(int j = 0; j < fullCompound.getTagList("QuestCategories", 10).tagCount(); j++) {
NBTTagCompound categoryCompound = categories.getCompoundTagAt(j);
if(categoryCompound.hasNoTags())
continue;
QuestCategory category = new QuestCategory();
category.readSmallNBT(categoryCompound.getCompoundTag("CatNBT"));
NBTTagList questList = categoryCompound.getTagList("Data", 10);
if(QuestController.Instance.categoriesSync.containsKey(category.id)){
category = QuestController.Instance.categoriesSync.get(category.id);
category.readSmallNBT(categoryCompound.getCompoundTag("CatNBT"));
}
for(int i = 0; i < questList.tagCount(); i++)
{
Quest quest = new Quest();
quest.readNBT(questList.getCompoundTagAt(i));
quest.category = category;
category.quests.put(quest.id, quest);
}
QuestController.Instance.categoriesSync.put(category.id, category);
}
}
HashMap<Integer,Quest> quests = new HashMap<Integer, Quest>();
for(QuestCategory category : QuestController.Instance.categoriesSync.values()){
for(Quest quest : category.quests.values()){
quests.put(quest.id, quest);
}
}
QuestController.Instance.categories = QuestController.Instance.categoriesSync;
QuestController.Instance.quests = quests;
QuestController.Instance.categoriesSync = new HashMap<Integer, QuestCategory>();
break;
}
case PLAYERDATA: {
ClientCacheHandler.playerData.setSyncNBTFull(fullCompound);
break;
}
}
}
/**
* Update Related Tasks
*/
@SideOnly(Side.CLIENT)
public static void clientUpdate(EnumSyncType enumSyncType, int category_id, NBTTagCompound compound) {
switch (enumSyncType){
case FACTION: {
Faction faction = new Faction();
faction.readNBT(compound);
FactionController.getInstance().factions.put(faction.id, faction);
break;
}
case DIALOG: {
DialogCategory category = DialogController.Instance.categories.get(category_id);
Dialog dialog = new Dialog();
dialog.category = category;
dialog.readNBT(compound);
DialogController.Instance.dialogs.put(dialog.id, dialog);
category.dialogs.put(dialog.id, dialog);
break;
}
case QUEST: {
QuestCategory category = QuestController.Instance.categories.get(category_id);
Quest quest = new Quest();
quest.category = category;
quest.readNBT(compound);
QuestController.Instance.quests.put(quest.id, quest);
category.quests.put(quest.id, quest);
break;
}
case QUEST_CATEGORY: {
QuestCategory category = new QuestCategory();
category.readSmallNBT(compound.getCompoundTag("CatNBT"));
NBTTagList list = compound.getTagList("Data", 10);
if(QuestController.Instance.categoriesSync.containsKey(category.id)){
category = QuestController.Instance.categoriesSync.get(category.id);
category.readSmallNBT(compound.getCompoundTag("CatNBT"));
}
for(int i = 0; i < list.tagCount(); i++)
{
Quest quest = new Quest();
quest.readNBT(list.getCompoundTagAt(i));
quest.category = category;
category.quests.put(quest.id, quest);
}
QuestController.Instance.categories.put(category.id, category);
break;
}
case DIALOG_CATEGORY: {
DialogCategory category = new DialogCategory();
category.readSmallNBT(compound.getCompoundTag("CatNBT"));
NBTTagList list = compound.getTagList("Data", 10);
if(DialogController.Instance.categoriesSync.containsKey(category.id)){
category = DialogController.Instance.categoriesSync.get(category.id);
category.readSmallNBT(compound.getCompoundTag("CatNBT"));
}
for(int i = 0; i < list.tagCount(); i++)
{
Dialog dialog = new Dialog();
dialog.readNBT(list.getCompoundTagAt(i));
dialog.category = category;
category.dialogs.put(dialog.id, dialog);
}
DialogController.Instance.categories.put(category.id, category);
break;
}
case PLAYERDATA: {
ClientCacheHandler.playerData.setSyncNBT(compound);
break;
}
}
}
public static void syncUpdate(EnumSyncType type, int cat, NBTTagCompound compound) {
PacketHandler.Instance.sendToAll(new LargeSyncPacket(
type,
EnumSyncAction.UPDATE,
cat,
compound
));
}
public static NBTTagCompound updateQuestCat(QuestCategory questCategory) {
NBTTagCompound questCompound = new NBTTagCompound();
NBTTagList questList = new NBTTagList();
for(int questID : questCategory.quests.keySet()){
Quest quest = questCategory.quests.get(questID);
questList.appendTag(quest.writeToNBT(new NBTTagCompound()));
}
questCompound.setTag("Data", questList);
questCompound.setTag("CatNBT", questCategory.writeSmallNBT(new NBTTagCompound()));
return questCompound;
}
public static NBTTagCompound updateDialogCat(DialogCategory dialogCategory) {
NBTTagCompound dialogCompound = new NBTTagCompound();
NBTTagList dialogList = new NBTTagList();
for(int questID : dialogCategory.dialogs.keySet()){
Dialog dialog = dialogCategory.dialogs.get(questID);
dialogList.appendTag(dialog.writeToNBT(new NBTTagCompound()));
}
dialogCompound.setTag("Data", dialogList);
dialogCompound.setTag("CatNBT", dialogCategory.writeSmallNBT(new NBTTagCompound()));
return dialogCompound;
}
@SideOnly(Side.CLIENT)
public static void clientSyncRemove(EnumSyncType enumSyncType, int id) {
switch (enumSyncType){
case FACTION:
FactionController.getInstance().factions.remove(id);
break;
case DIALOG:
Dialog dialog = DialogController.Instance.dialogs.remove(id);
if(dialog != null) {
dialog.category.dialogs.remove(id);
}
break;
case DIALOG_CATEGORY:
DialogCategory dialogCategory = DialogController.Instance.categories.remove(id);
if(dialogCategory != null) {
DialogController.Instance.dialogs.keySet().removeAll(dialogCategory.dialogs.keySet());
}
break;
case QUEST:
Quest quest = QuestController.Instance.quests.remove(id);
if(quest != null) {
quest.category.quests.remove(id);
}
break;
case QUEST_CATEGORY:
QuestCategory questCategory = QuestController.Instance.categories.remove(id);
if(questCategory != null) {
QuestController.Instance.quests.keySet().removeAll(questCategory.quests.keySet());
}
break;
case MAGIC:
break;
}
}
}
@@ -6,6 +6,8 @@ import io.netty.buffer.Unpooled;
import net.minecraft.entity.player.EntityPlayer;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
public abstract class AbstractPacket {
@@ -16,11 +18,22 @@ public abstract class AbstractPacket {
buf.writeInt(packetChannel.getChannelType().ordinal());
buf.writeInt(getType().ordinal());
sendData(buf);
return new FMLProxyPacket(buf, getChannel().getChannelName());
} catch (Exception ignored) {}
return new FMLProxyPacket(buf, packetChannel.getChannelName());
} catch (Exception e) {
// For debugging, you might want to log it
e.printStackTrace();
}
return null;
}
public List<FMLProxyPacket> generatePackets() {
FMLProxyPacket single = generatePacket();
if (single == null) {
return Collections.emptyList();
}
return Collections.singletonList(single);
}
public abstract Enum getType();
public abstract PacketChannel getChannel();
public abstract void sendData(ByteBuf out) throws IOException;
@@ -1,7 +1,7 @@
package kamkeel.npcs.network;
import kamkeel.npcs.network.packets.large.LargeScrollGroupPacket;
import kamkeel.npcs.network.packets.large.LargeScrollDataPacket;
import kamkeel.npcs.network.packets.client.large.LargeScrollGroupPacket;
import kamkeel.npcs.network.packets.client.large.LargeScrollDataPacket;
import net.minecraft.entity.player.EntityPlayerMP;
import java.util.Map;
@@ -1,11 +1,14 @@
package kamkeel.npcs.network;
import cpw.mods.fml.common.network.internal.FMLProxyPacket;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import net.minecraft.entity.player.EntityPlayer;
import org.lwjgl.Sys;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
@@ -13,28 +16,47 @@ import java.util.concurrent.ConcurrentHashMap;
public abstract class LargeAbstractPacket extends AbstractPacket {
private static final Map<UUID, PacketStorage> packetChunks = new ConcurrentHashMap<>();
private static final int CHUNK_SIZE = 5000;
private static final int CHUNK_SIZE = 10000;
/**
* Create multiple FMLProxyPacket objects, each containing up to CHUNK_SIZE bytes.
*/
@Override
public void sendData(ByteBuf out) throws IOException {
byte[] data = getData();
int totalSize = data.length;
public List<FMLProxyPacket> generatePackets() {
List<FMLProxyPacket> packets = new ArrayList<>();
PacketChannel packetChannel = getChannel();
byte[] fullData;
try {
fullData = getData();
} catch (IOException e) {
e.printStackTrace();
return packets;
}
int totalSize = fullData.length;
UUID packetId = UUID.randomUUID();
// Break the data into chunks of CHUNK_SIZE bytes each
// Break data into CHUNK_SIZE chunks
for (int offset = 0; offset < totalSize; offset += CHUNK_SIZE) {
int chunkSize = Math.min(CHUNK_SIZE, totalSize - offset);
ByteBuf chunkBuf = Unpooled.buffer();
// Write a header for each chunk
out.writeLong(packetId.getMostSignificantBits());
out.writeLong(packetId.getLeastSignificantBits());
out.writeInt(totalSize);
out.writeInt(offset);
out.writeInt((totalSize + CHUNK_SIZE - 1) / CHUNK_SIZE); // totalChunks
chunkBuf.writeInt(packetChannel.getChannelType().ordinal());
chunkBuf.writeInt(getType().ordinal());
// Write the chunk bytes
out.writeBytes(data, offset, chunkSize);
chunkBuf.writeLong(packetId.getMostSignificantBits());
chunkBuf.writeLong(packetId.getLeastSignificantBits());
chunkBuf.writeInt(totalSize);
chunkBuf.writeInt(offset);
chunkBuf.writeInt((totalSize + CHUNK_SIZE - 1) / CHUNK_SIZE);
chunkBuf.writeBytes(fullData, offset, chunkSize);
FMLProxyPacket proxyPacket = new FMLProxyPacket(chunkBuf, packetChannel.getChannelName());
packets.add(proxyPacket);
}
return packets;
}
@Override
@@ -87,6 +109,12 @@ public abstract class LargeAbstractPacket extends AbstractPacket {
protected abstract void handleCompleteData(ByteBuf data, EntityPlayer player) throws IOException;
@Override
public final FMLProxyPacket generatePacket() {return null;}
@Override
public void sendData(ByteBuf out) throws IOException {}
/**
* Simple storage class to keep track of partial data and how many bytes we've received so far.
*/
@@ -7,9 +7,10 @@ import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.network.internal.FMLProxyPacket;
import io.netty.buffer.ByteBuf;
import kamkeel.npcs.network.enums.EnumPacketType;
import kamkeel.npcs.network.packets.large.LargeScrollDataPacket;
import kamkeel.npcs.network.packets.large.LargeScrollGroupPacket;
import kamkeel.npcs.network.packets.large.LargeScrollListPacket;
import kamkeel.npcs.network.packets.client.large.LargeScrollDataPacket;
import kamkeel.npcs.network.packets.client.large.LargeScrollGroupPacket;
import kamkeel.npcs.network.packets.client.large.LargeScrollListPacket;
import kamkeel.npcs.network.packets.client.large.sync.LargeSyncPacket;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.network.NetHandlerPlayServer;
@@ -17,19 +18,19 @@ import noppes.npcs.CustomNpcs;
import noppes.npcs.LogWriter;
import java.util.*;
import java.util.function.Consumer;
public final class PacketHandler {
public static PacketHandler Instance;
// Channels
public Map<EnumPacketType, FMLEventChannel> channels = new Hashtable<>();
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 static final PacketChannel INFO_PACKET = new PacketChannel("CNPC+|Info", EnumPacketType.INFO);
public static final PacketChannel DATA_PACKET = new PacketChannel("CNPC+|Data", EnumPacketType.DATA);
public static final PacketChannel CLIENT_PACKET = new PacketChannel("CNPC+|Client", EnumPacketType.CLIENT);
public static final PacketChannel LARGE_PACKET = new PacketChannel("CNPC+|Large", EnumPacketType.LARGE);
private final static List<PacketChannel> packetChannels = new ArrayList<>();
private static final List<PacketChannel> packetChannels = new ArrayList<>();
public PacketHandler() {
// Register Channels
@@ -43,22 +44,20 @@ public final class PacketHandler {
LARGE_PACKET.registerPacket(new LargeScrollGroupPacket());
LARGE_PACKET.registerPacket(new LargeScrollDataPacket());
LARGE_PACKET.registerPacket(new LargeScrollListPacket());
// Log channel registration
System.out.println("PacketHandler initialized and channels registered.");
LARGE_PACKET.registerPacket(new LargeSyncPacket());
}
public void registerChannels() {
FMLEventChannel eventChannel;
private void registerChannels() {
for (PacketChannel channel : packetChannels) {
eventChannel = NetworkRegistry.INSTANCE.newEventDrivenChannel(channel.getChannelName());
FMLEventChannel eventChannel =
NetworkRegistry.INSTANCE.newEventDrivenChannel(channel.getChannelName());
eventChannel.register(this);
channels.put(channel.getChannelType(), eventChannel);
System.out.println("Registered channel: " + channel.getChannelName());
}
}
public PacketChannel getPacketChannel(EnumPacketType type){
public PacketChannel getPacketChannel(EnumPacketType type) {
return packetChannels.stream()
.filter(channel -> channel.getChannelType() == type)
.findFirst()
@@ -86,20 +85,26 @@ public final class PacketHandler {
private void handlePacket(FMLProxyPacket packet, EntityPlayer player) {
ByteBuf buf = packet.payload();
try {
// First two ints are channelTypeOrdinal and packetTypeOrdinal
int packetTypeOrdinal = buf.readInt();
EnumPacketType packetType = EnumPacketType.values()[packetTypeOrdinal];
PacketChannel packetChannel = getPacketChannel(packetType);
if (packetChannel == null) {
LogWriter.error("Error: Packet channel is null for packet type: " + packetType);
return;
}
int packetId = buf.readInt();
AbstractPacket abstractPacket = packetChannel.packets.get(packetId);
if (abstractPacket == null) {
LogWriter.error("Error: Abstract packet is null for packet ID: " + packetId);
return;
}
// Let the packet parse the rest
abstractPacket.receiveData(buf, player);
} catch (IndexOutOfBoundsException e) {
LogWriter.error("Error: IndexOutOfBoundsException in handlePacket: " + e.getMessage());
} catch (Exception e) {
@@ -107,20 +112,31 @@ public final class PacketHandler {
}
}
public void sendPacket(AbstractPacket packet, Consumer<FMLProxyPacket> sendFunction) {
FMLProxyPacket proxyPacket = packet.generatePacket();
if (proxyPacket != null) {
sendFunction.accept(proxyPacket);
/**
* Sends every FMLProxyPacket produced by packet.generatePackets()
*/
private void sendAllPackets(AbstractPacket packet, SendAction action) {
// get all generated FMLProxyPacket objects (could be 1 for normal, or many for large)
List<FMLProxyPacket> proxyPackets = packet.generatePackets();
if (proxyPackets.isEmpty()) {
LogWriter.error("Warning: No packets generated for " + packet.getClass().getName());
}
for (FMLProxyPacket proxy : proxyPackets) {
action.send(proxy);
}
}
// ------------------------------------------------------------------------
// Public API methods for sending
public void sendToPlayer(AbstractPacket packet, EntityPlayerMP player) {
FMLEventChannel eventChannel = getEventChannel(packet);
if (eventChannel == null) {
LogWriter.error("Error: Event channel is null for packet: " + packet.getClass().getName());
return;
}
sendPacket(packet, proxyPacket -> eventChannel.sendTo(proxyPacket, player));
sendAllPackets(packet, p -> eventChannel.sendTo(p, player));
}
public void sendToServer(AbstractPacket packet) {
@@ -129,7 +145,7 @@ public final class PacketHandler {
LogWriter.error("Error: Event channel is null for packet: " + packet.getClass().getName());
return;
}
sendPacket(packet, eventChannel::sendToServer);
sendAllPackets(packet, eventChannel::sendToServer);
}
public void sendToAll(AbstractPacket packet) {
@@ -138,7 +154,7 @@ public final class PacketHandler {
LogWriter.error("Error: Event channel is null for packet: " + packet.getClass().getName());
return;
}
sendPacket(packet, eventChannel::sendToAll);
sendAllPackets(packet, eventChannel::sendToAll);
}
public void sendToAllAround(AbstractPacket packet, NetworkRegistry.TargetPoint point) {
@@ -147,7 +163,7 @@ public final class PacketHandler {
LogWriter.error("Error: Event channel is null for packet: " + packet.getClass().getName());
return;
}
sendPacket(packet, proxyPacket -> eventChannel.sendToAllAround(proxyPacket, point));
sendAllPackets(packet, p -> eventChannel.sendToAllAround(p, point));
}
public void sendToDimension(AbstractPacket packet, int dimensionId) {
@@ -156,6 +172,11 @@ public final class PacketHandler {
LogWriter.error("Error: Event channel is null for packet: " + packet.getClass().getName());
return;
}
sendPacket(packet, proxyPacket -> eventChannel.sendToDimension(proxyPacket, dimensionId));
sendAllPackets(packet, p -> eventChannel.sendToDimension(p, dimensionId));
}
// Simple functional interface to unify the "send" action
private interface SendAction {
void send(FMLProxyPacket proxy);
}
}
@@ -2,6 +2,7 @@ package kamkeel.npcs.network.enums;
// Packets to the Client
public enum EnumClientPacket {
SYNC,
CHAT,
MESSAGE,
PARTY_MESSAGE,
@@ -0,0 +1,7 @@
package kamkeel.npcs.network.enums;
public enum EnumSyncAction {
RELOAD,
UPDATE,
REMOVE
}
@@ -0,0 +1,11 @@
package kamkeel.npcs.network.enums;
public enum EnumSyncType {
FACTION,
DIALOG,
DIALOG_CATEGORY,
QUEST,
QUEST_CATEGORY,
PLAYERDATA,
MAGIC
}
@@ -1,37 +0,0 @@
package kamkeel.npcs.network.packets.client;
import io.netty.buffer.ByteBuf;
import kamkeel.npcs.network.AbstractPacket;
import kamkeel.npcs.network.PacketChannel;
import kamkeel.npcs.network.PacketHandler;
import kamkeel.npcs.network.enums.EnumClientPacket;
import net.minecraft.entity.player.EntityPlayer;
import noppes.npcs.client.NoppesUtil;
import java.io.IOException;
public final class DBCFormPacket extends AbstractPacket {
public static final String packetName = "Client|DBCForm";
public DBCFormPacket() {}
@Override
public Enum getType() {
return EnumClientPacket.DBC_FORM;
}
@Override
public PacketChannel getChannel() {
return PacketHandler.CLIENT_PACKET;
}
@Override
public void sendData(ByteBuf out) throws IOException {
// TODO: Send Packet
}
@Override
public void receiveData(ByteBuf in, EntityPlayer player) throws IOException {
NoppesUtil.handleFormEnd();
}
}
@@ -1,38 +0,0 @@
package kamkeel.npcs.network.packets.client;
import io.netty.buffer.ByteBuf;
import kamkeel.npcs.network.AbstractPacket;
import kamkeel.npcs.network.PacketChannel;
import kamkeel.npcs.network.PacketHandler;
import kamkeel.npcs.network.enums.EnumClientPacket;
import net.minecraft.entity.player.EntityPlayer;
import noppes.npcs.client.NoppesUtil;
import java.io.IOException;
public final class LargeNBTEndPacket extends AbstractPacket {
public static final String packetName = "Client|LargeNBTEnd";
public LargeNBTEndPacket() {}
@Override
public Enum getType() {
return EnumClientPacket.LARGE_NBT_END;
}
@Override
public PacketChannel getChannel() {
return PacketHandler.CLIENT_PACKET;
}
@Override
public void sendData(ByteBuf out) throws IOException {
// TODO: Send Packet
}
@Override
public void receiveData(ByteBuf in, EntityPlayer player) throws IOException {
// Finalize handling of large NBT data (if needed).
NoppesUtil.handleLargeData(null);
}
}
@@ -1,39 +0,0 @@
package kamkeel.npcs.network.packets.client;
import io.netty.buffer.ByteBuf;
import kamkeel.npcs.network.AbstractPacket;
import kamkeel.npcs.network.PacketChannel;
import kamkeel.npcs.network.PacketHandler;
import kamkeel.npcs.network.enums.EnumClientPacket;
import net.minecraft.entity.player.EntityPlayer;
import noppes.npcs.client.NoppesUtil;
import java.io.IOException;
public final class LargeNBTPartPacket extends AbstractPacket {
public static final String packetName = "Client|LargeNBTPart";
public LargeNBTPartPacket() {}
@Override
public Enum getType() {
return EnumClientPacket.LARGE_NBT_PART;
}
@Override
public PacketChannel getChannel() {
return PacketHandler.CLIENT_PACKET;
}
@Override
public void sendData(ByteBuf out) throws IOException {
// TODO: Send Packet
}
@Override
public void receiveData(ByteBuf in, EntityPlayer player) throws IOException {
byte[] chunk = new byte[in.readShort()];
in.readBytes(chunk);
NoppesUtil.handleLargeData(chunk);
}
}
@@ -1,4 +1,4 @@
package kamkeel.npcs.network.packets.large;
package kamkeel.npcs.network.packets.client.large;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
@@ -1,4 +1,4 @@
package kamkeel.npcs.network.packets.large;
package kamkeel.npcs.network.packets.client.large;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
@@ -1,4 +1,4 @@
package kamkeel.npcs.network.packets.large;
package kamkeel.npcs.network.packets.client.large;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
@@ -0,0 +1,102 @@
package kamkeel.npcs.network.packets.client.large.sync;
import cpw.mods.fml.relauncher.Side;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import kamkeel.npcs.controllers.SyncMaster;
import kamkeel.npcs.network.LargeAbstractPacket;
import kamkeel.npcs.network.PacketChannel;
import kamkeel.npcs.network.PacketHandler;
import kamkeel.npcs.network.enums.EnumClientPacket;
import kamkeel.npcs.network.enums.EnumSyncAction;
import kamkeel.npcs.network.enums.EnumSyncType;
import kamkeel.npcs.util.ByteBufUtils;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.CustomNpcs;
import java.io.IOException;
/**
* A large sync packet that sends chunked data to the client for a given SyncType
* along with a SyncAction (RELOAD, UPDATE, REMOVE).
*/
public final class LargeSyncPacket extends LargeAbstractPacket {
private EnumSyncType enumSyncType;
private EnumSyncAction enumSyncAction;
private NBTTagCompound syncData;
private int operationID;
public LargeSyncPacket() {}
/**
* Constructs a new LargeSyncPacket.
*/
public LargeSyncPacket(EnumSyncType enumSyncType, EnumSyncAction enumSyncAction, int catId, NBTTagCompound syncData) {
this.enumSyncType = enumSyncType;
this.enumSyncAction = enumSyncAction;
this.syncData = syncData;
this.operationID = catId;
}
@Override
public Enum getType() {
return EnumClientPacket.SYNC;
}
@Override
public PacketChannel getChannel() {
return PacketHandler.LARGE_PACKET; // Or whichever channel you want.
}
@Override
protected byte[] getData() throws IOException {
ByteBuf buffer = Unpooled.buffer();
// 1) Write SyncType
buffer.writeInt(enumSyncType.ordinal());
// 2) Write SyncAction
buffer.writeInt(enumSyncAction.ordinal());
// 3) Optional Category ID
buffer.writeInt(operationID);
// 4) Write the NBTTagCompound
ByteBufUtils.writeNBT(buffer, syncData);
// Copy the buffer’s readable bytes into a byte[]
byte[] bytes = new byte[buffer.readableBytes()];
buffer.readBytes(bytes);
return bytes;
}
@Override
protected void handleCompleteData(ByteBuf data, EntityPlayer player) throws IOException {
if(CustomNpcs.side() != Side.CLIENT)
return;
// Reconstruct everything from the ByteBuf
int syncTypeOrdinal = data.readInt();
int syncActionOrdinal = data.readInt();
int categoryID = data.readInt();
EnumSyncType type = EnumSyncType.values()[syncTypeOrdinal];
EnumSyncAction action = EnumSyncAction.values()[syncActionOrdinal];
NBTTagCompound tag = ByteBufUtils.readNBT(data);
// Now do your client-side logic (similar to your old clientSync() or clientSyncUpdate() approach)
handleSyncPacketClient(type, action, categoryID, tag);
}
private void handleSyncPacketClient(EnumSyncType enumSyncType, EnumSyncAction enumSyncAction, int id, NBTTagCompound data) {
switch (enumSyncAction) {
case RELOAD:
SyncMaster.clientSync(enumSyncType, data);
break;
case UPDATE:
SyncMaster.clientUpdate(enumSyncType, id, data);
break;
case REMOVE:
SyncMaster.clientSyncRemove(enumSyncType, id);
break;
}
}
}
@@ -1,41 +0,0 @@
package kamkeel.npcs.network.packets.client.sync;
import io.netty.buffer.ByteBuf;
import kamkeel.npcs.network.AbstractPacket;
import kamkeel.npcs.network.PacketChannel;
import kamkeel.npcs.network.PacketHandler;
import kamkeel.npcs.network.enums.EnumClientPacket;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.Server;
import noppes.npcs.controllers.SyncController;
import java.io.IOException;
public final class SyncAddPacket extends AbstractPacket {
public static final String packetName = "Client|SyncAdd";
public SyncAddPacket() {}
@Override
public Enum getType() {
return EnumClientPacket.SYNC_ADD;
}
@Override
public PacketChannel getChannel() {
return PacketHandler.CLIENT_PACKET;
}
@Override
public void sendData(ByteBuf out) throws IOException {
// TODO: Send Packet
}
@Override
public void receiveData(ByteBuf in, EntityPlayer player) throws IOException {
int synctype = in.readInt();
NBTTagCompound compound = Server.readNBT(in);
SyncController.clientSync(synctype, compound, false);
}
}
@@ -1,41 +0,0 @@
package kamkeel.npcs.network.packets.client.sync;
import io.netty.buffer.ByteBuf;
import kamkeel.npcs.network.AbstractPacket;
import kamkeel.npcs.network.PacketChannel;
import kamkeel.npcs.network.PacketHandler;
import kamkeel.npcs.network.enums.EnumClientPacket;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.Server;
import noppes.npcs.controllers.SyncController;
import java.io.IOException;
public final class SyncEndPacket extends AbstractPacket {
public static final String packetName = "Client|SyncEnd";
public SyncEndPacket() {}
@Override
public Enum getType() {
return EnumClientPacket.SYNC_END;
}
@Override
public PacketChannel getChannel() {
return PacketHandler.CLIENT_PACKET;
}
@Override
public void sendData(ByteBuf out) throws IOException {
// TODO: Send Packet
}
@Override
public void receiveData(ByteBuf in, EntityPlayer player) throws IOException {
int synctype = in.readInt();
NBTTagCompound compound = Server.readNBT(in);
SyncController.clientSync(synctype, compound, true);
}
}
@@ -1,38 +0,0 @@
package kamkeel.npcs.network.packets.client.sync;
import io.netty.buffer.ByteBuf;
import kamkeel.npcs.network.AbstractPacket;
import kamkeel.npcs.network.PacketChannel;
import kamkeel.npcs.network.PacketHandler;
import kamkeel.npcs.network.enums.EnumClientPacket;
import net.minecraft.entity.player.EntityPlayer;
import noppes.npcs.client.NoppesUtil;
import java.io.IOException;
public final class SyncPlayerPacket extends AbstractPacket {
public static final String packetName = "Client|SyncPlayer";
public SyncPlayerPacket() {}
@Override
public Enum getType() {
return EnumClientPacket.SYNC_PLAYER;
}
@Override
public PacketChannel getChannel() {
return PacketHandler.CLIENT_PACKET;
}
@Override
public void sendData(ByteBuf out) throws IOException {
// TODO: Send Packet
}
@Override
public void receiveData(ByteBuf in, EntityPlayer player) throws IOException {
boolean sync = in.readBoolean();
NoppesUtil.handlePlayerDataEnd(sync);
}
}
@@ -1,39 +0,0 @@
package kamkeel.npcs.network.packets.client.sync;
import io.netty.buffer.ByteBuf;
import kamkeel.npcs.network.AbstractPacket;
import kamkeel.npcs.network.PacketChannel;
import kamkeel.npcs.network.PacketHandler;
import kamkeel.npcs.network.enums.EnumClientPacket;
import net.minecraft.entity.player.EntityPlayer;
import noppes.npcs.controllers.SyncController;
import java.io.IOException;
public final class SyncRemovePacket extends AbstractPacket {
public static final String packetName = "Client|SyncRemove";
public SyncRemovePacket() {}
@Override
public Enum getType() {
return EnumClientPacket.SYNC_REMOVE;
}
@Override
public PacketChannel getChannel() {
return PacketHandler.CLIENT_PACKET;
}
@Override
public void sendData(ByteBuf out) throws IOException {
// TODO: Send Packet
}
@Override
public void receiveData(ByteBuf in, EntityPlayer player) throws IOException {
int synctype = in.readInt();
int id = in.readInt();
SyncController.clientSyncRemove(synctype, id);
}
}
@@ -1,41 +0,0 @@
package kamkeel.npcs.network.packets.client.sync;
import io.netty.buffer.ByteBuf;
import kamkeel.npcs.network.AbstractPacket;
import kamkeel.npcs.network.PacketChannel;
import kamkeel.npcs.network.PacketHandler;
import kamkeel.npcs.network.enums.EnumClientPacket;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.Server;
import noppes.npcs.controllers.SyncController;
import java.io.IOException;
public final class SyncUpdatePacket extends AbstractPacket {
public static final String packetName = "Client|SyncUpdate";
public SyncUpdatePacket() {}
@Override
public Enum getType() {
return EnumClientPacket.SYNC_UPDATE;
}
@Override
public PacketChannel getChannel() {
return PacketHandler.CLIENT_PACKET;
}
@Override
public void sendData(ByteBuf out) throws IOException {
// TODO: Send Packet
}
@Override
public void receiveData(ByteBuf in, EntityPlayer player) throws IOException {
int synctype = in.readInt();
NBTTagCompound compound = Server.readNBT(in);
SyncController.clientSyncUpdate(synctype, compound, in);
}
}
+4 -2
View File
@@ -17,7 +17,7 @@ import noppes.npcs.containers.*;
import noppes.npcs.controllers.data.PlayerData;
import noppes.npcs.entity.EntityNPCInterface;
public abstract class CommonProxy implements IGuiHandler {
public class CommonProxy implements IGuiHandler {
public boolean newVersionAvailable = false;
public int revision = 1;
@@ -159,7 +159,9 @@ public abstract class CommonProxy implements IGuiHandler {
}
public abstract void generateBigSmokeParticles(World world, int x, int y, int z, boolean signalFire);
public void generateBigSmokeParticles(World world, int x, int y, int z, boolean signalFire){
}
public String getAchievementDesc(Achievement achievement) {
return "";
+2 -6
View File
@@ -90,6 +90,8 @@ public class CustomNpcs {
Channel = NetworkRegistry.INSTANCE.newEventDrivenChannel("CustomNPCs");
ChannelPlayer = NetworkRegistry.INSTANCE.newEventDrivenChannel("CustomNPCsPlayer");
PacketHandler.Instance = new PacketHandler();
MinecraftServer server = MinecraftServer.getServer();
String dir = "";
if (server != null) {
@@ -250,12 +252,6 @@ public class CustomNpcs {
ServerTagMapController.Instance = new ServerTagMapController();
}
//Loading items in the about to start event was corrupting items with a damage value
@EventHandler
public void postInit(FMLPostInitializationEvent event) {
PacketHandler.Instance = new PacketHandler();
}
@EventHandler
public void stopped(FMLServerStoppedEvent event){
@@ -1,6 +1,7 @@
package noppes.npcs;
import io.netty.buffer.ByteBuf;
import kamkeel.npcs.controllers.SyncMaster;
import net.minecraft.command.server.CommandBlockLogic;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityList;
@@ -572,7 +573,7 @@ public class NoppesUtilServer {
playerdata.save();
}
if(pl != null) {
SyncController.syncPlayer((EntityPlayerMP) pl);
SyncMaster.syncPlayer((EntityPlayerMP) pl);
}
sendPlayerData(type, player, name);
}
@@ -847,48 +848,6 @@ public class NoppesUtilServer {
return entity;
}
}
public static void sendPlayerDataCompound(EntityPlayerMP player, NBTTagCompound compound, boolean sync) {
try {
byte[] bytes = CompressedStreamTools.compress(compound);
// Split byte array into chunks and send each chunk
int chunkSize = 16000;
int totalChunks = (int) Math.ceil((double) bytes.length / chunkSize);
for (int i = 0; i < totalChunks; i++) {
int start = i * chunkSize;
int length = Math.min(bytes.length - start, chunkSize);
byte[] chunk = new byte[length];
System.arraycopy(bytes, start, chunk, 0, length);
Server.sendData(player, EnumPacketClient.LARGE_NBT_PART, (Object) chunk);
}
// Send end packet
Server.sendData(player, EnumPacketClient.SYNC_PLAYER, sync);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void sendDBCCompound(EntityPlayerMP player, NBTTagCompound compound) {
try {
byte[] bytes = CompressedStreamTools.compress(compound);
// Split byte array into chunks and send each chunk
int chunkSize = 16000;
int totalChunks = (int) Math.ceil((double) bytes.length / chunkSize);
for (int i = 0; i < totalChunks; i++) {
int start = i * chunkSize;
int length = Math.min(bytes.length - start, chunkSize);
byte[] chunk = new byte[length];
System.arraycopy(bytes, start, chunk, 0, length);
Server.sendData(player, EnumPacketClient.LARGE_NBT_PART, (Object) chunk);
}
// Send end packet
Server.sendData(player, EnumPacketClient.DBC_FORM);
} catch (IOException e) {
e.printStackTrace();
}
}
public static boolean isOp(EntityPlayer player) {
return MinecraftServer.getServer().getConfigurationManager().func_152596_g(player.getGameProfile());
@@ -5,6 +5,7 @@ import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.PlayerEvent;
import cpw.mods.fml.common.gameevent.TickEvent;
import cpw.mods.fml.relauncher.Side;
import kamkeel.npcs.controllers.SyncMaster;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
@@ -62,7 +63,7 @@ public class ScriptPlayerEventHandler {
PlayerData playerData = PlayerDataController.Instance.getPlayerData(player);
if(playerData.updateClient) {
NoppesUtilServer.sendPlayerDataCompound((EntityPlayerMP)player, playerData.getSyncNBT(), true);
SyncMaster.syncPlayerData((EntityPlayerMP)player, true);
playerData.updateClient = false;
}
@@ -5,6 +5,7 @@ import cpw.mods.fml.common.gameevent.PlayerEvent;
import cpw.mods.fml.common.gameevent.TickEvent;
import cpw.mods.fml.common.gameevent.TickEvent.Phase;
import kamkeel.npcs.controllers.ProfileController;
import kamkeel.npcs.controllers.SyncMaster;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.WorldServer;
@@ -52,7 +53,7 @@ public class ServerTickHandler {
playerData.onLogin();
}
SyncController.syncPlayer(player);
SyncMaster.syncPlayer(player);
ProfileController.login(player);
}
@@ -19,8 +19,6 @@ import org.lwjgl.opengl.GL11;
import java.util.Random;
import static noppes.npcs.client.ClientProxy.RAND;
public class BlockCampfire extends BlockLightable{
public BlockCampfire(boolean lit) {
@@ -329,51 +329,4 @@ public class NoppesUtil {
public static void isGUIOpen(boolean isGUIOpen) {
Client.sendData(EnumPacketServer.IsGuiOpen, isGUIOpen);
}
private static byte[] accumulatedData = new byte[0];
public static void handleLargeData(byte[] chunk) {
// Append the chunk to the accumulated data
accumulatedData = concatByteArrays(accumulatedData, chunk);
}
public static void handlePlayerDataEnd(boolean sync) {
NBTTagCompound compound;
try {
compound = CompressedStreamTools.func_152457_a(accumulatedData, new NBTSizeTracker(2097152L));
if(sync){
ClientCacheHandler.playerData.setSyncNBT(compound);
}
else {
ClientCacheHandler.playerData.setSyncNBTFull(compound);
}
} catch (IOException e) {
e.printStackTrace();
return;
} finally {
// Reset the accumulated data after processing
accumulatedData = new byte[0];
}
}
public static void handleFormEnd() {
NBTTagCompound compound;
try {
compound = CompressedStreamTools.func_152457_a(accumulatedData, new NBTSizeTracker(2097152L));
ClientCacheHandler.playerData.setDBCSync(compound);
} catch (IOException e) {
e.printStackTrace();
return;
} finally {
accumulatedData = new byte[0];
}
}
// Utility method to concatenate two byte arrays
private static byte[] concatByteArrays(byte[] a, byte[] b) {
byte[] result = new byte[a.length + b.length];
System.arraycopy(a, 0, result, 0, a.length);
System.arraycopy(b, 0, result, a.length, b.length);
return result;
}
}
@@ -111,31 +111,6 @@ public class PacketHandlerClient extends PacketHandlerServer{
RecipeController.Instance.anvilRecipes = RecipeController.syncRecipes;
RecipeController.syncRecipes = new HashMap<Integer, RecipeCarpentry>();
}
else if(type == EnumPacketClient.LARGE_NBT_PART){
byte[] chunk = new byte[buffer.readShort()];
buffer.readBytes(chunk);
NoppesUtil.handleLargeData(chunk);
}
else if(type == EnumPacketClient.SYNC_PLAYER){
boolean sync = buffer.readBoolean();
NoppesUtil.handlePlayerDataEnd(sync);
}
else if(type == EnumPacketClient.SYNC_ADD || type == EnumPacketClient.SYNC_END){
int synctype = buffer.readInt();
NBTTagCompound compound = Server.readNBT(buffer);
SyncController.clientSync(synctype, compound, type == EnumPacketClient.SYNC_END);
}
else if(type == EnumPacketClient.SYNC_UPDATE){
int synctype = buffer.readInt();
NBTTagCompound compound = Server.readNBT(buffer);
SyncController.clientSyncUpdate(synctype, compound, buffer);
}
else if(type == EnumPacketClient.SYNC_REMOVE){
int synctype = buffer.readInt();
int id = buffer.readInt();
SyncController.clientSyncRemove(synctype, id);
}
else if(type == EnumPacketClient.SYNC_CONFIG){
NBTTagCompound configNBT = Server.readNBT(buffer);
SyncController.receiveConfigs(configNBT);
@@ -450,8 +425,5 @@ public class PacketHandlerClient extends PacketHandlerServer{
if(gui instanceof IPartyData)
((IPartyData)gui).setPartyData(Server.readNBT(buffer));
}
else if(type == EnumPacketClient.DBC_FORM){
NoppesUtil.handleFormEnd();
}
}
}
@@ -55,9 +55,6 @@ public enum EnumPacketClient {
STOP_SOUNDS,
SYNC_WEAPON,
MARK_DATA,
SYNC_ADD, SYNC_END, SYNC_UPDATE, SYNC_REMOVE, SYNC_PLAYER,
SYNC_CONFIG,
DBC_FORM,
LARGE_NBT_PART, LARGE_NBT_END,
PARTY_DATA
}
@@ -1,11 +0,0 @@
package noppes.npcs.constants;
public class SyncType {
public static final int FACTION = 1;
public static final int QUEST = 2;
public static final int QUEST_CATEGORY = 3;
public static final int DIALOG = 4;
public static final int DIALOG_CATEGORY = 5;
public static final int PLAYER_DATA = 6;
public static final int MAGIC = 7;
}
@@ -1,18 +1,17 @@
package noppes.npcs.controllers;
import kamkeel.npcs.controllers.SyncMaster;
import kamkeel.npcs.network.enums.EnumSyncType;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import noppes.npcs.CustomNpcs;
import noppes.npcs.LogWriter;
import noppes.npcs.NoppesStringUtils;
import noppes.npcs.Server;
import noppes.npcs.api.handler.IDialogHandler;
import noppes.npcs.api.handler.data.IDialog;
import noppes.npcs.api.handler.data.IDialogCategory;
import noppes.npcs.constants.EnumOptionType;
import noppes.npcs.constants.EnumPacketClient;
import noppes.npcs.constants.SyncType;
import noppes.npcs.controllers.data.Dialog;
import noppes.npcs.controllers.data.DialogCategory;
import noppes.npcs.controllers.data.DialogOption;
@@ -230,7 +229,7 @@ public class DialogController implements IDialogHandler {
dir.mkdirs();
}
categories.put(category.id, category);
SyncController.updateDialogCat(category);
SyncMaster.syncUpdate(EnumSyncType.DIALOG_CATEGORY, -1, SyncMaster.updateDialogCat(category));
}
public void removeCategory(int category){
@@ -243,8 +242,8 @@ public class DialogController implements IDialogHandler {
for(int dia : cat.dialogs.keySet())
dialogs.remove(dia);
categories.remove(category);
Server.sendToAll(EnumPacketClient.SYNC_REMOVE, SyncType.DIALOG_CATEGORY, category);
}
SyncMaster.syncRemove(EnumSyncType.DIALOG_CATEGORY, category);
}
private boolean containsCategoryName(String name) {
name = name.toLowerCase();
@@ -291,7 +290,7 @@ public class DialogController implements IDialogHandler {
if(file2.exists())
file2.delete();
file.renameTo(file2);
Server.sendToAll(EnumPacketClient.SYNC_UPDATE, SyncType.DIALOG, dialog.writeToNBT(new NBTTagCompound()), category.id);
SyncMaster.syncUpdate(EnumSyncType.DIALOG, category.id, dialog.writeToNBT(new NBTTagCompound()));
} catch (Exception e) {
LogWriter.except(e);
}
@@ -305,7 +304,7 @@ public class DialogController implements IDialogHandler {
return;
category.dialogs.remove(dialog.id);
dialogs.remove(dialog.id);
Server.sendToAll(EnumPacketClient.SYNC_REMOVE, SyncType.DIALOG, dialog.id);
SyncMaster.syncRemove(EnumSyncType.DIALOG, dialog.id);
}
private File getDir(){
@@ -1,15 +1,14 @@
package noppes.npcs.controllers;
import kamkeel.npcs.controllers.SyncMaster;
import kamkeel.npcs.network.enums.EnumSyncType;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import noppes.npcs.CustomNpcs;
import noppes.npcs.LogWriter;
import noppes.npcs.Server;
import noppes.npcs.api.handler.IFactionHandler;
import noppes.npcs.api.handler.data.IFaction;
import noppes.npcs.constants.EnumPacketClient;
import noppes.npcs.constants.SyncType;
import noppes.npcs.controllers.data.Faction;
import java.io.*;
@@ -158,7 +157,7 @@ public class FactionController implements IFactionHandler {
NBTTagCompound facCompound = new NBTTagCompound();
faction.writeNBT(facCompound);
Server.sendToAll(EnumPacketClient.SYNC_UPDATE, SyncType.FACTION, facCompound);
SyncMaster.syncUpdate(EnumSyncType.FACTION, -1, facCompound);
saveFactions();
}
@@ -193,7 +192,7 @@ public class FactionController implements IFactionHandler {
} else {
this.saveFactions();
faction.id = -1;
Server.sendToAll(EnumPacketClient.SYNC_REMOVE, SyncType.FACTION, id);
SyncMaster.syncRemove(EnumSyncType.FACTION, id);
return faction;
}
} else {
@@ -5,10 +5,7 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import noppes.npcs.CustomNpcs;
import noppes.npcs.LogWriter;
import noppes.npcs.Server;
import noppes.npcs.api.handler.data.IMagic;
import noppes.npcs.constants.EnumPacketClient;
import noppes.npcs.constants.SyncType;
import noppes.npcs.controllers.data.Magic;
import java.io.*;
@@ -160,7 +157,7 @@ public class MagicController {
NBTTagCompound facCompound = new NBTTagCompound();
mag.writeNBT(facCompound);
Server.sendToAll(EnumPacketClient.SYNC_UPDATE, SyncType.MAGIC, facCompound);
// Server.sendToAll(EnumPacketClient.SYNC_UPDATE, SyncType.MAGIC, facCompound);
saveFactions();
}
@@ -1,17 +1,16 @@
package noppes.npcs.controllers;
import kamkeel.npcs.controllers.SyncMaster;
import kamkeel.npcs.network.enums.EnumSyncType;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import noppes.npcs.CustomNpcs;
import noppes.npcs.LogWriter;
import noppes.npcs.NoppesStringUtils;
import noppes.npcs.Server;
import noppes.npcs.api.handler.IQuestHandler;
import noppes.npcs.api.handler.data.IQuest;
import noppes.npcs.api.handler.data.IQuestCategory;
import noppes.npcs.constants.EnumPacketClient;
import noppes.npcs.constants.SyncType;
import noppes.npcs.controllers.data.Quest;
import noppes.npcs.controllers.data.QuestCategory;
import noppes.npcs.util.NBTJsonUtil;
@@ -146,7 +145,7 @@ public class QuestController implements IQuestHandler {
for(int dia : cat.quests.keySet())
quests.remove(dia);
categories.remove(category);
Server.sendToAll(EnumPacketClient.SYNC_REMOVE, SyncType.QUEST_CATEGORY, category);
SyncMaster.syncRemove(EnumSyncType.QUEST_CATEGORY, category);
}
public void saveCategory(QuestCategory category){
@@ -177,7 +176,7 @@ public class QuestController implements IQuestHandler {
dir.mkdirs();
}
categories.put(category.id, category);
SyncController.updateQuestCat(category);
SyncMaster.syncUpdate(EnumSyncType.QUEST_CATEGORY, -1, SyncMaster.updateQuestCat(category));
}
private boolean containsCategoryName(String name) {
name = name.toLowerCase();
@@ -225,7 +224,7 @@ public class QuestController implements IQuestHandler {
if(file2.exists())
file2.delete();
file.renameTo(file2);
Server.sendToAll(EnumPacketClient.SYNC_UPDATE, SyncType.QUEST, quest.writeToNBT(new NBTTagCompound()), category.id);
SyncMaster.syncUpdate(EnumSyncType.QUEST, category.id, quest.writeToNBT(new NBTTagCompound()));
} catch (Exception e) {
e.printStackTrace();
}
@@ -9,10 +9,6 @@ import noppes.npcs.NoppesUtilServer;
import noppes.npcs.Server;
import noppes.npcs.config.ConfigMain;
import noppes.npcs.constants.EnumPacketClient;
import noppes.npcs.constants.SyncType;
import noppes.npcs.controllers.data.*;
import java.util.HashMap;
public class SyncController {
@@ -26,332 +22,4 @@ public class SyncController {
ConfigMain.EnableProfiles = compound.getBoolean("Profiles");
}
public static void syncPlayer(EntityPlayerMP player){
NBTTagList list = new NBTTagList();
NBTTagCompound compound = new NBTTagCompound();
////////////////////////////////////////////////////////////////////////////////
// Faction Sync
for(Faction faction : FactionController.getInstance().factions.values()){
NBTTagCompound factioNBT = new NBTTagCompound();
faction.writeNBT(factioNBT);
list.appendTag(factioNBT);
if(list.tagCount() > 10){
compound = new NBTTagCompound();
compound.setTag("Data", list);
Server.sendData(player, EnumPacketClient.SYNC_ADD, SyncType.FACTION, compound);
list = new NBTTagList();
}
}
compound = new NBTTagCompound();
compound.setTag("Data", list);
Server.sendData(player, EnumPacketClient.SYNC_END, SyncType.FACTION, compound);
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Quest Sync
for(QuestCategory category : QuestController.Instance.categories.values()){
NBTTagCompound questCompound;
NBTTagList questList = new NBTTagList();
for(int questID : category.quests.keySet()){
Quest quest = category.quests.get(questID);
questList.appendTag(quest.writeToNBT(new NBTTagCompound()));
if(questList.tagCount() > 20){
questCompound = new NBTTagCompound();
questCompound.setTag("Data", questList);
questCompound.setTag("CatNBT", category.writeSmallNBT(new NBTTagCompound()));
Server.sendData(player, EnumPacketClient.SYNC_ADD, SyncType.QUEST_CATEGORY, questCompound);
questList = new NBTTagList();
}
}
questCompound = new NBTTagCompound();
questCompound.setTag("Data", questList);
questCompound.setTag("CatNBT", category.writeSmallNBT(new NBTTagCompound()));
Server.sendData(player, EnumPacketClient.SYNC_ADD, SyncType.QUEST_CATEGORY, questCompound);
}
Server.sendData(player, EnumPacketClient.SYNC_END, SyncType.QUEST_CATEGORY, new NBTTagCompound());
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Dialog Sync
for(DialogCategory category : DialogController.Instance.categories.values()){
NBTTagCompound dialogCompound;
NBTTagList dialogList = new NBTTagList();
for(int dialogID : category.dialogs.keySet()){
Dialog dialog = category.dialogs.get(dialogID);
dialogList.appendTag(dialog.writeToNBT(new NBTTagCompound()));
if(dialogList.tagCount() > 20){
dialogCompound = new NBTTagCompound();
dialogCompound.setTag("Data", dialogList);
dialogCompound.setTag("CatNBT", category.writeSmallNBT(new NBTTagCompound()));
Server.sendData(player, EnumPacketClient.SYNC_ADD, SyncType.DIALOG_CATEGORY, dialogCompound);
dialogList = new NBTTagList();
}
}
dialogCompound = new NBTTagCompound();
dialogCompound.setTag("Data", dialogList);
dialogCompound.setTag("CatNBT", category.writeSmallNBT(new NBTTagCompound()));
Server.sendData(player, EnumPacketClient.SYNC_ADD, SyncType.DIALOG_CATEGORY, dialogCompound);
}
Server.sendData(player, EnumPacketClient.SYNC_END, SyncType.DIALOG_CATEGORY, new NBTTagCompound());
////////////////////////////////////////////////////////////////////////////////
DBCAddon.instance.syncPlayer(player);
PlayerData data = PlayerData.get(player);
if(data != null){
NoppesUtilServer.sendPlayerDataCompound(player, data.getSyncNBTFull(), false);
}
}
public static void syncAllDialogs() {
for(DialogCategory category : DialogController.Instance.categories.values()){
NBTTagCompound dialogCompound;
NBTTagList dialogList = new NBTTagList();
for(int dialogID : category.dialogs.keySet()){
Dialog dialog = category.dialogs.get(dialogID);
dialogList.appendTag(dialog.writeToNBT(new NBTTagCompound()));
if(dialogList.tagCount() > 20){
dialogCompound = new NBTTagCompound();
dialogCompound.setTag("Data", dialogList);
dialogCompound.setTag("CatNBT", category.writeSmallNBT(new NBTTagCompound()));
Server.sendToAll(EnumPacketClient.SYNC_ADD, SyncType.DIALOG_CATEGORY, dialogCompound);
dialogList = new NBTTagList();
}
}
dialogCompound = new NBTTagCompound();
dialogCompound.setTag("Data", dialogList);
dialogCompound.setTag("CatNBT", category.writeSmallNBT(new NBTTagCompound()));
Server.sendToAll(EnumPacketClient.SYNC_ADD, SyncType.DIALOG_CATEGORY, dialogCompound);
}
Server.sendToAll(EnumPacketClient.SYNC_END, SyncType.DIALOG_CATEGORY, new NBTTagCompound());
}
public static void syncAllQuests() {
for(QuestCategory category : QuestController.Instance.categories.values()){
NBTTagCompound questCompound;
NBTTagList questList = new NBTTagList();
for(int questID : category.quests.keySet()){
Quest quest = category.quests.get(questID);
questList.appendTag(quest.writeToNBT(new NBTTagCompound()));
if(questList.tagCount() > 20){
questCompound = new NBTTagCompound();
questCompound.setTag("Data", questList);
questCompound.setTag("CatNBT", category.writeSmallNBT(new NBTTagCompound()));
Server.sendToAll(EnumPacketClient.SYNC_ADD, SyncType.QUEST_CATEGORY, questCompound);
questList = new NBTTagList();
}
}
questCompound = new NBTTagCompound();
questCompound.setTag("Data", questList);
questCompound.setTag("CatNBT", category.writeSmallNBT(new NBTTagCompound()));
Server.sendToAll(EnumPacketClient.SYNC_ADD, SyncType.QUEST_CATEGORY, questCompound);
}
Server.sendToAll(EnumPacketClient.SYNC_END, SyncType.QUEST_CATEGORY, new NBTTagCompound());
}
public static void updateQuestCat(QuestCategory questCategory) {
NBTTagCompound questCompound;
NBTTagList questList = new NBTTagList();
for(int questID : questCategory.quests.keySet()){
Quest quest = questCategory.quests.get(questID);
questList.appendTag(quest.writeToNBT(new NBTTagCompound()));
if(questList.tagCount() > 20){
questCompound = new NBTTagCompound();
questCompound.setTag("Data", questList);
questCompound.setTag("CatNBT", questCategory.writeSmallNBT(new NBTTagCompound()));
Server.sendToAll(EnumPacketClient.SYNC_UPDATE, SyncType.QUEST_CATEGORY, questCompound);
questList = new NBTTagList();
}
}
questCompound = new NBTTagCompound();
questCompound.setTag("Data", questList);
questCompound.setTag("CatNBT", questCategory.writeSmallNBT(new NBTTagCompound()));
Server.sendToAll(EnumPacketClient.SYNC_UPDATE, SyncType.QUEST_CATEGORY, questCompound);
}
public static void updateDialogCat(DialogCategory dialogCategory) {
NBTTagCompound dialogCompound;
NBTTagList dialogList = new NBTTagList();
for(int questID : dialogCategory.dialogs.keySet()){
Dialog dialog = dialogCategory.dialogs.get(questID);
dialogList.appendTag(dialog.writeToNBT(new NBTTagCompound()));
if(dialogList.tagCount() > 20){
dialogCompound = new NBTTagCompound();
dialogCompound.setTag("Data", dialogList);
dialogCompound.setTag("CatNBT", dialogCategory.writeSmallNBT(new NBTTagCompound()));
Server.sendToAll(EnumPacketClient.SYNC_UPDATE, SyncType.DIALOG_CATEGORY, dialogCompound);
dialogList = new NBTTagList();
}
}
dialogCompound = new NBTTagCompound();
dialogCompound.setTag("Data", dialogList);
dialogCompound.setTag("CatNBT", dialogCategory.writeSmallNBT(new NBTTagCompound()));
Server.sendToAll(EnumPacketClient.SYNC_UPDATE, SyncType.DIALOG_CATEGORY, dialogCompound);
}
public static void clientSync(int synctype, NBTTagCompound compound, boolean syncEnd) {
if(synctype == SyncType.FACTION){
NBTTagList list = compound.getTagList("Data", 10);
for(int i = 0; i < list.tagCount(); i++)
{
Faction faction = new Faction();
faction.readNBT(list.getCompoundTagAt(i));
FactionController.getInstance().factionsSync.put(faction.id, faction);
}
if(syncEnd){
FactionController.getInstance().factions = FactionController.getInstance().factionsSync;
FactionController.getInstance().factionsSync = new HashMap<Integer, Faction>();
}
}
else if(synctype == SyncType.QUEST_CATEGORY){
if(!compound.hasNoTags()){
QuestCategory category = new QuestCategory();
category.readSmallNBT(compound.getCompoundTag("CatNBT"));
NBTTagList list = compound.getTagList("Data", 10);
if(QuestController.Instance.categoriesSync.containsKey(category.id)){
category = QuestController.Instance.categoriesSync.get(category.id);
category.readSmallNBT(compound.getCompoundTag("CatNBT"));
}
for(int i = 0; i < list.tagCount(); i++)
{
Quest quest = new Quest();
quest.readNBT(list.getCompoundTagAt(i));
quest.category = category;
category.quests.put(quest.id, quest);
}
QuestController.Instance.categoriesSync.put(category.id, category);
}
if(syncEnd){
HashMap<Integer,Quest> quests = new HashMap<Integer, Quest>();
for(QuestCategory category : QuestController.Instance.categoriesSync.values()){
for(Quest quest : category.quests.values()){
quests.put(quest.id, quest);
}
}
QuestController.Instance.categories = QuestController.Instance.categoriesSync;
QuestController.Instance.quests = quests;
QuestController.Instance.categoriesSync = new HashMap<Integer, QuestCategory>();
}
}
else if(synctype == SyncType.DIALOG_CATEGORY){
if(!compound.hasNoTags()){
DialogCategory category = new DialogCategory();
category.readSmallNBT(compound.getCompoundTag("CatNBT"));
NBTTagList list = compound.getTagList("Data", 10);
if(DialogController.Instance.categoriesSync.containsKey(category.id)){
category = DialogController.Instance.categoriesSync.get(category.id);
category.readSmallNBT(compound.getCompoundTag("CatNBT"));
}
for(int i = 0; i < list.tagCount(); i++)
{
Dialog dialog = new Dialog();
dialog.readNBT(list.getCompoundTagAt(i));
dialog.category = category;
category.dialogs.put(dialog.id, dialog);
}
DialogController.Instance.categoriesSync.put(category.id, category);
}
if(syncEnd){
HashMap<Integer,Dialog> dialogs = new HashMap<Integer, Dialog>();
for(DialogCategory category : DialogController.Instance.categoriesSync.values()){
for(Dialog dialog : category.dialogs.values()){
dialogs.put(dialog.id, dialog);
}
}
DialogController.Instance.categories = DialogController.Instance.categoriesSync;
DialogController.Instance.dialogs = dialogs;
DialogController.Instance.categoriesSync = new HashMap<Integer, DialogCategory>();
}
}
}
public static void clientSyncUpdate(int synctype, NBTTagCompound compound, ByteBuf buffer) {
if(synctype == SyncType.FACTION){
Faction faction = new Faction();
faction.readNBT(compound);
FactionController.getInstance().factions.put(faction.id, faction);
}
else if(synctype == SyncType.DIALOG){
DialogCategory category = DialogController.Instance.categories.get(buffer.readInt());
Dialog dialog = new Dialog();
dialog.category = category;
dialog.readNBT(compound);
DialogController.Instance.dialogs.put(dialog.id, dialog);
category.dialogs.put(dialog.id, dialog);
}
else if(synctype == SyncType.DIALOG_CATEGORY){
DialogCategory category = new DialogCategory();
category.readSmallNBT(compound.getCompoundTag("CatNBT"));
NBTTagList list = compound.getTagList("Data", 10);
if(DialogController.Instance.categoriesSync.containsKey(category.id)){
category = DialogController.Instance.categoriesSync.get(category.id);
category.readSmallNBT(compound.getCompoundTag("CatNBT"));
}
for(int i = 0; i < list.tagCount(); i++)
{
Dialog dialog = new Dialog();
dialog.readNBT(list.getCompoundTagAt(i));
dialog.category = category;
category.dialogs.put(dialog.id, dialog);
}
DialogController.Instance.categories.put(category.id, category);
}
else if(synctype == SyncType.QUEST){
QuestCategory category = QuestController.Instance.categories.get(buffer.readInt());
Quest quest = new Quest();
quest.category = category;
quest.readNBT(compound);
QuestController.Instance.quests.put(quest.id, quest);
category.quests.put(quest.id, quest);
}
else if(synctype == SyncType.QUEST_CATEGORY){
QuestCategory category = new QuestCategory();
category.readSmallNBT(compound.getCompoundTag("CatNBT"));
NBTTagList list = compound.getTagList("Data", 10);
if(QuestController.Instance.categoriesSync.containsKey(category.id)){
category = QuestController.Instance.categoriesSync.get(category.id);
category.readSmallNBT(compound.getCompoundTag("CatNBT"));
}
for(int i = 0; i < list.tagCount(); i++)
{
Quest quest = new Quest();
quest.readNBT(list.getCompoundTagAt(i));
quest.category = category;
category.quests.put(quest.id, quest);
}
QuestController.Instance.categories.put(category.id, category);
}
}
public static void clientSyncRemove(int synctype, int id) {
if(synctype == SyncType.FACTION){
FactionController.getInstance().factions.remove(id);
}
else if(synctype == SyncType.DIALOG){
Dialog dialog = DialogController.Instance.dialogs.remove(id);
if(dialog != null) {
dialog.category.dialogs.remove(id);
}
}
else if(synctype == SyncType.DIALOG_CATEGORY){
DialogCategory category = DialogController.Instance.categories.remove(id);
if(category != null) {
DialogController.Instance.dialogs.keySet().removeAll(category.dialogs.keySet());
}
}
else if(synctype == SyncType.QUEST){
Quest quest = QuestController.Instance.quests.remove(id);
if(quest != null) {
quest.category.quests.remove(id);
}
}
else if(synctype == SyncType.QUEST_CATEGORY){
QuestCategory category = QuestController.Instance.categories.remove(id);
if(category != null) {
QuestController.Instance.quests.keySet().removeAll(category.quests.keySet());
}
}
}
}