diff --git a/README.md b/README.md index 8fba583..eaafcf4 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Key Benefits: (*details in the FAQ section below*) # Installation Guide -1. Ensure you have *Towny* 0.97.1.0 +. +1. Ensure you have *Towny* version 0.97.1.0 +. 2. Edit your townyperms.yml file, and add the following perms: - Mayor / Assistant / Treasurer: ``` @@ -22,16 +22,18 @@ Key Benefits: ``` - townyresources.command.nationcollect ``` -3. Using your permissions plugin, give this to any admins who are not already op: +3. Using your permissions plugin, give this to any admins who are not already OP: ``` - townyresources.admin.command.* ``` -4. If you have the *TownyDynmap* plugin installed, add this to the 'infowindow' section of your Dynmap-Towny config file +4. If you have the *Dynmap-Towny* plugin installed ([download](https://github.com/TownyAdvanced/Dynmap-Towny/releases)), add this to the 'infowindow' section of your *Dynmap-Towny* config file: > `
Resources: %town_resources%` -5. If you want to translate Material names to a Non-English language, click [here](https://github.com/NyaaCat/LanguageUtils/releases) to download the Jar for the *LanguageUtils* plugin, and drop it into your plugins folder. The language is configured in the *TownyResources* Config.yml file. +5. If you have the *Slimefun* plugin installed ([download](https://github.com/Slimefun/Slimefun4/releases)), you can add slimefun items to the **offers** list by using *Slimefun* item ID's. Example: + > `...{BUCKET_OF_OIL, 100, 1 BUCKET_OF_OIL}...{TIN_DUST, 100, 1, TIN_DUST}...` +5. If you have the *LangUtils* plugin installed ([download](https://ci.nyaacat.com/job/LanguageUtils/job/1.17/)), set your preferred Material language in the *TownyResources* Config.yml file. 6. Stop your server. -7. Download the latest TownyResources jar from [here](https://github.com/TownyAdvanced/TownyResources/releases). -8. Drop the TownyResources jar into your plugins folder. +7. Download the latest *TownyResources* Jar from [here](https://github.com/TownyAdvanced/TownyResources/releases). +8. Drop the *TownyResources* Jar into your plugins folder. 9. Start yor server. # Player Guide diff --git a/pom.xml b/pom.xml index c5e5a60..c45953b 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 io.github.townyadvanced TownyResources - 0.0.7 + 0.0.8 townyresources @@ -51,6 +51,12 @@ 2.5-SNAPSHOT provided + + com.github.TheBusyBiscuit + Slimefun4 + RC-15 + provided + diff --git a/src/main/java/io/github/townyadvanced/townyresources/TownyResources.java b/src/main/java/io/github/townyadvanced/townyresources/TownyResources.java index ae7f15e..422382c 100644 --- a/src/main/java/io/github/townyadvanced/townyresources/TownyResources.java +++ b/src/main/java/io/github/townyadvanced/townyresources/TownyResources.java @@ -1,6 +1,5 @@ package io.github.townyadvanced.townyresources; -import com.gmail.goosius.siegewar.settings.SiegeWarSettings; import com.palmergames.bukkit.towny.exceptions.TownyException; import com.palmergames.bukkit.util.Version; import io.github.townyadvanced.townyresources.commands.TownyResourcesAdminCommand; @@ -25,6 +24,7 @@ public class TownyResources extends JavaPlugin { private static boolean siegeWarInstalled; private static boolean dynmapTownyInstalled; private static boolean languageUtilsInstalled; + private static boolean slimeFunInstalled; @Override public void onEnable() { @@ -57,6 +57,7 @@ public boolean loadAll() { try { printSickASCIIArt(); townyVersionCheck(); + //Setup integrations with other plugins setupIntegrationsWithOtherPlugins(); //Load settings and languages TownyResourcesSettings.loadConfig(this.getDataFolder().getPath() + File.separator + "config.yml", getVersion()); @@ -165,10 +166,14 @@ public boolean isDynmapTownyInstalled() { public boolean isSiegeWarInstalled() { return siegeWarInstalled; } - + public boolean isLanguageUtilsInstalled() { return languageUtilsInstalled; } + + public boolean isSlimeFunInstalled() { + return slimeFunInstalled; + } private String getTownyVersion() { return Bukkit.getPluginManager().getPlugin("Towny").getDescription().getVersion(); @@ -188,9 +193,30 @@ private void setupIntegrationsWithOtherPlugins() { //Determine if other plugins are installed Plugin siegeWar = Bukkit.getPluginManager().getPlugin("SiegeWar"); siegeWarInstalled = siegeWar != null; + if(siegeWarInstalled) + info("SiegeWar Integration Enabled"); + else + info("SiegeWar Integration Not Enabled"); + Plugin dynmapTowny = Bukkit.getPluginManager().getPlugin("Dynmap-Towny"); - dynmapTownyInstalled = dynmapTowny!= null; + dynmapTownyInstalled = dynmapTowny != null; + if(dynmapTownyInstalled) + info("DynmapTowny Integration Enabled"); + else + info("DynmapTowny Integration Not Enabled"); + + Plugin slimeFun = Bukkit.getPluginManager().getPlugin("Slimefun"); + slimeFunInstalled = slimeFun != null; + if(slimeFunInstalled) + info("Slimefun Integration Enabled"); + else + info("Slimefun Integration Not Enabled"); + Plugin languageUtils = Bukkit.getPluginManager().getPlugin("LangUtils"); - languageUtilsInstalled = languageUtils!= null; + languageUtilsInstalled = languageUtils != null; + if(languageUtilsInstalled) + info("LanguageUtils Integration Enabled"); + else + info("LanguageUtils Integration Not Enabled"); } } diff --git a/src/main/java/io/github/townyadvanced/townyresources/commands/TownyResourcesCommand.java b/src/main/java/io/github/townyadvanced/townyresources/commands/TownyResourcesCommand.java index cc0fdd4..96cae57 100644 --- a/src/main/java/io/github/townyadvanced/townyresources/commands/TownyResourcesCommand.java +++ b/src/main/java/io/github/townyadvanced/townyresources/commands/TownyResourcesCommand.java @@ -98,7 +98,7 @@ private void parseSurveyCommand(Player player) throws TownyException{ //Check if there are resources left to discover at the town Town town = playerWorldCoord.getTownBlock().getTown(); - List discoveredResources = TownyResourcesGovernmentMetaDataController.getDiscoveredAsList(town); + List discoveredResources = TownyResourcesGovernmentMetaDataController.getDiscoveredAsList(town); List costPerResourceLevel = TownyResourcesSettings.getSurveyCostsPerResourceLevel(); List requiredNumTownblocksPerResourceLevel = TownyResourcesSettings.getSurveyNumTownblocksRequirementsPerResourceLevel(); if(discoveredResources.size() >= costPerResourceLevel.size()) @@ -145,7 +145,7 @@ private static void parseTownCollectCommand(Player player) throws TownyException throw new TownyException(TownyResourcesTranslation.of("msg_err_cannot_towncollect_not_in_own_town")); //Ensure some resources are available - Map availableForCollection = TownyResourcesGovernmentMetaDataController.getAvailableForCollectionAsMap(town); + Map availableForCollection = TownyResourcesGovernmentMetaDataController.getAvailableForCollectionAsMap(town); if(availableForCollection.isEmpty()) throw new TownyException(TownyResourcesTranslation.of("msg_err_cannot_towncollect_no_resources_available")); @@ -175,7 +175,7 @@ private static void parseNationCollectCommand(Player player) throws TownyExcepti throw new TownyException(TownyResourcesTranslation.of("msg_err_cannot_nationcollect_not_in_capital")); //Ensure some resources are available - Map availableForCollection = TownyResourcesGovernmentMetaDataController.getAvailableForCollectionAsMap(nation); + Map availableForCollection = TownyResourcesGovernmentMetaDataController.getAvailableForCollectionAsMap(nation); if(availableForCollection.isEmpty()) throw new TownyException(TownyResourcesTranslation.of("msg_err_cannot_nationcollect_no_resources_available")); 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 09b3394..e81209e 100644 --- a/src/main/java/io/github/townyadvanced/townyresources/controllers/PlayerExtractionLimitsController.java +++ b/src/main/java/io/github/townyadvanced/townyresources/controllers/PlayerExtractionLimitsController.java @@ -387,8 +387,7 @@ private static CategoryExtractionRecord getCategoryExtractionRecord(Map categoryExtractionRecord.getNextLimitWarningTime()) { - String categoryName = categoryExtractionRecord.getResourceExtractionCategory().getCategoryName(); - String translatedCategoryName = TownyResourcesTranslation.of("resource_category_" + categoryName).split(",")[0]; + String translatedCategoryName = TownyResourcesMessagingUtil.formatExtractionCategoryNameForDisplay(categoryExtractionRecord.getResourceExtractionCategory()); int categoryExtractionLimit = categoryExtractionRecord.getResourceExtractionCategory().getCategoryExtractionLimitItems(); String errorString = TownyResourcesTranslation.of("msg_error_daily_extraction_limit_reached", translatedCategoryName, categoryExtractionLimit); //Send temporary action bar message @@ -401,7 +400,7 @@ private static void sendLimitReachedWarningMessage(Player player, CategoryExtrac public static ResourceExtractionCategory getResourceExtractionCategory(String givenCategoryName) { for(ResourceExtractionCategory resourceExtractionCategory: resourceExtractionCategories) { - if(resourceExtractionCategory.getCategoryName().equals(givenCategoryName)) { + 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 7e38bb2..78419df 100644 --- a/src/main/java/io/github/townyadvanced/townyresources/controllers/TownResourceCollectionController.java +++ b/src/main/java/io/github/townyadvanced/townyresources/controllers/TownResourceCollectionController.java @@ -4,9 +4,12 @@ import com.palmergames.bukkit.towny.object.Government; import com.palmergames.bukkit.towny.object.Nation; import com.palmergames.bukkit.towny.object.Town; +import io.github.thebusybiscuit.slimefun4.implementation.SlimefunItems; +import io.github.townyadvanced.townyresources.TownyResources; import io.github.townyadvanced.townyresources.metadata.TownyResourcesGovernmentMetaDataController; import io.github.townyadvanced.townyresources.settings.TownyResourcesTranslation; import io.github.townyadvanced.townyresources.util.TownyResourcesMessagingUtil; +import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.entity.Player; @@ -19,14 +22,14 @@ public class TownResourceCollectionController { - public static synchronized void collectAvailableTownResources(Player player, Town town, Map availableForCollection) { + public static synchronized void collectAvailableTownResources(Player player, Town town, Map availableForCollection) { //Collect resources collectAvailableGovernmentResources(player, town, availableForCollection); //Notify Player TownyResourcesMessagingUtil.sendMsg(player, TownyResourcesTranslation.of("resource.towncollect.success")); } - public static synchronized void collectAvailableNationResources(Player player, Nation nation, Map availableForCollection) { + public static synchronized void collectAvailableNationResources(Player player, Nation nation, Map availableForCollection) { //Collect resources collectAvailableGovernmentResources(player, nation, availableForCollection); //Notify Player @@ -42,25 +45,42 @@ public static synchronized void collectAvailableNationResources(Player player, N * @param government the government * @param availableForCollection the list of currently available resources */ - private static synchronized void collectAvailableGovernmentResources(Player player, Government government, Map availableForCollection) { + private static synchronized void collectAvailableGovernmentResources(Player player, Government government, Map availableForCollection) { List itemStackList = new ArrayList<>(); //Calculate stuff to give player + String materialName; Material material; int amount; ItemStack itemStack; - for(Map.Entry mapEntry: availableForCollection.entrySet()) { - material = mapEntry.getKey(); - if(material == null) { - TownyResourcesMessagingUtil.sendErrorMsg(player, TownyResourcesTranslation.of("msg_err_cannot_collect_unknown_material", material)); - continue; - } + for(Map.Entry mapEntry: availableForCollection.entrySet()) { + materialName = mapEntry.getKey(); amount = mapEntry.getValue(); - itemStack = new ItemStack(material, amount); - itemStackList.add(itemStack); + + //Try creating a regular MC itemstack + material = Material.getMaterial(materialName); + if(material != null) { + itemStack = new ItemStack(material, amount); + itemStackList.add(itemStack); + continue; + } + + //Try creating a slimefun itemstack + if(TownyResources.getPlugin().isSlimeFunInstalled()) { + SlimefunItem slimeFunItem = SlimefunItem.getByID(materialName); + if(slimeFunItem != null) { + itemStack = slimeFunItem.getRecipeOutput(); + itemStack.setAmount(amount); + itemStackList.add(itemStack); + continue; + } + } + + //Unknown material. Send error message + TownyResourcesMessagingUtil.sendErrorMsg(player, TownyResourcesTranslation.of("msg_err_cannot_collect_unknown_material", material)); } - //Drop stuff near player + //Drop all collected itemstacks near player Towny.getPlugin().getServer().getScheduler().runTask(Towny.getPlugin(), new Runnable() { public void run() { Location location = player.getLocation(); diff --git a/src/main/java/io/github/townyadvanced/townyresources/controllers/TownResourceDiscoveryController.java b/src/main/java/io/github/townyadvanced/townyresources/controllers/TownResourceDiscoveryController.java index b0b6d5c..7ee6901 100644 --- a/src/main/java/io/github/townyadvanced/townyresources/controllers/TownResourceDiscoveryController.java +++ b/src/main/java/io/github/townyadvanced/townyresources/controllers/TownResourceDiscoveryController.java @@ -29,7 +29,7 @@ public class TownResourceDiscoveryController { * @param alreadyDiscoveredMaterials list of the town's already-discovered materials * @throws TownyException */ - public static void discoverNewResource(Resident resident, Town town, List alreadyDiscoveredMaterials) throws TownyException{ + public static void discoverNewResource(Resident resident, Town town, List alreadyDiscoveredMaterials) throws TownyException{ /* * Generate a list of candidate categories * This list will be comprised of all resource offer categories, except those of already discovered materials @@ -38,7 +38,7 @@ public static void discoverNewResource(Resident resident, Town town, List discoveredMaterials = new ArrayList<>(alreadyDiscoveredMaterials); + List discoveredMaterials = new ArrayList<>(alreadyDiscoveredMaterials); discoveredMaterials.add(winningMaterial); TownyResourcesGovernmentMetaDataController.setDiscovered(town, discoveredMaterials); town.save(); @@ -94,9 +94,8 @@ public static void discoverNewResource(Resident resident, Town town, List resourceOfferCategoryList = new ArrayList<>(); - private static Map materialToResourceOfferCategoryMap = new HashMap<>(); + private static Map materialToResourceOfferCategoryMap = new HashMap<>(); public static void loadAllResourceOfferCategories() throws TownyException { //Load all categories @@ -23,7 +23,7 @@ public static void loadAllResourceOfferCategories() throws TownyException { materialToResourceOfferCategoryMap.clear(); //Put each material on the map for(ResourceOfferCategory category: resourceOfferCategoryList) { - for(Material material: category.getMaterialsInCategory()) { + for(String material: category.getMaterialsInCategory()) { materialToResourceOfferCategoryMap.put(material, category); } } @@ -34,7 +34,7 @@ public static List getResourceOfferCategoryList() { return resourceOfferCategoryList; } - public static Map getMaterialToResourceOfferCategoryMap() { + public static Map getMaterialToResourceOfferCategoryMap() { return materialToResourceOfferCategoryMap; } } diff --git a/src/main/java/io/github/townyadvanced/townyresources/controllers/TownResourceProductionController.java b/src/main/java/io/github/townyadvanced/townyresources/controllers/TownResourceProductionController.java index cc150bf..1d985c6 100644 --- a/src/main/java/io/github/townyadvanced/townyresources/controllers/TownResourceProductionController.java +++ b/src/main/java/io/github/townyadvanced/townyresources/controllers/TownResourceProductionController.java @@ -51,12 +51,12 @@ private static void recalculateProductionForAllTowns() { static void recalculateProductionForOneTown(Town town) { try { //Get discovered resources - List discoveredResources = new ArrayList<>(TownyResourcesGovernmentMetaDataController.getDiscoveredAsList(town)); + List discoveredResources = new ArrayList<>(TownyResourcesGovernmentMetaDataController.getDiscoveredAsList(town)); //Remove any discovered resources which are no longer on offer - Map allOffers = TownResourceOffersController.getMaterialToResourceOfferCategoryMap(); - List resourcesToRemove = new ArrayList<>(); - for(Material resource: discoveredResources) { + Map allOffers = TownResourceOffersController.getMaterialToResourceOfferCategoryMap(); + List resourcesToRemove = new ArrayList<>(); + for(String resource: discoveredResources) { if(!allOffers.containsKey(resource)) { resourcesToRemove.add(resource); } @@ -71,7 +71,7 @@ static void recalculateProductionForOneTown(Town town) { double townCutNormalized = calculateTownCutNormalized(town); //Build the town production map - Map townProduction = calculateProduction(town, townCutNormalized); + Map townProduction = calculateProduction(town, townCutNormalized); //Save data TownyResourcesGovernmentMetaDataController.setDailyProduction(town, townProduction); @@ -133,15 +133,15 @@ static void recalculateProductionForOneNation(Nation nation) { //Take resources from towns and give to nation double nationCutNormalized = TownyResourcesSettings.getTownResourcesProductionNationTaxNormalized(); - Map nationProduction = new HashMap<>(); - Map resourcesTakenFromTown; - Material takenResource; + Map nationProduction = new HashMap<>(); + Map resourcesTakenFromTown; + String takenResource; int takenQuantity; for(Town town: townsToTakeFrom) { //Take resources from town resourcesTakenFromTown = calculateProduction(town, nationCutNormalized); //Add resources to nation - for(Map.Entry resourceTakenFromTown: resourcesTakenFromTown.entrySet()) { + for(Map.Entry resourceTakenFromTown: resourcesTakenFromTown.entrySet()) { takenResource = resourceTakenFromTown.getKey(); takenQuantity = resourceTakenFromTown.getValue(); if(nationProduction.containsKey(takenResource)) { @@ -167,19 +167,19 @@ static void recalculateProductionForOneNation(Nation nation) { * @param cutNormalized the cut of the resource to return * @return the production as a map, with each value multiplied by the given cutNormalized value */ - private static Map calculateProduction(Town town, double cutNormalized) { + private static Map calculateProduction(Town town, double cutNormalized) { //Get all offers - Map allOffers = TownResourceOffersController.getMaterialToResourceOfferCategoryMap(); + Map allOffers = TownResourceOffersController.getMaterialToResourceOfferCategoryMap(); //Get discovered resources - List discoveredResources = new ArrayList<>(TownyResourcesGovernmentMetaDataController.getDiscoveredAsList(town)); + List discoveredResources = new ArrayList<>(TownyResourcesGovernmentMetaDataController.getDiscoveredAsList(town)); //Get configured resource level bonuses List normalizedBonusesPerResourceLevel = TownyResourcesSettings.getNormalizedProductionBonusesPerResourceLevel(); //Calculate the production - Map production = new HashMap<>(); - Material material; + Map production = new HashMap<>(); + String material; double baseProducedAmount; int finalProducedAmount; @@ -237,23 +237,23 @@ private static void produceAllResourcesForAllNations() { private static boolean produceResourcesForOneGovernment(Government government) { try { //Get daily production - Map townDailyProduction = TownyResourcesGovernmentMetaDataController.getDailyProductionAsMap(government); + Map townDailyProduction = TownyResourcesGovernmentMetaDataController.getDailyProductionAsMap(government); if(townDailyProduction.isEmpty()) return false; //Get the list of resources which are already available for collection - Map availableResources = TownyResourcesGovernmentMetaDataController.getAvailableForCollectionAsMap(government); + Map availableResources = TownyResourcesGovernmentMetaDataController.getAvailableForCollectionAsMap(government); //Get storage Limit modifier int storageLimitModifier = TownyResourcesSettings.getStorageLimitModifier(); //Produce resources - Material resource; + String resource; int quantityToProduce; int currentQuantity; int storageLimit; - for(Map.Entry townProductionEntry: townDailyProduction.entrySet()) { + for(Map.Entry townProductionEntry: townDailyProduction.entrySet()) { resource = townProductionEntry.getKey(); quantityToProduce =townProductionEntry.getValue(); if(availableResources.containsKey(resource)) { diff --git a/src/main/java/io/github/townyadvanced/townyresources/metadata/TownyResourcesGovernmentMetaDataController.java b/src/main/java/io/github/townyadvanced/townyresources/metadata/TownyResourcesGovernmentMetaDataController.java index 74c8378..a6c3bdd 100644 --- a/src/main/java/io/github/townyadvanced/townyresources/metadata/TownyResourcesGovernmentMetaDataController.java +++ b/src/main/java/io/github/townyadvanced/townyresources/metadata/TownyResourcesGovernmentMetaDataController.java @@ -47,16 +47,14 @@ public static String getDiscovered(Government government) { * @param town the town * @return the town's discovered resources, as an IMMUTABLE list */ - public static List getDiscoveredAsList(Town town) { + public static List getDiscoveredAsList(Town town) { String discoveredMaterialsString = getDiscovered(town); if(discoveredMaterialsString.isEmpty()) { return Collections.emptyList(); } else { String[] discoveredMaterialsArray = discoveredMaterialsString.split(","); - List result = new ArrayList<>(); - for(String discoveredMaterial: discoveredMaterialsArray) { - result.add(Material.getMaterial(discoveredMaterial)); - } + List result = new ArrayList<>(); + result.addAll(Arrays.asList(discoveredMaterialsArray)); return result; } } @@ -96,13 +94,13 @@ public static List getDiscoveredResources(Town town) { */ - public static void setDiscovered(Government government, List discoveredResources) { + public static void setDiscovered(Government government, List discoveredResources) { //Convert materials list to single string StringBuilder metadataStringBuilder = new StringBuilder(); for(int i= 0; i < discoveredResources.size();i++) { if(i !=0) metadataStringBuilder.append(", "); - metadataStringBuilder.append(discoveredResources.get(i).toString()); //TODO nice format pls + metadataStringBuilder.append(discoveredResources.get(i)); } setDiscovered(government, metadataStringBuilder.toString()); } @@ -119,16 +117,16 @@ public static String getAvailableForCollection(Government government) { return MetaDataUtil.getSdf(government, availableForCollectionMetadataKey).replaceAll(" ",""); } - public static Map getDailyProductionAsMap(Government town) { + public static Map getDailyProductionAsMap(Government town) { return getResourceQuantitiesStringAsMap(getDailyProduction(town)); } - public static Map getAvailableForCollectionAsMap(Government town) { + public static Map getAvailableForCollectionAsMap(Government town) { return getResourceQuantitiesStringAsMap(getAvailableForCollection(town)); } - private static Map getResourceQuantitiesStringAsMap(String resourceQuantitiesString) { - Map result = new HashMap<>(); + private static Map getResourceQuantitiesStringAsMap(String resourceQuantitiesString) { + Map result = new HashMap<>(); if(!resourceQuantitiesString.isEmpty()) { String[] resourceQuantitiesArray = resourceQuantitiesString.split(","); String[] resourceQuantityPair; @@ -138,23 +136,23 @@ private static Map getResourceQuantitiesStringAsMap(String re resourceQuantityPair = resourceQuantityString.split("-"); amount = Integer.parseInt(resourceQuantityPair[0]); resource = resourceQuantityPair[1]; - result.put(Material.getMaterial(resource), amount); + result.put(resource, amount); } } return result; } - public static void setAvailableForCollection(Government government, Map availableForCollection) { + public static void setAvailableForCollection(Government government, Map availableForCollection) { setResourceQuantitiesString(government, availableForCollectionMetadataKey, availableForCollection); } - public static void setDailyProduction(Government government, Map dailyProduction) { + public static void setDailyProduction(Government government, Map dailyProduction) { setResourceQuantitiesString(government, dailyProductionMetadataKey, dailyProduction); } - private static void setResourceQuantitiesString(Government government, String metadataKey, Map resourceQuantitiesMap) { + private static void setResourceQuantitiesString(Government government, String metadataKey, Map resourceQuantitiesMap) { //Order map by descending values - Map sortedResourceQuantitiesMap = resourceQuantitiesMap.entrySet().stream() + Map sortedResourceQuantitiesMap = resourceQuantitiesMap.entrySet().stream() .sorted(Comparator.comparingInt(e -> -e.getValue())) .collect(Collectors.toMap( Map.Entry::getKey, @@ -165,7 +163,7 @@ private static void setResourceQuantitiesString(Government government, String me //Create list List resourceQuantitiesList = new ArrayList<>(); - for(Map.Entry resourceQuantity: sortedResourceQuantitiesMap.entrySet()) { + for(Map.Entry resourceQuantity: sortedResourceQuantitiesMap.entrySet()) { resourceQuantitiesList.add(resourceQuantity.getValue() + "-" + resourceQuantity.getKey()); } setResourceQuantitiesString(government, metadataKey, resourceQuantitiesList); diff --git a/src/main/java/io/github/townyadvanced/townyresources/metadata/TownyResourcesResidentMetaDataController.java b/src/main/java/io/github/townyadvanced/townyresources/metadata/TownyResourcesResidentMetaDataController.java index babf9ab..c3340b2 100644 --- a/src/main/java/io/github/townyadvanced/townyresources/metadata/TownyResourcesResidentMetaDataController.java +++ b/src/main/java/io/github/townyadvanced/townyresources/metadata/TownyResourcesResidentMetaDataController.java @@ -76,7 +76,7 @@ public static void setPlayerExtractionRecord(Resident resident, Map materialsInCategory; + private final List materialsInCategory; - public ResourceOfferCategory(String name, int discoveryWeight, int baseOfferItems, List materialsInCategory) { + public ResourceOfferCategory(String name, int discoveryWeight, int baseOfferItems, List materialsInCategory) { this.name = name; this.discoveryWeight = discoveryWeight; this.baseAmountItems = baseOfferItems; @@ -25,7 +23,7 @@ public int getBaseAmountItems() { return baseAmountItems; } - public List getMaterialsInCategory() { + public List getMaterialsInCategory() { return materialsInCategory; } 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 1ab1172..63d86cb 100644 --- a/src/main/java/io/github/townyadvanced/townyresources/settings/TownyResourcesSettings.java +++ b/src/main/java/io/github/townyadvanced/townyresources/settings/TownyResourcesSettings.java @@ -11,7 +11,9 @@ import io.github.townyadvanced.townyresources.objects.ResourceOffer; import io.github.townyadvanced.townyresources.objects.ResourceOfferCategory; import io.github.townyadvanced.townyresources.util.FileMgmt; +import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; public class TownyResourcesSettings { private static CommentedConfiguration config, newConfig; @@ -175,7 +177,8 @@ public static List getResourceOfferCategories() throws To double categoryBaseAmountStacks; int categoryBaseAmountItems; Material material; - List materialsInCategory = new ArrayList<>(); + List materialsInCategory; + String materialName; ResourceOfferCategory resourceOfferCategory; while (matcher.find()) { @@ -202,13 +205,13 @@ public static List getResourceOfferCategories() throws To //Read Materials materialsInCategory = new ArrayList<>(); for(int i = 3; i < categoryAsArray.length; i++) { - material = Material.getMaterial(categoryAsArray[i].trim()); - if(material == null) { + materialName = categoryAsArray[i].trim(); + if(!isValidMaterial(materialName)) { TownyResources.severe("Unknown material in offer category. Category: " + categoryName + ". Material: " + categoryAsArray[i]); problemLoadingCategories = true; - continue; + continue; } - materialsInCategory.add(material); + materialsInCategory.add(materialName); } //Construct ResourceExtractionCategory object @@ -226,6 +229,18 @@ public static List getResourceOfferCategories() throws To } } + private static boolean isValidMaterial(String materialName) { + Material material = Material.getMaterial(materialName); + if(material != null) + return true; //Known material + if(TownyResources.getPlugin().isSlimeFunInstalled()) { + SlimefunItem slimeFunItem = SlimefunItem.getByID(materialName); + if(slimeFunItem != null) + return true; //Known material + } + return false; //Unknown material + } + public static void loadConfig(String filepath, String version) throws TownyException{ if (FileMgmt.checkOrCreateFile(filepath)) { File file = new File(filepath); diff --git a/src/main/java/io/github/townyadvanced/townyresources/settings/TownyResourcesTranslation.java b/src/main/java/io/github/townyadvanced/townyresources/settings/TownyResourcesTranslation.java index 83c43fd..6e7003e 100644 --- a/src/main/java/io/github/townyadvanced/townyresources/settings/TownyResourcesTranslation.java +++ b/src/main/java/io/github/townyadvanced/townyresources/settings/TownyResourcesTranslation.java @@ -74,6 +74,10 @@ public static String of(String key) { return StringMgmt.translateHexColors(parseSingleLineString(data)); } + public static boolean hasKey(String key) { + return language.contains(key.toLowerCase()); + } + /** * Translates give key into its respective language. * 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 a7d2f91..5cfd40a 100644 --- a/src/main/java/io/github/townyadvanced/townyresources/util/TownyResourcesMessagingUtil.java +++ b/src/main/java/io/github/townyadvanced/townyresources/util/TownyResourcesMessagingUtil.java @@ -1,7 +1,10 @@ package io.github.townyadvanced.townyresources.util; import com.meowj.langutils.lang.LanguageHelper; +import io.github.townyadvanced.townyresources.objects.ResourceExtractionCategory; +import io.github.townyadvanced.townyresources.objects.ResourceOfferCategory; import io.github.townyadvanced.townyresources.settings.TownyResourcesSettings; +import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; import org.apache.commons.lang.WordUtils; import org.bukkit.Bukkit; import org.bukkit.Material; @@ -75,11 +78,9 @@ public static String formatProductionStringForDynmapTownyDisplay(String resource if(TownyResources.getPlugin().isLanguageUtilsInstalled()) { List resourcesAsFormattedList = new ArrayList<>(); String[] resourcesAsArray = resourcesAsString.replaceAll("\\d+-", "").split(","); - Material material; String translatedMaterialName; for(String resourceAsString: resourcesAsArray) { - material = Material.getMaterial(resourceAsString); - translatedMaterialName = getTranslatedMaterialName(material); + translatedMaterialName = formatMaterialNameForDisplay(resourceAsString); resourcesAsFormattedList.add(translatedMaterialName); } return Arrays.toString(resourcesAsFormattedList.toArray()).replace("[","").replace("]",""); @@ -89,20 +90,6 @@ public static String formatProductionStringForDynmapTownyDisplay(String resource } } - /** - * Format one material for display. - * - * @param material the material - * @return the formatted material name - */ - public static String formatMaterialForDisplay(Material material) { - if(TownyResources.getPlugin().isLanguageUtilsInstalled()) { - return getTranslatedMaterialName(material); - } else { - return WordUtils.capitalizeFully(material.toString().replaceAll("_", " ")); - } - } - /** * Convert a resource amount string formatted array * @@ -125,8 +112,7 @@ private static String[] convertResourceAmountsStringToFormattedArray(String reso amountAndMaterialName = resourceAsString.split("-"); amount = amountAndMaterialName[0]; materialName = amountAndMaterialName[1]; - material = Material.getMaterial(materialName); - translatedMaterialName = getTranslatedMaterialName(material); + translatedMaterialName = formatMaterialNameForDisplay(materialName); resourcesAsFormattedList.add(amount + " " + translatedMaterialName); } return resourcesAsFormattedList.toArray(new String[0]); @@ -139,10 +125,42 @@ private static String[] convertResourceAmountsStringToFormattedArray(String reso .split(","); } } + + public static String formatExtractionCategoryNameForDisplay(ResourceExtractionCategory resourceExtractionCategory) { + String categoryName = resourceExtractionCategory.getName(); + if(TownyResourcesTranslation.hasKey("resource_category_" + categoryName)) { + return TownyResourcesTranslation.of("resource_category_" + categoryName).split(",")[0]; + } else { + return formatMaterialNameForDisplay(categoryName); + } + } + + public static String formatOfferCategoryNameForDisplay(ResourceOfferCategory resourceOfferCategory) { + String categoryName = resourceOfferCategory.getName(); + if(TownyResourcesTranslation.hasKey("resource_category_"+ categoryName)) { + return TownyResourcesTranslation.of("resource_category_" + categoryName).split(",")[1].trim(); + } else { + return formatMaterialNameForDisplay(categoryName); + } + } - private static String getTranslatedMaterialName(Material material) { - ItemStack fakeItemStack = new ItemStack(material); - String translatedMaterialName = LanguageHelper.getItemDisplayName(fakeItemStack, TownyResourcesSettings.getMaterialsDisplayLanguage()); - return translatedMaterialName; + public static String formatMaterialNameForDisplay(String materialName) { + Material material = Material.getMaterial(materialName); + if(material == null) { + if(TownyResources.getPlugin().isSlimeFunInstalled()) { + SlimefunItem slimefunItem = SlimefunItem.getByID(materialName); + if(slimefunItem != null) { + return slimefunItem.getItemName().replaceAll("[^\\w\\s]\\w",""); + } + } + } else { + if(TownyResources.getPlugin().isLanguageUtilsInstalled()) { + ItemStack fakeItemStack = new ItemStack(material); + String translatedMaterialName = LanguageHelper.getItemDisplayName(fakeItemStack, TownyResourcesSettings.getMaterialsDisplayLanguage()); + return translatedMaterialName; + } + } + //Couldn't find a translation. Return un-translated material name + return WordUtils.capitalizeFully(materialName.replaceAll("_", " ")); } } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index db3b8c8..d2de880 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -6,7 +6,7 @@ author: Goosius website: 'https://github.com/TownyAdvanced/TownyResources' prefix: ${project.artifactId} depend: [Towny] -softdepend: [SiegeWar,Dynmap-Towny,LangUtils] +softdepend: [SiegeWar,Dynmap-Towny,LangUtils,Slimefun] loadbefore: [SiegeWar] 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.