Skip to content
This repository has been archived by the owner on Jun 14, 2024. It is now read-only.

Commit

Permalink
Beta 1.1.2 (#235)
Browse files Browse the repository at this point in the history
* [Enhance] Simplify Currency Configurations.

* [Patch] Remove deprecated methods.

* [Fix] Fix for strange issue on 1.13.

* [Minor] Upgrade items.yml to 1.13. Adds 8k lines.

* [Release] TNE Beta 1.1.1.

* [MISC] Start work on item signs.

* [Feat] Add command to download modules.

* [Feat] Auto download missing H2/MySQL Modules.

* [Fix] Fix for ender chest balances.

* [Fix] Fix module download command.
  • Loading branch information
creatorfromhell committed Aug 8, 2018
1 parent 18a3eba commit dfd40d0
Show file tree
Hide file tree
Showing 17 changed files with 213 additions and 36 deletions.
12 changes: 12 additions & 0 deletions TNE/src/net/tnemc/core/TNE.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
Expand Down Expand Up @@ -188,6 +189,12 @@ public void onEnable() {
loader = new ModuleLoader();
loader.load();

if(!loader.hasModule("MySQL") && !loader.hasModule("H2")) {
new File(getDataFolder(), "modules").mkdir();
ModuleLoader.downloadModule("h2");
loader.load("H2");
}

//Load modules
loader.getModules().forEach((key, value)->{
TNEModuleLoadEvent event = new TNEModuleLoadEvent(key, value.getInfo().version());
Expand Down Expand Up @@ -291,6 +298,11 @@ public void onEnable() {
consoleName = (configurations().getString("Core.Server.Account.Name").length() <= 100)? configurations().getString("Core.Server.Account.Name") : "Server_Account";
useUUID = configurations().getBoolean("Core.UUID");


if(!loader.hasModuleWithoutCase(configurations().getString("Core.Database.Type"))) {
getLogger().log(Level.SEVERE, "Unable to locate module with specified database type.");
}

TNESaveManager sManager = new TNESaveManager(new TNEDataManager(
configurations().getString("Core.Database.Type").toLowerCase(),
configurations().getString("Core.Database.MySQL.Host"),
Expand Down
1 change: 1 addition & 0 deletions TNE/src/net/tnemc/core/commands/module/ModuleCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class ModuleCommand extends TNECommand {

public ModuleCommand(TNE plugin) {
super(plugin);
subCommands.add(new ModuleDownloadCommand(plugin));
subCommands.add(new ModuleInfoCommand(plugin));
subCommands.add(new ModuleListCommand(plugin));
subCommands.add(new ModuleLoadCommand(plugin));
Expand Down
75 changes: 75 additions & 0 deletions TNE/src/net/tnemc/core/commands/module/ModuleDownloadCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package net.tnemc.core.commands.module;

import net.tnemc.core.TNE;
import net.tnemc.core.commands.TNECommand;
import net.tnemc.core.common.Message;
import net.tnemc.core.common.WorldVariant;
import net.tnemc.core.common.account.WorldFinder;
import net.tnemc.core.common.module.ModuleLoader;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;

/**
* The New Economy Minecraft Server Plugin
* <p>
* Created by Daniel on 8/8/2018.
* <p>
* This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.
* To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/ or send a letter to
* Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
* Created by creatorfromhell on 06/30/2017.
*/
public class ModuleDownloadCommand extends TNECommand {

public ModuleDownloadCommand(TNE plugin) {
super(plugin);
}

@Override
public String getName() {
return "download";
}

@Override
public String[] getAliases() {
return new String[] {
"dl"
};
}

@Override
public String getNode() {
return "tne.module.download";
}

@Override
public boolean console() {
return true;
}

@Override
public String getHelp() {
return "Messages.Commands.Module.Download";
}

@Override
public boolean execute(CommandSender sender, String command, String[] arguments) {
if(arguments.length >= 1) {
final String moduleName = arguments[0].toLowerCase().trim();
final String world = WorldFinder.getWorld(sender, WorldVariant.ACTUAL);
if(!ModuleLoader.modulePaths.containsKey(moduleName)) {
Message message = new Message("Messages.Module.Invalid");
message.addVariable("$module", moduleName);
message.translate(world, sender);
return false;
}
Bukkit.getScheduler().runTaskAsynchronously(TNE.instance(), ()->ModuleLoader.downloadModule(moduleName));
Message message = new Message("Messages.Module.Downloaded");
message.addVariable("$module", moduleName);
message.translate(world, sender);
return true;
}
help(sender);
return false;
}
}
7 changes: 6 additions & 1 deletion TNE/src/net/tnemc/core/commands/money/MoneyNoteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ public boolean execute(CommandSender sender, String command, String[] arguments)
return false;
}

if(!currency.isNotable()) {
new Message("Messages.Money.NoteFailed").translate(world, sender);
return false;
}

String parsed = CurrencyFormatter.parseAmount(currency, world, arguments[0]);
if(parsed.contains("Messages")) {
Message max = new Message(parsed);
Expand All @@ -96,7 +101,7 @@ public boolean execute(CommandSender sender, String command, String[] arguments)


if(result.proceed()) {
ItemStack stack = TNE.manager().currencyManager().createNote(id, currency.name(), world, value);
ItemStack stack = TNE.manager().currencyManager().createNote(currency.name(), world, value);
getPlayer(sender).getInventory().addItem(stack);
Message message = new Message(result.recipientMessage());
message.addVariable("$player", arguments[0]);
Expand Down
2 changes: 1 addition & 1 deletion TNE/src/net/tnemc/core/common/CurrencyManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public void register(net.tnemc.core.economy.currency.Currency currency) {
addCurrency(TNE.instance().defaultWorld, TNECurrency.fromReserve(currency));
}

public ItemStack createNote(UUID id, String currency, String world, BigDecimal amount) {
public ItemStack createNote(String currency, String world, BigDecimal amount) {
ItemStack stack = new ItemStack(Material.PAPER, 1);

ItemMeta meta = stack.getItemMeta();
Expand Down
15 changes: 15 additions & 0 deletions TNE/src/net/tnemc/core/common/account/TNEAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,21 @@ public BigDecimal getHoldings(String world, String currency, boolean core, boole
return holdings;
}

public BigDecimal getNonCoreHoldings(String world, String currency, boolean database) {
BigDecimal holdings = BigDecimal.ZERO;
for (Map.Entry<Integer, List<HoldingsHandler>> entry : TNE.manager().getHoldingsHandlers().descendingMap().entrySet()) {
for (HoldingsHandler handler : entry.getValue()) {
if (!handler.coreHandler()) {
if (handler.userContains().equalsIgnoreCase("") ||
displayName().contains(handler.userContains())) {
holdings = holdings.add(handler.getHoldings(identifier(), world, TNE.manager().currencyManager().get(world, currency), database));
}
}
}
}
return holdings;
}

public void saveItemCurrency(String world) {
saveItemCurrency(world, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public void load(FileConfiguration configurationFile) {
configurations.put("Messages.Language.Reload", "<white>Successfully reloaded all language files.");
configurations.put("Messages.Language.Set", "<white>Successfully set your language to $language.");

configurations.put("Messages.Module.Downloaded", "$module has been downloaded successfully.");
configurations.put("Messages.Module.Info", "<white>==== Module Info for $module ====<newline>Author: $author<newline>Version: $version");
configurations.put("Messages.Module.Invalid", "<red>Unable to find a module with the name of \"$module\".");
configurations.put("Messages.Module.List", "<white>This server is currently uses these TNE Modules: $modules.");
Expand All @@ -114,7 +115,6 @@ public void load(FileConfiguration configurationFile) {
configurations.put("Messages.Money.Converted", "<white>Successfully exchanged \"<gold>$from_amount<white>\" to \"<gold>$amount<white>\".");
configurations.put("Messages.Money.Noted", "<white>A note has been given to you in the amount of <gold>$amount<white> for currency <green>$currency<white>.");
configurations.put("Messages.Money.NoteClaimed", "<white>Successfully claimed note for currency <green>$currency<white> in the amount of <gold>$amount<white>.<newline>Your new balance is <gold>$balance<white>.");
configurations.put("Messages.Money.NoteFailed", "<red>I'm sorry, but your attempt to claim that currency note failed!.");
configurations.put("Messages.Money.NoteMinimum", "<red>The minimum note amount of $amount was not met.");
configurations.put("Messages.Money.NoteFailed", "<red>I'm sorry, but your attempt to claim that currency note failed!.");
configurations.put("Messages.Money.Top", "<white>=========[<gold>Economy Top<white>]========= Page: $page/$page_top");
Expand Down Expand Up @@ -168,6 +168,7 @@ public void load(FileConfiguration configurationFile) {
configurations.put("Messages.Commands.Language.Reload", "/language reload - Reloads all language files.");
configurations.put("Messages.Commands.Language.Set", "/language set <name> - Sets your current language to the one specified.");

configurations.put("Messages.Commands.Module.Download", "/tnem dl <module> - Attempts to download the specified module.");
configurations.put("Messages.Commands.Module.Info", "/tnem info <module> - Displays some information about a module.<newline>- Module ~ The module to look up.");
configurations.put("Messages.Commands.Module.List", "/tnem list - Lists all loaded TNE modules.");
configurations.put("Messages.Commands.Module.Load", "/tnem load <module> - Load a module from the modules directory.<newline>- Module ~ The module to load.");
Expand Down
4 changes: 4 additions & 0 deletions TNE/src/net/tnemc/core/common/module/Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public abstract class Module {
public Module() {
}

public String updateURL() {
return "";
}

/**
* @return a list of the classes that contain {@link net.tnemc.core.common.module.injectors.ModuleInjector module injectors} for this module.
*/
Expand Down
62 changes: 54 additions & 8 deletions TNE/src/net/tnemc/core/common/module/ModuleLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
Expand All @@ -32,6 +38,15 @@
**/
public class ModuleLoader {

public static Map<String, String> modulePaths = new HashMap<>();

static {
modulePaths.put("conversion", "https://github.com/TheNewEconomy/TNE-Bukkit/releases/download/Conversion/Conversion.jar");
modulePaths.put("h2", "https://github.com/TheNewEconomy/TNE-Bukkit/releases/download/H2/H2.jar");
modulePaths.put("mobs", "https://github.com/TheNewEconomy/TNE-Bukkit/releases/download/Mobs/Mobs.jar");
modulePaths.put("mysql", "https://github.com/TheNewEconomy/TNE-Bukkit/releases/download/mysql/MySQL.jar");
}

private File modulesYAML;
private FileConfiguration moduleConfigurations;
private Map<String, ModuleEntry> modules = new HashMap<>();
Expand Down Expand Up @@ -79,6 +94,13 @@ public boolean hasModule(String moduleName) {
return modules.containsKey(moduleName);
}

public boolean hasModuleWithoutCase(String moduleName) {
for (String key : modules.keySet()) {
if(key.equalsIgnoreCase(moduleName)) return true;
}
return false;
}

public ModuleEntry getModule(String moduleName) {
return modules.get(moduleName);
}
Expand All @@ -100,7 +122,7 @@ public String findPath(String moduleName) {
public void unload(String moduleName) {
if(hasModule(moduleName)) {
ModuleEntry entry = getModule(moduleName);
entry.getModule().getListeners(TNE.instance()).forEach(value->value.unregister());
entry.getModule().getListeners(TNE.instance()).forEach(ModuleListener::unregister);
entry.getModule().unload(TNE.instance());
entry.unload();

Expand Down Expand Up @@ -178,12 +200,8 @@ private Module getModuleClass(String modulePath) {
mainClass = urlClassLoader.loadClass(moduleMain);
moduleClass = mainClass.asSubclass(Module.class);
module = moduleClass.newInstance();
module.moduleInjectors().forEach(value->registerInjectors(value));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
module.moduleInjectors().forEach(this::registerInjectors);
} catch (MalformedURLException | IllegalAccessException | InstantiationException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
TNE.logger().info("Unable to locate module main class for file " + file.getName());
Expand Down Expand Up @@ -247,4 +265,32 @@ public Map<String, ModuleEntry> getModules() {
public String getLastVersion(String name) {
return moduleConfigurations.getString("Modules.DONTMODIFY." + name, modules.get(name).getInfo().version());
}

public static void downloadModule(String module) {
if(modulePaths.containsKey(module)) {
try {
final String fileURL = modulePaths.get(module);
final URL url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
int responseCode = httpConn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
String fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1, fileURL.length());

InputStream in = httpConn.getInputStream();
FileOutputStream out = new FileOutputStream(TNE.instance().getDataFolder() + File.separator + "modules" + File.separator + fileName);

int bytesRead = -1;
byte[] buffer = new byte[4096];
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}

out.close();
in.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ default TransactionResult perform(Transaction transaction) {
TNE.debug("Account null: " + (tneTransaction.getInitiator() == null));
TNE.debug("Transaction.initiator null: " + (tneTransaction == null));
TNE.debug("Transaction.initiatorCharge null: " + (tneTransaction.initiatorCharge() == null));
proceed = canCharge(tneTransaction.getInitiator(), tneTransaction.initiatorBalance().getAmount(), tneTransaction.initiatorCharge().getAmount(), tneTransaction.initiatorCharge());
proceed = canCharge(tneTransaction.initiatorBalance().getAmount(), tneTransaction.initiatorCharge().getAmount(), tneTransaction.initiatorCharge());
}
if(affected().equals(TransactionAffected.BOTH) || affected().equals(TransactionAffected.RECIPIENT)) {
TNE.debug("second if");
if(affected().equals(TransactionAffected.BOTH) && proceed || affected().equals(TransactionAffected.RECIPIENT)) {
proceed = canCharge(tneTransaction.getRecipient(), tneTransaction.recipientBalance().getAmount(), tneTransaction.recipientCharge().getAmount(), tneTransaction.recipientCharge());
proceed = canCharge(tneTransaction.recipientBalance().getAmount(), tneTransaction.recipientCharge().getAmount(), tneTransaction.recipientCharge());
}
}

Expand All @@ -93,15 +93,16 @@ default TransactionResult perform(Transaction transaction) {
}

default boolean handleCharge(TNEAccount account, BigDecimal balance, BigDecimal amount, TransactionCharge charge) {
if(amount.compareTo(BigDecimal.ZERO) == 0) return true;
if(charge.getType().equals(TransactionChargeType.LOSE)) {
account.setHoldings(charge.getWorld(), charge.getCurrency().name(), balance.subtract(amount), false, false);
account.removeHoldings(amount, charge.getCurrency(), charge.getWorld());
return true;
}
account.setHoldings(charge.getWorld(), charge.getCurrency().name(), balance.add(amount), false, false);
account.setHoldings(charge.getWorld(), charge.getCurrency().name(), balance.subtract(account.getNonCoreHoldings(charge.getWorld(), charge.getCurrency().name(), false)).add(amount));
return true;
}

default boolean canCharge(TNEAccount account, BigDecimal balance, BigDecimal amount, TransactionCharge charge) {
default boolean canCharge(BigDecimal balance, BigDecimal amount, TransactionCharge charge) {
if(charge.getType().equals(TransactionChargeType.LOSE)) {
return balance.compareTo(amount) >= 0;
}
Expand Down
14 changes: 0 additions & 14 deletions TNE/src/net/tnemc/core/listeners/PlayerListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.inventory.CraftItemEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.AsyncPlayerChatEvent;
Expand Down Expand Up @@ -240,19 +239,6 @@ public void onChat(AsyncPlayerChatEvent event) {
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onCraftEvent(CraftItemEvent event) {
if(!TNE.configurations().getBoolean("Core.Server.CurrencyCrafting")) {
final String world = WorldFinder.getWorld(event.getWhoClicked(), WorldVariant.BALANCE);
for (ItemStack item : event.getInventory().getMatrix()) {
if (item != null && TNE.manager().currencyManager().currencyFromItem(world, item).isPresent()) {
event.setCancelled(true);
break;
}
}
}
}

@EventHandler(priority = EventPriority.HIGHEST)
public void onLevelChange(final PlayerLevelChangeEvent event) {
if(TNE.manager().isXPGain(IDFinder.getID(event.getPlayer()))) {
Expand Down
3 changes: 0 additions & 3 deletions TNE/src/net/tnemc/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ Core:
#Whether or not experience gains should be disabled. This will help for servers that use Experience as currency.
ExperienceGain: false

#Whether or not players are able to use their currency for crafting.
CurrencyCrafting: true

#Whether or not players are able to use currency in villager trades.
CurrencyTrading: true

Expand Down
Loading

0 comments on commit dfd40d0

Please sign in to comment.