Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #1

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
# https://github.com/takari/maven-wrapper#usage-without-binary-jar
.mvn/wrapper/maven-wrapper.jar

# Eclipse m2e generated files
# Eclipse Core
.project
# JDT-specific (Eclipse Java Development Tools)
.classpath
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"java.compile.nullAnalysis.mode": "disabled",
"java.configuration.updateBuildConfiguration": "automatic"
}
19 changes: 13 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>

<groupId>net.laboulangerie</groupId>
<artifactId>gringottslands</artifactId>
<artifactId>gringotts-lands</artifactId>
<version>1.0-SNAPSHOT</version>

<name>GringottsLands</name>
Expand All @@ -18,7 +18,7 @@
<maven.compiler.target>17</maven.compiler.target>
</properties>
<build>
<finalName>GringottsLands</finalName>
<finalName>gringotts-lands</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
Expand All @@ -42,13 +42,20 @@
<dependency>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.20.2-R0.1-SNAPSHOT</version>
<version>1.21.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.LaBoulangerie</groupId>
<artifactId>Gringotts</artifactId>
<version>2.12.2</version>
<groupId>minecraftwars</groupId>
<artifactId>gringotts</artifactId>
<version>3.0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.angeschossen</groupId>
<artifactId>LandsAPI</artifactId>
<version>7.10.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
17 changes: 0 additions & 17 deletions src/main/java/net/laboulangerie/gringottslands/GringottsLand.java

This file was deleted.

101 changes: 101 additions & 0 deletions src/main/java/net/laboulangerie/gringottslands/GringottsLands.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package net.laboulangerie.gringottslands;

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.api.dependency.Dependency;

import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.logging.Level;
import java.util.logging.Logger;

public class GringottsLands extends JavaPlugin {

public static GringottsLands instance;
public static Logger LOGGER;

private static final String MESSAGES_YML = "messages.yml";

private LandsDependency landsDependency;

public GringottsLands() {
instance = this;
}

@Override
public void onLoad() {
try {
Plugin lands = Gringotts.instance.getDependencies()
.hookPlugin("Lands", "me.angeschossen.lands.Lands", "7.9.5");

this.landsDependency = new LandsDependency(lands);

if (lands != null && Gringotts.instance.getDependencies()
.registerDependency(this.landsDependency)) {
getLogger().warning("Lands plugin is already assigned into the dependencies.");
}
} catch (IllegalArgumentException e) {
getLogger().warning("Looks like Lands plugin is not compatible with Gringotts");
this.onDisable();
return;
}

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

LOGGER = this.getLogger();
if (LandsConfiguration.CONF.debug) {
LOGGER.setLevel(Level.ALL);
}

Gringotts.instance.getDependencies().getDependency("lands").ifPresent(Dependency::onLoad);
}

@Override
public void onEnable() {
this.landsDependency.checkLandBalanceConsistency();
}

/**
* Reload config.
* <p>
* override to handle custom config logic and language loading
*/
@Override
public void reloadConfig() {
super.reloadConfig();
LandsConfiguration.CONF.readConfig(getConfig());
LandsLanguage.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", LandsConfiguration.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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package net.laboulangerie.gringottslands;

import org.bukkit.configuration.file.FileConfiguration;

public enum LandsConfiguration {
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 landSignTypeName = "land";

public int maxLandVaults = -1;

public boolean vaultsOnlyInLands = false;
public long landStartBalance = 0;

public boolean debug = false;

public void readConfig(FileConfiguration savedConfig) {
CONF.language = savedConfig.getString("language", "custom");
CONF.landSignTypeName = savedConfig.getString("land_sign_type_name", "land");
CONF.maxLandVaults = savedConfig.getInt("max_land_vaults", -1);
CONF.vaultsOnlyInLands = savedConfig.getBoolean("vaults_only_in_lands", false);
CONF.landStartBalance = savedConfig.getLong("land_start_balance", 0);
CONF.debug = savedConfig.getBoolean("debug", false);
}
}
141 changes: 141 additions & 0 deletions src/main/java/net/laboulangerie/gringottslands/LandsDependency.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package net.laboulangerie.gringottslands;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
import org.gestern.gringotts.Configuration;
import org.gestern.gringotts.Gringotts;
import org.gestern.gringotts.GringottsAccount;
import org.gestern.gringotts.accountholder.AccountHolder;
import org.gestern.gringotts.api.dependency.Dependency;
import org.gestern.gringotts.event.PlayerVaultCreationEvent;

import me.angeschossen.lands.api.LandsIntegration;
import me.angeschossen.lands.api.land.Land;
import net.laboulangerie.gringottslands.land.LandAccountHolder;
import net.laboulangerie.gringottslands.land.LandHolderProvider;

public class LandsDependency implements Dependency, Listener {
private final LandHolderProvider landHolderProvider;
private final Plugin lands;
private final String id;
private final LandsIntegration api;

/**
* Instantiates a new Lands dependency.
*
* @param lands the plugin
*/
public LandsDependency(Plugin lands) {
if (lands == null) {
throw new NullPointerException("'lands' is null");
}

this.lands = lands;
this.id = "lands";

this.api = LandsIntegration.of(lands);
this.landHolderProvider = new LandHolderProvider(this.api);

}

/**
* Gets id.
*
* @return the id
*/
@Override
public String getId() {
return id;
}

/**
* Gets plugin.
*
* @return the plugin
*/
@Override
public Plugin getPlugin() {
return this.lands;
}

/**
* On enable.
*/
@Override
public void onEnable() {
Bukkit.getPluginManager().registerEvents(this, Gringotts.instance);
Bukkit.getPluginManager().registerEvents(this.landHolderProvider, Gringotts.instance);

Gringotts.instance.registerAccountHolderProvider(LandAccountHolder.ACCOUNT_TYPE, this.landHolderProvider);
}

public void checkLandBalanceConsistency() {
// Check gringotts/lands balance consistency
for (Land land : this.api.getLands()) {
GringottsLands.LOGGER.fine("Check Land " + land.getULID() + " balance consistency.");
AccountHolder holder = this.landHolderProvider.getAccountHolder(land);
GringottsAccount account = Gringotts.instance.getAccounting().getAccount(holder);
double landBalance = land.getBalance();
double balance = Configuration.CONF.getCurrency().getDisplayValue(account.getBalance());
if (landBalance != balance) {
GringottsLands.LOGGER.severe("Update Land " + land.getULID() + " balance to resolve inconsistency. (current: " + landBalance + " gringotts: " + balance + ")" );
land.setBalance(balance);
}
}
}

/**
* Vault created.
*
* @param event the event
*/
@EventHandler
public void vaultCreated(PlayerVaultCreationEvent event) {
// some listener already claimed this event
if (event.isValid() || !this.isEnabled()) {
return;
}

String line2String = event.getCause().getLine(2);

if (line2String == null) {
return;
}

Player player = event.getCause().getPlayer();

if (!event.getType().equals(LandsConfiguration.CONF.landSignTypeName)) {
return;

}

if (!LandsPermissions.CREATE_VAULT_LAND.isAllowed(player)) {
player.sendMessage(LandsLanguage.LANG.noLandVaultPerm);
return;
}

Land land = this.api.getLandByName(line2String);
if (land == null) {
player.sendMessage(LandsLanguage.LANG.noLandFound);
return;
}

AccountHolder owner = this.landHolderProvider.getAccountHolder(land);

if (LandsConfiguration.CONF.vaultsOnlyInLands && this.api.getArea(event.getCause().getBlock().getLocation()) == null) {
event.getCause().getPlayer().sendMessage(LandsLanguage.LANG.vaultNotInLand);
return;
}

if (LandsConfiguration.CONF.maxLandVaults != -1 && ((int) Gringotts.instance.getDao().retrieveChests().stream().filter(c -> c.account.owner.getId().equals(land.getULID().toString())).count() + 1) > LandsConfiguration.CONF.maxLandVaults) {
event.getCause().getPlayer().sendMessage(LandsLanguage.LANG.tooManyVaults);
return;
}

event.setOwner(owner);
event.setValid(true);
}
}
26 changes: 26 additions & 0 deletions src/main/java/net/laboulangerie/gringottslands/LandsLanguage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package net.laboulangerie.gringottslands;

import org.bukkit.configuration.file.FileConfiguration;

import java.util.function.BiFunction;

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

public enum LandsLanguage {
LANG;

public String noLandVaultPerm;
public String noLandFound;
public String vaultNotInLand;
public String tooManyVaults;

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

LANG.noLandVaultPerm = translator.apply("noLandPerm", "You do not have permission to create land vaults here.");
LANG.noLandFound = translator.apply("noLandFound", "Cannot create land vault: Land not found.");
LANG.vaultNotInLand = translator.apply("vaultNotInLand", "You cannot create vaults outside of lands.");
LANG.tooManyVaults = translator.apply("tooManyVaults", "You cannot create more vaults! Max: %max");

}
}
Loading