Compare commits
44 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9565ab0641 | ||
|
|
45c53b96dc | ||
|
|
861325ac3f | ||
|
|
70c015c1f0 | ||
|
|
8f3755b130 | ||
|
|
697ff0ccbb | ||
|
|
f117847fc3 | ||
|
|
bd88d40309 | ||
|
|
2fd8aab230 | ||
|
|
5ec10ec617 | ||
|
|
270270d015 | ||
|
|
3364b01c9d | ||
|
|
4beda35adf | ||
|
|
fd3e79378b | ||
|
|
4d0068e551 | ||
|
|
6933c0e5ef | ||
|
|
839b3724e7 | ||
|
|
6006865358 | ||
|
|
4907b4b6e6 | ||
|
|
29735cdf83 | ||
|
|
ae4b2ce0d0 | ||
|
|
786cbbcd7d | ||
|
|
6d3f2fc5dc | ||
|
|
6697cc412e | ||
|
|
17a877a89a | ||
|
|
9d4149c58f | ||
|
|
123ac79918 | ||
|
|
1670c8b3fb | ||
|
|
6125044e4c | ||
|
|
492439470c | ||
|
|
e9e39345ed | ||
|
|
cf29df7bce | ||
|
|
c259cb5a45 | ||
|
|
4148f55bb5 | ||
|
|
b63791c14b | ||
|
|
294dfabd54 | ||
|
|
d7f7cad863 | ||
|
|
afd8df5b48 | ||
|
|
b9d1f36604 | ||
|
|
403a7a7da8 | ||
|
|
f6fb5bcf66 | ||
|
|
194eb8b5f5 | ||
|
|
853aaca071 | ||
|
|
18dabd6bcf |
@@ -45,6 +45,9 @@ allprojects {
|
||||
|
||||
// NoCheatPlus
|
||||
maven { url 'https://repo.md-5.net/content/repositories/snapshots/' }
|
||||
|
||||
// CombatLogX
|
||||
maven { url 'https://nexus.sirblobman.xyz/repository/public/' }
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
||||
@@ -19,7 +19,7 @@ public final class VillagerTrade implements VillagerTradeProxy {
|
||||
fResult.setAccessible(true);
|
||||
|
||||
ItemStack result = merchantRecipe.getResult();
|
||||
Display.display(result);
|
||||
Display.displayAndFinalize(result);
|
||||
fResult.set(merchantRecipe, result);
|
||||
|
||||
// Get NMS MerchantRecipe from CraftMerchantRecipe
|
||||
@@ -31,7 +31,7 @@ public final class VillagerTrade implements VillagerTradeProxy {
|
||||
fSelling.setAccessible(true);
|
||||
|
||||
ItemStack selling = CraftItemStack.asBukkitCopy(handle.sellingItem);
|
||||
Display.display(selling);
|
||||
Display.displayAndFinalize(selling);
|
||||
fSelling.set(handle, CraftItemStack.asNMSCopy(selling));
|
||||
} catch (IllegalAccessException | NoSuchFieldException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
@@ -18,7 +18,7 @@ public final class VillagerTrade implements VillagerTradeProxy {
|
||||
Field fResult = MerchantRecipe.class.getDeclaredField("result");
|
||||
fResult.setAccessible(true);
|
||||
ItemStack result = merchantRecipe.getResult();
|
||||
Display.display(result);
|
||||
Display.displayAndFinalize(result);
|
||||
fResult.set(merchantRecipe, result);
|
||||
|
||||
// Get NMS MerchantRecipe from CraftMerchantRecipe
|
||||
@@ -30,7 +30,7 @@ public final class VillagerTrade implements VillagerTradeProxy {
|
||||
fSelling.setAccessible(true);
|
||||
|
||||
ItemStack selling = CraftItemStack.asBukkitCopy(handle.sellingItem);
|
||||
Display.display(selling);
|
||||
Display.displayAndFinalize(selling);
|
||||
|
||||
fSelling.set(handle, CraftItemStack.asNMSCopy(selling));
|
||||
} catch (IllegalAccessException | NoSuchFieldException e) {
|
||||
|
||||
@@ -18,7 +18,7 @@ public final class VillagerTrade implements VillagerTradeProxy {
|
||||
Field fResult = MerchantRecipe.class.getDeclaredField("result");
|
||||
fResult.setAccessible(true);
|
||||
ItemStack result = merchantRecipe.getResult();
|
||||
Display.display(result);
|
||||
Display.displayAndFinalize(result);
|
||||
fResult.set(merchantRecipe, result);
|
||||
|
||||
// Get NMS MerchantRecipe from CraftMerchantRecipe
|
||||
@@ -30,7 +30,7 @@ public final class VillagerTrade implements VillagerTradeProxy {
|
||||
fSelling.setAccessible(true);
|
||||
|
||||
ItemStack selling = CraftItemStack.asBukkitCopy(handle.sellingItem);
|
||||
Display.display(selling);
|
||||
Display.displayAndFinalize(selling);
|
||||
|
||||
fSelling.set(handle, CraftItemStack.asNMSCopy(selling));
|
||||
} catch (IllegalAccessException | NoSuchFieldException e) {
|
||||
|
||||
@@ -18,7 +18,7 @@ public final class VillagerTrade implements VillagerTradeProxy {
|
||||
Field fResult = MerchantRecipe.class.getDeclaredField("result");
|
||||
fResult.setAccessible(true);
|
||||
ItemStack result = merchantRecipe.getResult();
|
||||
Display.display(result);
|
||||
Display.displayAndFinalize(result);
|
||||
fResult.set(merchantRecipe, result);
|
||||
|
||||
// Get NMS MerchantRecipe from CraftMerchantRecipe
|
||||
@@ -30,7 +30,7 @@ public final class VillagerTrade implements VillagerTradeProxy {
|
||||
fSelling.setAccessible(true);
|
||||
|
||||
ItemStack selling = CraftItemStack.asBukkitCopy(handle.sellingItem);
|
||||
Display.display(selling);
|
||||
Display.displayAndFinalize(selling);
|
||||
|
||||
fSelling.set(handle, CraftItemStack.asNMSCopy(selling));
|
||||
} catch (IllegalAccessException | NoSuchFieldException e) {
|
||||
|
||||
@@ -16,11 +16,13 @@ import com.willfp.eco.spigot.eventlisteners.NaturalExpGainListeners;
|
||||
import com.willfp.eco.spigot.integrations.anticheat.AnticheatAAC;
|
||||
import com.willfp.eco.spigot.integrations.anticheat.AnticheatMatrix;
|
||||
import com.willfp.eco.spigot.integrations.anticheat.AnticheatNCP;
|
||||
import com.willfp.eco.spigot.integrations.antigrief.AntigriefCombatLogX;
|
||||
import com.willfp.eco.spigot.integrations.antigrief.AntigriefFactionsUUID;
|
||||
import com.willfp.eco.spigot.integrations.antigrief.AntigriefGriefPrevention;
|
||||
import com.willfp.eco.spigot.integrations.antigrief.AntigriefKingdoms;
|
||||
import com.willfp.eco.spigot.integrations.antigrief.AntigriefLands;
|
||||
import com.willfp.eco.spigot.integrations.antigrief.AntigriefTowny;
|
||||
import com.willfp.eco.spigot.integrations.antigrief.AntigriefUltimateLands;
|
||||
import com.willfp.eco.spigot.integrations.antigrief.AntigriefWorldGuard;
|
||||
import com.willfp.eco.spigot.integrations.mcmmo.McmmoIntegrationImpl;
|
||||
import com.willfp.eco.spigot.recipes.RecipeListener;
|
||||
@@ -107,6 +109,8 @@ public class EcoPlugin extends AbstractEcoPlugin {
|
||||
new IntegrationLoader("Towny", () -> AntigriefManager.register(new AntigriefTowny())),
|
||||
new IntegrationLoader("Lands", () -> AntigriefManager.register(new AntigriefLands(this))),
|
||||
new IntegrationLoader("Kingdoms", () -> AntigriefManager.register(new AntigriefKingdoms())),
|
||||
new IntegrationLoader("ULands", () -> AntigriefManager.register(new AntigriefUltimateLands())),
|
||||
new IntegrationLoader("CombatLogX", () -> AntigriefManager.register(new AntigriefCombatLogX())),
|
||||
|
||||
// Anticheat
|
||||
new IntegrationLoader("AAC5", () -> AnticheatManager.register(this, new AnticheatAAC())),
|
||||
|
||||
@@ -23,7 +23,7 @@ public class AnticheatNCP implements AnticheatWrapper {
|
||||
|
||||
@Override
|
||||
public void exempt(@NotNull final Player player) {
|
||||
if (!NCPExemptionManager.isExempted(player, CheckType.ALL)) {
|
||||
if (NCPExemptionManager.isExempted(player, CheckType.ALL)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.willfp.eco.spigot.integrations.antigrief;
|
||||
|
||||
import com.SirBlobman.combatlogx.api.ICombatLogX;
|
||||
import com.SirBlobman.combatlogx.api.expansion.Expansion;
|
||||
import com.SirBlobman.combatlogx.expansion.newbie.helper.NewbieHelper;
|
||||
import com.SirBlobman.combatlogx.expansion.newbie.helper.listener.ListenerPVP;
|
||||
import com.willfp.eco.spigot.EcoPlugin;
|
||||
import com.willfp.eco.util.integrations.antigrief.AntigriefWrapper;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.entity.LivingEntity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class AntigriefCombatLogX implements AntigriefWrapper {
|
||||
/**
|
||||
* Instance of CombatLogX.
|
||||
*/
|
||||
private final ICombatLogX instance = (ICombatLogX) Bukkit.getPluginManager().getPlugin("CombatLogX");
|
||||
|
||||
/**
|
||||
* PVPManager for CombatLogX NewbieHelper.
|
||||
*/
|
||||
private ListenerPVP pvp = null;
|
||||
|
||||
/**
|
||||
* Create new CombatLogX antigrief.
|
||||
*/
|
||||
public AntigriefCombatLogX() {
|
||||
assert instance != null;
|
||||
EcoPlugin.getInstance().getScheduler().runLater(() -> {
|
||||
Expansion expansionUncast = instance.getExpansionManager().getExpansionByName("NewbieHelper").orElse(null);
|
||||
if (expansionUncast instanceof NewbieHelper) {
|
||||
pvp = ((NewbieHelper) expansionUncast).getPVPListener();
|
||||
}
|
||||
}, 3);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canBreakBlock(@NotNull final Player player,
|
||||
@NotNull final Block block) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canCreateExplosion(@NotNull final Player player,
|
||||
@NotNull final Location location) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canPlaceBlock(@NotNull final Player player,
|
||||
@NotNull final Block block) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canInjure(@NotNull final Player player,
|
||||
@NotNull final LivingEntity victim) {
|
||||
if (!(victim instanceof Player)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (pvp == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return (pvp.isPVPEnabled(player) && pvp.isPVPEnabled((Player) victim));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPluginName() {
|
||||
return "CombatLogX";
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,10 @@
|
||||
package com.willfp.eco.spigot.integrations.antigrief;
|
||||
|
||||
import com.palmergames.bukkit.towny.TownyAPI;
|
||||
import com.palmergames.bukkit.towny.TownyUniverse;
|
||||
import com.palmergames.bukkit.towny.object.Town;
|
||||
import com.palmergames.bukkit.towny.object.TownyPermission;
|
||||
import com.palmergames.bukkit.towny.object.TownyWorld;
|
||||
import com.palmergames.bukkit.towny.object.WorldCoord;
|
||||
import com.palmergames.bukkit.towny.utils.PlayerCacheUtil;
|
||||
import com.willfp.eco.util.integrations.antigrief.AntigriefWrapper;
|
||||
@@ -16,24 +19,57 @@ public class AntigriefTowny implements AntigriefWrapper {
|
||||
@Override
|
||||
public boolean canBreakBlock(@NotNull final Player player,
|
||||
@NotNull final Block block) {
|
||||
return PlayerCacheUtil.getCachePermission(player, block.getLocation(), block.getType(), TownyPermission.ActionType.DESTROY);
|
||||
TownyWorld world = TownyUniverse.getInstance().getWorldMap().get(block.getLocation().getWorld().getName());
|
||||
if (world == null) {
|
||||
return true;
|
||||
}
|
||||
if (TownyAPI.getInstance().isWilderness(block)) {
|
||||
return true;
|
||||
}
|
||||
return PlayerCacheUtil.getCachePermission(player, block.getLocation(), block.getType(), TownyPermission.ActionType.DESTROY)
|
||||
|| PlayerCacheUtil.getCachePermission(player, block.getLocation(), block.getType(), TownyPermission.ActionType.BUILD);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canCreateExplosion(@NotNull final Player player,
|
||||
@NotNull final Location location) {
|
||||
TownyWorld world = TownyUniverse.getInstance().getWorldMap().get(location.getWorld().getName());
|
||||
if (world == null) {
|
||||
return true;
|
||||
}
|
||||
if (TownyAPI.getInstance().isWilderness(location)) {
|
||||
return true;
|
||||
}
|
||||
return PlayerCacheUtil.getCachePermission(player, location, Material.TNT, TownyPermission.ActionType.ITEM_USE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canPlaceBlock(@NotNull final Player player,
|
||||
@NotNull final Block block) {
|
||||
TownyWorld world = TownyUniverse.getInstance().getWorldMap().get(block.getLocation().getWorld().getName());
|
||||
if (world == null) {
|
||||
return true;
|
||||
}
|
||||
if (TownyAPI.getInstance().isWilderness(block)) {
|
||||
return true;
|
||||
}
|
||||
return PlayerCacheUtil.getCachePermission(player, block.getLocation(), block.getType(), TownyPermission.ActionType.BUILD);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canInjure(@NotNull final Player player,
|
||||
@NotNull final LivingEntity victim) {
|
||||
TownyWorld world = TownyUniverse.getInstance().getWorldMap().get(victim.getLocation().getWorld().getName());
|
||||
if (world == null) {
|
||||
return true;
|
||||
}
|
||||
if (TownyAPI.getInstance().isWilderness(victim.getLocation())) {
|
||||
if (victim instanceof Player) {
|
||||
return world.isPVP();
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (victim instanceof Player) {
|
||||
try {
|
||||
Town town = WorldCoord.parseWorldCoord(victim.getLocation()).getTownBlock().getTown();
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.willfp.eco.spigot.integrations.antigrief;
|
||||
|
||||
import com.willfp.eco.util.integrations.antigrief.AntigriefWrapper;
|
||||
import me.ulrich.lands.api.LandsAPI;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.entity.LivingEntity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class AntigriefUltimateLands implements AntigriefWrapper {
|
||||
@Override
|
||||
public boolean canBreakBlock(@NotNull final Player player,
|
||||
@NotNull final Block block) {
|
||||
return LandsAPI.getInstance().isOwnerOfChunk(player.getName(), block.getChunk()) || LandsAPI.getInstance().isMemberOfChunk(block.getChunk(), player);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canCreateExplosion(@NotNull final Player player,
|
||||
@NotNull final Location location) {
|
||||
return LandsAPI.getInstance().isOwnerOfChunk(player.getName(), location.getChunk()) || LandsAPI.getInstance().isMemberOfChunk(location.getChunk(), player);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canPlaceBlock(@NotNull final Player player,
|
||||
@NotNull final Block block) {
|
||||
return LandsAPI.getInstance().isOwnerOfChunk(player.getName(), block.getChunk()) || LandsAPI.getInstance().isMemberOfChunk(block.getChunk(), player);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canInjure(@NotNull final Player player,
|
||||
@NotNull final LivingEntity victim) {
|
||||
return LandsAPI.getInstance().isOwnerOfChunk(player.getName(), victim.getLocation().getChunk()) || LandsAPI.getInstance().isMemberOfChunk(victim.getLocation().getChunk(), player);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPluginName() {
|
||||
return "ULands";
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,6 @@ import com.sk89q.worldguard.LocalPlayer;
|
||||
import com.sk89q.worldguard.WorldGuard;
|
||||
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
|
||||
import com.sk89q.worldguard.protection.flags.Flags;
|
||||
import com.sk89q.worldguard.protection.flags.StateFlag;
|
||||
import com.sk89q.worldguard.protection.regions.RegionContainer;
|
||||
import com.sk89q.worldguard.protection.regions.RegionQuery;
|
||||
import com.willfp.eco.util.integrations.antigrief.AntigriefWrapper;
|
||||
@@ -25,7 +24,7 @@ public class AntigriefWorldGuard implements AntigriefWrapper {
|
||||
RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
|
||||
RegionQuery query = container.createQuery();
|
||||
|
||||
if (query.queryState(BukkitAdapter.adapt(block.getLocation()), localPlayer, Flags.BUILD) == StateFlag.State.DENY) {
|
||||
if (!query.testBuild(BukkitAdapter.adapt(block.getLocation()), localPlayer, Flags.BLOCK_BREAK)) {
|
||||
return WorldGuard.getInstance().getPlatform().getSessionManager().hasBypass(localPlayer, BukkitAdapter.adapt(block.getWorld()));
|
||||
}
|
||||
return true;
|
||||
@@ -40,7 +39,7 @@ public class AntigriefWorldGuard implements AntigriefWrapper {
|
||||
World world = location.getWorld();
|
||||
Validate.notNull(world, "World cannot be null!");
|
||||
|
||||
if (query.queryState(BukkitAdapter.adapt(location), localPlayer, Flags.OTHER_EXPLOSION) == StateFlag.State.DENY) {
|
||||
if (!query.testBuild(BukkitAdapter.adapt(location), localPlayer, Flags.TNT)) {
|
||||
return WorldGuard.getInstance().getPlatform().getSessionManager().hasBypass(localPlayer, BukkitAdapter.adapt(world));
|
||||
}
|
||||
return true;
|
||||
@@ -53,7 +52,7 @@ public class AntigriefWorldGuard implements AntigriefWrapper {
|
||||
RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
|
||||
RegionQuery query = container.createQuery();
|
||||
|
||||
if (query.queryState(BukkitAdapter.adapt(block.getLocation()), localPlayer, Flags.BLOCK_PLACE) == StateFlag.State.DENY) {
|
||||
if (!query.testBuild(BukkitAdapter.adapt(block.getLocation()), localPlayer, Flags.BLOCK_PLACE)) {
|
||||
return WorldGuard.getInstance().getPlatform().getSessionManager().hasBypass(localPlayer, BukkitAdapter.adapt(block.getWorld()));
|
||||
}
|
||||
return true;
|
||||
@@ -67,11 +66,11 @@ public class AntigriefWorldGuard implements AntigriefWrapper {
|
||||
RegionQuery query = container.createQuery();
|
||||
|
||||
if (victim instanceof Player) {
|
||||
if (query.queryState(BukkitAdapter.adapt(victim.getLocation()), localPlayer, Flags.PVP) == StateFlag.State.DENY) {
|
||||
if (!query.testBuild(BukkitAdapter.adapt(victim.getLocation()), localPlayer, Flags.PVP)) {
|
||||
return WorldGuard.getInstance().getPlatform().getSessionManager().hasBypass(localPlayer, BukkitAdapter.adapt(player.getWorld()));
|
||||
}
|
||||
} else {
|
||||
if (query.queryState(BukkitAdapter.adapt(victim.getLocation()), localPlayer, Flags.DAMAGE_ANIMALS) == StateFlag.State.DENY) {
|
||||
if (!query.testBuild(BukkitAdapter.adapt(victim.getLocation()), localPlayer, Flags.DAMAGE_ANIMALS)) {
|
||||
return WorldGuard.getInstance().getPlatform().getSessionManager().hasBypass(localPlayer, BukkitAdapter.adapt(player.getWorld()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,14 +3,33 @@ package com.willfp.eco.spigot.integrations.mcmmo;
|
||||
import com.gmail.nossr50.datatypes.meta.BonusDropMeta;
|
||||
import com.gmail.nossr50.events.fake.FakeEvent;
|
||||
import com.gmail.nossr50.mcMMO;
|
||||
import com.willfp.eco.util.ClassUtils;
|
||||
import com.willfp.eco.util.integrations.mcmmo.McmmoWrapper;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.event.Event;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class McmmoIntegrationImpl implements McmmoWrapper {
|
||||
/**
|
||||
* Disabled if mcmmo is outdated or missing classes.
|
||||
*/
|
||||
boolean disabled = false;
|
||||
|
||||
/**
|
||||
* Initialize mcMMO integration.
|
||||
*/
|
||||
public McmmoIntegrationImpl() {
|
||||
if (!ClassUtils.exists("com.gmail.nossr50.events.fake.FakeEvent")) {
|
||||
disabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getBonusDropCount(@NotNull final Block block) {
|
||||
if (disabled) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (block.getMetadata(mcMMO.BONUS_DROPS_METAKEY).size() > 0) {
|
||||
BonusDropMeta bonusDropMeta = (BonusDropMeta) block.getMetadata(mcMMO.BONUS_DROPS_METAKEY).get(0);
|
||||
return bonusDropMeta.asInt();
|
||||
@@ -21,6 +40,10 @@ public class McmmoIntegrationImpl implements McmmoWrapper {
|
||||
|
||||
@Override
|
||||
public boolean isFake(@NotNull final Event event) {
|
||||
if (disabled) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return event instanceof FakeEvent;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ loadbefore:
|
||||
- Talismans
|
||||
- StatTrackers
|
||||
- EcoArmor
|
||||
- Illusioner
|
||||
- EcoBosses
|
||||
depend:
|
||||
- ProtocolLib
|
||||
softdepend:
|
||||
@@ -25,3 +25,5 @@ softdepend:
|
||||
- Spartan
|
||||
- PlaceholderAPI
|
||||
- mcMMO
|
||||
- ULands
|
||||
- CombatLogX
|
||||
@@ -1,15 +1,11 @@
|
||||
package com.willfp.eco.internal.config;
|
||||
|
||||
import com.willfp.eco.util.StringUtils;
|
||||
import com.willfp.eco.util.internal.PluginDependent;
|
||||
import com.willfp.eco.util.plugin.AbstractEcoPlugin;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.InvalidConfigurationException;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
@@ -19,19 +15,8 @@ import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public abstract class AbstractConfig extends PluginDependent {
|
||||
/**
|
||||
* The linked {@link YamlConfiguration} where values are physically stored.
|
||||
*/
|
||||
@Getter(AccessLevel.PUBLIC)
|
||||
protected final YamlConfiguration config;
|
||||
|
||||
public abstract class AbstractConfig extends AbstractUndefinedConfig<YamlConfiguration> {
|
||||
/**
|
||||
* The physical config file, as stored on disk.
|
||||
*/
|
||||
@@ -39,9 +24,15 @@ public abstract class AbstractConfig extends PluginDependent {
|
||||
private final File configFile;
|
||||
|
||||
/**
|
||||
* The full name of the config file (eg config.yml).
|
||||
* Plugin handle.
|
||||
*/
|
||||
@Getter(AccessLevel.PROTECTED)
|
||||
private final AbstractEcoPlugin plugin;
|
||||
|
||||
/**
|
||||
* The full name of the config file (eg config.yml).
|
||||
*/
|
||||
@Getter
|
||||
private final String name;
|
||||
|
||||
/**
|
||||
@@ -56,11 +47,6 @@ public abstract class AbstractConfig extends PluginDependent {
|
||||
@Getter(AccessLevel.PROTECTED)
|
||||
private final Class<?> source;
|
||||
|
||||
/**
|
||||
* Cached values for faster reading.
|
||||
*/
|
||||
private final Map<String, Object> cache = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Abstract config.
|
||||
*
|
||||
@@ -73,10 +59,11 @@ public abstract class AbstractConfig extends PluginDependent {
|
||||
@NotNull final AbstractEcoPlugin plugin,
|
||||
@NotNull final String subDirectoryPath,
|
||||
@NotNull final Class<?> source) {
|
||||
super(plugin);
|
||||
super(configName);
|
||||
this.plugin = plugin;
|
||||
this.name = configName + ".yml";
|
||||
this.source = source;
|
||||
this.subDirectoryPath = subDirectoryPath;
|
||||
this.name = configName + ".yml";
|
||||
|
||||
File directory = new File(this.getPlugin().getDataFolder(), subDirectoryPath);
|
||||
if (!directory.exists()) {
|
||||
@@ -88,7 +75,7 @@ public abstract class AbstractConfig extends PluginDependent {
|
||||
}
|
||||
|
||||
this.configFile = new File(directory, this.name);
|
||||
this.config = YamlConfiguration.loadConfiguration(configFile);
|
||||
init(YamlConfiguration.loadConfiguration(configFile));
|
||||
}
|
||||
|
||||
private void createFile() {
|
||||
@@ -144,7 +131,7 @@ public abstract class AbstractConfig extends PluginDependent {
|
||||
InputStream newIn = source.getResourceAsStream(getResourcePath());
|
||||
|
||||
if (newIn == null) {
|
||||
throw new NullPointerException(this.getName() + " is null?");
|
||||
throw new NullPointerException(name + " is null?");
|
||||
}
|
||||
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(newIn, StandardCharsets.UTF_8));
|
||||
@@ -158,315 +145,4 @@ public abstract class AbstractConfig extends PluginDependent {
|
||||
|
||||
return newConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears cache.
|
||||
*/
|
||||
public final void clearCache() {
|
||||
cache.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get if the config contains a key.
|
||||
*
|
||||
* @param path The key to check.
|
||||
* @return If contained.
|
||||
*/
|
||||
public boolean has(@NotNull final String path) {
|
||||
return config.contains(path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get configuration section from config.
|
||||
*
|
||||
* @param path The key to check.
|
||||
* @return The configuration section. Throws NPE if not found.
|
||||
*/
|
||||
@NotNull
|
||||
public ConfigurationSection getSection(@NotNull final String path) {
|
||||
ConfigurationSection section = getSectionOrNull(path);
|
||||
if (section == null) {
|
||||
throw new NullPointerException("Section cannot be null!");
|
||||
} else {
|
||||
return section;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get configuration section from config.
|
||||
*
|
||||
* @param path The key to check.
|
||||
* @return The configuration section, or null if not found.
|
||||
*/
|
||||
@Nullable
|
||||
public ConfigurationSection getSectionOrNull(@NotNull final String path) {
|
||||
if (cache.containsKey(path)) {
|
||||
return (ConfigurationSection) cache.get(path);
|
||||
} else {
|
||||
cache.put(path, config.getConfigurationSection(path));
|
||||
return getSectionOrNull(path);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an integer from config.
|
||||
*
|
||||
* @param path The key to fetch the value from.
|
||||
* @return The found value, or 0 if not found.
|
||||
*/
|
||||
public int getInt(@NotNull final String path) {
|
||||
if (cache.containsKey(path)) {
|
||||
return (int) cache.get(path);
|
||||
} else {
|
||||
cache.put(path, config.getInt(path, 0));
|
||||
return getInt(path);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an integer from config.
|
||||
*
|
||||
* @param path The key to fetch the value from.
|
||||
* @return The found value, or null if not found.
|
||||
*/
|
||||
@Nullable
|
||||
public Integer getIntOrNull(@NotNull final String path) {
|
||||
if (has(path)) {
|
||||
return getInt(path);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an integer from config with a specified default (not found) value.
|
||||
*
|
||||
* @param path The key to fetch the value from.
|
||||
* @param def The value to default to if not found.
|
||||
* @return The found value, or the default.
|
||||
*/
|
||||
public int getInt(@NotNull final String path,
|
||||
final int def) {
|
||||
if (cache.containsKey(path)) {
|
||||
return (int) cache.get(path);
|
||||
} else {
|
||||
cache.put(path, config.getInt(path, def));
|
||||
return getInt(path);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of integers from config.
|
||||
*
|
||||
* @param path The key to fetch the value from.
|
||||
* @return The found value, or a blank {@link java.util.ArrayList} if not found.
|
||||
*/
|
||||
@NotNull
|
||||
public List<Integer> getInts(@NotNull final String path) {
|
||||
if (cache.containsKey(path)) {
|
||||
return (List<Integer>) cache.get(path);
|
||||
} else {
|
||||
cache.put(path, config.getIntegerList(path));
|
||||
return getInts(path);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of integers from config.
|
||||
*
|
||||
* @param path The key to fetch the value from.
|
||||
* @return The found value, or null if not found.
|
||||
*/
|
||||
@Nullable
|
||||
public List<Integer> getIntsOrNull(@NotNull final String path) {
|
||||
if (has(path)) {
|
||||
return getInts(path);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a boolean from config.
|
||||
*
|
||||
* @param path The key to fetch the value from.
|
||||
* @return The found value, or false if not found.
|
||||
*/
|
||||
public boolean getBool(@NotNull final String path) {
|
||||
if (cache.containsKey(path)) {
|
||||
return (boolean) cache.get(path);
|
||||
} else {
|
||||
cache.put(path, config.getBoolean(path));
|
||||
return getBool(path);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a boolean from config.
|
||||
*
|
||||
* @param path The key to fetch the value from.
|
||||
* @return The found value, or null if not found.
|
||||
*/
|
||||
@Nullable
|
||||
public Boolean getBoolOrNull(@NotNull final String path) {
|
||||
if (has(path)) {
|
||||
return getBool(path);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of booleans from config.
|
||||
*
|
||||
* @param path The key to fetch the value from.
|
||||
* @return The found value, or a blank {@link java.util.ArrayList} if not found.
|
||||
*/
|
||||
@NotNull
|
||||
public List<Boolean> getBools(@NotNull final String path) {
|
||||
if (cache.containsKey(path)) {
|
||||
return (List<Boolean>) cache.get(path);
|
||||
} else {
|
||||
cache.put(path, config.getBooleanList(path));
|
||||
return getBools(path);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of booleans from config.
|
||||
*
|
||||
* @param path The key to fetch the value from.
|
||||
* @return The found value, or null if not found.
|
||||
*/
|
||||
@Nullable
|
||||
public List<Boolean> getBoolsOrNull(@NotNull final String path) {
|
||||
if (has(path)) {
|
||||
return getBools(path);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a string from config.
|
||||
*
|
||||
* @param path The key to fetch the value from.
|
||||
* @return The found value, or an empty string if not found.
|
||||
*/
|
||||
@NotNull
|
||||
public String getString(@NotNull final String path) {
|
||||
if (cache.containsKey(path)) {
|
||||
return (String) cache.get(path);
|
||||
} else {
|
||||
cache.put(path, StringUtils.translate(Objects.requireNonNull(config.getString(path, ""))));
|
||||
return getString(path);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a string from config.
|
||||
*
|
||||
* @param path The key to fetch the value from.
|
||||
* @return The found value, or null if not found.
|
||||
*/
|
||||
@Nullable
|
||||
public String getStringOrNull(@NotNull final String path) {
|
||||
if (has(path)) {
|
||||
return getString(path);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of strings from config.
|
||||
*
|
||||
* @param path The key to fetch the value from.
|
||||
* @return The found value, or a blank {@link java.util.ArrayList} if not found.
|
||||
*/
|
||||
@NotNull
|
||||
public List<String> getStrings(@NotNull final String path) {
|
||||
if (cache.containsKey(path)) {
|
||||
return (List<String>) cache.get(path);
|
||||
} else {
|
||||
cache.put(path, config.getStringList(path));
|
||||
return getStrings(path);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of strings from config.
|
||||
*
|
||||
* @param path The key to fetch the value from.
|
||||
* @return The found value, or null if not found.
|
||||
*/
|
||||
@Nullable
|
||||
public List<String> getStringsOrNull(@NotNull final String path) {
|
||||
if (has(path)) {
|
||||
return getStrings(path);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a decimal from config.
|
||||
*
|
||||
* @param path The key to fetch the value from.
|
||||
* @return The found value, or 0 if not found.
|
||||
*/
|
||||
public double getDouble(@NotNull final String path) {
|
||||
if (cache.containsKey(path)) {
|
||||
return (double) cache.get(path);
|
||||
} else {
|
||||
cache.put(path, config.getDouble(path));
|
||||
return getDouble(path);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a decimal from config.
|
||||
*
|
||||
* @param path The key to fetch the value from.
|
||||
* @return The found value, or null if not found.
|
||||
*/
|
||||
@Nullable
|
||||
public Double getDoubleOrNull(@NotNull final String path) {
|
||||
if (has(path)) {
|
||||
return getDouble(path);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of decimals from config.
|
||||
*
|
||||
* @param path The key to fetch the value from.
|
||||
* @return The found value, or a blank {@link java.util.ArrayList} if not found.
|
||||
*/
|
||||
@NotNull
|
||||
public List<Double> getDoubles(@NotNull final String path) {
|
||||
if (cache.containsKey(path)) {
|
||||
return (List<Double>) cache.get(path);
|
||||
} else {
|
||||
cache.put(path, config.getDoubleList(path));
|
||||
return getDoubles(path);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of decimals from config.
|
||||
*
|
||||
* @param path The key to fetch the value from.
|
||||
* @return The found value, or null if not found.
|
||||
*/
|
||||
@Nullable
|
||||
public List<Double> getDoublesOrNull(@NotNull final String path) {
|
||||
if (has(path)) {
|
||||
return getDoubles(path);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,323 @@
|
||||
package com.willfp.eco.internal.config;
|
||||
|
||||
import com.willfp.eco.util.SerializationUtils;
|
||||
import com.willfp.eco.util.StringUtils;
|
||||
import com.willfp.eco.util.config.Config;
|
||||
import com.willfp.eco.util.serialization.EcoSerializable;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.MemorySection;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
@SuppressWarnings({"unchecked", "unused", "DeprecatedIsStillUsed"})
|
||||
public abstract class AbstractUndefinedConfig<T extends ConfigurationSection> implements Config {
|
||||
/**
|
||||
* The linked {@link MemorySection} where values are physically stored.
|
||||
*/
|
||||
@Getter(AccessLevel.PUBLIC)
|
||||
protected T config = null;
|
||||
|
||||
/**
|
||||
* Cached values for faster reading.
|
||||
*/
|
||||
private final Map<String, Object> cache = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Abstract config.
|
||||
*
|
||||
* @param configName The name of the config
|
||||
*/
|
||||
protected AbstractUndefinedConfig(@NotNull final String configName) {
|
||||
|
||||
}
|
||||
|
||||
protected Config init(@NotNull final T config) {
|
||||
this.config = config;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void clearCache() {
|
||||
cache.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean has(@NotNull final String path) {
|
||||
return config.contains(path);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<String> getKeys(final boolean deep) {
|
||||
return new ArrayList<>(config.getKeys(deep));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(@NotNull final String path,
|
||||
@NotNull final EcoSerializable<?> object) {
|
||||
Config serializedConfig = object.serialize();
|
||||
for (String key : serializedConfig.getKeys(true)) {
|
||||
Object raw = serializedConfig.getRaw(key);
|
||||
config.set(path + "." + key, raw);
|
||||
cache.put(path + "." + key, raw);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public Object getRaw(@NotNull final String path) {
|
||||
return config.get(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public <T extends EcoSerializable<T>> T get(@NotNull final String path,
|
||||
@NotNull final Class<T> clazz) {
|
||||
T object = getOrNull(path, clazz);
|
||||
if (object == null) {
|
||||
throw new NullPointerException("Object cannot be null!");
|
||||
} else {
|
||||
return object;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public <T extends EcoSerializable<T>> T getOrNull(@NotNull final String path,
|
||||
@NotNull final Class<T> clazz) {
|
||||
if (cache.containsKey(path)) {
|
||||
return (T) cache.get(path);
|
||||
} else {
|
||||
Config section = getSubsectionOrNull(path);
|
||||
if (section == null) {
|
||||
return null;
|
||||
}
|
||||
cache.put(path, SerializationUtils.deserialize(section, clazz));
|
||||
return getOrNull(path, clazz);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
@Deprecated
|
||||
public ConfigurationSection getSection(@NotNull final String path) {
|
||||
ConfigurationSection section = getSectionOrNull(path);
|
||||
if (section == null) {
|
||||
throw new NullPointerException("Section cannot be null!");
|
||||
} else {
|
||||
return section;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
@Deprecated
|
||||
public ConfigurationSection getSectionOrNull(@NotNull final String path) {
|
||||
if (cache.containsKey(path)) {
|
||||
return (ConfigurationSection) cache.get(path);
|
||||
} else {
|
||||
cache.put(path, config.getConfigurationSection(path));
|
||||
return getSectionOrNull(path);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Config getSubsection(@NotNull final String path) {
|
||||
return new ConfigSection(this.getSection(path));
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public Config getSubsectionOrNull(@NotNull final String path) {
|
||||
ConfigurationSection section = this.getSectionOrNull(path);
|
||||
if (section == null) {
|
||||
return null;
|
||||
}
|
||||
return new ConfigSection(section);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInt(@NotNull final String path) {
|
||||
if (cache.containsKey(path)) {
|
||||
return (int) cache.get(path);
|
||||
} else {
|
||||
cache.put(path, config.getInt(path, 0));
|
||||
return getInt(path);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public Integer getIntOrNull(@NotNull final String path) {
|
||||
if (has(path)) {
|
||||
return getInt(path);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInt(@NotNull final String path,
|
||||
final int def) {
|
||||
if (cache.containsKey(path)) {
|
||||
return (int) cache.get(path);
|
||||
} else {
|
||||
cache.put(path, config.getInt(path, def));
|
||||
return getInt(path);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public List<Integer> getInts(@NotNull final String path) {
|
||||
if (cache.containsKey(path)) {
|
||||
return (List<Integer>) cache.get(path);
|
||||
} else {
|
||||
cache.put(path, has(path) ? new ArrayList<>(config.getIntegerList(path)) : new ArrayList<>());
|
||||
return getInts(path);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public List<Integer> getIntsOrNull(@NotNull final String path) {
|
||||
if (has(path)) {
|
||||
return getInts(path);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBool(@NotNull final String path) {
|
||||
if (cache.containsKey(path)) {
|
||||
return (boolean) cache.get(path);
|
||||
} else {
|
||||
cache.put(path, config.getBoolean(path));
|
||||
return getBool(path);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public Boolean getBoolOrNull(@NotNull final String path) {
|
||||
if (has(path)) {
|
||||
return getBool(path);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public List<Boolean> getBools(@NotNull final String path) {
|
||||
if (cache.containsKey(path)) {
|
||||
return (List<Boolean>) cache.get(path);
|
||||
} else {
|
||||
cache.put(path, has(path) ? new ArrayList<>(config.getBooleanList(path)) : new ArrayList<>());
|
||||
return getBools(path);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public List<Boolean> getBoolsOrNull(@NotNull final String path) {
|
||||
if (has(path)) {
|
||||
return getBools(path);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getString(@NotNull final String path) {
|
||||
if (cache.containsKey(path)) {
|
||||
return (String) cache.get(path);
|
||||
} else {
|
||||
cache.put(path, StringUtils.translate(Objects.requireNonNull(config.getString(path, ""))));
|
||||
return getString(path);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public String getStringOrNull(@NotNull final String path) {
|
||||
if (has(path)) {
|
||||
return getString(path);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public List<String> getStrings(@NotNull final String path) {
|
||||
if (cache.containsKey(path)) {
|
||||
return (List<String>) cache.get(path);
|
||||
} else {
|
||||
cache.put(path, has(path) ? new ArrayList<>(config.getStringList(path)) : new ArrayList<>());
|
||||
return getStrings(path);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public List<String> getStringsOrNull(@NotNull final String path) {
|
||||
if (has(path)) {
|
||||
return getStrings(path);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getDouble(@NotNull final String path) {
|
||||
if (cache.containsKey(path)) {
|
||||
return (double) cache.get(path);
|
||||
} else {
|
||||
cache.put(path, config.getDouble(path));
|
||||
return getDouble(path);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public Double getDoubleOrNull(@NotNull final String path) {
|
||||
if (has(path)) {
|
||||
return getDouble(path);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public List<Double> getDoubles(@NotNull final String path) {
|
||||
if (cache.containsKey(path)) {
|
||||
return (List<Double>) cache.get(path);
|
||||
} else {
|
||||
cache.put(path, has(path) ? new ArrayList<>(config.getDoubleList(path)) : new ArrayList<>());
|
||||
return getDoubles(path);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public List<Double> getDoublesOrNull(@NotNull final String path) {
|
||||
if (has(path)) {
|
||||
return getDoubles(path);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.willfp.eco.internal.config;
|
||||
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class ConfigSection extends AbstractUndefinedConfig<ConfigurationSection> {
|
||||
/**
|
||||
* Config section.
|
||||
*
|
||||
* @param section The section.
|
||||
*/
|
||||
protected ConfigSection(@NotNull final ConfigurationSection section) {
|
||||
super("subsection");
|
||||
this.init(section);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.willfp.eco.util;
|
||||
|
||||
import com.willfp.eco.util.config.Config;
|
||||
import com.willfp.eco.util.serialization.Deserializer;
|
||||
import com.willfp.eco.util.serialization.EcoSerializable;
|
||||
import com.willfp.eco.util.serialization.NoRegisteredDeserializerException;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@UtilityClass
|
||||
public class SerializationUtils {
|
||||
/**
|
||||
* All registered deserializers.
|
||||
*/
|
||||
private static final Map<String, Deserializer<?>> REGISTRY = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Register deserializer.
|
||||
*
|
||||
* @param clazz The serializable class.
|
||||
* @param deserializer The deserializer.
|
||||
* @param <T> The object type.
|
||||
*/
|
||||
public <T extends EcoSerializable<T>> void registerDeserializer(@NotNull final Class<T> clazz,
|
||||
@NotNull final Deserializer<T> deserializer) {
|
||||
REGISTRY.put(clazz.getName(), deserializer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserialize object.
|
||||
*
|
||||
* @param config The config.
|
||||
* @param clazz The class.
|
||||
* @param <T> The object type.
|
||||
*/
|
||||
@SneakyThrows
|
||||
public <T extends EcoSerializable<T>> T deserialize(@NotNull final Config config,
|
||||
@NotNull final Class<T> clazz) {
|
||||
if (!REGISTRY.containsKey(clazz.getName())) {
|
||||
throw new NoRegisteredDeserializerException("No deserializer registered for " + clazz.getName());
|
||||
}
|
||||
|
||||
return clazz.cast(REGISTRY.get(clazz.getName()).deserialize(config));
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,11 @@ public class StringUtils {
|
||||
*/
|
||||
private static final Pattern HEX_PATTERN = Pattern.compile("&#" + "([A-Fa-f0-9]{6})" + "");
|
||||
|
||||
/**
|
||||
* Regex for hex codes.
|
||||
*/
|
||||
private static final Pattern ALT_HEX_PATTERN = Pattern.compile("\\{#" + "([A-Fa-f0-9]{6})" + "}");
|
||||
|
||||
/**
|
||||
* Translate a string - converts Placeholders and Color codes.
|
||||
*
|
||||
@@ -61,7 +66,22 @@ public class StringUtils {
|
||||
}
|
||||
|
||||
private static String translateHexColorCodes(@NotNull final String message) {
|
||||
Matcher matcher = HEX_PATTERN.matcher(message);
|
||||
String processedMessage = message;
|
||||
for (HexParseMode parseMode : HexParseMode.values()) {
|
||||
processedMessage = translateHexColorCodes(processedMessage, parseMode);
|
||||
}
|
||||
return processedMessage;
|
||||
}
|
||||
|
||||
private static String translateHexColorCodes(@NotNull final String message,
|
||||
@NotNull final HexParseMode mode) {
|
||||
Matcher matcher;
|
||||
if (mode == HexParseMode.CMI) {
|
||||
matcher = ALT_HEX_PATTERN.matcher(message);
|
||||
} else {
|
||||
matcher = HEX_PATTERN.matcher(message);
|
||||
}
|
||||
|
||||
StringBuffer buffer = new StringBuffer(message.length() + 4 * 8);
|
||||
while (matcher.find()) {
|
||||
String group = matcher.group(1);
|
||||
@@ -201,4 +221,9 @@ public class StringUtils {
|
||||
}
|
||||
return string;
|
||||
}
|
||||
|
||||
private enum HexParseMode {
|
||||
CMI,
|
||||
NORMAL
|
||||
}
|
||||
}
|
||||
|
||||
262
eco-util/src/main/java/com/willfp/eco/util/config/Config.java
Normal file
262
eco-util/src/main/java/com/willfp/eco/util/config/Config.java
Normal file
@@ -0,0 +1,262 @@
|
||||
package com.willfp.eco.util.config;
|
||||
|
||||
import com.willfp.eco.util.serialization.EcoSerializable;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface Config {
|
||||
/**
|
||||
* Clears cache.
|
||||
*/
|
||||
void clearCache();
|
||||
|
||||
/**
|
||||
* Get if the config contains a key.
|
||||
*
|
||||
* @param path The key to check.
|
||||
* @return If contained.
|
||||
*/
|
||||
boolean has(@NotNull String path);
|
||||
|
||||
/**
|
||||
* Get config keys.
|
||||
*
|
||||
* @param deep If keys from subsections should be fetched too.
|
||||
* @return A list of keys.
|
||||
*/
|
||||
@NotNull
|
||||
List<String> getKeys(boolean deep);
|
||||
|
||||
/**
|
||||
* Set an object in config.
|
||||
*
|
||||
* @param path The path.
|
||||
* @param object The object.
|
||||
*/
|
||||
void set(@NotNull String path,
|
||||
@NotNull EcoSerializable<?> object);
|
||||
|
||||
/**
|
||||
* Get an object from config.
|
||||
* Default implementations call {@link org.bukkit.configuration.file.YamlConfiguration#get(String)}.
|
||||
*
|
||||
* @param path The path.
|
||||
* @return The object.
|
||||
*/
|
||||
@Nullable
|
||||
Object getRaw(@NotNull String path);
|
||||
|
||||
/**
|
||||
* Get an object from config.
|
||||
*
|
||||
* @param path The path.
|
||||
* @param clazz The class of the object.
|
||||
* @param <T> The type of the object.
|
||||
* @return The object.
|
||||
*/
|
||||
@NotNull <T extends EcoSerializable<T>> T get(@NotNull String path,
|
||||
@NotNull Class<T> clazz);
|
||||
|
||||
/**
|
||||
* Get an object from config.
|
||||
*
|
||||
* @param path The path.
|
||||
* @param clazz The class of the object.
|
||||
* @param <T> The type of the object.
|
||||
* @return The object, or null if not found.
|
||||
*/
|
||||
@Nullable <T extends EcoSerializable<T>> T getOrNull(@NotNull String path,
|
||||
@NotNull Class<T> clazz);
|
||||
|
||||
/**
|
||||
* Get bukkit configuration section from config.
|
||||
*
|
||||
* @param path The key to check.
|
||||
* @return The configuration section. Throws NPE if not found.
|
||||
*/
|
||||
@NotNull
|
||||
@Deprecated
|
||||
ConfigurationSection getSection(@NotNull String path);
|
||||
|
||||
/**
|
||||
* Get bukkit configuration section from config.
|
||||
*
|
||||
* @param path The key to check.
|
||||
* @return The configuration section, or null if not found.
|
||||
*/
|
||||
@Nullable
|
||||
@Deprecated
|
||||
ConfigurationSection getSectionOrNull(@NotNull String path);
|
||||
|
||||
/**
|
||||
* Get subsection from config.
|
||||
*
|
||||
* @param path The key to check.
|
||||
* @return The subsection. Throws NPE if not found.
|
||||
*/
|
||||
@NotNull
|
||||
Config getSubsection(@NotNull String path);
|
||||
|
||||
/**
|
||||
* Get subsection from config.
|
||||
*
|
||||
* @param path The key to check.
|
||||
* @return The subsection, or null if not found.
|
||||
*/
|
||||
@Nullable
|
||||
Config getSubsectionOrNull(@NotNull String path);
|
||||
|
||||
/**
|
||||
* Get an integer from config.
|
||||
*
|
||||
* @param path The key to fetch the value from.
|
||||
* @return The found value, or 0 if not found.
|
||||
*/
|
||||
int getInt(@NotNull String path);
|
||||
|
||||
/**
|
||||
* Get an integer from config.
|
||||
*
|
||||
* @param path The key to fetch the value from.
|
||||
* @return The found value, or null if not found.
|
||||
*/
|
||||
@Nullable
|
||||
Integer getIntOrNull(@NotNull String path);
|
||||
|
||||
/**
|
||||
* Get an integer from config with a specified default (not found) value.
|
||||
*
|
||||
* @param path The key to fetch the value from.
|
||||
* @param def The value to default to if not found.
|
||||
* @return The found value, or the default.
|
||||
*/
|
||||
int getInt(@NotNull String path,
|
||||
int def);
|
||||
|
||||
/**
|
||||
* Get a list of integers from config.
|
||||
*
|
||||
* @param path The key to fetch the value from.
|
||||
* @return The found value, or a blank {@link java.util.ArrayList} if not found.
|
||||
*/
|
||||
@NotNull
|
||||
List<Integer> getInts(@NotNull String path);
|
||||
|
||||
/**
|
||||
* Get a list of integers from config.
|
||||
*
|
||||
* @param path The key to fetch the value from.
|
||||
* @return The found value, or null if not found.
|
||||
*/
|
||||
@Nullable
|
||||
List<Integer> getIntsOrNull(@NotNull String path);
|
||||
|
||||
/**
|
||||
* Get a boolean from config.
|
||||
*
|
||||
* @param path The key to fetch the value from.
|
||||
* @return The found value, or false if not found.
|
||||
*/
|
||||
boolean getBool(@NotNull String path);
|
||||
|
||||
/**
|
||||
* Get a boolean from config.
|
||||
*
|
||||
* @param path The key to fetch the value from.
|
||||
* @return The found value, or null if not found.
|
||||
*/
|
||||
@Nullable
|
||||
Boolean getBoolOrNull(@NotNull String path);
|
||||
|
||||
/**
|
||||
* Get a list of booleans from config.
|
||||
*
|
||||
* @param path The key to fetch the value from.
|
||||
* @return The found value, or a blank {@link java.util.ArrayList} if not found.
|
||||
*/
|
||||
@NotNull
|
||||
List<Boolean> getBools(@NotNull String path);
|
||||
|
||||
/**
|
||||
* Get a list of booleans from config.
|
||||
*
|
||||
* @param path The key to fetch the value from.
|
||||
* @return The found value, or null if not found.
|
||||
*/
|
||||
@Nullable
|
||||
List<Boolean> getBoolsOrNull(@NotNull String path);
|
||||
|
||||
/**
|
||||
* Get a string from config.
|
||||
*
|
||||
* @param path The key to fetch the value from.
|
||||
* @return The found value, or an empty string if not found.
|
||||
*/
|
||||
@NotNull
|
||||
String getString(@NotNull String path);
|
||||
|
||||
/**
|
||||
* Get a string from config.
|
||||
*
|
||||
* @param path The key to fetch the value from.
|
||||
* @return The found value, or null if not found.
|
||||
*/
|
||||
@Nullable
|
||||
String getStringOrNull(@NotNull String path);
|
||||
|
||||
/**
|
||||
* Get a list of strings from config.
|
||||
*
|
||||
* @param path The key to fetch the value from.
|
||||
* @return The found value, or a blank {@link java.util.ArrayList} if not found.
|
||||
*/
|
||||
@NotNull
|
||||
List<String> getStrings(@NotNull String path);
|
||||
|
||||
/**
|
||||
* Get a list of strings from config.
|
||||
*
|
||||
* @param path The key to fetch the value from.
|
||||
* @return The found value, or null if not found.
|
||||
*/
|
||||
@Nullable
|
||||
List<String> getStringsOrNull(@NotNull String path);
|
||||
|
||||
/**
|
||||
* Get a decimal from config.
|
||||
*
|
||||
* @param path The key to fetch the value from.
|
||||
* @return The found value, or 0 if not found.
|
||||
*/
|
||||
double getDouble(@NotNull String path);
|
||||
|
||||
/**
|
||||
* Get a decimal from config.
|
||||
*
|
||||
* @param path The key to fetch the value from.
|
||||
* @return The found value, or null if not found.
|
||||
*/
|
||||
@Nullable
|
||||
Double getDoubleOrNull(@NotNull String path);
|
||||
|
||||
/**
|
||||
* Get a list of decimals from config.
|
||||
*
|
||||
* @param path The key to fetch the value from.
|
||||
* @return The found value, or a blank {@link java.util.ArrayList} if not found.
|
||||
*/
|
||||
@NotNull
|
||||
List<Double> getDoubles(@NotNull String path);
|
||||
|
||||
/**
|
||||
* Get a list of decimals from config.
|
||||
*
|
||||
* @param path The key to fetch the value from.
|
||||
* @return The found value, or null if not found.
|
||||
*/
|
||||
@Nullable
|
||||
List<Double> getDoublesOrNull(@NotNull String path);
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.willfp.eco.util.config;
|
||||
|
||||
import com.willfp.eco.internal.config.AbstractUndefinedConfig;
|
||||
import com.willfp.eco.util.plugin.AbstractEcoPlugin;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public abstract class StaticOptionalConfig extends AbstractUndefinedConfig<YamlConfiguration> {
|
||||
/**
|
||||
* Config implementation for passing YamlConfigurations.
|
||||
* <p>
|
||||
* Does not automatically update.
|
||||
*
|
||||
* @param configName The name of the config
|
||||
* @param plugin The plugin.
|
||||
* @param config The YamlConfiguration handle.
|
||||
* @deprecated Plugin not required.
|
||||
*/
|
||||
@Deprecated
|
||||
public StaticOptionalConfig(@NotNull final String configName,
|
||||
@NotNull final AbstractEcoPlugin plugin,
|
||||
@NotNull final YamlConfiguration config) {
|
||||
this(configName, config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Config implementation for passing YamlConfigurations.
|
||||
* <p>
|
||||
* Does not automatically update.
|
||||
*
|
||||
* @param configName The name of the config
|
||||
* @param config The YamlConfiguration handle.
|
||||
*/
|
||||
public StaticOptionalConfig(@NotNull final String configName,
|
||||
@NotNull final YamlConfiguration config) {
|
||||
super(configName);
|
||||
|
||||
init(config);
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@UtilityClass
|
||||
@SuppressWarnings("deprecation")
|
||||
public class Display {
|
||||
/**
|
||||
* The prefix for lore lines.
|
||||
@@ -40,6 +41,8 @@ public class Display {
|
||||
public void registerDisplayModule(@NotNull final DisplayModule module) {
|
||||
List<DisplayModule> modules = MODULES.get(module.getPriority());
|
||||
|
||||
modules.removeIf(module1 -> module1.getPluginName().equalsIgnoreCase(module.getPluginName()));
|
||||
|
||||
modules.add(module);
|
||||
|
||||
MODULES.put(module.getPriority(), modules);
|
||||
@@ -52,17 +55,21 @@ public class Display {
|
||||
* @return The itemstack.
|
||||
*/
|
||||
public ItemStack display(@NotNull final ItemStack itemStack) {
|
||||
if (isFinalized(itemStack)) {
|
||||
unfinalize(itemStack);
|
||||
return itemStack;
|
||||
if (!itemStack.hasItemMeta()) {
|
||||
return itemStack; // return early if there's no customization of the item
|
||||
}
|
||||
|
||||
Map<String, Object[]> pluginVarArgs = new HashMap<>();
|
||||
|
||||
for (DisplayPriority priority : DisplayPriority.values()) {
|
||||
List<DisplayModule> modules = MODULES.get(priority);
|
||||
for (DisplayModule module : modules) {
|
||||
pluginVarArgs.put(module.getPluginName(), module.generateVarArgs(itemStack));
|
||||
}
|
||||
}
|
||||
|
||||
revert(itemStack);
|
||||
|
||||
if (!itemStack.hasItemMeta()) {
|
||||
return itemStack;
|
||||
}
|
||||
|
||||
ItemMeta meta = itemStack.getItemMeta();
|
||||
|
||||
if (meta == null) {
|
||||
@@ -72,7 +79,9 @@ public class Display {
|
||||
for (DisplayPriority priority : DisplayPriority.values()) {
|
||||
List<DisplayModule> modules = MODULES.get(priority);
|
||||
for (DisplayModule module : modules) {
|
||||
Object[] varargs = pluginVarArgs.get(module.getPluginName());
|
||||
module.display(itemStack);
|
||||
module.display(itemStack, varargs);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,9 +105,8 @@ public class Display {
|
||||
* @return The itemstack.
|
||||
*/
|
||||
public ItemStack revert(@NotNull final ItemStack itemStack) {
|
||||
if (isFinalized(itemStack)) {
|
||||
if (Display.isFinalized(itemStack)) {
|
||||
unfinalize(itemStack);
|
||||
return itemStack;
|
||||
}
|
||||
|
||||
if (!itemStack.hasItemMeta()) {
|
||||
@@ -113,16 +121,11 @@ public class Display {
|
||||
|
||||
List<String> lore = meta.getLore();
|
||||
|
||||
if (lore == null) {
|
||||
lore = new ArrayList<>();
|
||||
if (lore != null && lore.removeIf(line -> line.startsWith(Display.PREFIX))) { // only apply lore modification if needed
|
||||
meta.setLore(lore);
|
||||
itemStack.setItemMeta(meta);
|
||||
}
|
||||
|
||||
lore.removeIf(line -> line.startsWith(Display.PREFIX));
|
||||
|
||||
meta.setLore(lore);
|
||||
|
||||
itemStack.setItemMeta(meta);
|
||||
|
||||
for (DisplayPriority priority : DisplayPriority.values()) {
|
||||
List<DisplayModule> modules = MODULES.get(priority);
|
||||
for (DisplayModule module : modules) {
|
||||
|
||||
@@ -29,13 +29,51 @@ public abstract class DisplayModule extends PluginDependent {
|
||||
* Display an item.
|
||||
*
|
||||
* @param itemStack The item.
|
||||
* @param args Optional args for display.
|
||||
*/
|
||||
protected abstract void display(@NotNull ItemStack itemStack);
|
||||
protected void display(@NotNull final ItemStack itemStack,
|
||||
@NotNull final Object... args) {
|
||||
// Technically optional.
|
||||
}
|
||||
|
||||
/**
|
||||
* Display an item.
|
||||
* <p>
|
||||
* This method exists for parity with older plugins that don't include the varargs.
|
||||
*
|
||||
* @param itemStack The item.
|
||||
* @deprecated Use {@link this#display(ItemStack, Object...)} instead.
|
||||
*/
|
||||
@Deprecated
|
||||
protected void display(@NotNull final ItemStack itemStack) {
|
||||
// Technically optional.
|
||||
}
|
||||
|
||||
/**
|
||||
* Revert an item.
|
||||
*
|
||||
* @param itemStack The item.
|
||||
*/
|
||||
protected abstract void revert(@NotNull ItemStack itemStack);
|
||||
protected void revert(@NotNull final ItemStack itemStack) {
|
||||
// Technically optoinal.
|
||||
}
|
||||
|
||||
/**
|
||||
* Create varargs to pass back to itemstack after reverting, but before display.
|
||||
*
|
||||
* @param itemStack The itemStack.
|
||||
* @return The plugin-specific varargs.
|
||||
*/
|
||||
protected Object[] generateVarArgs(@NotNull final ItemStack itemStack) {
|
||||
return new Object[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get name of plugin.
|
||||
*
|
||||
* @return The plugin name.
|
||||
*/
|
||||
final String getPluginName() {
|
||||
return super.getPlugin().getPluginName();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.willfp.eco.util.serialization;
|
||||
|
||||
import com.willfp.eco.util.config.Config;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public abstract class Deserializer<T extends EcoSerializable<T>> {
|
||||
/**
|
||||
* Deserialize a config into an object.
|
||||
*
|
||||
* @param config The config.
|
||||
* @return The object.
|
||||
*/
|
||||
public abstract T deserialize(@NotNull Config config);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.willfp.eco.util.serialization;
|
||||
|
||||
import com.willfp.eco.util.config.Config;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public interface EcoSerializable<T> {
|
||||
/**
|
||||
* Serialize an object into a config.
|
||||
*
|
||||
* @return The config.
|
||||
*/
|
||||
@NotNull
|
||||
Config serialize();
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.willfp.eco.util.serialization;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class NoRegisteredDeserializerException extends Exception {
|
||||
/**
|
||||
* Create new NoRegisteredDeserializerException.
|
||||
*
|
||||
* @param message The message.
|
||||
*/
|
||||
public NoRegisteredDeserializerException(@NotNull final String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -1,2 +1,2 @@
|
||||
version = 4.0.0
|
||||
version = 4.3.0
|
||||
plugin-name = eco
|
||||
BIN
lib/CombatLogX.jar
Normal file
BIN
lib/CombatLogX.jar
Normal file
Binary file not shown.
BIN
lib/Newbie Helper.jar
Normal file
BIN
lib/Newbie Helper.jar
Normal file
Binary file not shown.
BIN
lib/ULands-Addon-2.8.7.jar
Normal file
BIN
lib/ULands-Addon-2.8.7.jar
Normal file
Binary file not shown.
Reference in New Issue
Block a user