Compare commits

..

17 Commits
6.0.2 ... 6.1.0

Author SHA1 Message Date
Auxilor
c36c0c247f Fixed enchant support in crafting recipes 2021-07-26 19:02:11 +01:00
Auxilor
6611a0f82c Fixed missing javadoc 2021-07-26 18:39:18 +01:00
Auxilor
625b981b81 Added support for enchantments in recipes 2021-07-26 18:38:12 +01:00
Auxilor
d8607917a1 Updated to 6.1.0 2021-07-26 18:26:13 +01:00
Auxilor
30d5f54459 Added EcoPlugin#reloadWithTime 2021-07-26 18:25:56 +01:00
Auxilor
a59c05174f Fixed bug with unloaded plugins 2021-07-26 18:21:52 +01:00
Auxilor
cf01abcf87 Added out of world check to blocks 2021-07-26 18:20:13 +01:00
Auxilor
70a4a06d4f Updated to 6.0.6 2021-07-24 01:12:55 +01:00
Auxilor
bbee18fd8a JSON config files now have same deletion behaviour as yaml config files 2021-07-24 01:12:46 +01:00
Auxilor
26ab9a327d Added warnings to invalid extensions 2021-07-23 22:14:14 +01:00
Auxilor
0676f5fa33 Updated to 6.0.5 2021-07-23 22:11:41 +01:00
Auxilor
051b95ad88 Extension loading change 2021-07-23 22:11:32 +01:00
Auxilor
d786014cbc Updated to 6.0.4 2021-07-22 18:40:17 +01:00
Auxilor
b62bb48bb6 Fixed loadable config reloading 2021-07-22 18:39:59 +01:00
Auxilor
b238a10209 Fixed EcoUpdatableYamlConfig not automatically registering itsefl 2021-07-22 18:34:00 +01:00
Auxilor
251049f1f1 Updated to 6.0.3 2021-07-22 16:42:00 +01:00
Auxilor
16d146dba0 Fixed ArrowUtils 2021-07-22 16:41:43 +01:00
12 changed files with 228 additions and 31 deletions

View File

@@ -52,6 +52,7 @@ import java.util.stream.Collectors;
* <b>IMPORTANT: When reloading a plugin, all runnables / tasks will * <b>IMPORTANT: When reloading a plugin, all runnables / tasks will
* be cancelled.</b> * be cancelled.</b>
*/ */
@SuppressWarnings("unused")
public abstract class EcoPlugin extends JavaPlugin { public abstract class EcoPlugin extends JavaPlugin {
/** /**
* The spigot resource ID of the plugin. * The spigot resource ID of the plugin.
@@ -464,7 +465,7 @@ public abstract class EcoPlugin extends JavaPlugin {
} }
/** /**
* Default code to be executed on plugin reload. * Reload the plugin.
*/ */
public final void reload() { public final void reload() {
this.getConfigHandler().updateConfigs(); this.getConfigHandler().updateConfigs();
@@ -476,6 +477,19 @@ public abstract class EcoPlugin extends JavaPlugin {
this.handleReload(); this.handleReload();
} }
/**
* Reload the plugin and return the time taken to reload.
*
* @return The time.
*/
public final long reloadWithTime() {
long startTime = System.currentTimeMillis();
this.reload();
return System.currentTimeMillis() - startTime;
}
/** /**
* The plugin-specific code to be executed on enable. * The plugin-specific code to be executed on enable.
* <p> * <p>

View File

@@ -1,15 +1,21 @@
package com.willfp.eco.core.items; package com.willfp.eco.core.items;
import com.willfp.eco.core.items.builder.ItemBuilder;
import com.willfp.eco.core.items.builder.ItemStackBuilder;
import com.willfp.eco.core.recipe.parts.EmptyTestableItem; import com.willfp.eco.core.recipe.parts.EmptyTestableItem;
import com.willfp.eco.core.recipe.parts.MaterialTestableItem; import com.willfp.eco.core.recipe.parts.MaterialTestableItem;
import com.willfp.eco.core.recipe.parts.ModifiedTestableItem;
import com.willfp.eco.core.recipe.parts.TestableStack; import com.willfp.eco.core.recipe.parts.TestableStack;
import com.willfp.eco.util.NamespacedKeyUtils; import com.willfp.eco.util.NamespacedKeyUtils;
import lombok.experimental.UtilityClass; import lombok.experimental.UtilityClass;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.NamespacedKey; import org.bukkit.NamespacedKey;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
@@ -36,6 +42,15 @@ public final class Items {
REGISTRY.put(key, part); REGISTRY.put(key, part);
} }
/**
* Remove an item.
*
* @param key The key of the recipe part.
*/
public void removeCustomItem(@NotNull final NamespacedKey key) {
REGISTRY.remove(key);
}
/** /**
* Lookup item from string. * Lookup item from string.
* <p> * <p>
@@ -45,14 +60,21 @@ public final class Items {
* @return The found testable item, or an empty item if not found. * @return The found testable item, or an empty item if not found.
*/ */
public TestableItem lookup(@NotNull final String key) { public TestableItem lookup(@NotNull final String key) {
String[] split = key.toLowerCase().split(":"); String[] args = key.split(" ");
if (args.length == 0) {
return new EmptyTestableItem();
}
TestableItem item = null;
String[] split = args[0].toLowerCase().split(":");
if (split.length == 1) { if (split.length == 1) {
Material material = Material.getMaterial(key.toUpperCase()); Material material = Material.getMaterial(args[0].toUpperCase());
if (material == null || material == Material.AIR) { if (material == null || material == Material.AIR) {
return new EmptyTestableItem(); return new EmptyTestableItem();
} }
return new MaterialTestableItem(material); item = new MaterialTestableItem(material);
} }
if (split.length == 2) { if (split.length == 2) {
@@ -63,18 +85,66 @@ public final class Items {
if (material == null || material == Material.AIR) { if (material == null || material == Material.AIR) {
return new EmptyTestableItem(); return new EmptyTestableItem();
} }
return new TestableStack(new MaterialTestableItem(material), Integer.parseInt(split[1])); item = new TestableStack(new MaterialTestableItem(material), Integer.parseInt(split[1]));
} else { } else {
return part; item = part;
} }
} }
if (split.length == 3) { if (split.length == 3) {
CustomItem part = REGISTRY.get(NamespacedKeyUtils.create(split[0], split[1])); CustomItem part = REGISTRY.get(NamespacedKeyUtils.create(split[0], split[1]));
return part == null ? new EmptyTestableItem() : new TestableStack(part, Integer.parseInt(split[2])); item = part == null ? new EmptyTestableItem() : new TestableStack(part, Integer.parseInt(split[2]));
} }
return new EmptyTestableItem(); if (item == null || item instanceof EmptyTestableItem) {
return new EmptyTestableItem();
}
String[] enchantArgs = Arrays.copyOfRange(args, 1, args.length);
Map<Enchantment, Integer> requiredEnchantments = new HashMap<>();
for (String enchantArg : enchantArgs) {
String[] enchantArgSplit = enchantArg.split(":");
Enchantment enchantment = Enchantment.getByKey(NamespacedKey.minecraft(enchantArgSplit[0].toLowerCase()));
int level = Integer.parseInt(enchantArgSplit[1]);
requiredEnchantments.put(enchantment, level);
}
if (requiredEnchantments.isEmpty()) {
return item;
}
ItemBuilder builder = new ItemStackBuilder(item.getItem());
requiredEnchantments.forEach(builder::addEnchantment);
ItemStack example = builder.build();
return new ModifiedTestableItem(
item,
itemStack -> {
if (!itemStack.hasItemMeta()) {
return false;
}
ItemMeta meta = itemStack.getItemMeta();
assert meta != null;
for (Map.Entry<Enchantment, Integer> entry : requiredEnchantments.entrySet()) {
if (!meta.hasEnchant(entry.getKey())) {
return false;
}
if (meta.getEnchantLevel(entry.getKey()) < entry.getValue()) {
return false;
}
}
return true;
},
example
);
} }
/** /**

View File

@@ -0,0 +1,63 @@
package com.willfp.eco.core.recipe.parts;
import com.willfp.eco.core.items.TestableItem;
import lombok.Getter;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.function.Predicate;
/**
* Existing testable items with an extra filter.
*
* @see com.willfp.eco.core.items.CustomItem
*/
public class ModifiedTestableItem implements TestableItem {
/**
* The item.
*/
private final TestableItem handle;
/**
* The amount.
*/
@Getter
private final Predicate<ItemStack> test;
/**
* The item for the modified test.
*/
private final ItemStack example;
/**
* Create a new modified testable item.
*
* @param item The item.
* @param test The test.
* @param example The example.
*/
public ModifiedTestableItem(@NotNull final TestableItem item,
@NotNull final Predicate<ItemStack> test,
@NotNull final ItemStack example) {
this.handle = item;
this.test = test;
this.example = example;
}
/**
* If the item matches the material.
*
* @param itemStack The item to test.
* @return If the item is of the specified material.
*/
@Override
public boolean matches(@Nullable final ItemStack itemStack) {
return itemStack != null && handle.matches(itemStack) && test.test(itemStack);
}
@Override
public ItemStack getItem() {
return example;
}
}

View File

@@ -29,10 +29,10 @@ public class ArrowUtils {
return null; return null;
} }
if (!(values.get(0) instanceof ItemStack)) { if (!(values.get(0).value() instanceof ItemStack)) {
return null; return null;
} }
return (ItemStack) values.get(0); return (ItemStack) values.get(0).value();
} }
} }

View File

@@ -112,6 +112,10 @@ public class BlockUtils {
Validate.isTrue(initialized, "Must be initialized!"); Validate.isTrue(initialized, "Must be initialized!");
Validate.notNull(blockBreakConsumer, "Must be initialized!"); Validate.notNull(blockBreakConsumer, "Must be initialized!");
if (block.getLocation().getY() < 0 || block.getLocation().getY() > 256) {
return;
}
blockBreakConsumer.accept(player, block); blockBreakConsumer.accept(player, block);
} }

View File

@@ -1,6 +1,8 @@
package com.willfp.eco.internal; package com.willfp.eco.internal;
import com.willfp.eco.core.EcoPlugin; import com.willfp.eco.core.EcoPlugin;
import com.willfp.eco.core.items.CustomItem;
import com.willfp.eco.core.items.Items;
import com.willfp.eco.core.proxy.Cleaner; import com.willfp.eco.core.proxy.Cleaner;
import com.willfp.eco.internal.proxy.EcoProxyFactory; import com.willfp.eco.internal.proxy.EcoProxyFactory;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -18,6 +20,12 @@ public class EcoCleaner implements Cleaner {
Plugins.LOADED_ECO_PLUGINS.remove(plugin.getName().toLowerCase()); Plugins.LOADED_ECO_PLUGINS.remove(plugin.getName().toLowerCase());
for (CustomItem customItem : Items.getCustomItems()) {
if (customItem.getKey().getNamespace().equalsIgnoreCase(plugin.getName().toLowerCase())) {
Items.removeCustomItem(customItem.getKey());
}
}
if (plugin.getClass().getClassLoader() instanceof URLClassLoader urlClassLoader) { if (plugin.getClass().getClassLoader() instanceof URLClassLoader urlClassLoader) {
try { try {
urlClassLoader.close(); urlClassLoader.close();

View File

@@ -63,6 +63,14 @@ public class EcoLoadableJSONConfig extends EcoJSONConfigWrapper implements Loada
plugin.getConfigHandler().addConfig(this); plugin.getConfigHandler().addConfig(this);
} }
public void reloadFromFile() {
try {
init(this.configFile);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override @Override
public void createFile() { public void createFile() {
String resourcePath = getResourcePath(); String resourcePath = getResourcePath();
@@ -108,8 +116,9 @@ public class EcoLoadableJSONConfig extends EcoJSONConfigWrapper implements Loada
@Override @Override
public void save() throws IOException { public void save() throws IOException {
configFile.delete(); if (configFile.delete()) {
Files.write(configFile.toPath(), this.toPlaintext().getBytes(), StandardOpenOption.CREATE, StandardOpenOption.WRITE); Files.write(configFile.toPath(), this.toPlaintext().getBytes(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
}
} }

View File

@@ -5,7 +5,9 @@ import com.willfp.eco.core.PluginDependent;
import com.willfp.eco.core.config.interfaces.LoadableConfig; import com.willfp.eco.core.config.interfaces.LoadableConfig;
import com.willfp.eco.core.config.updating.ConfigHandler; import com.willfp.eco.core.config.updating.ConfigHandler;
import com.willfp.eco.core.config.updating.ConfigUpdater; import com.willfp.eco.core.config.updating.ConfigUpdater;
import com.willfp.eco.internal.config.json.EcoLoadableJSONConfig;
import com.willfp.eco.internal.config.updating.exceptions.InvalidUpdateMethodException; import com.willfp.eco.internal.config.updating.exceptions.InvalidUpdateMethodException;
import com.willfp.eco.internal.config.yaml.EcoLoadableYamlConfig;
import com.willfp.eco.internal.config.yaml.EcoUpdatableYamlConfig; import com.willfp.eco.internal.config.yaml.EcoUpdatableYamlConfig;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.reflections.Reflections; import org.reflections.Reflections;
@@ -76,6 +78,12 @@ public class EcoConfigHandler extends PluginDependent<EcoPlugin> implements Conf
if (config instanceof EcoUpdatableYamlConfig updatableYamlConfig) { if (config instanceof EcoUpdatableYamlConfig updatableYamlConfig) {
updatableYamlConfig.update(); updatableYamlConfig.update();
} }
if (config instanceof EcoLoadableYamlConfig ecoLoadableYamlConfig) {
ecoLoadableYamlConfig.reloadFromFile();
}
if (config instanceof EcoLoadableJSONConfig ecoLoadableJSONConfig) {
ecoLoadableJSONConfig.reloadFromFile();
}
} }
} }
} }

View File

@@ -5,6 +5,7 @@ import com.willfp.eco.core.config.interfaces.LoadableConfig;
import com.willfp.eco.core.config.interfaces.WrappedYamlConfiguration; import com.willfp.eco.core.config.interfaces.WrappedYamlConfiguration;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.Getter; import lombok.Getter;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -49,9 +50,19 @@ public class EcoLoadableYamlConfig extends EcoYamlConfigWrapper<YamlConfiguratio
} }
this.configFile = new File(directory, this.name); this.configFile = new File(directory, this.name);
this.getPlugin().getConfigHandler().addConfig(this);
init(YamlConfiguration.loadConfiguration(configFile)); init(YamlConfiguration.loadConfiguration(configFile));
} }
public void reloadFromFile() {
try {
this.getHandle().load(this.getConfigFile());
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
}
}
@Override @Override
public void createFile() { public void createFile() {
String resourcePath = getResourcePath(); String resourcePath = getResourcePath();

View File

@@ -30,6 +30,8 @@ public class EcoUpdatableYamlConfig extends EcoLoadableYamlConfig {
this.updateBlacklist = new ArrayList<>(Arrays.asList(updateBlacklist)); this.updateBlacklist = new ArrayList<>(Arrays.asList(updateBlacklist));
this.updateBlacklist.removeIf(String::isEmpty); this.updateBlacklist.removeIf(String::isEmpty);
plugin.getConfigHandler().addConfig(this);
update(); update();
} }

View File

@@ -3,11 +3,12 @@ package com.willfp.eco.internal.extensions;
import com.willfp.eco.core.EcoPlugin; import com.willfp.eco.core.EcoPlugin;
import com.willfp.eco.core.PluginDependent; import com.willfp.eco.core.PluginDependent;
import com.willfp.eco.core.config.interfaces.Config;
import com.willfp.eco.core.config.yaml.YamlTransientConfig;
import com.willfp.eco.core.extensions.Extension; import com.willfp.eco.core.extensions.Extension;
import com.willfp.eco.core.extensions.ExtensionLoader; import com.willfp.eco.core.extensions.ExtensionLoader;
import com.willfp.eco.core.extensions.ExtensionMetadata; import com.willfp.eco.core.extensions.ExtensionMetadata;
import com.willfp.eco.core.extensions.MalformedExtensionException; import com.willfp.eco.core.extensions.MalformedExtensionException;
import org.apache.commons.lang.Validate;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -17,8 +18,6 @@ import java.io.InputStreamReader;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
@@ -50,12 +49,12 @@ public class EcoExtensionLoader extends PluginDependent<EcoPlugin> implements Ex
try { try {
loadExtension(extensionJar); loadExtension(extensionJar);
} catch (MalformedExtensionException e) { } catch (MalformedExtensionException e) {
this.getPlugin().getLogger().severe(extensionJar.getName() + " caused MalformedExtensionException: " + e.getMessage()); this.getPlugin().getLogger().warning(extensionJar.getName() + " caused an error!");
} }
} }
} }
private void loadExtension(@NotNull final File extensionJar) { private void loadExtension(@NotNull final File extensionJar) throws MalformedExtensionException {
URL url = null; URL url = null;
try { try {
url = extensionJar.toURI().toURL(); url = extensionJar.toURI().toURL();
@@ -71,22 +70,31 @@ public class EcoExtensionLoader extends PluginDependent<EcoPlugin> implements Ex
throw new MalformedExtensionException("No extension.yml found in " + extensionJar.getName()); throw new MalformedExtensionException("No extension.yml found in " + extensionJar.getName());
} }
YamlConfiguration extensionYml = YamlConfiguration.loadConfiguration(new InputStreamReader(ymlIn)); Config extensionYml = new YamlTransientConfig(YamlConfiguration.loadConfiguration(new InputStreamReader(ymlIn)));
Set<String> keys = extensionYml.getKeys(false); String mainClass = extensionYml.getStringOrNull("main");
ArrayList<String> required = new ArrayList<>(Arrays.asList("main", "name", "version")); String name = extensionYml.getStringOrNull("name");
required.removeAll(keys); String version = extensionYml.getStringOrNull("version");
if (!required.isEmpty()) { String author = extensionYml.getStringOrNull("author");
throw new MalformedExtensionException("Invalid extension.yml found in " + extensionJar.getName() + " - Missing: " + String.join(", ", required));
if (mainClass == null) {
throw new MalformedExtensionException("Invalid extension.yml found in " + extensionJar.getName());
} }
String mainClass = extensionYml.getString("main"); if (name == null) {
String name = extensionYml.getString("name"); this.getPlugin().getLogger().warning(extensionJar.getName() + " doesn't have a name!");
String version = extensionYml.getString("version"); name = "Unnamed Extension " + extensionJar.getName();
String author = extensionYml.getString("author"); }
Validate.notNull(name, "Name is missing!");
Validate.notNull(version, "Version is missing!"); if (version == null) {
Validate.notNull(author, "Author is missing!"); this.getPlugin().getLogger().warning(extensionJar.getName() + " doesn't have a version!");
version = "1.0.0";
}
if (author == null) {
this.getPlugin().getLogger().warning(extensionJar.getName() + " doesn't have an author!");
author = "Unnamed Author";
}
ExtensionMetadata metadata = new ExtensionMetadata(version, name, author); ExtensionMetadata metadata = new ExtensionMetadata(version, name, author);

View File

@@ -1,2 +1,2 @@
version = 6.0.2 version = 6.1.0
plugin-name = eco plugin-name = eco