Skip to content

Commit

Permalink
feat: add translatable messages and configuration
Browse files Browse the repository at this point in the history
add documentation
clean up code
  • Loading branch information
nikosgram committed Sep 4, 2022
1 parent 98494b6 commit 5b57811
Show file tree
Hide file tree
Showing 15 changed files with 258 additions and 64 deletions.
47 changes: 46 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,46 @@
# gringotts-towny
# gringotts-towny

Gringotts-Towny allows the creation of [Gringotts](https://github.com/nikosgram/gringotts) vaults and account
for [Towny](https://www.spigotmc.org/resources/towny-advanced.72694/)'s nations and towns.

## Configuration

```yaml
# supported languages: "custom" (default, english), "de" (german), and "pt-BR" (brazilian portuguese).
language: custom

# changes the required sign name for the creation of town/nation vaults
# Example: [town vault]
town_sign_vault_name: 'town'
nation_sign_vault_name: 'nation'
```
## Permissions
`gringotts.createvault.town`
---
Gives the necessary permissions to the players to create vault for their towns.

`gringotts.createvault.nation`
---
Gives the necessary permissions to the players to create vault for their nations.


Development
-----------
Would you like to make changes to Gringotts Towny yourself? Fork it!
Pull requests are very welcome, but please make sure your changes fulfill the Gringotts Towny quality baseline:

* new features, settings, permissions are documented
* required dependencies are all added to the build by Maven, not included in the repo
* the project builds with Maven out-of-the-box

Gringotts uses the [Maven 3](http://maven.apache.org/) build system. Build a working plugin jar with the command

```shell
mvn compile install
```

License
-------
All code within Gringotts is licensed under the BSD 2-clause license. See `LICENSE` for details.
53 changes: 50 additions & 3 deletions src/main/java/com/oglofus/gringotts/towny/GringottsTowny.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package com.oglofus.gringotts.towny;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.gestern.gringotts.Gringotts;
import org.gestern.gringotts.dependency.towny.TownyDependency;

import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;

/**
* The type Gringotts towny.
*/
public class GringottsTowny extends JavaPlugin {
private static final String DEPENDENCY_ID = "gringotts-towny";
@SuppressWarnings("unused") public class GringottsTowny extends JavaPlugin {
private static final String MESSAGES_YML = "messages.yml";

@Override
public void onLoad() {
Expand All @@ -26,6 +33,10 @@ public void onLoad() {
} catch (IllegalArgumentException e) {
getLogger().warning("Looks like Towny plugin is not compatible with Gringotts");
}

// load and init configuration
saveDefaultConfig(); // saves default configuration if no config.yml exists yet
reloadConfig();
}

@Override
Expand All @@ -37,4 +48,40 @@ public void onEnable() {
public void onDisable() {

}

/**
* Reload config.
* <p>
* override to handle custom config logic and language loading
*/
@Override
public void reloadConfig() {
super.reloadConfig();
TownyConfiguration.CONF.readConfig(getConfig());
TownyLanguage.LANG.readLanguage(getMessages());
}

/**
* Get the configured player interaction messages.
*
* @return the configured player interaction messages
*/
public FileConfiguration getMessages() {
String langPath = String.format("i18n/messages_%s.yml", TownyConfiguration.CONF.language);

// try configured language first
InputStream langStream = getResource(langPath);
FileConfiguration conf;

if (langStream != null) {
Reader langReader = new InputStreamReader(langStream, StandardCharsets.UTF_8);
conf = YamlConfiguration.loadConfiguration(langReader);
} else {
// use custom/default
File langFile = new File(getDataFolder(), MESSAGES_YML);
conf = YamlConfiguration.loadConfiguration(langFile);
}

return conf;
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/oglofus/gringotts/towny/TownyConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.oglofus.gringotts.towny;

import org.bukkit.configuration.file.FileConfiguration;

public enum TownyConfiguration {
CONF;

/**
* Language to be used for messages. Should be an ISO 639-1 (alpha-2) code.
* If a language is not supported by Gringotts, use user-configured or default (English) messages.
*/
public String language = "custom";
public String townSignTypeName = "town";
public String nationSignTypeName = "nation";

public void readConfig(FileConfiguration savedConfig) {
CONF.language = savedConfig.getString("language", "custom");
CONF.townSignTypeName = savedConfig.getString("town_sign_type_name", "town");
CONF.nationSignTypeName = savedConfig.getString("nation_sign_type_name", "nation");
}
}
79 changes: 37 additions & 42 deletions src/main/java/com/oglofus/gringotts/towny/TownyDependency.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.oglofus.gringotts.towny;

import com.oglofus.gringotts.towny.nation.NationAccountHolder;
import com.oglofus.gringotts.towny.nation.NationHolderProvider;
import com.oglofus.gringotts.towny.town.TownAccountHolder;
import com.oglofus.gringotts.towny.town.TownHolderProvider;
import com.palmergames.bukkit.towny.Towny;
import com.palmergames.bukkit.towny.TownyUniverse;
Expand All @@ -10,12 +12,10 @@
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
import org.gestern.gringotts.Gringotts;
import org.gestern.gringotts.Language;
import org.gestern.gringotts.Permissions;
import org.gestern.gringotts.accountholder.AccountHolder;
import org.gestern.gringotts.api.dependency.Dependency;
import org.gestern.gringotts.event.PlayerVaultCreationEvent;
import org.gestern.gringotts.event.VaultCreationEvent;

/**
* The type Towny dependency.
Expand Down Expand Up @@ -80,9 +80,8 @@ public void onEnable() {
Bukkit.getPluginManager().registerEvents(this.townHolderProvider, this.gringotts);
Bukkit.getPluginManager().registerEvents(this.nationHolderProvider, this.gringotts);

Gringotts.instance.registerAccountHolderProvider(VaultCreationEvent.Type.TOWN.getId(), this.townHolderProvider);
Gringotts.instance.registerAccountHolderProvider(VaultCreationEvent.Type.NATION.getId(),
this.nationHolderProvider);
Gringotts.instance.registerAccountHolderProvider(TownAccountHolder.ACCOUNT_TYPE, this.townHolderProvider);
Gringotts.instance.registerAccountHolderProvider(NationAccountHolder.ACCOUNT_TYPE, this.nationHolderProvider);
}

/**
Expand All @@ -109,60 +108,56 @@ public void vaultCreated(PlayerVaultCreationEvent event) {

AccountHolder owner;

switch (event.getType()) {
case TOWN -> {
if (!Permissions.CREATE_VAULT_TOWN.isAllowed(player)) {
player.sendMessage(Language.LANG.plugin_towny_noTownVaultPerm);
if (event.getType().equals(TownyConfiguration.CONF.townSignTypeName)) {
if (!TownyPermissions.CREATE_VAULT_TOWN.isAllowed(player)) {
player.sendMessage(TownyLanguage.LANG.noTownVaultPerm);

return;
}

if (forOther) {
owner = this.townHolderProvider.getAccountHolder(TownyUniverse.getInstance().getTown(line2String));
return;
}

if (owner == null) {
return;
}
} else {
owner = this.townHolderProvider.getAccountHolder(player);
}
if (forOther) {
owner = this.townHolderProvider.getAccountHolder(TownyUniverse.getInstance().getTown(line2String));

if (owner == null) {
player.sendMessage(Language.LANG.plugin_towny_noTownResident);

return;
}
} else {
owner = this.townHolderProvider.getAccountHolder(player);
}

if (owner == null) {
player.sendMessage(TownyLanguage.LANG.noTownResident);

event.setOwner(owner);
event.setValid(true);
return;
}
case NATION -> {
if (!Permissions.CREATE_VAULT_NATION.isAllowed(player)) {
player.sendMessage(Language.LANG.plugin_towny_noNationVaultPerm);

return;
}
event.setOwner(owner);
event.setValid(true);
} else if (event.getType().equals(TownyConfiguration.CONF.nationSignTypeName)) {
if (!TownyPermissions.CREATE_VAULT_NATION.isAllowed(player)) {
player.sendMessage(TownyLanguage.LANG.noNationVaultPerm);

if (forOther) {
owner = this.nationHolderProvider.getAccountHolder(
TownyUniverse.getInstance().getNation(line2String));
return;
}

if (owner == null) {
return;
}
} else {
owner = this.nationHolderProvider.getAccountHolder(player);
}
if (forOther) {
owner = this.nationHolderProvider.getAccountHolder(TownyUniverse.getInstance().getNation(line2String));

if (owner == null) {
player.sendMessage(Language.LANG.plugin_towny_notInNation);

return;
}
} else {
owner = this.nationHolderProvider.getAccountHolder(player);
}

event.setOwner(owner);
event.setValid(true);
if (owner == null) {
player.sendMessage(TownyLanguage.LANG.notInNation);

return;
}

event.setOwner(owner);
event.setValid(true);
}
}
}
31 changes: 31 additions & 0 deletions src/main/java/com/oglofus/gringotts/towny/TownyLanguage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.oglofus.gringotts.towny;

import org.bukkit.configuration.file.FileConfiguration;

import java.util.function.BiFunction;

import static org.gestern.gringotts.Util.translateColors;

public enum TownyLanguage {
LANG;
public String noTownVaultPerm;
public String noTownResident;
public String noNationVaultPerm;
public String notInNation;

public void readLanguage(FileConfiguration savedLanguage) {
BiFunction<String, String, String> translator =
(path, def) -> translateColors(savedLanguage.getString(path, def));

//towny plugin
LANG.noTownVaultPerm =
translator.apply("noTownPerm", "You do not have permission to create town vaults here.");
LANG.noTownResident =
translator.apply("noTownResident", "Cannot create town vault: You are not resident of a town.");
LANG.noNationVaultPerm =
translator.apply("NoNationVaultPerm", "You do not have permission to create nation vaults here.");
LANG.notInNation =
translator.apply("notInNation", "Cannot create nation vault: You do not belong to a nation.");

}
}
37 changes: 37 additions & 0 deletions src/main/java/com/oglofus/gringotts/towny/TownyPermissions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.oglofus.gringotts.towny;

import org.bukkit.entity.Player;

/**
* The Permissions.
*/
public enum TownyPermissions {
/**
* Create vault town permissions.
*/
CREATE_VAULT_TOWN("gringotts.createvault.town"),
/**
* Create vault nation permissions.
*/
CREATE_VAULT_NATION("gringotts.createvault.nation");

/**
* The Node.
*/
public final String node;

TownyPermissions(String node) {
this.node = node;
}

/**
* Check if a player has this permission.
*
* @param player player to check
* @return whether given player has this permission
*/
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
public boolean isAllowed(Player player) {
return player.hasPermission(this.node);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
import com.palmergames.bukkit.towny.TownyAPI;
import com.palmergames.bukkit.towny.object.Nation;
import org.gestern.gringotts.accountholder.AccountHolder;
import org.gestern.gringotts.event.VaultCreationEvent;

/**
* The type Nation account holder.
*/
public class NationAccountHolder implements AccountHolder {
private final VaultCreationEvent.Type type = VaultCreationEvent.Type.NATION;
private final Nation nation;
public static final String ACCOUNT_TYPE = "nation";
private final Nation nation;

/**
* Instantiates a new Nation account holder.
Expand Down Expand Up @@ -48,7 +47,7 @@ public void sendMessage(String message) {
*/
@Override
public String getType() {
return this.type.getId();
return ACCOUNT_TYPE;
}

/**
Expand Down
Loading

0 comments on commit 5b57811

Please sign in to comment.