diff --git a/src/main/java/kamkeel/npcs/network/PacketClient.java b/src/main/java/kamkeel/npcs/network/PacketClient.java new file mode 100644 index 000000000..45aa91e2b --- /dev/null +++ b/src/main/java/kamkeel/npcs/network/PacketClient.java @@ -0,0 +1,8 @@ +package kamkeel.npcs.network; + +public class PacketClient extends PacketHandler { + + public static void sendClient(AbstractPacket packet){ + PacketHandler.Instance.sendToServer(packet); + } +} diff --git a/src/main/java/kamkeel/npcs/network/PacketHandler.java b/src/main/java/kamkeel/npcs/network/PacketHandler.java index 7c62a1747..db8c9e776 100644 --- a/src/main/java/kamkeel/npcs/network/PacketHandler.java +++ b/src/main/java/kamkeel/npcs/network/PacketHandler.java @@ -14,6 +14,10 @@ import kamkeel.npcs.network.packets.data.gui.*; import kamkeel.npcs.network.packets.data.large.*; import kamkeel.npcs.network.packets.data.npc.*; import kamkeel.npcs.network.packets.data.script.*; +import kamkeel.npcs.network.packets.request.linked.LinkedAddPacket; +import kamkeel.npcs.network.packets.request.linked.LinkedGetAllPacket; +import kamkeel.npcs.network.packets.request.linked.LinkedRemovePacket; +import kamkeel.npcs.network.packets.request.linked.LinkedSetPacket; import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; @@ -22,11 +26,12 @@ import noppes.npcs.CustomNpcs; import noppes.npcs.CustomNpcsPermissions; import noppes.npcs.LogWriter; import noppes.npcs.NoppesUtilServer; +import noppes.npcs.config.ConfigMain; import noppes.npcs.entity.EntityNPCInterface; import java.util.*; -public final class PacketHandler { +public class PacketHandler { public static PacketHandler Instance; // Channels @@ -53,6 +58,14 @@ public final class PacketHandler { this.registerDataPackets(); } + public void registerRequestPackets(){ + // Linked Packets + REQUEST_PACKET.registerPacket(new LinkedGetAllPacket()); + REQUEST_PACKET.registerPacket(new LinkedRemovePacket()); + REQUEST_PACKET.registerPacket(new LinkedSetPacket()); + REQUEST_PACKET.registerPacket(new LinkedAddPacket()); + } + public void registerDataPackets(){ // Client Packets DATA_PACKET.registerPacket(new AchievementPacket()); @@ -159,6 +172,11 @@ public final class PacketHandler { } if(side == Side.SERVER){ + if(abstractPacket.getChannel() == REQUEST_PACKET && ConfigMain.OpsOnly && !NoppesUtilServer.isOp(player)){ + LogWriter.error(String.format("%s tried to use CNPC+ without being an op", player.getCommandSenderName())); + return; + } + // Check if permission is allowed if(abstractPacket.getPermission() != null && !CustomNpcsPermissions.hasPermission(player, abstractPacket.getPermission())){ return; diff --git a/src/main/java/kamkeel/npcs/network/packets/request/linked/LinkedGetAllPacket.java b/src/main/java/kamkeel/npcs/network/packets/request/linked/LinkedGetAllPacket.java index 279f572cf..9690f15de 100644 --- a/src/main/java/kamkeel/npcs/network/packets/request/linked/LinkedGetAllPacket.java +++ b/src/main/java/kamkeel/npcs/network/packets/request/linked/LinkedGetAllPacket.java @@ -22,8 +22,7 @@ import java.util.List; public final class LinkedGetAllPacket extends AbstractPacket { public static String packetName = "Request|LinkedGetAll"; - public LinkedGetAllPacket() { - } + public LinkedGetAllPacket() {} @Override public Enum getType() { diff --git a/src/main/java/kamkeel/npcs/network/packets/request/linked/LinkedRemovePacket.java b/src/main/java/kamkeel/npcs/network/packets/request/linked/LinkedRemovePacket.java index b60bd3249..a30366cd8 100644 --- a/src/main/java/kamkeel/npcs/network/packets/request/linked/LinkedRemovePacket.java +++ b/src/main/java/kamkeel/npcs/network/packets/request/linked/LinkedRemovePacket.java @@ -29,8 +29,7 @@ public final class LinkedRemovePacket extends AbstractPacket { this.data = data; } - public LinkedRemovePacket() { - } + public LinkedRemovePacket() {} @Override public Enum getType() { diff --git a/src/main/java/kamkeel/npcs/network/packets/request/linked/LinkedSetPacket.java b/src/main/java/kamkeel/npcs/network/packets/request/linked/LinkedSetPacket.java index e2c664f56..6051e5a66 100644 --- a/src/main/java/kamkeel/npcs/network/packets/request/linked/LinkedSetPacket.java +++ b/src/main/java/kamkeel/npcs/network/packets/request/linked/LinkedSetPacket.java @@ -26,8 +26,7 @@ public final class LinkedSetPacket extends AbstractPacket { this.linkedName = linkedName; } - public LinkedSetPacket() { - } + public LinkedSetPacket() {} @Override public Enum getType() { diff --git a/src/main/java/noppes/npcs/PacketHandlerServer.java b/src/main/java/noppes/npcs/PacketHandlerServer.java index 883e08735..ae6c70d31 100644 --- a/src/main/java/noppes/npcs/PacketHandlerServer.java +++ b/src/main/java/noppes/npcs/PacketHandlerServer.java @@ -68,13 +68,7 @@ public class PacketHandlerServer{ ItemStack item = player.inventory.getCurrentItem(); EntityNPCInterface npc = NoppesUtilServer.getEditingNpc(player); - if(type.needsNpc && npc == null){ - - } - else if(type.hasPermission() && !CustomNpcsPermissions.hasPermission(player, type.permission)){ - //player doesnt have permission to do this - } - else if(item == null && (type == EnumPacketServer.ScriptPlayerGet || type == EnumPacketServer.ScriptPlayerSave || type == EnumPacketServer.ScriptGlobalNPCGet || type == EnumPacketServer.ScriptGlobalNPCSave || type == EnumPacketServer.ScriptForgeGet || type == EnumPacketServer.ScriptForgeSave)) + if(item == null && (type == EnumPacketServer.ScriptPlayerGet || type == EnumPacketServer.ScriptPlayerSave || type == EnumPacketServer.ScriptGlobalNPCGet || type == EnumPacketServer.ScriptGlobalNPCSave || type == EnumPacketServer.ScriptForgeGet || type == EnumPacketServer.ScriptForgeSave)) warn(player, "tried to use custom npcs without a tool in hand, probably a hacker"); else { if (item != null) { @@ -455,27 +449,7 @@ public class PacketHandlerServer{ } private void wandPackets(EnumPacketServer type, ByteBuf buffer, EntityPlayerMP player, EntityNPCInterface npc) throws IOException{ - if(type == EnumPacketServer.LinkedRemove){ - LinkedNpcController.Instance.removeData(ByteBufUtils.readString(buffer)); - - List list = new ArrayList(); - for(LinkedData data : LinkedNpcController.Instance.list) - list.add(data.name); - ScrollListPacket.sendList((EntityPlayerMP)player, list); - } - else if(type == EnumPacketServer.LinkedGetAll){ - List list = new ArrayList(); - for(LinkedData data : LinkedNpcController.Instance.list) - list.add(data.name); - ScrollListPacket.sendList((EntityPlayerMP)player, list); - if(npc != null) - ScrollSelectedPacket.setSelectedList(player, npc.linkedName); - } - else if(type == EnumPacketServer.LinkedSet){ - npc.linkedName = ByteBufUtils.readString(buffer); - LinkedNpcController.Instance.loadNpcData(npc); - } - else if(type == EnumPacketServer.BanksGet){ + if(type == EnumPacketServer.BanksGet){ NoppesUtilServer.sendBankDataAll(player); } else if(type == EnumPacketServer.BankGet){ diff --git a/src/main/java/noppes/npcs/client/gui/advanced/GuiNPCAdvancedLinkedNpc.java b/src/main/java/noppes/npcs/client/gui/advanced/GuiNPCAdvancedLinkedNpc.java index e5faca9e9..f739440f1 100644 --- a/src/main/java/noppes/npcs/client/gui/advanced/GuiNPCAdvancedLinkedNpc.java +++ b/src/main/java/noppes/npcs/client/gui/advanced/GuiNPCAdvancedLinkedNpc.java @@ -1,5 +1,8 @@ package noppes.npcs.client.gui.advanced; +import kamkeel.npcs.network.PacketClient; +import kamkeel.npcs.network.packets.request.linked.LinkedGetAllPacket; +import kamkeel.npcs.network.packets.request.linked.LinkedSetPacket; import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.GuiScreen; import noppes.npcs.client.Client; @@ -15,21 +18,21 @@ import java.util.Vector; public class GuiNPCAdvancedLinkedNpc extends GuiNPCInterface2 implements IScrollData, ICustomScrollListener{ private GuiCustomScroll scroll; private List data = new ArrayList(); - + public static GuiScreen Instance; - + public GuiNPCAdvancedLinkedNpc(EntityNPCInterface npc){ super(npc); Instance = this; - Client.sendData(EnumPacketServer.LinkedGetAll); + PacketClient.sendClient(new LinkedGetAllPacket()); } @Override public void initGui(){ super.initGui(); - + this.addButton(new GuiNpcButton(1,guiLeft + 358, guiTop + 38, 58, 20, "gui.clear")); - + if(scroll == null){ scroll = new GuiCustomScroll(this,0); scroll.setSize(143, 208); @@ -44,9 +47,9 @@ public class GuiNPCAdvancedLinkedNpc extends GuiNPCInterface2 implements IScroll @Override public void buttonEvent(GuiButton button){ if(button.id == 1){ - Client.sendData(EnumPacketServer.LinkedSet, ""); + PacketClient.sendClient(new LinkedSetPacket("")); } - + } @Override @@ -62,13 +65,13 @@ public class GuiNPCAdvancedLinkedNpc extends GuiNPCInterface2 implements IScroll @Override public void save() { - + } @Override public void customScrollClicked(int i, int j, int k, GuiCustomScroll guiCustomScroll) { - Client.sendData(EnumPacketServer.LinkedSet, guiCustomScroll.getSelected()); + PacketClient.sendClient(new LinkedSetPacket(guiCustomScroll.getSelected())); } } diff --git a/src/main/java/noppes/npcs/client/gui/global/GuiNPCManageLinkedNpc.java b/src/main/java/noppes/npcs/client/gui/global/GuiNPCManageLinkedNpc.java index 165207e6e..90e5a4495 100644 --- a/src/main/java/noppes/npcs/client/gui/global/GuiNPCManageLinkedNpc.java +++ b/src/main/java/noppes/npcs/client/gui/global/GuiNPCManageLinkedNpc.java @@ -1,5 +1,9 @@ package noppes.npcs.client.gui.global; +import kamkeel.npcs.network.PacketClient; +import kamkeel.npcs.network.packets.request.linked.LinkedAddPacket; +import kamkeel.npcs.network.packets.request.linked.LinkedGetAllPacket; +import kamkeel.npcs.network.packets.request.linked.LinkedRemovePacket; import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.GuiScreen; import noppes.npcs.client.Client; @@ -23,7 +27,7 @@ public class GuiNPCManageLinkedNpc extends GuiNPCInterface2 implements IScrollDa public GuiNPCManageLinkedNpc(EntityNPCInterface npc){ super(npc); Instance = this; - Client.sendData(EnumPacketServer.LinkedGetAll); + PacketClient.sendClient(new LinkedGetAllPacket()); } @Override @@ -79,7 +83,7 @@ public class GuiNPCManageLinkedNpc extends GuiNPCInterface2 implements IScrollDa } if(button.id == 2){ if(scroll.hasSelected()) - Client.sendData(EnumPacketServer.LinkedRemove, scroll.getSelected()); + PacketClient.sendClient(new LinkedRemovePacket(scroll.getSelected())); } } @@ -87,7 +91,7 @@ public class GuiNPCManageLinkedNpc extends GuiNPCInterface2 implements IScrollDa @Override public void subGuiClosed(SubGuiInterface subgui) { if(!((SubGuiEditText)subgui).cancelled){ - Client.sendData(EnumPacketServer.LinkedAdd, ((SubGuiEditText)subgui).text); + PacketClient.sendClient(new LinkedAddPacket(((SubGuiEditText)subgui).text)); } } diff --git a/src/main/java/noppes/npcs/constants/EnumPacketServer.java b/src/main/java/noppes/npcs/constants/EnumPacketServer.java index 1b0b44a7f..1f26dcfa1 100644 --- a/src/main/java/noppes/npcs/constants/EnumPacketServer.java +++ b/src/main/java/noppes/npcs/constants/EnumPacketServer.java @@ -1,6 +1,8 @@ package noppes.npcs.constants; +import net.minecraft.client.gui.Gui; import noppes.npcs.CustomNpcsPermissions; +import noppes.npcs.client.gui.custom.components.CustomGuiButton; public enum EnumPacketServer { NPCDelete(CustomNpcsPermissions.NPC_DELETE, true), @@ -33,7 +35,6 @@ public enum EnumPacketServer { RoleSave(CustomNpcsPermissions.NPC_ADVANCED, true), RoleGet(true), JobSpawnerAdd(CustomNpcsPermissions.NPC_ADVANCED, true), JobSpawnerRemove(CustomNpcsPermissions.NPC_ADVANCED, true), RoleCompanionUpdate(CustomNpcsPermissions.NPC_ADVANCED, true), - LinkedSet(CustomNpcsPermissions.NPC_ADVANCED, true), ClonePreSave(CustomNpcsPermissions.NPC_CLONE), CloneSave(CustomNpcsPermissions.NPC_CLONE), CloneRemove(CustomNpcsPermissions.NPC_CLONE), CloneList, CloneTagList, CloneAllTags, CloneAllTagsShort, @@ -55,9 +56,6 @@ public enum EnumPacketServer { ScriptBlockDataSave(CustomNpcsPermissions.SCRIPT_BLOCK, false), ScriptBlockDataGet(false), - LinkedGetAll, - LinkedRemove(CustomNpcsPermissions.GLOBAL_LINKED), LinkedAdd(CustomNpcsPermissions.GLOBAL_LINKED), - ScriptDataSave(CustomNpcsPermissions.SCRIPT_NPC, true), ScriptDataGet(true), EventScriptDataSave(CustomNpcsPermissions.SCRIPT_NPC, true), EventScriptDataGet(true),