Skip to content

Commit

Permalink
Version 1.7.0
Browse files Browse the repository at this point in the history
- Add tool damage default.
- Add tool crop filter (speciality).
- Allow specifying multiple materials at once in tool filter "material" and "speciality".
  • Loading branch information
Torm committed Dec 30, 2023
1 parent 71792b9 commit cc9daf7
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 85 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group 'no.hyp'
version '1.6.0'
version '1.7.0'

sourceCompatibility = 1.16
targetCompatibility = 1.16
Expand All @@ -14,6 +14,6 @@ repositories {
}

dependencies {
compileOnly 'org.spigotmc:spigot-api:1.19.2-R0.1-SNAPSHOT'
implementation 'org.jetbrains:annotations:23.0.0'
compileOnly 'org.spigotmc:spigot-api:1.20.2-R0.1-SNAPSHOT'
implementation 'org.jetbrains:annotations:24.0.1'
}
121 changes: 74 additions & 47 deletions src/main/java/no/hyp/farmingupgrade/FarmingUpgradePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ public void readConfig() {
var replantDefault = configuration.getBoolean("toolUpgrade.replantDefault");
var collectDefault = configuration.getBoolean("toolUpgrade.collectDefault");
var plantDefault = configuration.getBoolean("toolUpgrade.plantDefault");
var tools = readTools(configuration, replantDefault, collectDefault, plantDefault);
var damageDefault = configuration.getInt("toolUpgrade.damageDefault");
var tools = readTools(configuration, replantDefault, collectDefault, plantDefault, damageDefault);
var crops = readCrops(configuration);
return new ToolUpgrade(
tools, crops, radiusPerEfficiencyLevel, applyUnbreaking, onlyHarvestMature, minimumReplantDelay,
Expand All @@ -181,40 +182,80 @@ public void readConfig() {
);
}

List<HarvestToolType> readTools(Configuration configuration, boolean replantDefault, boolean collectDefault, boolean plantDefault) {
List<HarvestToolType> readTools(Configuration configuration, boolean replantDefault, boolean collectDefault, boolean plantDefault, int damageDefault) {
var tools = new LinkedList<HarvestToolType>();
var toolSectionMaps = (List<Map<?, ?>>) configuration.getList("toolUpgrade.tools");
assert toolSectionMaps != null;
for (var toolSectionMap : toolSectionMaps) {
var toolSection = new MemoryConfiguration().createSection("section", toolSectionMap);
@Nullable var materialString = toolSection.getString("material", null);
@Nullable var material = materialString != null ? Material.matchMaterial(materialString) : null;
@Nullable var material = readMaterialStringOrList(toolSection, "material").orElse(null);
@Nullable var lore = toolSection.getString("lore", null);
@Nullable var nbtTag = toolSection.getString("nbt", null);
@Nullable var permission = toolSection.getString("permission", null);
var radius = toolSection.getDouble("radius", 0);
var damage = toolSection.getInt("damage", 1);
var damage = toolSection.getInt("damage", damageDefault);
var replant = toolSection.getBoolean("replant", replantDefault);
var collect = toolSection.getBoolean("collect", collectDefault);
var plant = toolSection.getBoolean("plant", plantDefault);
tools.add(new HarvestToolType(material, lore, nbtTag, permission, radius, damage, replant, collect, plant));
@Nullable var speciality = readMaterialStringOrList(toolSection, "speciality").orElse(null);
tools.add(new HarvestToolType(material, lore, nbtTag, permission, radius, damage, replant, collect, plant, speciality));
}
return tools;
}

record HarvestToolType(@Nullable Material material, @Nullable String lore, @Nullable String nbtTag, @Nullable String permission, double radius, int damage, boolean replant, boolean collect, boolean plant) { }
Optional<List<Material>> readMaterialStringOrList(ConfigurationSection section, String key) {
if (section.contains(key)) {
if (section.isString(key)) {
var materialString = section.getString(key);
assert materialString != null;
@Nullable var material = Material.matchMaterial(materialString);
if (material == null) {
this.getLogger().severe(materialString + " is not a valid tool material.");
return Optional.empty();
}
return Optional.of(List.of(material));
} else {
if (!section.isList(key)) {
this.getLogger().severe("Configuration error: material must be a string or a list of materials.");
return Optional.empty();
}
var l = section.getStringList(key);
var m = new ArrayList<Material>();
for (var materialString : l) {
@Nullable var material = Material.matchMaterial(materialString);
if (material == null) {
this.getLogger().severe(materialString + " is not a valid tool material.");
continue;
}
m.add(material);
}
return Optional.of(m);
}
} else {
return Optional.empty();
}
}

record HarvestToolType(@Nullable List<Material> material, @Nullable String lore, @Nullable String nbtTag, @Nullable String permission, double radius, int damage, boolean replant, boolean collect, boolean plant, @Nullable List<Material> speciality) { }

static ImmutableList<ReplantableCrop> readCrops(ConfigurationSection configuration) {
ImmutableList<ReplantableCrop> readCrops(ConfigurationSection configuration) {
var crops = new LinkedList<ReplantableCrop>();
var cropSectionMaps = (List<Map<?, ?>>) configuration.getList("toolUpgrade.crops");
assert cropSectionMaps != null;
for (var cropSectionMap : cropSectionMaps) {
var cropSection = new MemoryConfiguration().createSection("section", cropSectionMap);
var cropName = cropSection.getString("crop");
assert cropName != null;
var cropMaterial = Material.matchMaterial(cropName);
@Nullable var seedsName = cropSection.getString("seeds");
@Nullable var seedsMaterial = seedsName != null ? Material.matchMaterial(seedsName) : null;
var cropString = cropSection.getString("crop");
assert cropString != null;
@Nullable var cropMaterial = Material.matchMaterial(cropString);
if (cropMaterial == null) this.getLogger().severe(cropString + " is not a valid crop material.");
@Nullable var seedsString = cropSection.getString("seeds");
@Nullable Material seedsMaterial;
if (seedsString != null) {
seedsMaterial = Material.matchMaterial(seedsString);
if (seedsMaterial == null) this.getLogger().severe(seedsString + " is not a valid seeds material.");
} else {
seedsMaterial = null;
}
crops.add(new ReplantableCrop(cropMaterial, seedsMaterial));
}
return ImmutableList.copyOf(crops);
Expand Down Expand Up @@ -283,25 +324,13 @@ record ToolUpgrade(
boolean toolSwingParticleEffect
) {

public Optional<HarvestToolType> toolType(FarmingUpgradePlugin plugin, Player player, ItemStack toolItem) {
var potentialTools = toolType(plugin, toolItem);
for (var tool : potentialTools) {
@Nullable var permission = tool.permission;
if (permission == null) return Optional.of(tool);
if (player.hasPermission(permission)) return Optional.of(tool);
}
return Optional.empty();
}

public LinkedList<HarvestToolType> toolType(FarmingUpgradePlugin plugin, ItemStack toolItem) {
Optional<HarvestToolType> toolType(FarmingUpgradePlugin plugin, ItemStack toolItem, Player player, Material crop) {
var list = new LinkedList<HarvestToolType>();
for (var toolType : tools) {
// If the type has a material, the item must be of the same material.
@Nullable var typeMaterial = toolType.material();
if (typeMaterial != null) {
if (typeMaterial != toolItem.getType()) {
continue;
}
if (!typeMaterial.contains(toolItem.getType())) continue;
}
// If the type has a lore, some line in the item lore must contain the type lore as a substring.
@Nullable var lore = toolType.lore();
Expand Down Expand Up @@ -332,9 +361,19 @@ public LinkedList<HarvestToolType> toolType(FarmingUpgradePlugin plugin, ItemSta
assert tag != null;
if (tag == 0) continue;
}
list.add(toolType);
@Nullable var permissionString = toolType.permission();
if (permissionString != null) {
@Nullable Permission permission = Bukkit.getServer().getPluginManager().getPermission(permissionString);
if (permission == null) continue; // The permission does not exist.
if (!player.hasPermission(permission)) continue;
}
@Nullable var specialty = toolType.speciality();
if (specialty != null) {
if (!specialty.contains(crop)) continue;
}
return Optional.of(toolType);
}
return list;
return Optional.empty();
}

public boolean isCrop(Material type) {
Expand Down Expand Up @@ -423,22 +462,10 @@ public void onFarm(BlockBreakEvent event) {
if (callingBlockBreakEvent) return; // Do not handle events that are called by FarmingUpgrade.
var player = event.getPlayer();
var centre = event.getBlock();
if (!toolUpgrade.isCrop(centre.getType())) return; // Farming only applies to crops.
var material = centre.getType();
if (!toolUpgrade.isCrop(material)) return; // Farming only applies to crops.
var toolItem = player.getInventory().getItemInMainHand();
@Nullable HarvestToolType toolType = null;
for (var maybeToolType : toolUpgrade.toolType(this, toolItem)) {
@Nullable var permissionString = maybeToolType.permission();
if (permissionString == null) {
toolType = maybeToolType;
break;
}
@Nullable Permission permission = getServer().getPluginManager().getPermission(permissionString);
if (permission == null) continue; // The player does not have the tool permission because it does not exist.
if (player.hasPermission(permission)) {
toolType = maybeToolType;
break;
}
}
@Nullable HarvestToolType toolType = toolUpgrade.toolType(this, toolItem, player, material).orElse(null);
if (toolType == null) return; // If the crop was not broken by a harvest tool, proceed with Vanilla mechanics.
event.setCancelled(true); // Cancel the Vanilla event to cancel the Vanilla mechanics.
initiateHarvest(player, toolType, toolItem, centre);
Expand All @@ -452,7 +479,7 @@ void initiateHarvest(Player player, HarvestToolType toolType, ItemStack toolItem
if (toolUpgrade.toolSwingParticleEffect()) harvestSwingParticles(player);
var toolDamage = toolType.damage();
var radius = calculateRadius(toolType, toolItem);
var cropMaterials = toolUpgrade.cropMaterials();
var cropMaterials = toolType.speciality() != null ? toolType.speciality() : toolUpgrade.cropMaterials();
var adjacentCropBlocks = findAdjacentMaterials(cropMaterials, centre, radius, true);
var replant = toolType.replant;
var applyUnbreaking = toolUpgrade.applyUnbreaking;
Expand Down Expand Up @@ -644,13 +671,13 @@ void onPlant(BlockPlaceEvent event) {
ItemStack toolItem;
{
var mainHand = player.getInventory().getItemInMainHand();
var mainHandTool = toolUpgrade.toolType(this, player, mainHand).orElse(null);
var mainHandTool = toolUpgrade.toolType(this, mainHand, player, cropType).orElse(null);
if (mainHandTool != null) {
toolItem = mainHand.clone();
tool = mainHandTool;
} else {
var offHand = player.getInventory().getItemInOffHand();
tool = toolUpgrade.toolType(this, player, offHand).orElse(null);
tool = toolUpgrade.toolType(this, offHand, player, cropType).orElse(null);
toolItem = offHand.clone();
}
}
Expand Down
57 changes: 23 additions & 34 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ toolUpgrade:
# Dropped items from crops broken by a hoe are collected in the player's inventory if there is space.
# This is a default value for every tool. This can be overridden by specific tool properties.
collectDefault: false
# Determines if a tool can plant seeds on all empty farmland within the tool radius by default.
# Determines if a tool can plant seeds on all empty farmland within the tool radius by default. This can be overridden by specific tool properties.
plantDefault: true
# Default damage that a tool takes when harvesting crops. This can be overridden by specific tool properties.
damageDefault: 1
# A list of crops and their seeds.
crops:
- crop: WHEAT
Expand All @@ -56,12 +58,13 @@ toolUpgrade:
# The first (ordered from top to bottom) tool with filters allowing an item, is the one that is used to determine the item's tool features. If no tool matches an item, then the item is not a harvesting tool.
#
# Tool filters:
# - material: String/Material. The item must have this material. Remove to allow any material.
# - material: String/Material OR List of Materials. The tool must be of one of these materials. Remove to allow any material.
# - lore: String. The item must have this string [as a substring, somewhere] in its lore. Remove to allow any lore.
# - permission: String/Permission. The user must have this permission. Remove to allow any permissions.
# - nbt: String. The item must have this FarmingUpgrade NBT-tag set to true. Remove to allow any NBT-tags.
# Example: If "nbt: golden", then the NBT-tag PublicBukkitValues/"farmingupgrade:golden" must be set to true (nonzero byte).
# Such an item can be created with the command [ /give @p minecraft:golden_hoe{PublicBukkitValues:{"farmingupgrade:golden":1b}} ].
# - speciality: String/Material OR List of Materials. An exclusive list of crops that this tool can be applied to. Remove to apply tool to any crop.
#
# Tool features:
# - radius: Decimal. Base tool radius. Total radius may be affected by other modifiers. The total radius is rounded down. 0 radius means that only the clicked block is affected.
Expand All @@ -75,73 +78,59 @@ toolUpgrade:
#
# - permission: farmer
# radius: 1.5
# damage: 1

# Fake lore enchantments
#
# - lore: Harvest I
# radius: 1.5
# damage: 1
# - lore: Harvest II
# radius: 2.5
# damage: 1

# Example of permission disabling replant for wooden hoes.
# Example of permission enabling replant for wooden hoes.
#
# - material: WOODEN_HOE
# permission: replant
# radius: 0.5
# damage: 1
# permission: noreplant
# replant: false
# replant: true
#
# - material: WOODEN_HOE
# radius: 0.5
# damage: 1
# replant: true
# replant: false

# Example of tool that can plant seeds.
#
# - material: IRON_HOE
# radius: 1.5
# damage: 1
# plant: true

# Example of a tool that only harvests, plants and collects potatoes, carrots and beetroots.
#
# - nbt: earth
# speciality: [POTATOES, CARROTS, BEETROOTS]
# radius: 1.5
# collect: true
#
# - nbt: earth
# radius: 0
# replant: false
# plant: false

# Hoes
- material: WOODEN_HOE
radius: 0.5
damage: 1
- material: STONE_HOE
radius: 0.5
damage: 1
- material: IRON_HOE
radius: 1.5
damage: 1
- material: GOLDEN_HOE
radius: 1.5
damage: 1
- material: DIAMOND_HOE
radius: 2.5
damage: 1
- material: NETHERITE_HOE
radius: 2.5
damage: 1

# Swords
- material: WOODEN_SWORD
radius: 0
damage: 2
- material: STONE_SWORD
radius: 0
damage: 2
- material: IRON_SWORD
radius: 0
damage: 2
- material: GOLDEN_SWORD
radius: 0
damage: 2
- material: DIAMOND_SWORD
radius: 0
damage: 2
- material: NETHERITE_SWORD
- material: [WOODEN_SWORD, STONE_SWORD, IRON_SWORD, GOLDEN_SWORD, DIAMOND_SWORD, NETHERITE_SWORD]
radius: 0
damage: 2

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: FarmingUpgrade
author: Torm
description: Upgraded farming mechanics.
version: 1.6.0
version: 1.7.0
main: no.hyp.farmingupgrade.FarmingUpgradePlugin
api-version: 1.16
permissions:
Expand Down

0 comments on commit cc9daf7

Please sign in to comment.