Skip to content

Commit

Permalink
Prevention from creating multiple unused nms classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Straikerinos committed Aug 15, 2024
1 parent a102cb3 commit 4adf3d9
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 1,796 deletions.
5 changes: 5 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -313,20 +313,29 @@ private void broadcastSystemInfo() {

private void loadProvider(boolean canUseJavaFile) throws Exception {
String serverVersion = Ref.serverVersion().replace('.', '_');
if (!serverVersion.startsWith("v"))
if (!serverVersion.startsWith("v")) {
if(Ref.serverType()==ServerType.PAPER && (Ref.isNewerThan(20)||Ref.isNewerThan(19) && Ref.serverVersionRelease()>=6)) {
try {
Config mappings = Config.loadFromInput(new URL("https://raw.githubusercontent.com/TheDevTec/TheAPI/main/paper-mappings.yml").openStream());
serverVersion = mappings.getString(serverVersion);
}catch(Exception noInternetConnection){

}
}
serverVersion = 'v' + serverVersion;
}
if (ToolProvider.getSystemJavaCompiler() != null && !canUseJavaFile)
try {
getAllJarFiles();
checkForUpdateAndDownload();
checkForUpdateAndDownload(serverVersion);
if (new File("plugins/TheAPI/NmsProviders/" + serverVersion + ".java").exists()) {
nmsProvider = (NmsProvider) new MemoryCompiler(NO_OBFUSCATED_NMS_MODE ? getClassLoader() : Bukkit.getServer().getClass().getClassLoader(), serverVersion, new File("plugins/TheAPI/NmsProviders/" + serverVersion + ".java")).buildClass().newInstance();
nmsProvider.loadParticles();
}
} catch (Exception err) {
err.printStackTrace();
Bukkit.getConsoleSender().sendMessage(ColorUtils.colorize("&7> &4Error! Failed to load NmsProvider from .java file, loading from .jar."));
checkForUpdateAndDownloadCompiled();
checkForUpdateAndDownloadCompiled(serverVersion);
if (new File("plugins/TheAPI/NmsProviders/" + serverVersion + ".jar").exists())
try (URLClassLoader cl = new URLClassLoader(new URL[]{new URL("jar:file:" + "plugins/TheAPI/NmsProviders/" + serverVersion + ".jar" + "!/")}, getClassLoader())) {
Class<?> c = cl.loadClass(serverVersion);
Expand All @@ -337,7 +346,7 @@ private void loadProvider(boolean canUseJavaFile) throws Exception {
}
}
else { // JRE
checkForUpdateAndDownloadCompiled();
checkForUpdateAndDownloadCompiled(serverVersion);
if (new File("plugins/TheAPI/NmsProviders/" + serverVersion + ".jar").exists())
try (URLClassLoader cl = new URLClassLoader(new URL[]{new URL("jar:file:" + "plugins/TheAPI/NmsProviders/" + serverVersion + ".jar" + "!/")}, getClassLoader())) {
Class<?> c = cl.loadClass(serverVersion);
Expand Down Expand Up @@ -485,10 +494,7 @@ public void onDisable() {
API.offlineCache().saveToConfig().setFile(new File("plugins/TheAPI/Cache.dat")).save("properties");
}

private void checkForUpdateAndDownloadCompiled() {
String serverVersion = Ref.serverVersion().replace('.', '_');
if (!serverVersion.startsWith("v"))
serverVersion = 'v' + serverVersion;
private void checkForUpdateAndDownloadCompiled(String serverVersion) {
try {
Config gitVersion = Config.loadFromInput(new URL("https://raw.githubusercontent.com/TheDevTec/TheAPI/main/version.yml").openStream());
Config localVersion = new Config("plugins/TheAPI/version.yml");
Expand Down Expand Up @@ -519,10 +525,7 @@ private Version getGitVersion(Config localVersion, Config gitVersion) {
return VersionUtils.getVersion(gitVersion.getString("release"), "" + release);
}

private void checkForUpdateAndDownload() {
String serverVersion = Ref.serverVersion().replace('.', '_');
if (!serverVersion.startsWith("v"))
serverVersion = 'v' + serverVersion;
private void checkForUpdateAndDownload(String serverVersion) {
try {
Config gitVersion = Config.loadFromInput(new URL("https://raw.githubusercontent.com/TheDevTec/TheAPI/main/version.yml").openStream());
Config localVersion = new Config("plugins/TheAPI/version.yml");
Expand Down
14 changes: 7 additions & 7 deletions NmsProvider - 1.20.6/src/main/java/v1_20_6.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,17 +229,17 @@ public Object packetEntityDestroy(int... ids) {

@Override
public Object packetSpawnEntity(Object entity, int id) {
return new ClientboundAddEntityPacket((net.minecraft.world.entity.Entity) entity, id);
return new ClientboundAddEntityPacket((net.minecraft.world.entity.Entity) entity, id, ((net.minecraft.world.entity.Entity) entity).blockPosition());
}

@Override
public Object packetNamedEntitySpawn(Object player) {
return new ClientboundAddEntityPacket((net.minecraft.world.entity.player.Player) player);
return new ClientboundAddEntityPacket((net.minecraft.world.entity.player.Player) player,0, ((net.minecraft.world.entity.Entity) player).blockPosition());
}

@Override
public Object packetSpawnEntityLiving(Object entityLiving) {
return new ClientboundAddEntityPacket((net.minecraft.world.entity.LivingEntity) entityLiving);
return new ClientboundAddEntityPacket((net.minecraft.world.entity.LivingEntity) entityLiving, 0, ((net.minecraft.world.entity.LivingEntity) entityLiving).blockPosition());
}

@Override
Expand Down Expand Up @@ -340,7 +340,7 @@ private net.minecraft.network.chat.Component convert(Component c) {
try {
ComponentEntity compoundTag = (ComponentEntity) c.getHoverEvent().getValue();
net.minecraft.network.chat.Component component = compoundTag.getName() == null ? null : (net.minecraft.network.chat.Component) toIChatBaseComponent(compoundTag.getName());
EntityType<?> entityType = BuiltInRegistries.ENTITY_TYPE.get(new ResourceLocation(compoundTag.getType()));
EntityType<?> entityType = BuiltInRegistries.ENTITY_TYPE.get(ResourceLocation.tryParse(compoundTag.getType()));
modif = modif.withHoverEvent(new net.minecraft.network.chat.HoverEvent(net.minecraft.network.chat.HoverEvent.Action.SHOW_ENTITY,
new net.minecraft.network.chat.HoverEvent.EntityTooltipInfo(entityType, compoundTag.getId(), component)));
} catch (Exception commandSyntaxException) {
Expand Down Expand Up @@ -1402,8 +1402,8 @@ public boolean processServerListPing(String player, Object channel, Object packe
BufferedImage var1;
try {
var1 = ImageIO.read(new File(event.getFavicon()));
Preconditions.checkState(var1.getWidth() == 64, "Must be 64 pixels wide");
Preconditions.checkState(var1.getHeight() == 64, "Must be 64 pixels high");
if (var1.getWidth() != 64) throw new IOException("Must be 64 pixels wide");
if (var1.getHeight() != 64) throw new IOException("Must be 64 pixels high");
ByteArrayOutputStream var2 = new ByteArrayOutputStream();
ImageIO.write(var1, "PNG", var2);
serverIcon = Optional.of(new Favicon(var2.toByteArray()));
Expand Down Expand Up @@ -1652,7 +1652,7 @@ public Object packetRespawn(Player player) {

@Override
public String getProviderName() {
return "PaperMC 1.20.6";
return "PaperMC (1.20.6) "+Bukkit.getMinecraftVersion();
}

@Override
Expand Down
Binary file removed NmsProvider - 1.21/build/NmsProvider.jar
Binary file not shown.
83 changes: 0 additions & 83 deletions NmsProvider - 1.21/pom.xml

This file was deleted.

Loading

0 comments on commit 4adf3d9

Please sign in to comment.