Skip to content

Commit

Permalink
Fixes reloading problems
Browse files Browse the repository at this point in the history
  • Loading branch information
sarhatabaot committed Nov 22, 2024
1 parent a84d1c1 commit bd54038
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 82 deletions.
2 changes: 1 addition & 1 deletion src/main/java/me/tychsen/enchantgui/EnchantGUIPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void onDisable() {
}

public void onReload() {
config.reloadConfig();
config.reload();

hookPlayerPoints();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void onGui(final Player player) {
@Subcommand("toggle")
@CommandPermission(EShopPermissionSys.TOGGLE)
public void onToggle(final Player player) {
if (!EnchantGUIPlugin.getInstance().getMainConfig().getBoolean("right-click-enchanting-table")) {
if (EnchantGUIPlugin.getInstance().getMainConfig().isNotRightClickEnchantingTable()) {
tell(player, lm.getLanguageString("disabled-feature"));
return;
}
Expand Down
37 changes: 19 additions & 18 deletions src/main/java/me/tychsen/enchantgui/config/EShopConfig.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package me.tychsen.enchantgui.config;

import com.github.sarhatabaot.kraken.core.chat.ChatUtil;
import com.github.sarhatabaot.kraken.core.config.ConfigFile;
import dev.dejvokep.boostedyaml.YamlDocument;
import dev.dejvokep.boostedyaml.settings.loader.LoaderSettings;
import me.tychsen.enchantgui.EnchantGUIPlugin;
Expand All @@ -13,33 +12,35 @@

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class EShopConfig extends ConfigFile<EnchantGUIPlugin> {
private YamlDocument newConfig;
public class EShopConfig {
private YamlDocument config;
private final EnchantGUIPlugin plugin;
private static PaymentStrategy economy;

public EShopConfig() {
super(EnchantGUIPlugin.getInstance(), "", "config.yml", "");
saveDefaultConfig();


this.plugin = EnchantGUIPlugin.getInstance();
this.createAndLoad();
}

public void createAndLoad() {
try {
this.newConfig = YamlDocument.create(new File(plugin.getDataFolder(), "config.yml"), plugin.getResource("config.yml"),
this.config = YamlDocument.create(
new File(plugin.getDataFolder(), "config.yml"), plugin.getResource("config.yml"),
LoaderSettings.builder()
.setAutoUpdate(true)
.build());
.build()
);
} catch (IOException e) {
this.plugin.getLogger().severe("Failed to load config.yml");
}
}

public void reload() {
try {
this.newConfig.reload();
this.config.reload();
} catch (IOException e) {
this.plugin.getLogger().severe("Failed to reload config.yml");
}
Expand All @@ -60,9 +61,9 @@ public double getPrice(@NotNull Enchantment enchantment, int level) {
return config.getDouble(path);
}

@SuppressWarnings("BooleanMethodIsAlwaysInverted")
public boolean getBoolean(String path) {
return config.getBoolean(path);

public boolean isNotRightClickEnchantingTable() {
return !config.getBoolean("right-click-enchanting-table");
}

public String getMenuName() {
Expand All @@ -83,16 +84,16 @@ public boolean getShowPerItem() {
}

public void reloadConfig(@NotNull CommandSender sender) {
reloadConfig();
ChatUtil.sendMessage(sender, EnchantGUIPlugin.getInstance().getLm().getPrefix() + " " + EnchantGUIPlugin.getInstance().getLm().getLanguageString("config-reloaded"));
reload();
ChatUtil.sendMessage(sender, EnchantGUIPlugin.getInstance().getLm().getPrefix() + " " + EnchantGUIPlugin.getInstance().getLm().getLanguageString("config-reloaded"));
}


public @NotNull String[] getEnchantLevels(@NotNull Enchantment enchantment) {
String path = enchantment.getKey().toString().toLowerCase();
path = path.split(":")[1];
EnchantGUIPlugin.debug(path);
Map<String, Object> enchantMap = config.getConfigurationSection(path).getValues(false);
Map<String, Object> enchantMap = config.getSection(path).getStringRouteMappedValues(false);
String[] enchantLevels = new String[enchantMap.size()];

var position = 0;
Expand All @@ -117,7 +118,7 @@ public PaymentStrategy getPaymentStrategy() {
case "disable" -> setEconomy(new NullPayment());
default -> {
final Material possibleMaterial = checkMaterialCurrency();
if(possibleMaterial == Material.AIR) {
if (possibleMaterial == Material.AIR) {
setEconomy(new NullPayment());
} else {
setEconomy(new MaterialPayment(possibleMaterial));
Expand All @@ -131,7 +132,7 @@ public PaymentStrategy getPaymentStrategy() {

private Material checkMaterialCurrency() {
final String paymentType = getPaymentType();
if(paymentType.startsWith("material")) {
if (paymentType.startsWith("material")) {
final String possibleMaterial = paymentType.split(":")[1];
return Material.matchMaterial(possibleMaterial.toUpperCase());
}
Expand Down
53 changes: 36 additions & 17 deletions src/main/java/me/tychsen/enchantgui/config/EShopConfig.java~
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package me.tychsen.enchantgui.config;

import com.github.sarhatabaot.kraken.core.chat.ChatUtil;
import com.github.sarhatabaot.kraken.core.config.ConfigFile;
import dev.dejvokep.boostedyaml.YamlDocument;
import dev.dejvokep.boostedyaml.settings.loader.LoaderSettings;
import me.tychsen.enchantgui.EnchantGUIPlugin;
Expand All @@ -12,20 +11,39 @@ import org.bukkit.enchantments.Enchantment;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class EShopConfig extends ConfigFile<EnchantGUIPlugin> {
private YamlDocument newConfig;
public class EShopConfig {
private YamlDocument config;
private final EnchantGUIPlugin plugin;
private static PaymentStrategy economy;

public EShopConfig() {
super(EnchantGUIPlugin.getInstance(), "", "config.yml", "");
saveDefaultConfig();
this.plugin = EnchantGUIPlugin.getInstance();
this.createAndLoad();
}

public void createAndLoad() {
try {
this.config = YamlDocument.create(
new File(plugin.getDataFolder(), "config.yml"), plugin.getResource("config.yml"),
LoaderSettings.builder()
.setAutoUpdate(true)
.build()
);
} catch (IOException e) {
this.plugin.getLogger().severe("Failed to load config.yml");
}
}

newConfig = YamlDocument.create(new File(plugin.getDataFolder(), "config.yml"), plugin.getResource("config.yml"),
LoaderSettings.builder()
.setAutoUpdate(true)
.build());
public void reload() {
try {
this.config.reload();
} catch (IOException e) {
this.plugin.getLogger().severe("Failed to reload config.yml");
}
}


Expand All @@ -43,9 +61,9 @@ public class EShopConfig extends ConfigFile<EnchantGUIPlugin> {
return config.getDouble(path);
}

@SuppressWarnings("BooleanMethodIsAlwaysInverted")
public boolean getBoolean(String path) {
return config.getBoolean(path);

public boolean isNotRightClickEnchantingTable() {
return !config.getBoolean("right-click-enchanting-table");
}

public String getMenuName() {
Expand All @@ -66,16 +84,16 @@ public class EShopConfig extends ConfigFile<EnchantGUIPlugin> {
}

public void reloadConfig(@NotNull CommandSender sender) {
reloadConfig();
ChatUtil.sendMessage(sender, EnchantGUIPlugin.getInstance().getLm().getPrefix() + " " + EnchantGUIPlugin.getInstance().getLm().getLanguageString("config-reloaded"));
reload();
ChatUtil.sendMessage(sender, EnchantGUIPlugin.getInstance().getLm().getPrefix() + " " + EnchantGUIPlugin.getInstance().getLm().getLanguageString("config-reloaded"));
}


public @NotNull String[] getEnchantLevels(@NotNull Enchantment enchantment) {
String path = enchantment.getKey().toString().toLowerCase();
path = path.split(":")[1];
EnchantGUIPlugin.debug(path);
Map<String, Object> enchantMap = config.getConfigurationSection(path).getValues(false);
Map<String, Object> enchantMap = (Map<String, Object>) config.getMapList(path).get(0);
String[] enchantLevels = new String[enchantMap.size()];

var position = 0;
Expand All @@ -97,9 +115,10 @@ public class EShopConfig extends ConfigFile<EnchantGUIPlugin> {
case "money" -> setEconomy(new VaultPayment());
case "xp" -> setEconomy(new XPPayment());
case "playerpoints" -> setEconomy(new PlayerPointsPayment());
case "disable" -> setEconomy(new NullPayment());
default -> {
final Material possibleMaterial = checkMaterialCurrency();
if(possibleMaterial == Material.AIR) {
if (possibleMaterial == Material.AIR) {
setEconomy(new NullPayment());
} else {
setEconomy(new MaterialPayment(possibleMaterial));
Expand All @@ -113,7 +132,7 @@ public class EShopConfig extends ConfigFile<EnchantGUIPlugin> {

private Material checkMaterialCurrency() {
final String paymentType = getPaymentType();
if(paymentType.startsWith("material")) {
if (paymentType.startsWith("material")) {
final String possibleMaterial = paymentType.split(":")[1];
return Material.matchMaterial(possibleMaterial.toUpperCase());
}
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/me/tychsen/enchantgui/event/EventManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ public class EventManager implements Listener {

@EventHandler
public void onPlayerInteractEvent(PlayerInteractEvent e) {
if (!EnchantGUIPlugin.getInstance().getMainConfig().getBoolean("right-click-enchanting-table")) {
if (EnchantGUIPlugin.getInstance().getMainConfig().isNotRightClickEnchantingTable()) {
return;
}
if (EnchantGUIPlugin.getInstance().getToggleRightClickPlayers().contains(e.getPlayer().getUniqueId()))
if (EnchantGUIPlugin.getInstance().getToggleRightClickPlayers().contains(e.getPlayer().getUniqueId())) {
return;
if (!e.getPlayer().hasPermission(EShopPermissionSys.ENCHANTING_TABLE))
}
if (!e.getPlayer().hasPermission(EShopPermissionSys.ENCHANTING_TABLE)) {
return;
}

if (e.getAction() == Action.RIGHT_CLICK_BLOCK && e.getClickedBlock().getType() == Material.ENCHANTING_TABLE) {
e.setCancelled(true);
Expand Down
42 changes: 0 additions & 42 deletions src/main/java/me/tychsen/enchantgui/event/EventManager.java~

This file was deleted.

0 comments on commit bd54038

Please sign in to comment.