Skip to content

Commit

Permalink
Merge pull request #9 from okocraft/dev/1.20.5
Browse files Browse the repository at this point in the history
Minecraft 1.20.5 & Box v6
  • Loading branch information
Siroshun09 committed Jun 5, 2024
2 parents 445de0d + c296255 commit 75758b2
Show file tree
Hide file tree
Showing 17 changed files with 480 additions and 618 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: Build
on: [ push, pull_request ]

jobs:
build:
uses: okocraft/workflows/.github/workflows/gradle.yml@v1
with:
java-version: '21'
package-name: BoxTradeStick-Build-${{ github.run_number }}
upload-test-results: true
42 changes: 19 additions & 23 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
plugins {
java
id("io.papermc.paperweight.userdev") version "1.5.15"
id("io.github.goooler.shadow") version "8.1.7"
id("io.papermc.paperweight.userdev") version "1.7.1"
}

group = "net.okocraft.boxtradestick"
version = "1.5"
version = "1.6-SNAPSHOT"

val mcVersion = "1.20.6"
val fullVersion = "${version}-mc${mcVersion}"

repositories {
mavenCentral()
Expand All @@ -14,48 +16,42 @@ repositories {
}

dependencies {
paperweight.paperDevBundle("1.20.4-R0.1-SNAPSHOT")

implementation("com.github.siroshun09.configapi:configapi-yaml:4.6.4")
implementation("com.github.siroshun09.translationloader:translationloader:2.0.2")
paperweight.paperDevBundle("$mcVersion-R0.1-SNAPSHOT")

compileOnly("net.okocraft.box:box-api:5.5.2")
compileOnly("net.okocraft.box:box-storage-api:5.5.2")
compileOnly("net.okocraft.box:box-stick-feature:5.5.2")
compileOnly("net.okocraft.box:box-api:6.0.0-rc.1")
compileOnly("net.okocraft.box:box-gui-feature:6.0.0-rc.1")
compileOnly("net.okocraft.box:box-stick-feature:6.0.0-rc.1")

testImplementation("org.junit.jupiter:junit-jupiter-api:5.10.2")
testRuntimeOnly("io.papermc.paper:paper-api:1.20.4-R0.1-SNAPSHOT")
testRuntimeOnly("io.papermc.paper:paper-api:$mcVersion-R0.1-SNAPSHOT")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
}

paperweight.reobfArtifactConfiguration = io.papermc.paperweight.userdev.ReobfArtifactConfiguration.MOJANG_PRODUCTION

tasks {
compileJava {
options.encoding = Charsets.UTF_8.name()
options.release.set(17)
}

build {
dependsOn(reobfJar)
options.release.set(21)
}

processResources {
filesMatching(listOf("plugin.yml", "en.yml", "ja_JP.yml")) {
expand("projectVersion" to version)
filesMatching(listOf("paper-plugin.yml")) {
expand("projectVersion" to version, "apiVersion" to mcVersion)
}
}

test {
useJUnitPlatform()
}

shadowJar {
minimize()
relocate("com.github.siroshun09", "net.okocraft.boxtradestick.lib")
jar {
archiveFileName = "BoxTradeStick-$fullVersion.jar"
}
}
114 changes: 51 additions & 63 deletions src/main/java/net/okocraft/boxtradestick/BoxTradeStickPlugin.java
Original file line number Diff line number Diff line change
@@ -1,90 +1,78 @@
package net.okocraft.boxtradestick;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Locale;
import java.util.jar.JarFile;
import java.util.logging.Level;
import net.kyori.adventure.key.Key;
import com.github.siroshun09.configapi.api.Configuration;
import com.github.siroshun09.configapi.api.util.ResourceUtils;
import com.github.siroshun09.configapi.yaml.YamlConfiguration;
import com.github.siroshun09.translationloader.ConfigurationLoader;
import com.github.siroshun09.translationloader.TranslationLoader;
import com.github.siroshun09.translationloader.directory.TranslationDirectory;
import java.util.Map;
import com.github.siroshun09.messages.api.directory.DirectorySource;
import com.github.siroshun09.messages.api.directory.MessageProcessors;
import com.github.siroshun09.messages.api.source.StringMessageMap;
import com.github.siroshun09.messages.api.util.PropertiesFile;
import com.github.siroshun09.messages.minimessage.localization.MiniMessageLocalization;
import com.github.siroshun09.messages.minimessage.source.MiniMessageSource;
import net.okocraft.box.api.BoxAPI;
import net.okocraft.box.feature.stick.StickFeature;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public final class BoxTradeStickPlugin extends JavaPlugin {

private final Path jarFile;

private final TranslationDirectory translationDirectory;

public BoxTradeStickPlugin() {
this.jarFile = Paths.get(getClass().getProtectionDomain().getCodeSource().getLocation().getPath());

Path pluginDirectory = getDataFolder().toPath();

this.translationDirectory =
TranslationDirectory.newBuilder()
.setDirectory(pluginDirectory.resolve("languages"))
.setKey(Key.key("boxtradestick", "languages"))
.setDefaultLocale(Locale.ENGLISH)
.onDirectoryCreated(this::saveDefaultLanguages)
.setVersion(getPluginMeta().getVersion()) // getPluginMeta never returns null
.setTranslationLoaderCreator(this::getBundledTranslation)
.build();
}

private void saveDefaultLanguages(@NotNull Path directory) throws IOException {
var english = "en.yml";
ResourceUtils.copyFromJarIfNotExists(jarFile, english, directory.resolve(english));

var japanese = "ja_JP.yml";
ResourceUtils.copyFromJarIfNotExists(jarFile, japanese, directory.resolve(japanese));
}

private @Nullable TranslationLoader getBundledTranslation(@NotNull Locale locale) throws IOException {
var strLocale = locale.toString();

if (!(strLocale.equals("en") || strLocale.equals("ja_JP"))) {
return null;
}

Configuration source;

try (var jar = new JarFile(getFile());
var input = ResourceUtils.getInputStreamFromJar(jar, strLocale + ".yml")) {
source = YamlConfiguration.loadFromInputStream(input);
}

var loader = ConfigurationLoader.create(locale, source);
loader.load();

return loader;
}
private MiniMessageLocalization localization;

@Override
public void onLoad() {

try {
translationDirectory.load();
this.loadMessages();
} catch (IOException e) {
getLogger().log(Level.SEVERE, "Could not load languages", e);
this.getSLF4JLogger().error("Could not load languages.", e);
}
}

@Override
public void onEnable() {
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
if (!BoxAPI.isLoaded()) {
this.getSLF4JLogger().error("Box is not loaded. All features of BoxTradeStick will not be working.");
return;
}

BoxAPI.api().getFeatureProvider().getFeature(StickFeature.class)
.map(StickFeature::getBoxStickItem)
.map(stick -> new PlayerListener(stick, this.localization))
.ifPresentOrElse(
listener -> this.getServer().getPluginManager().registerEvents(listener, this),
() -> this.getSLF4JLogger().error("Failed to get the Box Stick from Box.")
);
}

@Override
public void onDisable() {
translationDirectory.unload();
this.getServer().getOnlinePlayers().forEach(player -> {
if (MerchantRecipesGUI.fromInventory(player.getOpenInventory().getTopInventory()) != null) {
player.closeInventory();
}
});
}

private void loadMessages() throws IOException {
if (this.localization == null) { // on startup
this.localization = new MiniMessageLocalization(MiniMessageSource.create(StringMessageMap.create(Languages.defaultMessages())), Languages::getLocaleFrom);
} else { // on reload
this.localization.clearSources();
}

DirectorySource.propertiesFiles(this.getDataFolder().toPath().resolve("languages"))
.defaultLocale(Locale.ENGLISH, Locale.JAPANESE)
.messageProcessor(MessageProcessors.appendMissingMessagesToPropertiesFile(this::loadDefaultMessageMap))
.load(loaded -> this.localization.addSource(loaded.locale(), MiniMessageSource.create(loaded.messageSource())));
}

private @Nullable Map<String, String> loadDefaultMessageMap(@NotNull Locale locale) throws IOException {
if (locale.equals(Locale.ENGLISH)) {
return Languages.defaultMessages();
} else {
try (var input = this.getResource(locale + ".properties")) {
return input != null ? PropertiesFile.load(input) : null;
}
}
}
}
116 changes: 0 additions & 116 deletions src/main/java/net/okocraft/boxtradestick/BoxUtil.java

This file was deleted.

Loading

0 comments on commit 75758b2

Please sign in to comment.