-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from okocraft/refactoring
Refactoring
- Loading branch information
Showing
16 changed files
with
448 additions
and
581 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 54 additions & 65 deletions
119
src/main/java/net/okocraft/boxtradestick/BoxTradeStickPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,79 @@ | ||
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 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); | ||
} | ||
import java.io.IOException; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
|
||
var loader = ConfigurationLoader.create(locale, source); | ||
loader.load(); | ||
public final class BoxTradeStickPlugin extends JavaPlugin { | ||
|
||
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; | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.