Fix modded entities

This commit is contained in:
libraryaddict
2020-06-11 17:12:55 +12:00
parent 8bb9051e59
commit c4717d7453
10 changed files with 278 additions and 86 deletions

View File

@@ -1,43 +1,51 @@
package me.libraryaddict.disguise.utilities.modded;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.utility.StreamSerializer;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import lombok.Getter;
import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.utilities.listeners.ModdedListener;
import me.libraryaddict.disguise.utilities.packets.packetlisteners.PacketListenerModdedClient;
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.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* Created by libraryaddict on 14/04/2020.
*/
public class ModdedManager implements PluginMessageListener {
public class ModdedManager {
@Getter
private static final HashMap<NamespacedKey, ModdedEntity> entities = new HashMap<>();
@Getter
private static byte[] fmlHandshake;
@Getter
private static byte[] fmlRegistries;
@Getter
private static final Cache<String, ArrayList<String>> forgeMods = CacheBuilder.newBuilder()
.expireAfterWrite(1, TimeUnit.MINUTES).build();
public ModdedManager(ArrayList<String> channels) {
if (getEntities().isEmpty()) {
return;
}
if (getEntities().values().stream().noneMatch(e -> e.getMod() != null)) {
return;
if (fmlRegistries == null) {
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketListenerModdedClient());
Bukkit.getPluginManager().registerEvents(new ModdedListener(), LibsDisguises.getInstance());
}
Bukkit.getMessenger().registerOutgoingPluginChannel(LibsDisguises.getInstance(), "fml:handshake");
Bukkit.getMessenger().registerIncomingPluginChannel(LibsDisguises.getInstance(), "fml:handshake", this);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(stream);
@@ -57,14 +65,58 @@ public class ModdedManager implements PluginMessageListener {
s.serializeString(output, channel.substring(channel.indexOf("|") + 1));
}
// We have no resources to declare
// We want to declare some entities
s.serializeVarInt(output, 0);
// Only this one thx
// s.serializeString(output, "minecraft:entity_type");
}
catch (IOException e) {
e.printStackTrace();
}
fmlHandshake = stream.toByteArray();
stream = new ByteArrayOutputStream();
output = new DataOutputStream(stream);
s = StreamSerializer.getDefault();
try {
// Packet id 3
s.serializeVarInt(output, 3);
// What registry we're modifying
s.serializeString(output, "minecraft:entity_type");
// Yes... We're doing custom data
s.serializeVarInt(output, 1);
// We have this many entities
s.serializeVarInt(output, entities.size());
// Write the entity names and ids
for (Map.Entry<NamespacedKey, ModdedEntity> entry : entities.entrySet()) {
s.serializeString(output, entry.getKey().toString());
s.serializeVarInt(output, entry.getValue().getTypeId());
}
// Sir, we do not want to declare aliases
s.serializeVarInt(output, 0);
// Or overrides
s.serializeVarInt(output, 0);
// No.. Not even blocked
s.serializeVarInt(output, 0);
// Or dummied
s.serializeVarInt(output, 0);
}
catch (IOException e) {
e.printStackTrace();
}
fmlRegistries = stream.toByteArray();
}
public static void registerModdedEntity(NamespacedKey name, ModdedEntity entity, boolean register) {
@@ -76,20 +128,22 @@ public class ModdedManager implements PluginMessageListener {
throw new IllegalArgumentException("Modded entity " + entity.getName() + " has already been registered");
}
Object entityType;
if (register) {
Object entityType = ReflectionManager.registerEntityType(name);
entityType = ReflectionManager.registerEntityType(name);
int entityId = ReflectionManager.getEntityTypeId(entityType);
entity.setTypeId(entityId);
entity.setEntityType(entityType);
} else {
Object entityType = ReflectionManager.getEntityType(name);
entityType = ReflectionManager.getEntityType(name);
int entityId = ReflectionManager.getEntityTypeId(entityType);
entity.setTypeId(entityId);
entity.setEntityType(entityType);
}
entity.setEntityType(entityType);
entities.put(name, entity);
}
@@ -120,54 +174,4 @@ public class ModdedManager implements PluginMessageListener {
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 (ModdedEntity 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();
}
}
}