2025-01-29 21:50:55 -05:00
|
|
|
|
package kamkeel.npcs.network;
|
|
|
|
|
|
|
2025-02-02 22:47:40 -05:00
|
|
|
|
import io.netty.buffer.ByteBuf;
|
2025-02-01 21:23:49 -05:00
|
|
|
|
import kamkeel.npcs.network.enums.EnumItemPacketType;
|
2025-02-02 22:47:40 -05:00
|
|
|
|
import kamkeel.npcs.network.packets.data.large.GuiDataPacket;
|
|
|
|
|
|
import kamkeel.npcs.util.ByteBufUtils;
|
2025-02-01 21:23:49 -05:00
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2025-02-02 22:47:40 -05:00
|
|
|
|
import net.minecraft.entity.player.EntityPlayerMP;
|
2025-02-01 21:23:49 -05:00
|
|
|
|
import net.minecraft.item.Item;
|
|
|
|
|
|
import net.minecraft.item.ItemStack;
|
2025-02-02 22:47:40 -05:00
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2025-02-01 21:23:49 -05:00
|
|
|
|
import noppes.npcs.CustomItems;
|
|
|
|
|
|
import noppes.npcs.LogWriter;
|
2025-02-02 22:47:40 -05:00
|
|
|
|
import noppes.npcs.NBTTags;
|
|
|
|
|
|
import noppes.npcs.controllers.ScriptContainer;
|
|
|
|
|
|
import noppes.npcs.controllers.ScriptController;
|
|
|
|
|
|
import noppes.npcs.controllers.data.IScriptHandler;
|
2025-03-31 21:19:16 -04:00
|
|
|
|
import noppes.npcs.items.ItemNpcTool;
|
2025-02-01 21:23:49 -05:00
|
|
|
|
|
2025-02-02 22:47:40 -05:00
|
|
|
|
import java.io.IOException;
|
2025-02-02 02:25:21 -05:00
|
|
|
|
import java.util.LinkedHashSet;
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
2025-01-29 21:50:55 -05:00
|
|
|
|
public class PacketUtil {
|
|
|
|
|
|
|
2025-03-23 01:56:25 -04:00
|
|
|
|
public static boolean verifyItemPacket(String name, EnumItemPacketType type, EntityPlayer player) {
|
2025-02-02 02:25:21 -05:00
|
|
|
|
if (player == null)
|
2025-02-01 21:23:49 -05:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
ItemStack item = player.inventory.getCurrentItem();
|
2025-02-02 02:25:21 -05:00
|
|
|
|
if (item == null) {
|
2025-03-23 01:56:25 -04:00
|
|
|
|
LogWriter.error(String.format("%s attempted to utilize a %s %s Packet without an item, they could be a hacker",
|
|
|
|
|
|
player.getCommandSenderName(), type, name));
|
2025-02-01 21:23:49 -05:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-02 02:25:21 -05:00
|
|
|
|
switch (type) {
|
2025-02-01 21:23:49 -05:00
|
|
|
|
case WAND:
|
2025-02-02 02:25:21 -05:00
|
|
|
|
if (item.getItem() != CustomItems.wand) {
|
|
|
|
|
|
LogWriter.error(String.format("%s attempted to utilize a %s Packet without a Wand, they could be a hacker",
|
2025-03-23 01:56:25 -04:00
|
|
|
|
player.getCommandSenderName(), type, name));
|
2025-02-01 21:23:49 -05:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case MOUNTER:
|
2025-02-02 02:25:21 -05:00
|
|
|
|
if (item.getItem() != CustomItems.mount) {
|
|
|
|
|
|
LogWriter.error(String.format("%s attempted to utilize a %s Packet without a Mounter, they could be a hacker",
|
2025-03-23 01:56:25 -04:00
|
|
|
|
player.getCommandSenderName(), type, name));
|
2025-02-01 21:23:49 -05:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case CLONER:
|
2025-02-02 02:25:21 -05:00
|
|
|
|
if (item.getItem() != CustomItems.cloner) {
|
2025-03-23 01:56:25 -04:00
|
|
|
|
LogWriter.error(String.format("%s attempted to utilize a %s %s Packet without a Cloner, they could be a hacker",
|
|
|
|
|
|
player.getCommandSenderName(), type, name));
|
2025-02-01 21:23:49 -05:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case TELEPORTER:
|
2025-02-02 02:25:21 -05:00
|
|
|
|
if (item.getItem() != CustomItems.teleporter) {
|
2025-03-23 01:56:25 -04:00
|
|
|
|
LogWriter.error(String.format("%s attempted to utilize a %s %s Packet without a Teleporter, they could be a hacker",
|
|
|
|
|
|
player.getCommandSenderName(), type, name));
|
2025-02-01 21:23:49 -05:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case SCRIPTER:
|
2025-02-02 02:25:21 -05:00
|
|
|
|
if (item.getItem() != CustomItems.scripter) {
|
2025-03-23 01:56:25 -04:00
|
|
|
|
LogWriter.error(String.format("%s attempted to utilize a %s %s Packet without a Scripter, they could be a hacker",
|
|
|
|
|
|
player.getCommandSenderName(), type, name));
|
2025-02-01 21:23:49 -05:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case PATHER:
|
2025-02-02 02:25:21 -05:00
|
|
|
|
if (item.getItem() != CustomItems.moving) {
|
2025-03-23 01:56:25 -04:00
|
|
|
|
LogWriter.error(String.format("%s attempted to utilize a %s %s Packet without a Pather, they could be a hacker",
|
|
|
|
|
|
player.getCommandSenderName(), type, name));
|
2025-02-01 21:23:49 -05:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case BLOCK:
|
2025-02-02 02:25:21 -05:00
|
|
|
|
if (item.getItem() == Item.getItemFromBlock(CustomItems.waypoint)
|
|
|
|
|
|
|| item.getItem() == Item.getItemFromBlock(CustomItems.border)
|
|
|
|
|
|
|| item.getItem() == Item.getItemFromBlock(CustomItems.redstoneBlock)) {
|
2025-03-23 01:56:25 -04:00
|
|
|
|
LogWriter.error(String.format("%s attempted to utilize a %s %s Packet without a valid Block, they could be a hacker",
|
|
|
|
|
|
player.getCommandSenderName(), type, name));
|
2025-02-01 21:23:49 -05:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
2025-02-13 02:09:50 -05:00
|
|
|
|
case BRUSH:
|
|
|
|
|
|
if (item.getItem() != CustomItems.tool || item.getItemDamage() != 1) {
|
2025-03-23 01:56:25 -04:00
|
|
|
|
LogWriter.error(String.format("%s attempted to utilize a %s %s Packet without a Pather, they could be a hacker",
|
|
|
|
|
|
player.getCommandSenderName(), type, name));
|
2025-02-13 02:09:50 -05:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
2025-03-09 20:02:24 -04:00
|
|
|
|
case HAMMER:
|
|
|
|
|
|
if (item.getItem() != CustomItems.tool || item.getItemDamage() != 0) {
|
2025-03-23 01:56:25 -04:00
|
|
|
|
LogWriter.error(String.format("%s attempted to utilize a %s %s Packet without a Hammer, they could be a hacker",
|
|
|
|
|
|
player.getCommandSenderName(), type, name));
|
2025-03-14 20:32:26 -04:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case MAGIC_BOOK:
|
|
|
|
|
|
if (item.getItem() != CustomItems.tool || item.getItemDamage() != 2) {
|
2025-03-23 01:56:25 -04:00
|
|
|
|
LogWriter.error(String.format("%s attempted to utilize a %s %s Packet without a Magic Book, they could be a hacker",
|
|
|
|
|
|
player.getCommandSenderName(), type, name));
|
2025-03-09 20:02:24 -04:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
2025-02-01 21:23:49 -05:00
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2025-02-02 02:25:21 -05:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* New method that accepts multiple EnumItemPacketTypes.
|
|
|
|
|
|
* It returns true if any one of the provided types is valid.
|
|
|
|
|
|
* If none are valid, a merged error message is logged.
|
|
|
|
|
|
*/
|
2025-03-23 01:56:25 -04:00
|
|
|
|
public static boolean verifyItemPacket(String name, EntityPlayer player, EnumItemPacketType... types) {
|
2025-02-02 02:25:21 -05:00
|
|
|
|
if (player == null)
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
ItemStack item = player.inventory.getCurrentItem();
|
|
|
|
|
|
if (item == null) {
|
2025-03-23 01:56:25 -04:00
|
|
|
|
LogWriter.error(String.format("%s attempted to utilize a %s without an item. Expected one of: %s, they could be a hacker",
|
|
|
|
|
|
player.getCommandSenderName(), name, getExpectedItemNames(types)));
|
2025-02-02 02:25:21 -05:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (EnumItemPacketType type : types) {
|
|
|
|
|
|
if (isValidItemForType(item, type)) {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// None passed; log one merged error message listing all expected items.
|
2025-03-23 01:56:25 -04:00
|
|
|
|
LogWriter.error(String.format("%s attempted to utilize a %s without a valid item. Expected one of: %s, they could be a hacker",
|
|
|
|
|
|
player.getCommandSenderName(), name, getExpectedItemNames(types)));
|
2025-02-02 02:25:21 -05:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static boolean isValidItemForType(ItemStack item, EnumItemPacketType type) {
|
2025-03-13 17:14:45 -04:00
|
|
|
|
if (item == null || item.getItem() == null)
|
2025-02-09 01:09:23 -05:00
|
|
|
|
return false;
|
|
|
|
|
|
|
2025-02-02 02:25:21 -05:00
|
|
|
|
switch (type) {
|
|
|
|
|
|
case WAND:
|
|
|
|
|
|
return item.getItem() == CustomItems.wand;
|
|
|
|
|
|
case MOUNTER:
|
|
|
|
|
|
return item.getItem() == CustomItems.mount;
|
|
|
|
|
|
case CLONER:
|
|
|
|
|
|
return item.getItem() == CustomItems.cloner;
|
|
|
|
|
|
case TELEPORTER:
|
|
|
|
|
|
return item.getItem() == CustomItems.teleporter;
|
|
|
|
|
|
case SCRIPTER:
|
|
|
|
|
|
return item.getItem() == CustomItems.scripter;
|
|
|
|
|
|
case PATHER:
|
|
|
|
|
|
return item.getItem() == CustomItems.moving;
|
|
|
|
|
|
case BLOCK:
|
|
|
|
|
|
// For BLOCK, the item is valid as long as it is NOT one of these blocks.
|
|
|
|
|
|
return !(item.getItem() == Item.getItemFromBlock(CustomItems.waypoint)
|
|
|
|
|
|
|| item.getItem() == Item.getItemFromBlock(CustomItems.border)
|
|
|
|
|
|
|| item.getItem() == Item.getItemFromBlock(CustomItems.redstoneBlock));
|
2025-02-13 02:09:50 -05:00
|
|
|
|
case BRUSH:
|
2025-03-31 21:19:16 -04:00
|
|
|
|
return item.getItem() instanceof ItemNpcTool && item.getItemDamage() == 1;
|
2025-03-09 20:02:24 -04:00
|
|
|
|
case HAMMER:
|
2025-03-31 21:19:16 -04:00
|
|
|
|
return item.getItem() instanceof ItemNpcTool && item.getItemDamage() == 0;
|
2025-03-14 20:32:26 -04:00
|
|
|
|
case MAGIC_BOOK:
|
2025-03-31 21:19:16 -04:00
|
|
|
|
return item.getItem() instanceof ItemNpcTool && item.getItemDamage() == 2;
|
2025-02-02 02:25:21 -05:00
|
|
|
|
default:
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Builds a comma‐separated list of expected item names for the given types.
|
|
|
|
|
|
*/
|
|
|
|
|
|
private static String getExpectedItemNames(EnumItemPacketType... types) {
|
|
|
|
|
|
Set<String> expectedNames = new LinkedHashSet<>();
|
|
|
|
|
|
for (EnumItemPacketType type : types) {
|
|
|
|
|
|
expectedNames.add(getExpectedItemName(type));
|
|
|
|
|
|
}
|
|
|
|
|
|
return String.join(", ", expectedNames);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Returns the expected item name for the given EnumItemPacketType.
|
|
|
|
|
|
*/
|
|
|
|
|
|
private static String getExpectedItemName(EnumItemPacketType type) {
|
|
|
|
|
|
switch (type) {
|
|
|
|
|
|
case WAND:
|
|
|
|
|
|
return "Wand";
|
|
|
|
|
|
case MOUNTER:
|
|
|
|
|
|
return "Mounter";
|
|
|
|
|
|
case CLONER:
|
|
|
|
|
|
return "Cloner";
|
|
|
|
|
|
case TELEPORTER:
|
|
|
|
|
|
return "Teleporter";
|
|
|
|
|
|
case SCRIPTER:
|
|
|
|
|
|
return "Scripter";
|
|
|
|
|
|
case PATHER:
|
|
|
|
|
|
return "Pather";
|
|
|
|
|
|
case BLOCK:
|
|
|
|
|
|
return "valid Block";
|
2025-02-13 02:09:50 -05:00
|
|
|
|
case BRUSH:
|
|
|
|
|
|
return "Paintbrush";
|
2025-03-09 20:02:24 -04:00
|
|
|
|
case HAMMER:
|
|
|
|
|
|
return "Hammer";
|
2025-03-14 20:32:26 -04:00
|
|
|
|
case MAGIC_BOOK:
|
|
|
|
|
|
return "Magic Book";
|
2025-02-02 02:25:21 -05:00
|
|
|
|
default:
|
|
|
|
|
|
return type.toString();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-02-02 22:47:40 -05:00
|
|
|
|
|
|
|
|
|
|
public static void getScripts(IScriptHandler data, EntityPlayerMP player) {
|
|
|
|
|
|
NBTTagCompound compound = new NBTTagCompound();
|
|
|
|
|
|
compound.setBoolean("ScriptEnabled", data.getEnabled());
|
|
|
|
|
|
compound.setString("ScriptLanguage", data.getLanguage());
|
|
|
|
|
|
compound.setTag("Languages", ScriptController.Instance.nbtLanguages());
|
|
|
|
|
|
compound.setTag("ScriptConsole", NBTTags.NBTLongStringMap(data.getConsoleText()));
|
|
|
|
|
|
GuiDataPacket.sendGuiData(player, compound);
|
|
|
|
|
|
List<ScriptContainer> containers = data.getScripts();
|
|
|
|
|
|
for (int i = 0; i < containers.size(); i++) {
|
|
|
|
|
|
ScriptContainer container = containers.get(i);
|
|
|
|
|
|
NBTTagCompound tabCompound = new NBTTagCompound();
|
2025-03-13 17:14:45 -04:00
|
|
|
|
tabCompound.setInteger("Tab", i);
|
|
|
|
|
|
tabCompound.setTag("Script", container.writeToNBT(new NBTTagCompound()));
|
|
|
|
|
|
tabCompound.setInteger("TotalScripts", containers.size());
|
2025-02-02 22:47:40 -05:00
|
|
|
|
GuiDataPacket.sendGuiData(player, tabCompound);
|
|
|
|
|
|
}
|
2025-05-29 21:09:58 -04:00
|
|
|
|
|
|
|
|
|
|
NBTTagCompound loadComplete = new NBTTagCompound();
|
|
|
|
|
|
loadComplete.setInteger("LoadComplete", 1);
|
|
|
|
|
|
GuiDataPacket.sendGuiData(player, loadComplete);
|
2025-02-02 22:47:40 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-02 23:15:53 -05:00
|
|
|
|
public static void saveScripts(IScriptHandler data, ByteBuf buffer) throws IOException {
|
2025-02-02 22:47:40 -05:00
|
|
|
|
int tab = buffer.readInt();
|
|
|
|
|
|
int totalScripts = buffer.readInt();
|
|
|
|
|
|
if (totalScripts == 0) {
|
|
|
|
|
|
data.getScripts().clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (tab >= 0) {
|
|
|
|
|
|
if (data.getScripts().size() > totalScripts) {
|
2025-03-13 17:14:45 -04:00
|
|
|
|
data.setScripts(data.getScripts().subList(0, totalScripts));
|
2025-02-02 22:47:40 -05:00
|
|
|
|
} else while (data.getScripts().size() < totalScripts) {
|
|
|
|
|
|
data.getScripts().add(new ScriptContainer(data));
|
|
|
|
|
|
}
|
|
|
|
|
|
NBTTagCompound tabCompound = ByteBufUtils.readNBT(buffer);
|
|
|
|
|
|
ScriptContainer script = new ScriptContainer(data);
|
|
|
|
|
|
script.readFromNBT(tabCompound);
|
2025-03-13 17:14:45 -04:00
|
|
|
|
data.getScripts().set(tab, script);
|
2025-02-02 22:47:40 -05:00
|
|
|
|
} else {
|
|
|
|
|
|
NBTTagCompound compound = ByteBufUtils.readNBT(buffer);
|
|
|
|
|
|
data.setLanguage(compound.getString("ScriptLanguage"));
|
|
|
|
|
|
if (!ScriptController.Instance.languages.containsKey(data.getLanguage())) {
|
|
|
|
|
|
if (!ScriptController.Instance.languages.isEmpty()) {
|
|
|
|
|
|
data.setLanguage((String) ScriptController.Instance.languages.keySet().toArray()[0]);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
data.setLanguage("ECMAScript");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
data.setEnabled(compound.getBoolean("ScriptEnabled"));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-01-29 21:50:55 -05:00
|
|
|
|
}
|