diff --git a/README.md b/README.md index 298954a..b64d409 100644 --- a/README.md +++ b/README.md @@ -73,9 +73,22 @@ The plugin also has an optional feature to protect resource value, via daily pla categories: '{mmo_items, 100, 0.015625, SWORD:CUTLASS}' ``` The above example gives one cutlass per day to a town. -12. If you want to translate material names into a non-english language, first ensure you have the [*LangUtils*](https://ci.nyaacat.com/job/LanguageUtils/job/1.17/) plugin installed, then set your preferred language in the *TownyResources* Config.yml file. -13. Edit the *TownyResources* config.yml file, and set `surveys > enabled` to `true`. -14. Run `/ta resources reload`, then `/ta reload`. +12. If you want to use the [*ItemsAdder*](https://www.spigotmc.org/resources/73355/) plugin with *TownyResources*: + - Town Production: +
You can add *ItemsAdder* items to the offers list, simply by using *ItemsAdder* item names. Example: + ``` + In the TownyResources config, use the item name from the ItemsAdder config as shown below: + categories: {crystal_category_name, 100, 1, crystal_block} + + In ItemsAdder item config (plugins/ItemsAdder/contents/iasurvival/configs/blocks/items/minerals/crystal_block.yml) + items: + crystal_block: <---- this is the name you use in TownyResources. + enabled: true + ``` + The above example gives one stack of Crystal blocks. +13. If you want to translate material names into a non-english language, first ensure you have the [*LangUtils*](https://ci.nyaacat.com/job/LanguageUtils/job/1.17/) plugin installed, then set your preferred language in the *TownyResources* Config.yml file. +14. Edit the *TownyResources* config.yml file, and set `surveys > enabled` to `true`. +15. Run `/ta resources reload`, then `/ta reload`. ## (Optional Installation Step) SurveySite Plots & Biome-dependent resources. - An (Optional) feature in TownyResources is a new Towny plot type, called the SurveySite. diff --git a/pom.xml b/pom.xml index 4ed80e8..35e26e1 100644 --- a/pom.xml +++ b/pom.xml @@ -89,6 +89,12 @@ 6.9.2-SNAPSHOT provided + + com.github.LoneDev6 + api-itemsadder + 3.6.1 + provided + com.github.bsideup.jabel jabel-javac-plugin diff --git a/src/main/java/io/github/townyadvanced/townyresources/TownyResources.java b/src/main/java/io/github/townyadvanced/townyresources/TownyResources.java index fc60f78..7ef5011 100644 --- a/src/main/java/io/github/townyadvanced/townyresources/TownyResources.java +++ b/src/main/java/io/github/townyadvanced/townyresources/TownyResources.java @@ -41,7 +41,7 @@ public class TownyResources extends JavaPlugin { private static boolean slimeFunInstalled; private static boolean mythicMobsInstalled; private static boolean mmmoItemsInstalled; - + private static boolean itemsAdderInstalled; public TownyResources() { plugin = this; @@ -88,10 +88,9 @@ public boolean loadAll() { loadLocalization(false); new TownyResourcesMessagingUtil(this); - //Load controllers - TownResourceOffersController.loadAllResourceOfferCategories(); - //WARNING: Do not try to recalculate production here, because unless SW has been loaded first, the results will be incorrect. - PlayerExtractionLimitsController.loadAllResourceExtractionCategories(); + // Run later to give items plugins a chance to load their items (ItemsAdder specifically.) + getScheduler().runLater(() -> loadOffersAndExtractionLimitCategories(), 2L); + //Load commands and listeners registerCommands(); registerListeners(); @@ -112,6 +111,24 @@ public boolean loadAll() { return true; } + private void loadOffersAndExtractionLimitCategories() { + try { + TownResourceOffersController.loadAllResourceOfferCategories(); + PlayerExtractionLimitsController.loadAllResourceExtractionCategories(); + } catch (TownyException te) { + severe(te.getMessage()); + severe("TownyResources failed to load offers and extraction categories!"); + onDisable(); + return; + } catch (Exception e) { + severe(e.getMessage()); + e.printStackTrace(); + severe("TownyResources failed to load offers and extraction categories!"); + onDisable(); + return; + } + } + /** * Re-Load towny resources * @@ -222,6 +239,10 @@ public boolean isMythicMobsV5() { return mythicMobsInstalled; } + public boolean isItemsAdderInstalled() { + return itemsAdderInstalled; + } + public boolean isMMOItemsInstalled() { return mmmoItemsInstalled; } @@ -264,6 +285,11 @@ private void setupIntegrationsWithOtherPlugins() { } } + Plugin itemsAdder = Bukkit.getPluginManager().getPlugin("ItemsAdder"); + itemsAdderInstalled = itemsAdder != null; + if (itemsAdderInstalled) + info(" ItemsAdder Integration Enabled"); + Plugin mmmoItems = Bukkit.getPluginManager().getPlugin("MMOItems"); mmmoItemsInstalled = mmmoItems != null; if (mmmoItemsInstalled) diff --git a/src/main/java/io/github/townyadvanced/townyresources/controllers/PlayerExtractionLimitsController.java b/src/main/java/io/github/townyadvanced/townyresources/controllers/PlayerExtractionLimitsController.java index 606ff8c..ecd6358 100644 --- a/src/main/java/io/github/townyadvanced/townyresources/controllers/PlayerExtractionLimitsController.java +++ b/src/main/java/io/github/townyadvanced/townyresources/controllers/PlayerExtractionLimitsController.java @@ -413,6 +413,14 @@ private static void sendLimitReachedWarningMessage(Player player, CategoryExtrac } public static ResourceExtractionCategory getResourceExtractionCategory(String givenCategoryName) { + // This should not be able to happen outside of a 2 tick window at startup and is here only as a failsafe. + if (resourceExtractionCategories.isEmpty()) + try { + loadAllResourceExtractionCategories(); + } catch (Exception e) { + e.printStackTrace(); + TownyResources.severe("Failed to load the Resource Extraction Categories!"); + } for(ResourceExtractionCategory resourceExtractionCategory: resourceExtractionCategories) if(resourceExtractionCategory.getName().equals(givenCategoryName)) return resourceExtractionCategory; diff --git a/src/main/java/io/github/townyadvanced/townyresources/controllers/TownResourceCollectionController.java b/src/main/java/io/github/townyadvanced/townyresources/controllers/TownResourceCollectionController.java index 6ca247f..0901f74 100644 --- a/src/main/java/io/github/townyadvanced/townyresources/controllers/TownResourceCollectionController.java +++ b/src/main/java/io/github/townyadvanced/townyresources/controllers/TownResourceCollectionController.java @@ -7,6 +7,7 @@ import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; import io.github.townyadvanced.townyresources.TownyResources; import io.github.townyadvanced.townyresources.metadata.TownyResourcesGovernmentMetaDataController; +import io.github.townyadvanced.townyresources.util.ItemsAdderUtil; import io.github.townyadvanced.townyresources.util.MMOItemsUtil; import io.github.townyadvanced.townyresources.util.MythicMobsUtil; import io.github.townyadvanced.townyresources.util.TownyResourcesMessagingUtil; @@ -139,6 +140,17 @@ private static List buildItemStackList(Player player, Set getResourceOfferCategoryList() { + // Realistically this shouldn't happen, the list is populated 2 ticks after server load. This is only here as a failsafe. + if (resourceOfferCategoryList.isEmpty()) + try { + loadAllResourceOfferCategories(); + } catch (TownyException e) { + TownyResources.severe(e.getMessage()); + TownyResources.severe("TownyResources failed to load offer category list!"); + } return resourceOfferCategoryList; } public static Map getMaterialToResourceOfferCategoryMap() { + // Realistically this shouldn't happen, the map is populated 2 ticks after server load. This is only here as a failsafe. + if (materialToResourceOfferCategoryMap.isEmpty()) + try { + loadAllResourceOfferCategories(); + } catch (TownyException e) { + TownyResources.severe(e.getMessage()); + TownyResources.severe("TownyResources failed to load resource offer category map!"); + } return materialToResourceOfferCategoryMap; } } diff --git a/src/main/java/io/github/townyadvanced/townyresources/settings/TownyResourcesConfigNodes.java b/src/main/java/io/github/townyadvanced/townyresources/settings/TownyResourcesConfigNodes.java index 00873e2..afe71f6 100644 --- a/src/main/java/io/github/townyadvanced/townyresources/settings/TownyResourcesConfigNodes.java +++ b/src/main/java/io/github/townyadvanced/townyresources/settings/TownyResourcesConfigNodes.java @@ -309,7 +309,7 @@ public enum TownyResourcesConfigNodes { "# The default values give a weight of 200 to strategic resources (coal, iron etc.), and 100 to most non-strategic resources. (wheat, quartz etc.)", "# This gives each survey approx. 70% chance to discover a strategic resources, and a 30% to discover a non-strategic resource.", "# ", - "# This list supports Slimefun, MythicMobs and MMOItems items as well. When entering an MMOItem use the TYPE:ID format,", + "# This list supports Slimefun, MythicMobs, ItemsAdder and MMOItems items as well. When entering an MMOItem use the TYPE:ID format,", "# ie: SWORD:CUTLASS where TownyResources expects a material."), TOWN_RESOURCES_OFFERS_CATEGORIES_BIOMES( "town_resources.offers.categories_biomes", @@ -391,7 +391,7 @@ public enum TownyResourcesConfigNodes { "# Ie: the amounts that are set in the above offers.categories sections are exactly how many each town will receive, every time.", "# Materials put into this list, will not have their amounts modified by the Town's Town_Level resourceProductionModifier (in the Towny config,)", "# or by the productivity_percentage_per_resource_level settings in this config.", - "# You may find it useful for controlling very valuable materials or custom items from Slimefun, MythicMobs or MMOItems."), + "# You may find it useful for controlling very valuable materials or custom items from Slimefun, MythicMobs, ItemsAdder or MMOItems."), TOWN_RESOURCES_LANGUAGE( "town_resources.language", "", diff --git a/src/main/java/io/github/townyadvanced/townyresources/settings/TownyResourcesSettings.java b/src/main/java/io/github/townyadvanced/townyresources/settings/TownyResourcesSettings.java index cbf1298..ed3317a 100644 --- a/src/main/java/io/github/townyadvanced/townyresources/settings/TownyResourcesSettings.java +++ b/src/main/java/io/github/townyadvanced/townyresources/settings/TownyResourcesSettings.java @@ -20,6 +20,7 @@ import io.github.townyadvanced.townyresources.TownyResources; import io.github.townyadvanced.townyresources.objects.ResourceExtractionCategory; import io.github.townyadvanced.townyresources.objects.ResourceOfferCategory; +import io.github.townyadvanced.townyresources.util.ItemsAdderUtil; import io.github.townyadvanced.townyresources.util.MMOItemsUtil; import io.github.townyadvanced.townyresources.util.MythicMobsUtil; @@ -264,6 +265,11 @@ private static boolean isValidMaterial(String materialName) { && MMOItemsUtil.isValidItem(materialName)) return true; + // ItemsAdder integration + if (TownyResources.getPlugin().isItemsAdderInstalled() + && ItemsAdderUtil.isValidItem(materialName)) + return true; + return false; //Unknown material } diff --git a/src/main/java/io/github/townyadvanced/townyresources/util/ItemsAdderUtil.java b/src/main/java/io/github/townyadvanced/townyresources/util/ItemsAdderUtil.java new file mode 100644 index 0000000..ad710a4 --- /dev/null +++ b/src/main/java/io/github/townyadvanced/townyresources/util/ItemsAdderUtil.java @@ -0,0 +1,30 @@ +package io.github.townyadvanced.townyresources.util; + +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.Nullable; + +import dev.lone.itemsadder.api.CustomStack; + +public class ItemsAdderUtil { + + @Nullable + public static ItemStack getItemStack(String materialName, int amount) { + CustomStack itemsAdderItem = CustomStack.getInstance(materialName); + if (itemsAdderItem == null) + return null; + ItemStack itemStack = itemsAdderItem.getItemStack(); + itemStack.setAmount(amount); + return itemStack; + } + + public static String getMaterialNameForDisplay(String materialName) { + CustomStack itemsAdderItem = CustomStack.getInstance(materialName); + if (itemsAdderItem == null) + return null; + return itemsAdderItem.getDisplayName(); + } + + public static boolean isValidItem(String materialName) { + return CustomStack.getInstance(materialName) != null; + } +} diff --git a/src/main/java/io/github/townyadvanced/townyresources/util/TownyResourcesMessagingUtil.java b/src/main/java/io/github/townyadvanced/townyresources/util/TownyResourcesMessagingUtil.java index 30dabf5..a23e838 100644 --- a/src/main/java/io/github/townyadvanced/townyresources/util/TownyResourcesMessagingUtil.java +++ b/src/main/java/io/github/townyadvanced/townyresources/util/TownyResourcesMessagingUtil.java @@ -203,6 +203,14 @@ public static String formatMaterialNameForDisplay(String materialName) { if (miName != null) return miName; } + + // ItemsAdder integration + if(TownyResources.getPlugin().isItemsAdderInstalled()) { + String mmName = ItemsAdderUtil.getMaterialNameForDisplay(materialName); + if (mmName != null) { + return mmName; + } + } } else { if(TownyResources.getPlugin().isLanguageUtilsInstalled()) { ItemStack fakeItemStack = new ItemStack(material); diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 5e2b076..ae3219a 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -6,7 +6,7 @@ author: 'Goosius, LlmDL' website: 'https://github.com/TownyAdvanced/TownyResources' prefix: ${project.artifactId} depend: [Towny] -softdepend: [SiegeWar,Dynmap-Towny,LangUtils,Slimefun,MythicMobs,MMOItems] +softdepend: [SiegeWar,Dynmap-Towny,LangUtils,Slimefun,MythicMobs,MMOItems,ItemsAdder] folia-supported: true description: This is an add-on plugin for Towny, which gives each town a unique set of automatically-extracted resources, and then protects the economic value of those resources with moderate limits to player resource extraction.