Skip to content

Commit

Permalink
Add build id & hash in compiled jar
Browse files Browse the repository at this point in the history
Use VersionConfig from FancyLib
  • Loading branch information
OliverSchlueter committed Sep 10, 2023
1 parent 5e8837d commit 0380b98
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 112 deletions.
14 changes: 13 additions & 1 deletion api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,19 @@ plugins {
dependencies {
compileOnly("io.papermc.paper:paper-api:1.20.1-R0.1-SNAPSHOT")

compileOnly("de.oliver:FancyLib:1.0.4")
compileOnly("de.oliver:FancyLib:1.0.5.1")

compileOnly("me.clip:placeholderapi:2.11.3")
}

tasks {
javadoc {
options.encoding = Charsets.UTF_8.name()
}

compileJava {
options.encoding = Charsets.UTF_8.name()

options.release.set(17)
}
}
29 changes: 27 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import java.io.BufferedReader
import java.io.InputStreamReader

plugins {
id("java-library")
id("maven-publish")
Expand Down Expand Up @@ -30,7 +33,7 @@ dependencies {
implementation(project(":implementation_1_20", configuration = "reobf"))
implementation(project(":implementation_1_19_4", configuration = "reobf"))

implementation("de.oliver:FancyLib:1.0.4")
implementation("de.oliver:FancyLib:1.0.5.1")

compileOnly("me.clip:placeholderapi:2.11.3")
compileOnly("com.intellectualsites.plotsquared:plotsquared-core:7.0.0")
Expand Down Expand Up @@ -91,17 +94,39 @@ tasks {

processResources {
filteringCharset = Charsets.UTF_8.name() // We want UTF-8 for everything

val props = mapOf(
"version" to project.version,
"description" to project.description,
"version" to project.version,
"hash" to getCurrentCommitHash(),
"build" to (System.getenv("BUILD_ID") ?: "").ifEmpty { "undefined" }
)

inputs.properties(props)

filesMatching("plugin.yml") {
expand(props)
}

filesMatching("version.yml") {
expand(props)
}
}
}

java {
toolchain.languageVersion.set(JavaLanguageVersion.of(17))
}

fun getCurrentCommitHash(): String {
val process = ProcessBuilder("git", "rev-parse", "HEAD").start()
val reader = BufferedReader(InputStreamReader(process.inputStream))
val commitHash = reader.readLine()
reader.close()
process.waitFor()
if (process.exitValue() == 0) {
return commitHash ?: ""
} else {
throw IllegalStateException("Failed to retrieve the commit hash.")
}
}
2 changes: 1 addition & 1 deletion implementation_1_19_4/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependencies {
paperweight.paperDevBundle("$minecraftVersion-R0.1-SNAPSHOT")

implementation(project(":api"))
implementation("de.oliver:FancyLib:1.0.4")
implementation("de.oliver:FancyLib:1.0.5.1")
compileOnly("me.clip:placeholderapi:2.11.3")
}

Expand Down
2 changes: 1 addition & 1 deletion implementation_1_20/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependencies {
paperweight.paperDevBundle("$minecraftVersion-R0.1-SNAPSHOT")

implementation(project(":api"))
implementation("de.oliver:FancyLib:1.0.4")
implementation("de.oliver:FancyLib:1.0.5.1")
compileOnly("me.clip:placeholderapi:2.11.3")
}

Expand Down
2 changes: 1 addition & 1 deletion implementation_1_20_1/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependencies {
paperweight.paperDevBundle("$minecraftVersion-R0.1-SNAPSHOT")

implementation(project(":api"))
implementation("de.oliver:FancyLib:1.0.4")
implementation("de.oliver:FancyLib:1.0.5.1")
compileOnly("me.clip:placeholderapi:2.11.3")
}

Expand Down
79 changes: 12 additions & 67 deletions src/main/java/de/oliver/fancynpcs/FancyNpcs.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package de.oliver.fancynpcs;

import de.oliver.fancylib.FancyLib;
import de.oliver.fancylib.LanguageConfig;
import de.oliver.fancylib.Metrics;
import de.oliver.fancylib.*;
import de.oliver.fancylib.featureFlags.FeatureFlag;
import de.oliver.fancylib.featureFlags.FeatureFlagConfig;
import de.oliver.fancylib.serverSoftware.ServerSoftware;
Expand Down Expand Up @@ -34,13 +32,6 @@
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.function.Function;

public class FancyNpcs extends JavaPlugin implements FancyNpcsPlugin {
Expand All @@ -52,6 +43,7 @@ public class FancyNpcs extends JavaPlugin implements FancyNpcsPlugin {
private final FancyScheduler scheduler;
private final FancyNpcConfig config;
private final LanguageConfig languageConfig;
private final VersionConfig versionConfig;
private final FeatureFlagConfig featureFlagConfig;
private final VersionFetcher versionFetcher;
private Function<NpcData, Npc> npcAdapter;
Expand All @@ -66,9 +58,10 @@ public FancyNpcs() {
? new FoliaScheduler(instance)
: new BukkitScheduler(instance);
this.config = new FancyNpcConfig();
this.versionFetcher = new MasterVersionFetcher(getName());
this.languageConfig = new LanguageConfig(this);
this.versionConfig = new VersionConfig(this, versionFetcher);
this.featureFlagConfig = new FeatureFlagConfig(this);
this.versionFetcher = new MasterVersionFetcher(getName());
}

public static FancyNpcs getInstance() {
Expand Down Expand Up @@ -104,7 +97,7 @@ public void onLoad() {
return;
}

saveFile("lang.yml");
new FileUtils().saveFile(this, "lang.yml");
}

@Override
Expand All @@ -121,7 +114,7 @@ public void onEnable() {
attributeManager = new AttributeManagerImpl();

// Load language file
String defaultLang = readResource("lang.yml");
String defaultLang = new FileUtils().readResource("lang.yml");
if (defaultLang != null) {
// Update language file
try {
Expand All @@ -136,6 +129,8 @@ public void onEnable() {
}
languageConfig.load();

versionConfig.load();

new Thread(() -> {
ComparableVersion newestVersion = versionFetcher.fetchNewestVersion();
ComparableVersion currentVersion = new ComparableVersion(getDescription().getVersion());
Expand Down Expand Up @@ -208,60 +203,6 @@ public void onDisable() {
}
}

private String readResource(String name) {
URL url = getClass().getClassLoader().getResource(name);
if (url == null) {
getLogger().severe(name + " not found");
return null;
}
URLConnection connection = null;
try {
connection = url.openConnection();
} catch (IOException e) {
getLogger().severe("Failed unpack file " + name + ":" + e.getMessage());
}
connection.setUseCaches(false);
try (InputStream inputStream = connection.getInputStream()) {
byte[] file_raw = new byte[inputStream.available()];
inputStream.read(file_raw);
inputStream.close();
return new String(file_raw, StandardCharsets.UTF_8);
} catch (IOException e) {
getLogger().severe("Failed read file " + name + ":" + e.getMessage());
}
return null;
}

private void saveFile(String name) {
URL url = getClass().getClassLoader().getResource(name);
if (url == null) {
getLogger().severe(name + " not found");
return;
}
File file = new File(getDataFolder() + "/" + name);
if (file.exists()) return;
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
URLConnection connection = null;
try {
connection = url.openConnection();
} catch (IOException e) {
getLogger().severe("Failed unpack file " + name + ":" + e.getMessage());
}
connection.setUseCaches(false);
try (FileOutputStream outputStream = new FileOutputStream(file)) {
int read;
InputStream inputStream = connection.getInputStream();
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
} catch (IOException e) {
getLogger().severe("Failed unpack file " + name + ":" + e.getMessage());
}
}

@Override
public Function<NpcData, Npc> getNpcAdapter() {
return npcAdapter;
Expand Down Expand Up @@ -294,6 +235,10 @@ public LanguageConfig getLanguageConfig() {
return languageConfig;
}

public VersionConfig getVersionConfig() {
return versionConfig;
}

public FeatureFlagConfig getFeatureFlagConfig() {
return featureFlagConfig;
}
Expand Down
22 changes: 5 additions & 17 deletions src/main/java/de/oliver/fancynpcs/commands/FancyNpcsCMD.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import de.oliver.fancylib.LanguageConfig;
import de.oliver.fancylib.MessageHelper;
import de.oliver.fancynpcs.FancyNpcs;
import org.apache.maven.artifact.versioning.ComparableVersion;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
Expand Down Expand Up @@ -35,33 +34,22 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
FancyNpcs plugin = FancyNpcs.getInstance();

if (args.length >= 1 && args[0].equalsIgnoreCase("version")) {
MessageHelper.info(sender, lang.get("fetching-version"));
new Thread(() -> {
ComparableVersion newestVersion = plugin.getVersionFetcher().fetchNewestVersion();
ComparableVersion currentVersion = new ComparableVersion(FancyNpcs.getInstance().getDescription().getVersion());
if (newestVersion == null) {
MessageHelper.error(sender, lang.get("fetching-version-cancelled"));
} else if (newestVersion.compareTo(currentVersion) > 0) {
MessageHelper.warning(sender, (
lang.get("outdated-version")
+ "\n"
+ lang.get("download-newest-version", "new_version", newestVersion.toString(), "download_url", plugin.getVersionFetcher().getDownloadUrl())
));
} else {
MessageHelper.success(sender, lang.get("fetching-version-success", "current_version", currentVersion.toString()));
}
}).start();
FancyNpcs.getInstance().getVersionConfig().checkVersionAndDisplay(sender, false);

} else if (args.length >= 1 && args[0].equalsIgnoreCase("reload")) {
plugin.getLanguageConfig().load();
plugin.getFancyNpcConfig().reload();
plugin.getNpcManagerImpl().reloadNpcs();
MessageHelper.success(sender, lang.get("reloaded-config"));

} else if (args.length >= 1 && args[0].equalsIgnoreCase("save")) {
plugin.getNpcManagerImpl().saveNpcs(true);
MessageHelper.success(sender, lang.get("saved-npcs"));

} else if (args.length >= 1 && args[0].equalsIgnoreCase("featureFlags")) {
MessageHelper.info(sender, "<b>Feature flags:</b>");
MessageHelper.info(sender, " - player-npcs: " + FancyNpcs.PLAYER_NPCS_FEATURE_FLAG.isEnabled());

} else {
MessageHelper.info(sender, lang.get("fancynpcs-syntax"));
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
package de.oliver.fancynpcs.listeners;

import de.oliver.fancylib.LanguageConfig;
import de.oliver.fancylib.MessageHelper;
import de.oliver.fancynpcs.FancyNpcs;
import de.oliver.fancynpcs.api.Npc;
import de.oliver.fancynpcs.v1_19_4.PacketReader_1_19_4;
import de.oliver.fancynpcs.v1_20.PacketReader_1_20;
import org.apache.maven.artifact.versioning.ComparableVersion;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;

public class PlayerJoinListener implements Listener {

private final LanguageConfig lang = FancyNpcs.getInstance().getLanguageConfig();

@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
String mcVersion = Bukkit.getMinecraftVersion();
Expand All @@ -31,18 +26,7 @@ public void onPlayerJoin(PlayerJoinEvent event) {
}

if (!FancyNpcs.getInstance().getFancyNpcConfig().isMuteVersionNotification() && event.getPlayer().hasPermission("FancyNpcs.admin")) {
new Thread(() -> {
ComparableVersion newestVersion = FancyNpcs.getInstance().getVersionFetcher().fetchNewestVersion();
ComparableVersion currentVersion = new ComparableVersion(FancyNpcs.getInstance().getDescription().getVersion());
if (newestVersion != null && newestVersion.compareTo(currentVersion) > 0) {
MessageHelper.warning(event.getPlayer(), lang.get("outdated-version"));
MessageHelper.warning(event.getPlayer(), lang.get(
"download-newest-version",
"new_version", newestVersion.toString(),
"download_url", FancyNpcs.getInstance().getVersionFetcher().getDownloadUrl()
));
}
}).start();
FancyNpcs.getInstance().getVersionConfig().checkVersionAndDisplay(event.getPlayer(), true);
}
}
}
5 changes: 0 additions & 5 deletions src/main/resources/lang.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
messages:
outdated-version: You are using an outdated version of the FancyNpcs Plugin
download-newest-version: '[!] Please download the newest version ({new_version}): <click:open_url:''{download_url}''><u>click here</u></click>'
fetching-version: <i>Checking version, please wait...</i>
fetching-version-cancelled: Could not find latest version
fetching-version-success: You are using the latest version of the FancyNpcs Plugin ({current_version})
reloaded-config: Reloaded the config
saved-npcs: Saved all NPCs
fancynpcs-syntax: /FancyNpcs <version|reload|save>
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/version.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version: $version
build: $build
hash: $hash

0 comments on commit 0380b98

Please sign in to comment.