Skip to content

Commit

Permalink
Merge pull request #5 from sarhatabaot/feature/toggle-right-click
Browse files Browse the repository at this point in the history
Feature/toggle right click
  • Loading branch information
sarhatabaot authored Jul 4, 2019
2 parents dc1d250 + 113223b commit 69b1a56
Show file tree
Hide file tree
Showing 13 changed files with 216 additions and 162 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = 'me.tychsen'
version = '1.5.9'
version = '1.5.10'

sourceCompatibility = 1.8
targetCompatibility = 1.8
Expand Down
20 changes: 16 additions & 4 deletions src/main/java/me/tychsen/enchantgui/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.tychsen.enchantgui;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import me.tychsen.enchantgui.commands.ShopCommand;
Expand All @@ -14,16 +15,23 @@
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

public class Main extends JavaPlugin implements Listener {
@Setter
@Setter (AccessLevel.PRIVATE)
@Getter
private static Main instance;
@Setter (AccessLevel.PRIVATE)
@Getter
@Setter
private static Economy economy = null;
@Getter
@Setter
private static MenuSystem menuSystem;
@Getter
@Setter (AccessLevel.PRIVATE)
private static Set<UUID> toggleRightClickPlayers = new HashSet<>();


@Override
Expand All @@ -45,7 +53,8 @@ public void onEnable() {
Common.registerCommand(new ShopCommand());

// Enable Metrics
Metrics metrics = new Metrics(this);
if(!getConfig().getBoolean("opt-out"))
new Metrics(this);

getLogger().info(getName() + " " + getDescription().getVersion() + " enabled!");
}
Expand All @@ -55,6 +64,7 @@ public void onDisable() {
setInstance(null);
setEconomy(null);
setMenuSystem(null);
setToggleRightClickPlayers(null);
getLogger().info(getName() + " " + getDescription().getVersion() + " disabled!");
}

Expand All @@ -63,7 +73,7 @@ public static String getMinecraftVersion(){
}

public static void debug(String msg) {
if (EShopConfig.getInstance().getDebug())
if (EShopConfig.getDebug())
Main.getInstance().getLogger().warning(String.format("DEBUG %s", msg));
}

Expand All @@ -81,4 +91,6 @@ private boolean setupEconomy() {
return economy != null;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected void returnTell(String message) {
}

protected void tell(String message) {
Common.tell(player, (prefix != null ? "&8[&7" + prefix + "&8] " : "") + "&7" + message);
Common.tell(player, (prefix != null ? prefix + " " : "") + message);
}

@RequiredArgsConstructor
Expand Down
26 changes: 25 additions & 1 deletion src/main/java/me/tychsen/enchantgui/commands/ShopCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public ShopCommand() {
setDescription("Command for the EnchantGUI plugin.");
setUsage("/eshop");
setPermission("eshop.use");
setPrefix(LocalizationManager.getInstance().getString("prefix"));
menuSystem = Main.getMenuSystem();
}

Expand All @@ -30,14 +31,37 @@ protected void run(final Player player, final String[] args) {
if(args.length > 1){
returnTell("&cToo many arguments.");
}
LocalizationManager lm = LocalizationManager.getInstance();

if(args.length > 0 && args[0].equalsIgnoreCase("reload")){
EShopConfig.getInstance().reloadConfig(player);
if(!player.hasPermission("eshop.reload")) {
tell(lm.getString("no-permission"));
return;
}

EShopConfig.reloadConfig(player);
LocalizationManager.getInstance().reload(player);
EShopShop.getInstance().reload(player);
return;
}

if(args.length > 0 && args[0].equalsIgnoreCase("toggle")){
if(!player.hasPermission("eshop.enchantingtable.toggle")) {
tell(lm.getString("no-permission"));
return;
}

if(Main.getToggleRightClickPlayers().contains(player.getUniqueId())){
Main.getToggleRightClickPlayers().remove(player.getUniqueId());
tell(lm.getString("toggle-on"));
}
else {
Main.getToggleRightClickPlayers().add(player.getUniqueId());
tell(lm.getString("toggle-off"));
}
return;
}

menuSystem.showMainMenu(player);

}
Expand Down
83 changes: 83 additions & 0 deletions src/main/java/me/tychsen/enchantgui/config/AConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package me.tychsen.enchantgui.config;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

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

/**
* @author sarhatabaot
*/
public abstract class AConfig {
@Setter
@Getter (AccessLevel.PROTECTED)
private String fileName;
private File configFile;
private JavaPlugin plugin;
private FileConfiguration config;

/**
*
* @param fileName file name including the type.
* @param plugin plugin instance.
*/
public AConfig(final String fileName, final JavaPlugin plugin) {
this.fileName = fileName;
this.plugin = plugin;
}

/**
* Reloads the file
*/
protected void reloadConfig(){
if (configFile == null) {
configFile = new File(plugin.getDataFolder(), getFileName());
}
config = YamlConfiguration.loadConfiguration(configFile);

// Look for defaults in the jar
Reader defaultConfigStream;
InputStream defaultConfigInputStream = plugin.getResource(getFileName());
if (defaultConfigInputStream != null) {
defaultConfigStream = new InputStreamReader(defaultConfigInputStream, StandardCharsets.UTF_8);
YamlConfiguration defaultConfig = YamlConfiguration.loadConfiguration(defaultConfigStream);
config.setDefaults(defaultConfig);
}

plugin.getLogger().info(getFileName()+" reloaded.");
}

/**
* Saves the default configuration from the jar.
*/
protected void saveDefaultConfiguration(){
if (configFile == null) {
configFile = new File(plugin.getDataFolder(), getFileName());
}
if (!configFile.exists()) {
plugin.saveResource(getFileName(), false);
}
}

/**
* @return FileConfiguration config.
*/
protected FileConfiguration getConfig() {
if (config == null) {
reloadConfig();
}
return config;
}

public abstract String getString(String path);


}
23 changes: 14 additions & 9 deletions src/main/java/me/tychsen/enchantgui/config/EShopConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,24 @@
import me.tychsen.enchantgui.localization.LocalizationManager;
import me.tychsen.enchantgui.menu.DefaultMenuSystem;
import me.tychsen.enchantgui.Main;
import me.tychsen.enchantgui.util.Common;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.enchantments.Enchantment;

import java.util.Map;

/**
* TODO: make this a static accessor class.
*/
public class EShopConfig {
@Setter
@Getter
private static EShopConfig instance;
@Setter
private static FileConfiguration config;
private PaymentStrategy economy;
@Setter
private static PaymentStrategy economy;

public EShopConfig() {
setInstance(this);
Expand Down Expand Up @@ -63,19 +68,19 @@ public static boolean getShowPerItem(){
return config.getBoolean("show-per-item");
}

public void reloadConfig(CommandSender sender) {
public static void reloadConfig(CommandSender sender) {
LocalizationManager lm = LocalizationManager.getInstance();
if (sender.isOp() || sender.hasPermission("eshop.admin")) {
Main.getInstance().reloadConfig();
setConfig(Main.getInstance().getConfig());
economy = null;
sender.sendMessage(DefaultMenuSystem.START + lm.getString("config-reloaded"));
Common.tell(sender,DefaultMenuSystem.PREFIX + lm.getString("config-reloaded"));
} else {
sender.sendMessage(DefaultMenuSystem.START + lm.getString("no-permission"));
Common.tell(sender,DefaultMenuSystem.PREFIX + lm.getString("no-permission"));
}
}

public String[] getEnchantLevels(Enchantment enchantment) {
public static String[] getEnchantLevels(Enchantment enchantment) {
String path = enchantment.getKey().toString().toLowerCase();
path = path.split(":")[1];
Main.debug(path);
Expand All @@ -91,7 +96,7 @@ public String[] getEnchantLevels(Enchantment enchantment) {
return enchantLevels;
}

public String getEconomyCurrency(){
public static String getEconomyCurrency(){
switch (config.getString("payment-currency").toLowerCase()) {
case "money":
return Main.getEconomy().currencyNameSingular();
Expand All @@ -106,13 +111,13 @@ public PaymentStrategy getEconomy() {
if (economy == null) {
switch (config.getString("payment-currency").toLowerCase()) {
case "money":
economy = new MoneyPayment();
setEconomy(new MoneyPayment());
break;
case "xp":
economy = new XPPayment();
setEconomy(new XPPayment());
break;
default:
economy = new NullPayment();
setEconomy(new NullPayment());
break;
}
}
Expand Down
54 changes: 6 additions & 48 deletions src/main/java/me/tychsen/enchantgui/config/EShopShop.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,27 @@

import me.tychsen.enchantgui.Main;
import me.tychsen.enchantgui.localization.LocalizationManager;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
* @author sarhatabaot
*/
public class EShopShop {
private static final String FILE_NAME_SHOP = "shop.yml";
public class EShopShop extends AConfig{
private static EShopShop instance;
private Plugin plugin;
private File configFile;
private FileConfiguration config;

private EShopShop(){
plugin = Main.getInstance();
super("shop.yml", Main.getInstance());
saveDefaultConfiguration();
}

public void reload(CommandSender sender) {
reloadConfig();
String start = ChatColor.AQUA + LocalizationManager.getInstance().getString("prefix") + " " + ChatColor.WHITE;
sender.sendMessage(start + LocalizationManager.getInstance().getString("shop-reloaded"));
}

public String getString(String string){
return getConfig().getString(string);
}

private void saveDefaultConfiguration(){
if(configFile == null){
configFile = new File(plugin.getDataFolder(),FILE_NAME_SHOP);
}
if(!configFile.exists()){
plugin.saveResource(FILE_NAME_SHOP,false);
}
}

private void reloadConfig(){
if(configFile == null){
configFile = new File(plugin.getDataFolder(),FILE_NAME_SHOP);
}
config = YamlConfiguration.loadConfiguration(configFile);
Reader defaultConfigStream;
InputStream defaultConfigInputStream = plugin.getResource(FILE_NAME_SHOP);
if (defaultConfigInputStream != null) {
defaultConfigStream = new InputStreamReader(defaultConfigInputStream, StandardCharsets.UTF_8);
YamlConfiguration defaultConfig = YamlConfiguration.loadConfiguration(defaultConfigStream);
config.setDefaults(defaultConfig);
}
Main.debug("Config reloaded.");
sender.sendMessage(LocalizationManager.getInstance().getString("prefix") +" "+ LocalizationManager.getInstance().getString("shop-reloaded"));
}


private FileConfiguration getConfig(){
if(config==null)
reloadConfig();
return config;
@Override
public String getString(String path){
return getConfig().getString(path);
}

public static EShopShop getInstance(){
Expand Down
Loading

0 comments on commit 69b1a56

Please sign in to comment.