Files
CustomNPC-Plus/src/main/java/kamkeel/npcs/network/PacketUtil.java
T

232 lines
9.6 KiB
Java
Raw Normal View History

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-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.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
2025-01-29 21:50:55 -05:00
public class PacketUtil {
2025-02-02 02:25:21 -05:00
public static boolean verifyItemPacket(EnumItemPacketType type, EntityPlayer player) {
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) {
LogWriter.error(String.format("%s attempted to utilize a %s Packet without an item, they could be a hacker",
player.getCommandSenderName(), type));
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",
player.getCommandSenderName(), type));
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",
player.getCommandSenderName(), type));
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) {
LogWriter.error(String.format("%s attempted to utilize a %s Packet without a Cloner, they could be a hacker",
player.getCommandSenderName(), type));
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) {
LogWriter.error(String.format("%s attempted to utilize a %s Packet without a Teleporter, they could be a hacker",
player.getCommandSenderName(), type));
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) {
LogWriter.error(String.format("%s attempted to utilize a %s Packet without a Scripter, they could be a hacker",
player.getCommandSenderName(), type));
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) {
LogWriter.error(String.format("%s attempted to utilize a %s Packet without a Pather, they could be a hacker",
player.getCommandSenderName(), type));
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)) {
LogWriter.error(String.format("%s attempted to utilize a %s Packet without a valid Block, they could be a hacker",
player.getCommandSenderName(), type));
2025-02-01 21:23:49 -05:00
return false;
}
break;
}
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.
*/
public static boolean verifyItemPacket(EntityPlayer player, EnumItemPacketType... types) {
if (player == null)
return false;
ItemStack item = player.inventory.getCurrentItem();
if (item == null) {
LogWriter.error(String.format("%s attempted to utilize a Packet without an item. Expected one of: %s, they could be a hacker",
player.getCommandSenderName(), getExpectedItemNames(types)));
return false;
}
for (EnumItemPacketType type : types) {
if (isValidItemForType(item, type)) {
return true;
}
}
// None passed; log one merged error message listing all expected items.
LogWriter.error(String.format("%s attempted to utilize a Packet without a valid item. Expected one of: %s, they could be a hacker",
player.getCommandSenderName(), getExpectedItemNames(types)));
return false;
}
private static boolean isValidItemForType(ItemStack item, EnumItemPacketType type) {
2025-02-09 01:09:23 -05:00
if(item == null || item.getItem() == null)
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));
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";
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();
tabCompound.setInteger("Tab",i);
tabCompound.setTag("Script",container.writeToNBT(new NBTTagCompound()));
tabCompound.setInteger("TotalScripts",containers.size());
GuiDataPacket.sendGuiData(player, tabCompound);
}
}
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) {
data.setScripts(data.getScripts().subList(0,totalScripts));
} 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);
data.getScripts().set(tab,script);
} 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
}