Compare commits

...

3 Commits

Author SHA1 Message Date
PretzelJohn 542eb20e8a Version 1.6.3:
* Slightly better default config
2023-03-20 21:19:18 -04:00
PretzelJohn 71fc8bd4f2 Version 1.6.3:
* Added left and right matching for trade overrides
2023-03-20 19:34:01 -04:00
PretzelJohn 03f8d1b84c Version 1.6.3:
* Fixed case sensitivity for disabled items and professions in config
* Updated version
2023-03-20 19:02:43 -04:00
6 changed files with 48 additions and 22 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.pretzel.dev</groupId> <groupId>com.pretzel.dev</groupId>
<artifactId>VillagerTradeLimiter</artifactId> <artifactId>VillagerTradeLimiter</artifactId>
<version>1.6.2</version> <version>1.6.3</version>
<build> <build>
<sourceDirectory>src</sourceDirectory> <sourceDirectory>src</sourceDirectory>
<plugins> <plugins>

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.pretzel.dev</groupId> <groupId>com.pretzel.dev</groupId>
<artifactId>VillagerTradeLimiter</artifactId> <artifactId>VillagerTradeLimiter</artifactId>
<version>1.6.2</version> <version>1.6.3</version>
<properties> <properties>
<java.version>1.8</java.version> <java.version>1.8</java.version>

View File

@ -4,7 +4,6 @@ import com.pretzel.dev.villagertradelimiter.VillagerTradeLimiter;
import com.pretzel.dev.villagertradelimiter.data.Cooldown; import com.pretzel.dev.villagertradelimiter.data.Cooldown;
import com.pretzel.dev.villagertradelimiter.data.PlayerData; import com.pretzel.dev.villagertradelimiter.data.PlayerData;
import com.pretzel.dev.villagertradelimiter.settings.Settings; import com.pretzel.dev.villagertradelimiter.settings.Settings;
import org.bukkit.entity.Item;
import org.bukkit.entity.Villager; import org.bukkit.entity.Villager;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -45,9 +44,11 @@ public class VillagerListener implements Listener {
//Checks each item if it should be removed from the trade list //Checks each item if it should be removed from the trade list
for(ItemStack item : items) { for(ItemStack item : items) {
if(disabledItems.contains(item.getType().name().toLowerCase())) { for(String disabledItem : disabledItems) {
event.setCancelled(true); if(disabledItem.equalsIgnoreCase(item.getType().name())) {
return; event.setCancelled(true);
return;
}
} }
} }
} }
@ -62,8 +63,11 @@ public class VillagerListener implements Listener {
List<String> disabledProfessions = instance.getCfg().getStringList("DisableProfessions"); List<String> disabledProfessions = instance.getCfg().getStringList("DisableProfessions");
//Changes the new profession to none if disabled in config //Changes the new profession to none if disabled in config
if(disabledProfessions.contains(profession.name().toLowerCase())) { for(String disabledProfession : disabledProfessions) {
event.setProfession(Villager.Profession.NONE); if(disabledProfession.equalsIgnoreCase(profession.name())) {
event.setProfession(Villager.Profession.NONE);
return;
}
} }
} }

View File

@ -12,6 +12,8 @@ import org.bukkit.entity.Entity;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.EnchantmentStorageMeta; import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import java.util.Arrays;
public class Settings { public class Settings {
private final VillagerTradeLimiter instance; private final VillagerTradeLimiter instance;
@ -114,11 +116,10 @@ public class Settings {
*/ */
public ConfigurationSection getOverride(final ItemStack buy, ItemStack sell) { public ConfigurationSection getOverride(final ItemStack buy, ItemStack sell) {
final ConfigurationSection overrides = instance.getCfg().getConfigurationSection("Overrides"); final ConfigurationSection overrides = instance.getCfg().getConfigurationSection("Overrides");
if(overrides != null) { if(overrides == null) return null;
for(final String override : overrides.getKeys(false)) { for(final String override : overrides.getKeys(false)) {
final ConfigurationSection item = this.getItem(buy, sell, override); final ConfigurationSection item = this.getItem(buy, sell, override);
if(item != null) return item; if(item != null) return item;
}
} }
return null; return null;
} }
@ -129,30 +130,40 @@ public class Settings {
* @param key The key where the override settings are stored in config.yml * @param key The key where the override settings are stored in config.yml
* @return The corresponding override config section for the recipe, if it exists, or null * @return The corresponding override config section for the recipe, if it exists, or null
*/ */
public ConfigurationSection getItem(final ItemStack buy, final ItemStack sell, final String key) { public ConfigurationSection getItem(final ItemStack buy, final ItemStack sell, String key) {
final ConfigurationSection item = instance.getCfg().getConfigurationSection("Overrides."+key); final ConfigurationSection item = instance.getCfg().getConfigurationSection("Overrides."+key);
if(item == null) return null; if(item == null) return null;
String match = "both";
if(!key.contains("_")) { if(!key.contains("_")) {
//Return the item if the item name is valid //Return the item if the item name is valid
if(this.verify(buy, sell, Material.matchMaterial(key))) return item; if(verify(buy, sell, match, Material.matchMaterial(key))) return item;
return null; return null;
} }
final String[] words = key.split("_"); String[] words = key.split("_");
if(words[words.length-1].equalsIgnoreCase("left")) {
match = "left";
key = key.replace("_left", "");
words = Arrays.copyOf(words, words.length-1);
} else if(words[words.length-1].equalsIgnoreCase("right")) {
match = "right";
key = key.replace("_right", "");
words = Arrays.copyOf(words, words.length-1);
}
try { try {
//Return the enchanted book item if there's a number in the item name //Return the enchanted book item if there's a number in the item name
final int level = Integer.parseInt(words[words.length-1]); final int level = Integer.parseInt(words[words.length-1]);
if(sell.getType() == Material.ENCHANTED_BOOK) { if(sell.getType() == Material.ENCHANTED_BOOK) {
final EnchantmentStorageMeta meta = (EnchantmentStorageMeta) sell.getItemMeta(); final EnchantmentStorageMeta meta = (EnchantmentStorageMeta) sell.getItemMeta();
final Enchantment enchantment = EnchantmentWrapper.getByKey(NamespacedKey.minecraft(key.substring(0, key.lastIndexOf("_")))); final Enchantment enchantment = EnchantmentWrapper.getByKey(NamespacedKey.minecraft(key.substring(0, key.lastIndexOf("_"))));
if (meta == null || enchantment == null) return null; if(meta == null || enchantment == null) return null;
if (meta.hasStoredEnchant(enchantment) && meta.getStoredEnchantLevel(enchantment) == level) return item; if(meta.hasStoredEnchant(enchantment) && meta.getStoredEnchantLevel(enchantment) == level) return item;
} }
} catch(NumberFormatException e) { } catch(NumberFormatException e) {
//Return the item if the item name is valid //Return the item if the item name is valid
if(this.verify(buy, sell, Material.matchMaterial(key))) if(verify(buy, sell, match, Material.matchMaterial(key))) return item;
return item;
return null; return null;
} catch(Exception e2) { } catch(Exception e2) {
//Send an error message //Send an error message
@ -167,7 +178,14 @@ public class Settings {
* @param material The material to compare the recipe against * @param material The material to compare the recipe against
* @return True if a recipe matches an override section, false otherwise * @return True if a recipe matches an override section, false otherwise
*/ */
private boolean verify(final ItemStack buy, final ItemStack sell, final Material material) { private boolean verify(final ItemStack buy, final ItemStack sell, final String match, final Material material) {
if(match.equals("left")) {
if(buy == null) return false;
return buy.getType().equals(material);
} else if(match.equals("right")) {
if(sell == null) return false;
return sell.getType().equals(material);
}
if(buy == null && sell == null) return false; if(buy == null && sell == null) return false;
if(buy == null) return sell.getType() == material; if(buy == null) return sell.getType() == material;
if(sell == null) return buy.getType() == material; if(sell == null) return buy.getType() == material;

View File

@ -118,6 +118,10 @@ Overrides:
Result: Result:
Material: "name_tag" Material: "name_tag"
Amount: 2 Amount: 2
flint_left:
MaxUses: 8
flint_right:
MaxUses: 1
clock: clock:
MaxDemand: 12 MaxDemand: 12
paper: paper:

View File

@ -1,7 +1,7 @@
name: VillagerTradeLimiter name: VillagerTradeLimiter
author: PretzelJohn author: PretzelJohn
main: com.pretzel.dev.villagertradelimiter.VillagerTradeLimiter main: com.pretzel.dev.villagertradelimiter.VillagerTradeLimiter
version: 1.6.2 version: 1.6.3
api-version: 1.14 api-version: 1.14
commands: commands: