0

Merge branch 'feature/phantom-classes' into dev/modules

This commit is contained in:
2021-01-08 20:05:57 +03:00
16 changed files with 113 additions and 51 deletions

View File

@@ -5,7 +5,6 @@ class LibsExtention {
final def commons_text = 'org.apache.commons:commons-text:1.9' final def commons_text = 'org.apache.commons:commons-text:1.9'
final def lombok = 'org.projectlombok:lombok:1.18.12' final def lombok = 'org.projectlombok:lombok:1.18.12'
final def refobj = 'ru.dmitriymx:reflection-object:1.2'
final def bukkit = filter([ final def bukkit = filter([
lib : 'org.bukkit:bukkit:1.12.2-R0.1-SNAPSHOT', lib : 'org.bukkit:bukkit:1.12.2-R0.1-SNAPSHOT',

View File

@@ -0,0 +1,3 @@
dependencies {
compileOnly libs.bukkit
}

View File

@@ -0,0 +1,2 @@
moduleName=phantom-classes
moduleVersion=1.0-SNAPSHOT

View File

@@ -0,0 +1,16 @@
package com.mojang.authlib;
import com.mojang.authlib.properties.PropertyMap;
import java.util.UUID;
public class GameProfile {
public GameProfile(UUID id, String name) {
}
public PropertyMap getProperties() {
return null;
}
}

View File

@@ -0,0 +1,8 @@
package com.mojang.authlib.properties;
public class Property {
public Property(String value, String name) {
}
}

View File

@@ -0,0 +1,12 @@
package com.mojang.authlib.properties;
import com.google.common.collect.ForwardingMultimap;
import com.google.common.collect.Multimap;
public class PropertyMap extends ForwardingMultimap<String, Property> {
@Override
protected Multimap<String, Property> delegate() {
return null;
}
}

View File

@@ -0,0 +1,7 @@
package net.minecraft.server.v1_12_R1;
public class BlockPosition {
public BlockPosition(double x, double y, double z) {
}
}

View File

@@ -0,0 +1,5 @@
package net.minecraft.server.v1_12_R1;
public class TileEntity {
}

View File

@@ -0,0 +1,10 @@
package net.minecraft.server.v1_12_R1;
import com.mojang.authlib.GameProfile;
public class TileEntitySkull extends TileEntity {
public void setGameProfile(GameProfile gameprofile) {
}
}

View File

@@ -0,0 +1,8 @@
package net.minecraft.server.v1_12_R1;
public class WorldServer {
public TileEntity getTileEntity(BlockPosition pos) {
return null;
}
}

View File

@@ -0,0 +1,10 @@
package org.bukkit.craftbukkit.v1_12_R1;
import org.bukkit.command.SimpleCommandMap;
public class CraftServer {
public SimpleCommandMap getCommandMap() {
return null;
}
}

View File

@@ -0,0 +1,10 @@
package org.bukkit.craftbukkit.v1_12_R1;
import net.minecraft.server.v1_12_R1.WorldServer;
public class CraftWorld {
public WorldServer getHandle() {
return null;
}
}

View File

@@ -1,2 +1,2 @@
include 'phantom-classes'
include 'tools' include 'tools'

View File

@@ -4,9 +4,9 @@ repositories {
} }
dependencies { dependencies {
compileOnly project(':phantom-classes')
compileOnly libs.bukkit compileOnly libs.bukkit
implementation libs.commons_text implementation libs.commons_text
implementation libs.refobj
testImplementation libs.bukkit testImplementation libs.bukkit
testImplementation libs.test.h2db testImplementation libs.test.h2db

View File

@@ -1,6 +1,10 @@
package ghast; package ghast;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import lombok.experimental.UtilityClass; import lombok.experimental.UtilityClass;
import net.minecraft.server.v1_12_R1.BlockPosition;
import net.minecraft.server.v1_12_R1.TileEntitySkull;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.SkullType; import org.bukkit.SkullType;
@@ -8,8 +12,7 @@ import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.block.Sign; import org.bukkit.block.Sign;
import org.bukkit.block.Skull; import org.bukkit.block.Skull;
import ru.dmitriymx.reflection.ReflectionClass; import org.bukkit.craftbukkit.v1_12_R1.CraftWorld;
import ru.dmitriymx.reflection.ReflectionObject;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Base64; import java.util.Base64;
@@ -19,9 +22,6 @@ import java.util.UUID;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public class BuildHelper { public class BuildHelper {
private final Class<?> CLASS_BLOCKPOSITION = getClassForName("net.minecraft.server.v1_12_R1.BlockPosition");
private final Class<?> CLASS_GAMEPROFILE = getClassForName("com.mojang.authlib.GameProfile");
/** /**
* Установка черепа. * Установка черепа.
* <p> * <p>
@@ -95,17 +95,10 @@ public class BuildHelper {
* @param skinUrl URL на текстуру * @param skinUrl URL на текстуру
*/ */
public static void setPlayerHeadSkin(Skull skull, String skinUrl) { public static void setPlayerHeadSkin(Skull skull, String skinUrl) {
//TODO заменить рефлексию на "фантомные" классы ((TileEntitySkull) (((CraftWorld) skull.getWorld())
ReflectionObject refobjBlockPosition = new ReflectionClass(CLASS_BLOCKPOSITION) .getHandle()
.constructor(double.class, double.class, double.class) .getTileEntity(new BlockPosition(skull.getX(), skull.getY(), skull.getZ()))))
.newInstance(skull.getX(), skull.getY(), skull.getZ()); .setGameProfile(generateTexturedPlayerProfile(skinUrl));
new ReflectionObject(skull.getWorld())
.method("getHandle").invoke()
.method("getTileEntity", CLASS_BLOCKPOSITION)
.invoke(refobjBlockPosition.getOriginalObject())
.method("setGameProfile", CLASS_GAMEPROFILE)
.invoke(getRefObjPlayerProfile(skinUrl).getOriginalObject());
} }
public Sign placeSignWall(Location location, BlockFace face) { public Sign placeSignWall(Location location, BlockFace face) {
@@ -119,29 +112,12 @@ public class BuildHelper {
return sign; return sign;
} }
private ReflectionObject getRefObjPlayerProfile(String url){ private GameProfile generateTexturedPlayerProfile(String skinUrl) {
ReflectionObject refobjProperty = new ReflectionClass( GameProfile gameProfile = new GameProfile(UUID.randomUUID(), null);
getClassForName("com.mojang.authlib.properties.Property"))
.constructor(String.class, String.class)
.newInstance("textures", Base64.getEncoder()
.encodeToString(("{textures:{SKIN:{url:\"" + url + "\"}}}").getBytes(StandardCharsets.UTF_8)));
ReflectionObject refobjGameProfile = new ReflectionClass(CLASS_GAMEPROFILE) gameProfile.getProperties().put("textures",
.constructor(UUID.class, String.class) new Property("textures", Base64.getEncoder().encodeToString(
.newInstance(UUID.randomUUID(), null); ("{textures:{SKIN:{url:\"" + skinUrl + "\"}}}").getBytes(StandardCharsets.UTF_8))));
refobjGameProfile return gameProfile;
.method("getProperties").invoke()
.method("put", Object.class, Object.class)
.invoke("textures", refobjProperty.getOriginalObject());
return refobjGameProfile;
}
private Class<?> getClassForName(String className) {
try {
return Class.forName(className);
} catch (Exception e) {
throw new RuntimeException(e);
}
} }
} }

View File

@@ -4,8 +4,7 @@ import lombok.AccessLevel;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.experimental.UtilityClass; import lombok.experimental.UtilityClass;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.Command; import org.bukkit.craftbukkit.v1_12_R1.CraftServer;
import ru.dmitriymx.reflection.ReflectionObject;
@UtilityClass @UtilityClass
@SuppressWarnings("unused") @SuppressWarnings("unused")
@@ -59,12 +58,9 @@ public class CommandManager {
} }
public void register() { public void register() {
//TODO для Paper такие "извращения" не требуются. Нужно продумать. ((CraftServer) Bukkit.getServer()).getCommandMap().register(
new ReflectionObject(Bukkit.getServer()) name,
.method("getCommandMap").invoke() new CommandWrapper(name, this.onlyPlayer, this.deniedMessage, this.executer, this.errorConsumer)
.method("register", String.class, Command.class).invoke(
name, new CommandWrapper(name, this.onlyPlayer, this.deniedMessage,
this.executer, this.errorConsumer)
); );
} }
} }