From b76c1a660182db4b2088e9e40720d7c74fa32cde Mon Sep 17 00:00:00 2001 From: LlmDl Date: Wed, 24 Jan 2024 11:08:11 -0600 Subject: [PATCH 1/4] Add support for ItemsAdder. TODO: finish readme with instructions for itemsadder. Closes #141. --- pom.xml | 6 ++++ .../townyresources/TownyResources.java | 11 ++++++- .../TownResourceCollectionController.java | 12 ++++++++ .../settings/TownyResourcesSettings.java | 6 ++++ .../townyresources/util/ItemsAdderUtil.java | 30 +++++++++++++++++++ .../util/TownyResourcesMessagingUtil.java | 8 +++++ src/main/resources/plugin.yml | 2 +- 7 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 src/main/java/io/github/townyadvanced/townyresources/util/ItemsAdderUtil.java 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..5d2b091 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; @@ -222,6 +222,10 @@ public boolean isMythicMobsV5() { return mythicMobsInstalled; } + public boolean isItemsAdderInstalled() { + return itemsAdderInstalled; + } + public boolean isMMOItemsInstalled() { return mmmoItemsInstalled; } @@ -264,6 +268,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/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 Date: Wed, 24 Jan 2024 18:10:07 -0600 Subject: [PATCH 2/4] Update README, config and load offers 2 ticks later. --- README.md | 18 ++++++++++--- .../townyresources/TownyResources.java | 27 ++++++++++++++++--- .../settings/TownyResourcesConfigNodes.java | 4 +-- 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 298954a..cc9cb22 100644 --- a/README.md +++ b/README.md @@ -73,9 +73,21 @@ 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: + ``` + {crystal_category_name, 100, 1, crystal_block} + + ItemsAdder item config (plugins/ItemsAdder/contents/iasurvival/configs/blocks/items/minerals/crystal_block.yml) + items: + crystal_block: + 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/src/main/java/io/github/townyadvanced/townyresources/TownyResources.java b/src/main/java/io/github/townyadvanced/townyresources/TownyResources.java index 5d2b091..44cd1b0 100644 --- a/src/main/java/io/github/townyadvanced/townyresources/TownyResources.java +++ b/src/main/java/io/github/townyadvanced/townyresources/TownyResources.java @@ -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(() -> loadOffersAndCategories(), 2L); + //Load commands and listeners registerCommands(); registerListeners(); @@ -112,6 +111,26 @@ public boolean loadAll() { return true; } + private void loadOffersAndCategories() { + try { + //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(); + } catch (TownyException te) { + severe(te.getMessage()); + severe("TownyResources failed to load! Disabling!"); + onDisable(); + return; + } catch (Exception e) { + severe(e.getMessage()); + e.printStackTrace(); + severe("TownyResources failed to load! Disabling!"); + onDisable(); + return; + } + } + /** * Re-Load towny resources * 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", "", From c8f528ab08a67d9e7ad5400a3fe5173b9d983cbb Mon Sep 17 00:00:00 2001 From: LlmDl Date: Thu, 25 Jan 2024 10:15:59 -0600 Subject: [PATCH 3/4] A couple of failsafes. --- .../townyresources/TownyResources.java | 20 +++++++++---------- .../PlayerExtractionLimitsController.java | 8 ++++++++ .../TownResourceOffersController.java | 16 +++++++++++++++ 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/main/java/io/github/townyadvanced/townyresources/TownyResources.java b/src/main/java/io/github/townyadvanced/townyresources/TownyResources.java index 44cd1b0..7ef5011 100644 --- a/src/main/java/io/github/townyadvanced/townyresources/TownyResources.java +++ b/src/main/java/io/github/townyadvanced/townyresources/TownyResources.java @@ -89,7 +89,7 @@ public boolean loadAll() { new TownyResourcesMessagingUtil(this); // Run later to give items plugins a chance to load their items (ItemsAdder specifically.) - getScheduler().runLater(() -> loadOffersAndCategories(), 2L); + getScheduler().runLater(() -> loadOffersAndExtractionLimitCategories(), 2L); //Load commands and listeners registerCommands(); @@ -111,23 +111,21 @@ public boolean loadAll() { return true; } - private void loadOffersAndCategories() { + private void loadOffersAndExtractionLimitCategories() { try { - //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(); } catch (TownyException te) { severe(te.getMessage()); - severe("TownyResources failed to load! Disabling!"); - onDisable(); - return; + severe("TownyResources failed to load offers and extraction categories!"); + onDisable(); + return; } catch (Exception e) { severe(e.getMessage()); - e.printStackTrace(); - severe("TownyResources failed to load! Disabling!"); - onDisable(); - return; + e.printStackTrace(); + severe("TownyResources failed to load offers and extraction categories!"); + onDisable(); + return; } } 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/TownResourceOffersController.java b/src/main/java/io/github/townyadvanced/townyresources/controllers/TownResourceOffersController.java index 9379f64..c6921ab 100644 --- a/src/main/java/io/github/townyadvanced/townyresources/controllers/TownResourceOffersController.java +++ b/src/main/java/io/github/townyadvanced/townyresources/controllers/TownResourceOffersController.java @@ -29,10 +29,26 @@ public static void loadAllResourceOfferCategories() throws TownyException { } public static List 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; } } From 94bf1006ce994d78d66ef7b85966388c4c2504d0 Mon Sep 17 00:00:00 2001 From: LlmDl Date: Thu, 25 Jan 2024 10:21:07 -0600 Subject: [PATCH 4/4] Better looking readme. --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cc9cb22..b64d409 100644 --- a/README.md +++ b/README.md @@ -77,11 +77,12 @@ The plugin also has an optional feature to protect resource value, via daily pla - Town Production:
You can add *ItemsAdder* items to the offers list, simply by using *ItemsAdder* item names. Example: ``` - {crystal_category_name, 100, 1, crystal_block} + In the TownyResources config, use the item name from the ItemsAdder config as shown below: + categories: {crystal_category_name, 100, 1, crystal_block} - ItemsAdder item config (plugins/ItemsAdder/contents/iasurvival/configs/blocks/items/minerals/crystal_block.yml) + In ItemsAdder item config (plugins/ItemsAdder/contents/iasurvival/configs/blocks/items/minerals/crystal_block.yml) items: - crystal_block: + crystal_block: <---- this is the name you use in TownyResources. enabled: true ``` The above example gives one stack of Crystal blocks.