Add requirements for mods and see mods in use if so

This commit is contained in:
libraryaddict
2020-04-16 18:32:09 +12:00
parent 1791622323
commit caa0aa69d9
8 changed files with 209 additions and 10 deletions

View File

@@ -1,10 +1,18 @@
package me.libraryaddict.disguise.utilities.modded;
import com.comphenix.protocol.utility.StreamSerializer;
import lombok.Getter;
import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.utilities.parser.DisguisePerm;
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.plugin.messaging.PluginMessageListener;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
@@ -12,8 +20,52 @@ import java.util.Map;
/**
* Created by libraryaddict on 14/04/2020.
*/
public class ModdedManager {
public class ModdedManager implements PluginMessageListener {
@Getter
private static final HashMap<NamespacedKey, CustomEntity> entities = new HashMap<>();
@Getter
private static byte[] fmlHandshake;
public ModdedManager(ArrayList<String> channels) {
if (getEntities().isEmpty()) {
return;
}
if (getEntities().values().stream().noneMatch(e -> e.getMod() != null)) {
return;
}
Bukkit.getMessenger().registerOutgoingPluginChannel(LibsDisguises.getInstance(), "fml:handshake");
Bukkit.getMessenger().registerIncomingPluginChannel(LibsDisguises.getInstance(), "fml:handshake", this);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(stream);
StreamSerializer s = StreamSerializer.getDefault();
try {
// Packet id 1
s.serializeVarInt(output, 1);
// We have no mods to declare
s.serializeVarInt(output, 0);
// We want to declare some channels
s.serializeVarInt(output, channels.size());
for (String channel : channels) {
s.serializeString(output, channel.substring(0, channel.indexOf("|")));
s.serializeString(output, channel.substring(channel.indexOf("|") + 1));
}
// We have no resources to declare
s.serializeVarInt(output, 0);
}
catch (IOException e) {
e.printStackTrace();
}
fmlHandshake = stream.toByteArray();
}
public static void registerCustomEntity(NamespacedKey name, CustomEntity entity, boolean register) {
if (entities.keySet().stream().anyMatch(n -> n.toString().equalsIgnoreCase(name.toString()))) {
@@ -68,4 +120,54 @@ public class ModdedManager {
return perms;
}
@Override
public void onPluginMessageReceived(String channel, Player player, byte[] bytes) {
if (player.hasMetadata("forge_mods")) {
return;
}
DataInputStream stream = new DataInputStream(new ByteArrayInputStream(bytes));
try {
StreamSerializer s = StreamSerializer.getDefault();
int packetId = s.deserializeVarInt(stream);
if (packetId != 2) {
return;
}
int count = s.deserializeVarInt(stream);
ArrayList<String> mods = new ArrayList<>();
for (int i = 0; i < count; i++) {
mods.add(s.deserializeString(stream, 256));
}
player.setMetadata("forge_mods", new FixedMetadataValue(LibsDisguises.getInstance(), mods));
for (CustomEntity e : getEntities().values()) {
if (e.getMod() == null) {
continue;
}
if (mods.contains(e.getMod().toLowerCase())) {
continue;
}
// TODO Idk, something because they don't have a mod?
if (e.getRequired() == null) {
continue;
}
player.kickPlayer(e.getRequired());
break;
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@@ -306,7 +306,7 @@ public class PacketHandlerSpawn implements IPacketHandler {
PacketContainer spawnEntity;
if (NmsVersion.v1_14.isSupported()) {
if (NmsVersion.v1_14.isSupported()) {System.out.println("...");
Object entityType;
if (disguise.isCustomDisguise()) {

View File

@@ -134,8 +134,11 @@ public enum LibsMsg {
MADE_REF(ChatColor.RED + "Constructed a %s disguise! Your reference is %s"),
MADE_REF_EXAMPLE(ChatColor.RED + "Example usage: /disguise %s"),
NO_CONSOLE(ChatColor.RED + "You may not use this command from the console!"),
NO_MODS(ChatColor.RED + "%s is not using any mods!"),
MODS_LIST(ChatColor.DARK_GREEN + "%s has the mods:" + ChatColor.AQUA + " %s"),
NO_PERM(ChatColor.RED + "You are forbidden to use this command."),
NO_PERM_DISGUISE(ChatColor.RED + "You do not have permission for that disguise!"),
NO_MODS_LISTENING(ChatColor.RED + "This server is not listening for mods!"),
NO_PERMS_USE_OPTIONS(ChatColor.RED +
"Ignored %s methods you do not have permission to use. Add 'show' to view unusable methods."),
OWNED_BY(ChatColor.GOLD + "Plugin registered to '%%__USER__%%'!"),