This commit is contained in:
Kam committed 2025-01-30 22:50:10 -05:00
1 parent 9ea1385ac2
commit 3979d1895b
37 files changed
+217 -95

No files matched your search

@@ -4,6 +4,7 @@ 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 noppes.npcs.LogWriter;
import org.lwjgl.Sys;
import java.io.IOException;
@@ -61,48 +62,77 @@ public abstract class LargeAbstractPacket extends AbstractPacket {
@Override
public void receiveData(ByteBuf in, EntityPlayer player) throws IOException {
long mostSigBits = in.readLong();
long leastSigBits = in.readLong();
UUID packetId = new UUID(mostSigBits, leastSigBits);
UUID packetId = null;
PacketStorage storage = null;
ByteBuf chunk = null;
ByteBuf completeData = null;
int totalSize = in.readInt();
int offset = in.readInt();
int totalChunks = in.readInt(); // Currently unused aside from potential debugging/verification
int chunkSize = Math.min(CHUNK_SIZE, totalSize - offset);
try {
// Read UUID from the buffer
long mostSigBits = in.readLong();
long leastSigBits = in.readLong();
packetId = new UUID(mostSigBits, leastSigBits);
// Basic validation to ensure the chunk won't overwrite out of bounds
if (chunkSize <= 0 || offset < 0 || (offset + chunkSize) > totalSize) {
throw new IndexOutOfBoundsException("Invalid chunk size/offset: chunkSize="
+ chunkSize + ", offset=" + offset + ", totalSize=" + totalSize);
// Read packet metadata
int totalSize = in.readInt();
int offset = in.readInt();
int totalChunks = in.readInt(); // Debugging info
int chunkSize = Math.min(CHUNK_SIZE, totalSize - offset);
// Basic validation to ensure the chunk won't overwrite out of bounds
if (chunkSize <= 0 || offset < 0 || (offset + chunkSize) > totalSize) {
throw new IndexOutOfBoundsException("Invalid chunk size/offset: chunkSize="
+ chunkSize + ", offset=" + offset + ", totalSize=" + totalSize);
}
// Read the exact chunk data
chunk = in.readBytes(chunkSize);
// Store the received chunk in a PacketStorage
storage = packetChunks.computeIfAbsent(packetId, k -> new PacketStorage(Unpooled.buffer(totalSize), 0, totalSize));
// Write this chunk into the buffer at the correct offset
storage.data.setBytes(offset, chunk);
storage.receivedSoFar += chunkSize;
// If we've received all bytes, finalize
if (storage.receivedSoFar >= totalSize) {
packetChunks.remove(packetId);
completeData = storage.data.copy(0, totalSize);
try {
handleCompleteData(completeData, player);
} finally {
completeData.release();
}
storage.data.release();
}
} catch (Exception e) {
// Log the exception with contextual information
LogWriter.error("Error in receiveData for packetId " + (packetId != null ? packetId.toString() : "null") + ": " + e.getMessage());
e.printStackTrace();
// If storage was created but the packet is incomplete, release storage.data
if (storage != null && storage.receivedSoFar < storage.totalSize) {
storage.data.release();
if (packetId != null) {
packetChunks.remove(packetId);
}
}
throw e; // Rethrow the exception after logging
} finally {
// Ensure that 'chunk' is released if it was allocated
if (chunk != null && chunk.refCnt() > 0) {
chunk.release();
}
// Ensure that 'completeData' is released if it was created and not yet released
if (completeData != null && completeData.refCnt() > 0) {
completeData.release();
}
}
// Read the exact chunk data
ByteBuf chunk = in.readBytes(chunkSize);
// Store the received chunk in a PacketStorage
PacketStorage storage = packetChunks.computeIfAbsent(packetId,
k -> new PacketStorage(Unpooled.buffer(totalSize), 0, totalSize));
// Write this chunk into the buffer at the correct offset
storage.data.setBytes(offset, chunk);
storage.receivedSoFar += chunkSize;
// If we've received all bytes, finalize
if (storage.receivedSoFar >= totalSize) {
// Remove from the map to clean up
packetChunks.remove(packetId);
// We now have the complete data in storage.data
// Copy (or slice) the exact bytes, then handle them
ByteBuf completeData = storage.data.copy(0, totalSize);
// Clean up and handle
storage.data.release();
handleCompleteData(completeData, player);
completeData.release();
}
chunk.release();
}
protected abstract byte[] getData() throws IOException;
@@ -1,26 +1,121 @@
package kamkeel.npcs.network.enums;
import noppes.npcs.CustomNpcsPermissions;
public enum EnumRequestPacket {
Delete,
RemoteMainMenu,
RemoteGlobalMenu,
NpcMenuClose,
RemoteDelete,
RemoteFreeze,
RemoteFreezeGet,
RemoteReset,
SpawnMob,
MobSpawner,
ok;
MainmenuAISave,
MainmenuAIGet,
MainmenuInvSave,
MainmenuInvGet,
MainmenuStatsSave,
MainmenuStatsGet,
MainmenuDisplaySave,
MainmenuDisplayGet,
ModelDataSave,
MainmenuAdvancedSave,
MainmenuAdvancedGet,
MainmenuAdvancedMarkData,
public CustomNpcsPermissions.Permission permission = null;
public boolean needsNpc = false;
DialogNpcSet,
DialogNpcRemove,
DialogNpcGet,
DialogCategorySave,
DialogCategoriesGet,
DialogsGetFromDialog,
DialogCategoryRemove,
DialogCategoryGet,
DialogSave,
DialogsGet,
DialogGet,
DialogRemove,
EnumRequestPacket() {}
EnumRequestPacket(noppes.npcs.CustomNpcsPermissions.Permission permission, boolean npc) {
this(permission);
}
EnumRequestPacket(boolean npc) {
needsNpc = npc;
}
EnumRequestPacket(CustomNpcsPermissions.Permission permission) {
this.permission = permission;
}
public boolean hasPermission() {
return permission != null;
}
TransportSave,
TransportCategoriesGet,
TransportCategorySave,
TransportCategoryRemove,
TransportRemove,
TransportGetLocation,
TransportsGet,
FactionSet,
FactionSave,
FactionsGet,
FactionGet,
FactionRemove,
TagSet,
TagSave,
TagsGet,
TagGet,
NpcTagsGet,
TagRemove,
QuestCategorySave,
QuestCategoriesGet,
QuestRemove,
QuestCategoryRemove,
QuestRewardSave,
QuestSave,
QuestsGetFromQuest,
QuestsGet,
QuestDialogGetTitle,
QuestOpenGui,
QuestGet,
RecipeSave,
RecipeRemove,
RecipesGet,
RecipeGet,
NaturalSpawnSave,
NaturalSpawnGet,
NaturalSpawnRemove,
NaturalSpawnGetAll,
MerchantUpdate,
PlayerRider,
SpawnRider,
MovingPathSave,
MovingPathGet,
AnimationsGet,
AnimationGet,
AnimationRemove,
AnimationSave,
RemoteNpcsGet,
RemoteTpToNpc,
SaveTileEntity,
GetTileEntity,
Gui,
IsGuiOpen,
CacheAnimation,
CustomGuiButton,
CustomGuiScrollClick,
CustomGuiClose,
CustomGuiUnfocused,
SavePartyData,
CreateParty,
GetPartyData,
DisbandParty,
KickPlayer,
LeavePlayer,
PartyInvite,
GetPartyInviteList,
AcceptInvite,
IgnoreInvite,
SetPartyLeader,
SetPartyQuest,
ServerUpdateSkinOverlays
}
@@ -23,7 +23,7 @@ import noppes.npcs.config.ConfigClient;
import java.io.IOException;
public final class AchievementPacket extends AbstractPacket {
public static final String packetName = "Client|Achievement";
public static final String packetName = "Data|Achievement";
private boolean isParty;
private String description;
@@ -16,7 +16,7 @@ import noppes.npcs.config.ConfigClient;
import java.io.IOException;
public final class ChatAlertPacket extends AbstractPacket {
public static final String packetName = "Client|ChatAlert";
public static final String packetName = "Data|ChatAlert";
private Object[] objects;
@@ -19,7 +19,7 @@ import noppes.npcs.entity.EntityNPCInterface;
import java.io.IOException;
public final class ChatBubblePacket extends AbstractPacket {
public static final String packetName = "Client|ChatBubble";
public static final String packetName = "Data|ChatBubble";
private int entityId;
private String text;
@@ -17,7 +17,7 @@ import noppes.npcs.client.gui.GuiNpcMobSpawnerAdd;
import java.io.IOException;
public final class ClonerPacket extends LargeAbstractPacket {
public static final String packetName = "Client|Clone";
public static final String packetName = "Data|Clone";
private NBTTagCompound compound;
@@ -18,7 +18,7 @@ import net.minecraft.util.ChatComponentTranslation;
import java.io.IOException;
public final class ConfigCommandPacket extends AbstractPacket {
public static final String packetName = "Client|ConfigCommand";
public static final String packetName = "Data|ConfigCommand";
private EnumConfigOperation configOperation;
private Object[] objects;
@@ -14,7 +14,7 @@ import noppes.npcs.client.ClientEventHandler;
import java.io.IOException;
public final class DisableMouseInputPacket extends AbstractPacket {
public static final String packetName = "Client|DisableMouseInput";
public static final String packetName = "Data|DisableMouseInput";
private long duration;
private String keys;
@@ -17,7 +17,7 @@ import noppes.npcs.entity.EntityNPCInterface;
import java.io.IOException;
public final class MarkDataPacket extends AbstractPacket {
public static final String packetName = "Client|MarkData";
public static final String packetName = "Data|MarkData";
private int entityID;
private NBTTagCompound compound;
@@ -17,7 +17,7 @@ import noppes.npcs.client.ClientCacheHandler;
import java.io.IOException;
public final class OverlayQuestTrackingPacket extends AbstractPacket {
public static final String packetName = "Client|OverlayQuestTracking";
public static final String packetName = "Data|OverlayQuestTracking";
private NBTTagCompound compound;
@@ -16,7 +16,7 @@ import java.io.IOException;
import java.util.Random;
public final class ParticlePacket extends AbstractPacket {
public static final String packetName = "Client|Particle";
public static final String packetName = "Data|Particle";
private double x;
private double y;
@@ -16,7 +16,7 @@ import noppes.npcs.client.NoppesUtil;
import java.io.IOException;
public final class PlayerUpdateSkinOverlaysPacket extends AbstractPacket {
public static final String packetName = "Client|PlayerUpdateSkinOverlays";
public static final String packetName = "Data|PlayerUpdateSkinOverlays";
private String playerName;
private NBTTagCompound compound;
@@ -16,7 +16,7 @@ import noppes.npcs.client.NoppesUtil;
import java.io.IOException;
public final class QuestCompletionPacket extends AbstractPacket {
public static final String packetName = "Client|QuestCompletion";
public static final String packetName = "Data|QuestCompletion";
private NBTTagCompound compound;
@@ -17,7 +17,7 @@ import noppes.npcs.client.gui.util.IScrollData;
import java.io.IOException;
public final class ScrollSelectedPacket extends AbstractPacket {
public static final String packetName = "Client|ScrollSelected";
public static final String packetName = "Data|ScrollSelected";
private String selected;
@@ -13,7 +13,7 @@ import net.minecraft.entity.player.EntityPlayer;
import java.io.IOException;
public final class SwingPlayerArmPacket extends AbstractPacket {
public static final String packetName = "Client|SwingPlayerArm";
public static final String packetName = "Data|SwingPlayerArm";
public SwingPlayerArmPacket() {}
@@ -20,7 +20,7 @@ import noppes.npcs.entity.EntityNPCInterface;
import java.io.IOException;
public final class UpdateAnimationsPacket extends AbstractPacket {
public static final String packetName = "Client|UpdateAnimations";
public static final String packetName = "Data|UpdateAnimations";
private NBTTagCompound animationCompound;
private String playername;
@@ -16,7 +16,7 @@ import net.minecraft.village.MerchantRecipeList;
import java.io.IOException;
public final class VillagerListPacket extends AbstractPacket {
public static final String packetName = "Client|VillagerList";
public static final String packetName = "Data|VillagerList";
private MerchantRecipeList merchantrecipelist;
@@ -18,7 +18,7 @@ import noppes.npcs.client.gui.util.IGuiClose;
import java.io.IOException;
public final class GuiClosePacket extends AbstractPacket {
public static final String packetName = "Client|GuiClose";
public static final String packetName = "Data|GuiClose";
private int code;
private NBTTagCompound compound;
@@ -18,7 +18,7 @@ import noppes.npcs.client.gui.util.IGuiError;
import java.io.IOException;
public final class GuiErrorPacket extends AbstractPacket {
public static final String packetName = "Client|GuiError";
public static final String packetName = "Data|GuiError";
private int code;
private NBTTagCompound compound;
@@ -17,7 +17,7 @@ import noppes.npcs.client.NoppesUtil;
import java.io.IOException;
public final class GuiOpenBookPacket extends AbstractPacket {
public static final String packetName = "Client|GuiOpenBook";
public static final String packetName = "Data|GuiOpenBook";
private int i;
private int j;
@@ -16,7 +16,7 @@ import noppes.npcs.constants.EnumGuiType;
import java.io.IOException;
public final class GuiOpenPacket extends AbstractPacket {
public static final String packetName = "Client|OpenGui";
public static final String packetName = "Data|OpenGui";
private EnumGuiType type;
private int x;
@@ -15,7 +15,7 @@ import noppes.npcs.client.NoppesUtil;
import java.io.IOException;
public final class GuiRedstonePacket extends AbstractPacket {
public static final String packetName = "Client|GuiRedstone";
public static final String packetName = "Data|GuiRedstone";
private NBTTagCompound compound;
@@ -15,7 +15,7 @@ import noppes.npcs.entity.EntityNPCInterface;
import java.io.IOException;
public final class GuiTeleporterPacket extends AbstractPacket {
public static final String packetName = "Client|Teleporter";
public static final String packetName = "Data|Teleporter";
public GuiTeleporterPacket(){}
@@ -15,7 +15,7 @@ import noppes.npcs.client.NoppesUtil;
import java.io.IOException;
public final class GuiWaypointPacket extends AbstractPacket {
public static final String packetName = "Client|GuiWaypoint";
public static final String packetName = "Data|GuiWaypoint";
private NBTTagCompound compound;
@@ -14,7 +14,7 @@ import noppes.npcs.client.NoppesUtil;
import java.io.IOException;
public final class IsGuiOpenPacket extends AbstractPacket {
public static final String packetName = "Client|IsGuiOpen";
public static final String packetName = "Data|IsGuiOpen";
public IsGuiOpenPacket() {}
@@ -21,7 +21,7 @@ import noppes.npcs.client.gui.util.IGuiData;
import java.io.IOException;
public final class GuiDataPacket extends LargeAbstractPacket {
public static final String packetName = "Client|GuiData";
public static final String packetName = "Data|GuiData";
private NBTTagCompound compound;
@@ -21,7 +21,7 @@ import noppes.npcs.client.gui.util.IPartyData;
import java.io.IOException;
public final class PartyDataPacket extends LargeAbstractPacket {
public static final String packetName = "Client|PartyData";
public static final String packetName = "Data|PartyData";
private NBTTagCompound compound;
@@ -15,7 +15,7 @@ import noppes.npcs.entity.EntityNPCInterface;
import java.io.IOException;
public final class DeleteNpcPacket extends AbstractPacket {
public static final String packetName = "Client|DeleteNpc";
public static final String packetName = "Data|DeleteNpc";
private int entityId;
@@ -19,7 +19,7 @@ import noppes.npcs.entity.EntityNPCInterface;
import java.io.IOException;
public final class DialogPacket extends AbstractPacket {
public static final String packetName = "Client|Dialog";
public static final String packetName = "Data|Dialog";
private String dummyName;
private int entityID;
@@ -16,7 +16,7 @@ import noppes.npcs.entity.EntityNPCInterface;
import java.io.IOException;
public final class EditNpcPacket extends AbstractPacket {
public static final String packetName = "Client|EditNpc";
public static final String packetName = "Data|EditNpc";
int entityId;
public EditNpcPacket() {}
@@ -18,7 +18,7 @@ import noppes.npcs.entity.EntityNPCInterface;
import java.io.IOException;
public final class RolePacket extends AbstractPacket {
public static final String packetName = "Client|Role";
public static final String packetName = "Data|Role";
private NBTTagCompound compound;
@@ -17,7 +17,7 @@ import noppes.npcs.entity.EntityNPCInterface;
import java.io.IOException;
public final class UpdateNpcPacket extends AbstractPacket {
public static final String packetName = "Client|UpdateNpc";
public static final String packetName = "Data|UpdateNpc";
private NBTTagCompound npcCompound;
@@ -18,7 +18,7 @@ import noppes.npcs.entity.EntityNPCInterface;
import java.io.IOException;
public final class WeaponNpcPacket extends AbstractPacket {
public static final String packetName = "Client|WeaponNpc";
public static final String packetName = "Data|WeaponNpc";
private int entityId;
private int weaponIndex;
@@ -13,7 +13,7 @@ import noppes.npcs.client.ClientCacheHandler;
import java.io.IOException;
public final class ScriptOverlayClosePacket extends AbstractPacket {
public static final String packetName = "Client|ScriptOverlayClose";
public static final String packetName = "Data|ScriptOverlayClose";
private int particleId;
@@ -17,7 +17,7 @@ import noppes.npcs.client.ClientCacheHandler;
import java.io.IOException;
public final class ScriptOverlayDataPacket extends AbstractPacket {
public static final String packetName = "Client|ScriptOverlayData";
public static final String packetName = "Data|ScriptOverlayData";
private NBTTagCompound overlayCompound;
@@ -20,7 +20,7 @@ import noppes.npcs.scripted.ScriptParticle;
import java.io.IOException;
public final class ScriptedParticlePacket extends AbstractPacket {
public static final String packetName = "Client|ScriptedParticle";
public static final String packetName = "Data|ScriptedParticle";
private NBTTagCompound compound;
@@ -2,9 +2,6 @@ package noppes.npcs.client;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.network.FMLNetworkEvent;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import noppes.npcs.PacketHandlerServer;
import java.io.IOException;