Skip to content

Commit

Permalink
1.1.0: Implement languages for this plugin
Browse files Browse the repository at this point in the history
Merge pull request #5 from KCodeYT/develop
  • Loading branch information
KCodeYT authored May 28, 2022
2 parents 20d957d + a11e2e9 commit 30d0092
Show file tree
Hide file tree
Showing 12 changed files with 403 additions and 75 deletions.
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

<groupId>de.kcodeyt</groupId>
<artifactId>HeadsDatabase</artifactId>
<version>1.0.1</version>
<version>1.1.0</version>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>8</source>
<target>8</target>
Expand Down Expand Up @@ -48,7 +49,7 @@
<dependency>
<groupId>de.kcodeyt</groupId>
<artifactId>Heads</artifactId>
<version>1.0.0</version>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>cn.nukkit</groupId>
Expand Down
48 changes: 47 additions & 1 deletion src/main/java/de/kcodeyt/headsdb/HeadsDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,27 @@
package de.kcodeyt.headsdb;

import cn.nukkit.plugin.PluginBase;
import cn.nukkit.utils.Config;
import de.kcodeyt.headsdb.command.HeadDBCommand;
import de.kcodeyt.headsdb.database.Database;
import de.kcodeyt.headsdb.lang.Language;
import lombok.Getter;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

@Getter
public class HeadsDB extends PluginBase {

private static final String DEFAULT_LANGUAGE = "en_US";

public static final String MHF_QUESTION_TEXTURE_ID = "d34e063cafb467a5c8de43ec78619399f369f4a52434da8017a983cdd92516a0";

private final Database database = new Database();
private final Database database = new Database(this);

private Language language;

@Override
public void onEnable() {
Expand All @@ -39,6 +50,41 @@ public void onEnable() {
this.getLogger().info("Successfully load " + this.database.getHeadEntries().size() + " Heads!");
}
});

this.saveResource("config.yml");
final Config config = this.getConfig();

if(!config.exists("default_lang")) {
config.set("default_lang", DEFAULT_LANGUAGE);
config.save();
}

final File langDir = new File(this.getDataFolder(), "lang");
final File[] files = langDir.listFiles();
if(!langDir.exists() || files == null || files.length == 0) {
if(!langDir.exists() && !langDir.mkdirs()) {
this.getLogger().error("Could not create the language directory for this plugin!");
return;
}

try(final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(this.getResource("lang/lang_list.txt")))) {
String line;
while((line = bufferedReader.readLine()) != null)
this.saveResource("lang/" + line + ".txt");
} catch(Exception e) {
this.getLogger().error("Could not find the language resources of this plugin!", e);
return;
}
}

try {
final String defaultLang = config.getString("default_lang");

this.language = new Language(langDir, defaultLang);
this.getLogger().info("This plugin is using the " + this.language.getDefaultLang() + " as default language file!");
} catch(IOException e) {
this.getLogger().error(e.getMessage(), e);
}
}

@Override
Expand Down
49 changes: 30 additions & 19 deletions src/main/java/de/kcodeyt/headsdb/command/HeadDBCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
import cn.nukkit.form.window.FormWindowCustom;
import de.kcodeyt.headsdb.HeadsDB;
import de.kcodeyt.headsdb.database.HeadEntry;
import de.kcodeyt.headsdb.lang.Language;
import de.kcodeyt.headsdb.lang.TranslationKey;
import de.kcodeyt.headsdb.util.FormAPI;

import java.io.IOException;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
Expand All @@ -42,21 +45,31 @@ public HeadDBCommand(HeadsDB headsDB) {

@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
if(!this.testPermission(sender))
return false;
if(!this.testPermission(sender)) return false;

final Language language = this.headsDB.getLanguage();

if(args.length > 0) {
final String subCommand = args[0].toLowerCase();
switch(subCommand) {
case "reload":
if(sender.isOp()) {
sender.sendMessage("§7Reload the heads database...");
sender.sendMessage(language.translate(sender instanceof Player ? (Player) sender : null, TranslationKey.DATABASE_RELOAD_START));

try {
this.headsDB.getLanguage().reload();
} catch(IOException e) {
this.headsDB.getLogger().error("Error while reloading language file", e);
sender.sendMessage(language.translate(sender instanceof Player ? (Player) sender : null, TranslationKey.DATABASE_RELOAD_FAILED));
return true;
}

this.headsDB.getDatabase().reload().whenComplete((result, error) -> {
if(!result || error != null) {
this.headsDB.getLogger().error("Could not load heads database!", error);
sender.sendMessage("§cCould not reload the database!");
sender.sendMessage(language.translate(sender instanceof Player ? (Player) sender : null, TranslationKey.DATABASE_RELOAD_FAILED));
} else {
sender.sendMessage("§aSuccessfully reload the head database!");
sender.sendMessage(language.translate(sender instanceof Player ? (Player) sender : null, TranslationKey.DATABASE_RELOAD_SUCCESS));
this.headsDB.getLogger().info("Successfully load " + this.headsDB.getDatabase().getHeadEntries().size() + " Heads!");
}
});
Expand All @@ -65,8 +78,7 @@ public boolean execute(CommandSender sender, String commandLabel, String[] args)
break;
case "random":
if(sender.isOp()) {
if(this.isConsole(sender))
return false;
if(this.isConsole(sender)) return false;

int amount;
try {
Expand All @@ -83,38 +95,37 @@ public boolean execute(CommandSender sender, String commandLabel, String[] args)
final Random random = ThreadLocalRandom.current();
for(int i = 0; i < amount; i++)
this.headsDB.getDatabase().giveItem((Player) sender, headEntries.get(random.nextInt(headEntries.size())));

sender.sendMessage(language.translate((Player) sender, TranslationKey.GAVE_RANDOM_HEADS, amount));
return true;
}
break;
case "config":
if(this.isConsole(sender))
return false;
if(this.isConsole(sender)) return false;

final Player player = (Player) sender;
final FormWindowCustom configForm = new FormWindowCustom("Configure");
configForm.addElement(new ElementSlider("Page length", 20, 120, 5, 40));
final FormWindowCustom configForm = new FormWindowCustom(language.translate(player, TranslationKey.CONFIG_FORM_TITLE));
configForm.addElement(new ElementSlider(language.translate(player, TranslationKey.CONFIG_FORM_PAGE_SIZE), 20, 120, 5, 40));
FormAPI.create(player, configForm, () -> {
if(configForm.wasClosed())
return;
if(configForm.wasClosed()) return;

final int pageLength = (int) configForm.getResponse().getSliderResponse(0);
this.headsDB.getDatabase().getPageCount().put(player.getName(), pageLength);
player.sendMessage("§aSet your page length to " + pageLength + "!");
player.sendMessage(language.translate(player, TranslationKey.CONFIG_FORM_PAGE_SIZE_CHANGED, pageLength));
});
return true;
}
}

if(this.isConsole(sender))
return false;
if(this.isConsole(sender)) return false;

this.headsDB.getDatabase().showForm((Player) sender);
return true;
}

private boolean isConsole(CommandSender sender) {
if(sender instanceof Player)
return false;
sender.sendMessage("You must be logged in, to be able to use this command!");
if(sender instanceof Player) return false;
sender.sendMessage(this.headsDB.getLanguage().translate(null, TranslationKey.CONSOLE_USES_PLAYER_COMMAND));
return true;
}

Expand Down
15 changes: 5 additions & 10 deletions src/main/java/de/kcodeyt/headsdb/database/Category.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,15 @@

package de.kcodeyt.headsdb.database;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import lombok.Value;

import java.util.List;

@Getter
@ToString
@RequiredArgsConstructor
@Value
class Category {

private final CategoryEnum categoryEnum;
private final String displayName;
private final String displaySkin;
private final List<HeadEntry> entries;
CategoryEnum categoryEnum;
String displaySkin;
List<HeadEntry> entries;

}
24 changes: 13 additions & 11 deletions src/main/java/de/kcodeyt/headsdb/database/CategoryEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,27 @@

package de.kcodeyt.headsdb.database;

import de.kcodeyt.headsdb.lang.TranslationKey;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum CategoryEnum {

ALPHABET("Alphabet", "alphabet"),
ANIMALS("Animals", "animals"),
BLOCKS("Blocks", "blocks"),
DECORATION("Decoration", "decoration"),
FOOD_DRINKS("Food & Drinks", "food-drinks"),
HUMANS("Humans", "humans"),
HUMANOID("Humanoid", "humanoid"),
MISCELLANEOUS("Miscellaneous", "miscellaneous"),
MONSTERS("Monsters", "monsters"),
PLANTS("Plants", "plants");
ALPHABET(TranslationKey.CATEGORY_ALPHABET, TranslationKey.CATEGORY_ALPHABET_TITLE, "alphabet"),
ANIMALS(TranslationKey.CATEGORY_ANIMALS, TranslationKey.CATEGORY_ANIMALS_TITLE, "animals"),
BLOCKS(TranslationKey.CATEGORY_BLOCKS, TranslationKey.CATEGORY_BLOCKS_TITLE, "blocks"),
DECORATION(TranslationKey.CATEGORY_DECORATION, TranslationKey.CATEGORY_DECORATION_TITLE, "decoration"),
FOOD_DRINKS(TranslationKey.CATEGORY_FOOD_DRINKS, TranslationKey.CATEGORY_FOOD_DRINKS_TITLE, "food-drinks"),
HUMANS(TranslationKey.CATEGORY_HUMANS, TranslationKey.CATEGORY_HUMANS_TITLE, "humans"),
HUMANOID(TranslationKey.CATEGORY_HUMANOID, TranslationKey.CATEGORY_HUMANOID_TITLE, "humanoid"),
MISCELLANEOUS(TranslationKey.CATEGORY_MISCELLANEOUS, TranslationKey.CATEGORY_MISCELLANEOUS_TITLE, "miscellaneous"),
MONSTERS(TranslationKey.CATEGORY_MONSTERS, TranslationKey.CATEGORY_MONSTERS_TITLE, "monsters"),
PLANTS(TranslationKey.CATEGORY_PLANTS, TranslationKey.CATEGORY_PLANTS_TITLE, "plants");

private final String displayName;
private final TranslationKey buttonTranslationKey;
private final TranslationKey titleTranslationKey;
private final String identifier;

}
Loading

0 comments on commit 30d0092

Please sign in to comment.