Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

restructure the project for multiversion support #3

Merged
merged 3 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ subprojects {
repositories {
mavenCentral()
}

val common by configurations.creating
}

val version: String by project
val version: String by project
18 changes: 5 additions & 13 deletions bukkit/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
plugins {
id("java")
id("com.github.johnrengelman.shadow") version "8.1.1"
}

repositories {
Expand All @@ -9,20 +8,10 @@ repositories {

dependencies {
compileOnly("org.spigotmc:spigot-api:1.20.3-R0.1-SNAPSHOT")
implementation(project(":common"))

compileOnly(files("libs/ProtocolLib.jar"))
}

tasks.shadowJar {
dependencies {
exclude(dependency("org.apache.commons:commons-compress:1.21"))
exclude(dependency("commons-codec:commons-codec:1.16.0"))
}
}

tasks.build {
dependsOn(tasks.shadowJar)
implementation(project(":common"))
common(project(":common"))
}

tasks.processResources {
Expand All @@ -36,4 +25,7 @@ tasks.processResources {

tasks.withType<AbstractArchiveTask> {
setProperty("archiveFileName", "arte-bukkit-${rootProject.version}.jar")

from(configurations.getByName("common").map { if (it.isDirectory) it else zipTree(it) })
duplicatesStrategy = DuplicatesStrategy.WARN
}
37 changes: 12 additions & 25 deletions bukkit/src/main/java/io/shardmc/arte/bukkit/ArtePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import io.shardmc.arte.bukkit.config.BukkitArteConfig;
import io.shardmc.arte.bukkit.logger.BukkitArteLogger;
import io.shardmc.arte.bukkit.pack.base.BukkitPackManager;
import io.shardmc.arte.bukkit.pack.PaperPackManager;
import io.shardmc.arte.bukkit.pack.SpigotPackManager;
import io.shardmc.arte.bukkit.pack.BukkitPackManager;
import io.shardmc.arte.bukkit.platform.BukkitPlatform;
import org.bukkit.command.PluginCommand;
import org.bukkit.command.TabExecutor;
import org.bukkit.plugin.java.JavaPlugin;
Expand All @@ -14,31 +13,25 @@
import io.shardmc.arte.common.config.ArteConfig;
import io.shardmc.arte.common.logger.ArteLogger;

import java.io.File;
import java.io.IOException;
import java.net.URL;

public final class ArtePlugin extends JavaPlugin implements Arte {

private BukkitPlatform platform;

private ArteLogger logger;
private ArteConfig config;

private BukkitPackManager packManager;

@Override
public void onEnable() {
this.platform = BukkitPlatform.create(this);

this.logger = new BukkitArteLogger(this);
this.config = new BukkitArteConfig(this);

this.command("arte", new ArteCommand(this));

try {
Class.forName("com.destroystokyo.paper.ParticleBuilder");
this.packManager = new PaperPackManager(this);
} catch (ClassNotFoundException e) {
this.packManager = new SpigotPackManager(this);
}
this.packManager = new BukkitPackManager(this);

this.command("arte", new ArteCommand(this));
this.getServer().getPluginManager().registerEvents(new PlayerListener(this), this);
}

Expand All @@ -63,18 +56,12 @@ public BukkitPackManager getPackManager() {
}

@Override
public File getConfigFile() {
return new File(this.getDataFolder(), "config.yml");
public BukkitPlatform platform() {
return this.platform;
}

@Override
public URL getResourceUrl(String path) throws IOException {
URL url = this.getClassLoader().getResource(path);

if (url == null)
throw new IOException("Couldn't find the default config! The build may be corrupt! Path: " + path);

return url;
public ClassLoader getLoader() {
return super.getClassLoader();
}

private void command(String name, TabExecutor executor) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.shardmc.arte.bukkit.pack.base;
package io.shardmc.arte.bukkit.pack;

import io.shardmc.arte.bukkit.ArtePlugin;
import io.shardmc.arte.common.pack.manager.PackManager;
Expand All @@ -8,7 +8,7 @@
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerResourcePackStatusEvent;

public abstract class BukkitPackManager extends PackManager implements Listener {
public class BukkitPackManager extends PackManager implements Listener {

public BukkitPackManager(ArtePlugin arte) {
super(arte);
Expand All @@ -21,14 +21,15 @@ public void apply(Player player) {
String prompt = this.arte.config().getPrompt();

for (BuiltPack pack : this.zipper.getPacks()) {
this.apply(player, pack, prompt);
this.arte.platform().applyPack(player,
pack.uuid(), pack.getAddress(this.server),
pack.hash(), pack.force(), prompt
);
}
}

@EventHandler
public void onPack(PlayerResourcePackStatusEvent event) {
this.arte.logger().info("Status: " + event.getStatus());
}

protected abstract void apply(Player player, BuiltPack pack, String prompt);
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package io.shardmc.arte.bukkit.platform;

import io.shardmc.arte.bukkit.ArtePlugin;
import io.shardmc.arte.bukkit.platform.impl.PaperPlatform;
import io.shardmc.arte.bukkit.platform.impl.SpigotPlatform;
import io.shardmc.arte.common.platform.Platform;
import org.bukkit.entity.Player;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.UUID;

public abstract class BukkitPlatform implements Platform<ArtePlugin> {

protected ArtePlugin arte;

public static BukkitPlatform create(ArtePlugin arte) {
BukkitPlatform result = BukkitPlatform.isPaper() ? new PaperPlatform() : new SpigotPlatform();
result.init(arte);

return result;
}

private static boolean isPaper() {
try {
Class.forName("com.destroystokyo.paper.ParticleBuilder");
return true;
} catch (ClassNotFoundException ignored) { }

return false;
}

@Override
public void init(ArtePlugin arte) {
this.arte = arte;
}

@Override
public File getConfigFile() {
return new File(this.getDataFolder(), "config.yml");
}

@Override
public File getDataFolder() {
return this.arte.getDataFolder();
}

@Override
public URL getResourceUrl(String path) throws IOException {
URL url = this.arte.getLoader().getResource(path);

if (url == null)
throw new IOException("Couldn't find the default config! The build may be corrupt! Path: " + path);

return url;
}

@Override
public void applyPack(Object playerObject, UUID id, String address, String hash, boolean force, Object textObject) {
if (!(playerObject instanceof Player player))
return;

if (!(textObject instanceof String text))
return;

this.applyPack(player, id, address, hash, force, text);
}

public abstract void applyPack(Player player, UUID id, String address, String hash, boolean force, String text);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.shardmc.arte.bukkit.platform.impl;

import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.wrappers.WrappedChatComponent;
import io.shardmc.arte.bukkit.platform.BukkitPlatform;
import org.bukkit.entity.Player;

import java.util.UUID;

public class PaperPlatform extends BukkitPlatform {

private final ProtocolManager manager = ProtocolLibrary.getProtocolManager();

@Override
public void applyPack(Player player, UUID id, String address, String hash, boolean force, String text) {
PacketContainer packet = this.manager.createPacket(PacketType.Play.Server.ADD_RESOURCE_PACK, false);
packet.getUUIDs().write(0, id);
packet.getStrings().write(0, address);
packet.getStrings().write(1, hash);
packet.getBooleans().write(0, force);
packet.getChatComponents().write(0, WrappedChatComponent.fromText(text));

this.manager.sendServerPacket(player, packet);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.shardmc.arte.bukkit.platform.impl;

import io.shardmc.arte.bukkit.platform.BukkitPlatform;
import org.bukkit.entity.Player;

import java.util.UUID;

public class SpigotPlatform extends BukkitPlatform {

@Override
public void applyPack(Player player, UUID id, String address, String hash, boolean force, String text) {
player.setResourcePack(id, address, hash.getBytes(), text, force);
}
}
4 changes: 2 additions & 2 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ plugins {
}

dependencies {
implementation("org.apache.commons:commons-compress:1.21")
implementation("commons-codec:commons-codec:1.16.0")
compileOnly("org.apache.commons:commons-compress:1.21")
compileOnly("commons-codec:commons-codec:1.16.0")
}
22 changes: 7 additions & 15 deletions common/src/main/java/io/shardmc/arte/common/Arte.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,19 @@
import io.shardmc.arte.common.config.ArteConfig;
import io.shardmc.arte.common.logger.ArteLogger;
import io.shardmc.arte.common.pack.manager.PackManager;
import io.shardmc.arte.common.platform.Platform;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ServiceLoader;
import java.util.stream.Stream;

public interface Arte {
ArteLogger logger();
ArteConfig config();
PackManager getPackManager();
Platform<?> platform();

File getDataFolder();
File getConfigFile();

URL getResourceUrl(String path) throws IOException;

default InputStream getResourceStream(String path) throws IOException {
return this.getResourceUrl(path).openStream();
}

default File getResourceFile(String path) throws IOException {
return new File(this.getResourceUrl(path).getFile());
default <T extends Platform<?>> Stream<T> loadPlatform(ClassLoader loader, Class<T> clazz) {
return ServiceLoader.load(clazz, loader).stream()
.map(ServiceLoader.Provider::get);
}
}
Loading
Loading