Skip to content

Commit

Permalink
Version 1.3.5
Browse files Browse the repository at this point in the history
- Add replant property to tools.
- Add collect property to tools.
- Fix bug where players were counted as having nonexistent permissions.
  • Loading branch information
Torm committed May 17, 2022
1 parent 415f498 commit 4022d1f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 46 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group 'no.hyp'
version '1.3.4'
version '1.3.5'

sourceCompatibility = 1.16
targetCompatibility = 1.16
Expand Down
66 changes: 32 additions & 34 deletions src/main/java/no/hyp/farmingupgrade/FarmingUpgradePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.permissions.*;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.Vector;
Expand Down Expand Up @@ -224,13 +225,15 @@ static ImmutableList<HarvestToolType> readTools(ConfigurationSection configurati
var toolSectionMaps = (List<Map<?, ?>>) configuration.getList(path);
for (var toolSectionMap : toolSectionMaps) {
var toolSection = new MemoryConfiguration().createSection("section", toolSectionMap);
@Nullable var materialString = toolSection.getString("material");
@Nullable var materialString = toolSection.getString("material", null);
@Nullable var material = materialString != null ? Material.matchMaterial(materialString) : null;
@Nullable var lore = toolSection.getString("lore");
@Nullable var permission = toolSection.getString("permission");
var radius = toolSection.getDouble("radius");
var damage = toolSection.getInt("damage");
tools.add(new HarvestToolType(material, lore, permission, radius, damage));
@Nullable var lore = toolSection.getString("lore", null);
@Nullable var permission = toolSection.getString("permission", null);
var radius = toolSection.getDouble("radius", 0);
var damage = toolSection.getInt("damage", 1);
var replant = toolSection.getBoolean("replant", true);
var collect = toolSection.getBoolean("collect", true);
tools.add(new HarvestToolType(material, lore, permission, radius, damage, replant, collect));
}
return ImmutableList.copyOf(tools);
}
Expand Down Expand Up @@ -262,7 +265,8 @@ static ImmutableList<FertilisablePlant> readPlants(ConfigurationSection configur
return ImmutableList.copyOf(plants);
}

public Optional<HarvestToolType> toolType(ItemStack toolItem) {
public LinkedList<HarvestToolType> toolType(ItemStack toolItem) {
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();
Expand All @@ -286,9 +290,9 @@ public Optional<HarvestToolType> toolType(ItemStack toolItem) {
}
if (!foundLore) continue;
}
return Optional.of(toolType);
list.add(toolType);
}
return Optional.empty();
return list;
}

public boolean isCrop(Material type) {
Expand Down Expand Up @@ -328,7 +332,7 @@ public double fertilisableGrowth(Material material) throws IllegalArgumentExcept

}

record HarvestToolType(@Nullable Material material, @Nullable String lore, @Nullable String permission, double radius, int damage) { }
record HarvestToolType(@Nullable Material material, @Nullable String lore, @Nullable String permission, double radius, int damage, boolean replant, boolean collect) { }

record ReplantableCrop(Material crop, @Nullable Material seeds) { }

Expand All @@ -353,10 +357,21 @@ public void onFarm(BlockBreakEvent event) {
var centre = event.getBlock();
if (!configuration.isCrop(centre.getType())) return; // Farming only applies to crops.
var toolItem = player.getInventory().getItemInMainHand();
@Nullable var toolType = configuration().toolType(toolItem).orElse(null);
@Nullable HarvestToolType toolType = null;
for (var maybeToolType : configuration().toolType(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;
}
}
if (toolType == null) return; // If the crop was not broken by a harvest tool, proceed with Vanilla mechanics.
@Nullable var permission = toolType.permission();
if (permission != null && !player.hasPermission(permission)) return; // The player must have the tool permission to use the tool.
event.setCancelled(true); // Cancel the Vanilla event to cancel the Vanilla mechanics.
initiateHarvest(player, toolType, toolItem, centre);
}
Expand All @@ -370,9 +385,9 @@ void initiateHarvest(Player player, HarvestToolType toolType, ItemStack toolItem
var radius = calculateRadius(toolType, toolItem);
var cropMaterials = configuration().cropMaterials();
var adjacentCropBlocks = findAdjacentMaterials(cropMaterials, centre, radius);
var replant = configuration().harvestReplant();
var replant = configuration().harvestReplant() && toolType.replant();
var applyUnbreaking = configuration().harvestApplyUnbreaking();
var collect = configuration().harvestCollect();
var collect = configuration().harvestCollect() && toolType.collect();
var onlyMature = configuration().harvestOnlyMature();
for (var adjacentCropBlock : adjacentCropBlocks) {
@Nullable var seeds = configuration().seeds(adjacentCropBlock.getType()).orElse(null);
Expand Down Expand Up @@ -511,7 +526,7 @@ boolean harvestCrop(Player player, Block block, ItemStack tool, boolean replant,
*/

/**
* TODO: Ugly hack
* TODO: Kinda ugly, but no better way currently
*
* When a player tramples farmland, Bukkit calls a PlayerInteractEvent, and then
* a BlockFadeEvent. Minecraft also calls FadeEvent when a Farmland is turning to
Expand Down Expand Up @@ -971,11 +986,6 @@ static Optional<Boolean> isMature(Block block) {

/**
* Find all horizontally adjacent blocks.
*
* @param materials
* @param centre
* @param radius
* @return
*/
Collection<Block> findAdjacentMaterials(Collection<Material> materials, Block centre, int radius) {
var diameter = radius + radius + 1;
Expand Down Expand Up @@ -1135,26 +1145,14 @@ Collection<Block> findAdjacentMaterials(Collection<Material> materials, Block ce
}

/**
* Convert relative coördinates to grid coördinates.
*
* @param diameter
* @param radius
* @param i
* @param k
* @return
* Convert relative coordinates to grid coordinates.
*/
static int gridIndex(int diameter, int radius, int i, int k) {
return ((i + radius) * diameter) + (k + radius);
}

/**
*
* @param materials
* @param centre
* @param i
* @param k
* @param adjacents Nullable adjacent blocks.
* @return
*/
static Optional<Block> locateAdjacentBlock(Collection<Material> materials, Block centre, int i, int k, Block... adjacents) {
for (@Nullable var adjacent : adjacents) {
Expand Down
36 changes: 26 additions & 10 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,41 @@ trample:
revertEmpty: true


# A list of harvesting tools, their harvest radiï and the damage they take from harvesting crops. Note that if an item
# has the unbreakable item attribute, it will never take any damage.
# The radius is a decimal, but is rounded down when used to find surrounding blocks.
# The first entry that applies to an item, is the one that is used to determine radius and damage. If no entry applies
# to an item, then the item is not a harvesting tool. An entry may only apply to an item with the same material. If an
# entry has no material, then it may apply to an item with any material. An entry may only apply to an item which has
# the same lore as a substring somewhere in its item lore. If an entry has no lore, then it may apply to an item with
# any lore.
# A list of harvesting tools and their properties.
# - Note that if an item has the unbreakable item attribute, it will never take any damage.
# - Damage determines how much damage a tool takes when harvesting one crop.
# - The radius is a decimal, but the final result from calculations is rounded down when used to find surrounding
# blocks. 0 radius means that only the clicked block is found.
# - The first entry that applies to an item, is the one that is used to determine tool properties. If no entry applies
# to an item, then the item is not a harvesting tool.
# - An entry may only apply to an item with the same material. If an entry has no material, then it may apply to an item
# with any material.
# - An entry may only apply to an item which has the same lore as a substring somewhere in its item lore. If an entry
# has no lore, then it may apply to an item with any lore.
# - The replant property can be true or false and determines if a tool replants. It is true by default.
# - The collect property can be true or false and determines if a tool collects. It is true by default.
tools:
# # Permission example - Player must have permission "farmer" to apply the tool.

# Permission example - Player must have permission "farmer" to apply the tool.
# - permission: farmer
# radius: 1.5
# damage: 1
# # Fake enchantments

# 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.
# - material: WOODEN_HOE
# radius: 0.5
# damage: 1
# permission: noreplant
# replant: false

# Hoes
- material: WOODEN_HOE
radius: 0.5
Expand All @@ -103,6 +118,7 @@ tools:
- material: NETHERITE_HOE
radius: 2.5
damage: 1

# Swords
- material: WOODEN_SWORD
radius: 0
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.3.4
version: 1.3.5
main: no.hyp.farmingupgrade.FarmingUpgradePlugin
api-version: 1.16
permissions:
Expand Down

0 comments on commit 4022d1f

Please sign in to comment.