Fix Packets for Effects + Syncing

This commit is contained in:
Kam committed 2025-02-10 16:38:50 -05:00
1 parent 070e032907
commit d2c7e634ec
18 files changed
+502 -41

No files matched your search

@@ -11,10 +11,7 @@ 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.RecipeController;
import noppes.npcs.controllers.*;
import noppes.npcs.controllers.data.*;
import java.util.HashMap;
@@ -69,6 +66,13 @@ public class SyncController {
anvilNBT()
), player);
PacketHandler.Instance.sendToPlayer(new SyncPacket(
EnumSyncType.CUSTOM_EFFECTS,
EnumSyncAction.RELOAD,
-1,
customEffectsNBT()
), player);
DBCAddon.instance.syncPlayer(player);
syncPlayerData(player, false);
}
@@ -154,6 +158,16 @@ public class SyncController {
return compound;
}
public static NBTTagCompound customEffectsNBT(){
NBTTagList list = new NBTTagList();
NBTTagCompound compound = new NBTTagCompound();
for (CustomEffect effect : StatusEffectController.getInstance().customEffects.values()) {
list.appendTag(effect.writeToNBT(false));
}
compound.setTag("Data", list);
return compound;
}
public static void syncPlayerData(EntityPlayerMP player, boolean update){
PlayerData data = PlayerData.get(player);
if(data != null){
@@ -228,6 +242,15 @@ public class SyncController {
));
}
public static void syncAllCustomEffects() {
PacketHandler.Instance.sendToAll(new SyncPacket(
EnumSyncType.CUSTOM_EFFECTS,
EnumSyncAction.RELOAD,
-1,
customEffectsNBT()
));
}
@SideOnly(Side.CLIENT)
public static void clientSync(EnumSyncType enumSyncType, NBTTagCompound fullCompound) {
switch (enumSyncType){
@@ -367,6 +390,18 @@ public class SyncController {
RecipeController.syncAnvilRecipes = new HashMap<Integer, RecipeAnvil>();
break;
}
case CUSTOM_EFFECTS: {
NBTTagList list = fullCompound.getTagList("Data", 10);
for (int i = 0; i < list.tagCount(); i++) {
CustomEffect effect = new CustomEffect();
effect.readFromNBT(list.getCompoundTagAt(i));
StatusEffectController.getInstance().customEffectsSync.put(effect.id, effect);
}
StatusEffectController.getInstance().customEffects = StatusEffectController.getInstance().customEffectsSync;
StatusEffectController.getInstance().customEffectsSync = new HashMap<>();
break;
}
}
}
@@ -440,6 +475,14 @@ public class SyncController {
ClientCacheHandler.playerData.setSyncNBT(compound);
break;
}
case CUSTOM_EFFECTS: {
CustomEffect effect = new CustomEffect();
effect.readFromNBT(compound);
ClientCacheHandler.getImageData(effect.icon);
StatusEffectController.Instance.customEffects.put(effect.id, effect);
break;
}
}
}
@@ -509,6 +552,9 @@ public class SyncController {
break;
case MAGIC:
break;
case CUSTOM_EFFECTS:
StatusEffectController.Instance.customEffects.remove(id);
break;
}
}
}
@@ -121,6 +121,7 @@ public class PacketHandler {
REQUEST_PACKET.registerPacket(new NPCScriptPacket());
REQUEST_PACKET.registerPacket(new PlayerScriptPacket());
REQUEST_PACKET.registerPacket(new ScriptInfoPacket());
REQUEST_PACKET.registerPacket(new EffectScriptPacket());
// Cloner Packets
REQUEST_PACKET.registerPacket(new CloneListPacket());
@@ -168,6 +168,13 @@ public enum EnumRequestPacket {
BlockScript,
NPCScript,
ScriptInfo,
EffectScript,
DimensionTeleport
DimensionTeleport,
EffectList,
EffectGet,
EffectScriptGet,
EffectRemove,
EffectSave,
}
@@ -11,4 +11,5 @@ public enum EnumSyncType {
WORKBENCH_RECIPES,
CARPENTRY_RECIPES,
ANVIL_RECIPES,
CUSTOM_EFFECTS
}
@@ -0,0 +1,56 @@
package kamkeel.npcs.network.packets.request.effects;
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.EnumRequestPacket;
import kamkeel.npcs.network.packets.data.large.GuiDataPacket;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.controllers.StatusEffectController;
import noppes.npcs.controllers.data.CustomEffect;
import java.io.IOException;
public final class EffectGetPacket extends AbstractPacket {
public static final String packetName = "NPC|GetEffect";
private int effectID;
public EffectGetPacket(int EffectID) {
this.effectID = EffectID;
}
public EffectGetPacket() {
}
@Override
public Enum getType() {
return EnumRequestPacket.EffectGet;
}
@Override
public PacketChannel getChannel() {
return PacketHandler.REQUEST_PACKET;
}
@Override
public void sendData(ByteBuf out) throws IOException {
out.writeInt(this.effectID);
}
@Override
public void receiveData(ByteBuf in, EntityPlayer player) throws IOException {
effectID = in.readInt();
NBTTagCompound compound = new NBTTagCompound();
if (effectID != -1 && StatusEffectController.getInstance().has(effectID)) {
CustomEffect Effect = (CustomEffect) StatusEffectController.getInstance().get(effectID);
if (Effect != null) {
compound = Effect.writeToNBT(false);
compound.setString("Type", "ViewEffect");
}
}
GuiDataPacket.sendGuiData((EntityPlayerMP) player, compound);
}
}
@@ -0,0 +1,52 @@
package kamkeel.npcs.network.packets.request.effects;
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.EnumRequestPacket;
import kamkeel.npcs.network.packets.data.large.GuiDataPacket;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.NoppesUtilServer;
import noppes.npcs.controllers.StatusEffectController;
import noppes.npcs.controllers.data.CustomEffect;
import java.io.IOException;
public class EffectRemovePacket extends AbstractPacket {
public static final String packetName = "NPC|EffectRemove";
private int effectID;
public EffectRemovePacket(int outlineID) {
this.effectID = outlineID;
}
public EffectRemovePacket() {}
@Override
public Enum getType() {
return EnumRequestPacket.EffectRemove;
}
@Override
public PacketChannel getChannel() {
return PacketHandler.REQUEST_PACKET;
}
@Override
public void sendData(ByteBuf out) throws IOException {
out.writeInt(this.effectID);
}
@Override
public void receiveData(ByteBuf in, EntityPlayer player) throws IOException {
effectID = in.readInt();
StatusEffectController.getInstance().delete(effectID);
NoppesUtilServer.sendCustomEffectDataAll((EntityPlayerMP) player);
NBTTagCompound compound = (new CustomEffect()).writeToNBT(false);
GuiDataPacket.sendGuiData((EntityPlayerMP) player, compound);
}
}
@@ -0,0 +1,62 @@
package kamkeel.npcs.network.packets.request.effects;
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.EnumRequestPacket;
import kamkeel.npcs.util.ByteBufUtils;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.NoppesUtilServer;
import noppes.npcs.controllers.StatusEffectController;
import noppes.npcs.controllers.data.CustomEffect;
import java.io.IOException;
public class EffectSavePacket extends AbstractPacket {
public static final String packetName = "NPC|EffectSave";
private String prevName;
private NBTTagCompound effectCompound;
public EffectSavePacket(NBTTagCompound compound, String prev){
this.effectCompound = compound;
this.prevName = prev;
}
public EffectSavePacket() {}
@Override
public Enum getType() {
return EnumRequestPacket.EffectSave;
}
@Override
public PacketChannel getChannel() {
return PacketHandler.REQUEST_PACKET;
}
@Override
public void sendData(ByteBuf out) throws IOException {
ByteBufUtils.writeString(out, prevName);
ByteBufUtils.writeNBT(out, effectCompound);
}
@Override
public void receiveData(ByteBuf in, EntityPlayer player) throws IOException {
String prevName = ByteBufUtils.readString(in);
CustomEffect effect = new CustomEffect();
effect.readFromNBT(ByteBufUtils.readNBT(in));
StatusEffectController.getInstance().saveEffect(effect);
if(!prevName.isEmpty() && !prevName.equals(effect.name)){
StatusEffectController.getInstance().deleteEffectFile(prevName);
}
NoppesUtilServer.sendCustomEffectDataAll((EntityPlayerMP) player);
}
}
@@ -0,0 +1,58 @@
package kamkeel.npcs.network.packets.request.effects;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import kamkeel.npcs.network.LargeAbstractPacket;
import kamkeel.npcs.network.PacketChannel;
import kamkeel.npcs.network.PacketHandler;
import kamkeel.npcs.network.enums.EnumRequestPacket;
import kamkeel.npcs.util.ByteBufUtils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.client.gui.script.GuiScriptEffect;
import java.io.IOException;
public class EffectScriptGetPacket extends LargeAbstractPacket {
public static final String packetName = "NPC|EffectScriptGetPacket";
private NBTTagCompound compound;
public EffectScriptGetPacket() {}
public EffectScriptGetPacket(NBTTagCompound compound) {
this.compound = compound;
}
@Override
public Enum getType() {
return EnumRequestPacket.EffectScriptGet;
}
@Override
public PacketChannel getChannel() {
return PacketHandler.REQUEST_PACKET;
}
@Override
protected byte[] getData() throws IOException {
ByteBuf byteBuf = Unpooled.buffer();
ByteBufUtils.writeNBT(byteBuf, compound);
return byteBuf.array();
}
@SideOnly(Side.CLIENT)
@Override
protected void handleCompleteData(ByteBuf data, EntityPlayer player) throws IOException {
NBTTagCompound scriptData = ByteBufUtils.readNBT(data);
GuiScreen screen = Minecraft.getMinecraft().currentScreen;
if (screen instanceof GuiScriptEffect) {
GuiScriptEffect scriptGui = (GuiScriptEffect) screen;
scriptGui.setGuiData(scriptData);
}
}
}
@@ -0,0 +1,58 @@
package kamkeel.npcs.network.packets.request.effects;
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.EnumRequestPacket;
import kamkeel.npcs.network.packets.data.large.GuiDataPacket;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.NoppesUtilServer;
import noppes.npcs.controllers.StatusEffectController;
import noppes.npcs.controllers.data.CustomEffect;
import java.io.IOException;
public final class EffectsGetPacket extends AbstractPacket {
public static final String packetName = "NPC|EffectsGetPacket";
private int effectID;
public EffectsGetPacket(int outlineID) {
this.effectID = outlineID;
}
public EffectsGetPacket() {}
@Override
public Enum getType() {
return EnumRequestPacket.EffectList;
}
@Override
public PacketChannel getChannel() {
return PacketHandler.REQUEST_PACKET;
}
@Override
public void sendData(ByteBuf out) throws IOException {
out.writeInt(this.effectID);
}
@Override
public void receiveData(ByteBuf in, EntityPlayer player) throws IOException {
this.effectID = in.readInt();
if (effectID != -1) {
CustomEffect effect = (CustomEffect) StatusEffectController.getInstance().get(effectID);
if (effect != null) {
NBTTagCompound compound = effect.writeToNBT(false);
compound.setString("PACKETTYPE", "Effect");
GuiDataPacket.sendGuiData((EntityPlayerMP) player, compound);
}
} else {
NoppesUtilServer.sendCustomEffectDataAll((EntityPlayerMP) player);
}
}
}
@@ -0,0 +1,109 @@
package kamkeel.npcs.network.packets.request.script;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import io.netty.buffer.ByteBuf;
import kamkeel.npcs.network.*;
import kamkeel.npcs.network.enums.EnumItemPacketType;
import kamkeel.npcs.network.enums.EnumRequestPacket;
import kamkeel.npcs.util.ByteBufUtils;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.CustomNpcsPermissions;
import noppes.npcs.LogWriter;
import noppes.npcs.config.ConfigDebug;
import noppes.npcs.config.ConfigScript;
import noppes.npcs.controllers.ScriptController;
import noppes.npcs.controllers.StatusEffectController;
import noppes.npcs.controllers.data.CustomEffect;
import noppes.npcs.controllers.data.EffectScriptHandler;
import noppes.npcs.controllers.data.IScriptHandler;
import noppes.npcs.controllers.data.PlayerDataScript;
import java.io.IOException;
public final class EffectScriptPacket extends AbstractPacket {
public static String packetName = "Request|EffectScript";
private EffectScriptPacket.Action type;
private int id;
private int page;
private int maxSize;
private NBTTagCompound compound;
public EffectScriptPacket() {}
public EffectScriptPacket(Action type, int id, int page, int maxSize, NBTTagCompound compound) {
this.type = type;
this.id = id;
this.page = page;
this.maxSize = maxSize;
this.compound = compound;
}
@Override
public Enum getType() {
return EnumRequestPacket.EffectScript;
}
@Override
public PacketChannel getChannel() {
return PacketHandler.REQUEST_PACKET;
}
@Override
public CustomNpcsPermissions.Permission getPermission(){
return CustomNpcsPermissions.SCRIPT_PLAYER;
}
@SideOnly(Side.CLIENT)
@Override
public void sendData(ByteBuf out) throws IOException {
out.writeInt(type.ordinal());
out.writeInt(type.ordinal());
if(type == Action.SAVE){
out.writeInt(this.page);
out.writeInt(this.maxSize);
ByteBufUtils.writeNBT(out, this.compound);
}
}
@Override
public void receiveData(ByteBuf in, EntityPlayer player) throws IOException {
if (!(player instanceof EntityPlayerMP))
return;
if (!ConfigScript.canScript(player, CustomNpcsPermissions.SCRIPT))
return;
Action requestedAction = Action.values()[in.readInt()];
CustomEffect effect = StatusEffectController.getInstance().get(in.readInt());
if(effect == null)
return;
EffectScriptHandler data = effect.getOrCreateScriptHandler();
if(requestedAction == Action.GET){
PacketUtil.getScripts((IScriptHandler) data, (EntityPlayerMP) player);
} else {
data.saveScript(in);
if(ConfigDebug.PlayerLogging && FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER){
LogWriter.script(String.format("[%s] (Player) %s SAVED EFFECT %s [%s]", "EFFECT SCRIPTS", player.getCommandSenderName(), effect.getName()));
}
}
}
public static void Save(int effectID, int id, int maxSize, NBTTagCompound compound) {
PacketClient.sendClient(new EffectScriptPacket(Action.SAVE, effectID, id, maxSize, compound));
}
public static void Get(int effectID) {
PacketClient.sendClient(new EffectScriptPacket(Action.GET, effectID, -1, -1, new NBTTagCompound()));
}
private enum Action {
GET,
SAVE
}
}