Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ItemsAdder to the plugins supported by TownyResources. #142

Merged
merged 4 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
<br>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.
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@
<version>6.9.2-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.LoneDev6</groupId>
<artifactId>api-itemsadder</artifactId>
<version>3.6.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.bsideup.jabel</groupId>
<artifactId>jabel-javac-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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
*
Expand Down Expand Up @@ -222,6 +239,10 @@ public boolean isMythicMobsV5() {
return mythicMobsInstalled;
}

public boolean isItemsAdderInstalled() {
return itemsAdderInstalled;
}

public boolean isMMOItemsInstalled() {
return mmmoItemsInstalled;
}
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -139,6 +140,17 @@ private static List<ItemStack> buildItemStackList(Player player, Set<Entry<Strin
continue;
}
}

// ItemsAdder integration
if (TownyResources.getPlugin().isItemsAdderInstalled()) {
ItemStack itemsAdderItem = ItemsAdderUtil.getItemStack(materialName, amount);
if (itemsAdderItem != null) {
itemStack = itemsAdderItem;
itemStackList.add(itemStack);
continue;
}
}

//Unknown material. Send error message
TownyResourcesMessagingUtil.sendErrorMsg(player, Translatable.of("townyresources.msg_err_cannot_collect_unknown_material", materialName));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,26 @@ public static void loadAllResourceOfferCategories() throws TownyException {
}

public static List<ResourceOfferCategory> 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<String, ResourceOfferCategory> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
"",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down