Skip to content

Commit

Permalink
Version 1.3.1
Browse files Browse the repository at this point in the history
- Allow tool radius to be specified in decimals, not just integers.
- Fix bug where tool takes damage attempting but failing to harvest crops that were protected by some protection plugin
  • Loading branch information
Torm committed Sep 21, 2021
1 parent 089a6fd commit 0924256
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 69 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.0'
version '1.3.1'

sourceCompatibility = 1.16
targetCompatibility = 1.16
Expand Down
115 changes: 57 additions & 58 deletions src/main/java/no/hyp/farmingupgrade/FarmingUpgradePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ static ImmutableList<HarvestToolType> readTools(ConfigurationSection configurati
@Nullable var materialString = toolSection.getString("material");
@Nullable var material = materialString != null ? Material.matchMaterial(materialString) : null;
@Nullable var lore = toolSection.getString("lore");
var radius = toolSection.getInt("radius");
var radius = toolSection.getDouble("radius");
var damage = toolSection.getInt("damage");
tools.add(new HarvestToolType(material, lore, radius, damage));
}
Expand Down Expand Up @@ -374,12 +374,12 @@ void harvestSwingParticles(Player player) {
}

int calculateRadius(HarvestToolType toolType, ItemStack toolItem) {
var radius = 0;
var radius = 0.0;
radius += toolType.radius();
var efficiencyRangePerLevel = configuration().harvestRadiusPerEfficiencyLevel();
radius += toolItem.getEnchantmentLevel(Enchantment.DIG_SPEED) * efficiencyRangePerLevel;
radius = Math.min(10, radius); // Do not allow the radius to crash the server.
return radius;
return (int) radius;
}

/**
Expand All @@ -398,68 +398,67 @@ boolean harvestCrop(Player player, Block block, ItemStack tool, boolean replant,
callingBlockBreakEvent = true;
Bukkit.getServer().getPluginManager().callEvent(upgradedEvent);
callingBlockBreakEvent = false;
if (!upgradedEvent.isCancelled()) {
// A state representing the crop after the harvest.
BlockState state = block.getState();
// Calculate drops depending on tool.
Collection<ItemStack> itemDrops = block.getDrops(tool);
// Break the crop and spawn effects.
FarmingUpgradePlugin.breakBlockEffect(block, state, Sound.BLOCK_CROP_BREAK);
block.setType(Material.AIR);

// Whether a seed has been collected for replanting.
boolean seedFound = false;
GameMode mode = player.getGameMode();
// Drop items in survival.
if (upgradedEvent.isDropItems() && mode != GameMode.CREATIVE) {
// Search for a seed to replant the crop with if replanting is enabled.
if (replant) {
for (ItemStack itemDrop : itemDrops) {
// If replanting is enabled, and a seed is not found yet, search for one in this ItemStack.
if (!seedFound) {
int amount = itemDrop.getAmount();
if (itemDrop.getType() == seed && amount >= 1) {
itemDrop.setAmount(amount - 1);
seedFound = true;
}
if (upgradedEvent.isCancelled()) return false;
// A state representing the crop after the harvest.
BlockState state = block.getState();
// Calculate drops depending on tool.
Collection<ItemStack> itemDrops = block.getDrops(tool);
// Break the crop and spawn effects.
FarmingUpgradePlugin.breakBlockEffect(block, state, Sound.BLOCK_CROP_BREAK);
block.setType(Material.AIR);

// Whether a seed has been collected for replanting.
boolean seedFound = false;
GameMode mode = player.getGameMode();
// Drop items in survival.
if (upgradedEvent.isDropItems() && mode != GameMode.CREATIVE) {
// Search for a seed to replant the crop with if replanting is enabled.
if (replant) {
for (ItemStack itemDrop : itemDrops) {
// If replanting is enabled, and a seed is not found yet, search for one in this ItemStack.
if (!seedFound) {
int amount = itemDrop.getAmount();
if (itemDrop.getType() == seed && amount >= 1) {
itemDrop.setAmount(amount - 1);
seedFound = true;
}
}
}
// If collect is enabled, items are sent to the inventory if there is space.
if (collect) {
Inventory inventory = player.getInventory();
Set<ItemStack> notAddedItems = new HashSet<>();
for (ItemStack item : itemDrops) {
notAddedItems.addAll(inventory.addItem(item).values());
}
itemDrops = notAddedItems;
//player.playSound(player.getLocation(), Sound.ENTITY_ITEM_PICKUP, 0.2f, 1.0f + random.nextFloat());
}
// Calculate the dropped item entities.
List<Item> drops = new ArrayList<>();
for (ItemStack itemDrop : itemDrops) {
if (itemDrop.getType().isItem() && itemDrop.getType() != Material.AIR && itemDrop.getAmount() >= 1) {
drops.add(block.getWorld().dropItemNaturally(block.getLocation(), itemDrop));
}
}
// If collect is enabled, items are sent to the inventory if there is space.
if (collect) {
Inventory inventory = player.getInventory();
Set<ItemStack> notAddedItems = new HashSet<>();
for (ItemStack item : itemDrops) {
notAddedItems.addAll(inventory.addItem(item).values());
}
// Send a BlockDropItemEvent for the drops.
List<Item> copy = Lists.newArrayList(drops);
BlockDropItemEvent dropEvent = new BlockDropItemEvent(block, state, player, copy);
Bukkit.getServer().getPluginManager().callEvent(dropEvent);
// Kill those items that were removed from the copied drop list, or all of them
// if the event is cancelled.
if (dropEvent.isCancelled()) {
copy.clear();
itemDrops = notAddedItems;
//player.playSound(player.getLocation(), Sound.ENTITY_ITEM_PICKUP, 0.2f, 1.0f + random.nextFloat());
}
// Calculate the dropped item entities.
List<Item> drops = new ArrayList<>();
for (ItemStack itemDrop : itemDrops) {
if (itemDrop.getType().isItem() && itemDrop.getType() != Material.AIR && itemDrop.getAmount() >= 1) {
drops.add(block.getWorld().dropItemNaturally(block.getLocation(), itemDrop));
}
for (Item drop : drops) {
if (!copy.contains(drop)) {
drop.remove();
}
}
// Send a BlockDropItemEvent for the drops.
List<Item> copy = Lists.newArrayList(drops);
BlockDropItemEvent dropEvent = new BlockDropItemEvent(block, state, player, copy);
Bukkit.getServer().getPluginManager().callEvent(dropEvent);
// Kill those items that were removed from the copied drop list, or all of them
// if the event is cancelled.
if (dropEvent.isCancelled()) {
copy.clear();
}
for (Item drop : drops) {
if (!copy.contains(drop)) {
drop.remove();
}
}
if (mode == GameMode.CREATIVE && replant) seedFound = true; // A crop is always replanted in creative mode.
if (seedFound) block.setType(state.getType()); // Replant the crop if the conditions are satisfied.
}
if (mode == GameMode.CREATIVE && replant) seedFound = true; // A crop is always replanted in creative mode.
if (seedFound) block.setType(state.getType()); // Replant the crop if the conditions are satisfied.
return true;
}

Expand Down Expand Up @@ -998,7 +997,7 @@ public static Optional<Block> findHighestMaterial(Collection<Material> materials
return Optional.empty();
}

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

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

Expand Down
19 changes: 10 additions & 9 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ harvest:
upgrade: 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
Expand All @@ -21,29 +22,29 @@ harvest:
tools:
# # Fake enchantments
# - lore: Harvest I
# radius: 2
# radius: 1.5
# damage: 1
# - lore: Harvest II
# radius: 3
# radius: 2.5
# damage: 1
# Hoes
- material: WOODEN_HOE
radius: 0
radius: 0.5
damage: 1
- material: STONE_HOE
radius: 0
radius: 0.5
damage: 1
- material: IRON_HOE
radius: 1
radius: 1.5
damage: 1
- material: GOLDEN_HOE
radius: 1
radius: 1.5
damage: 1
- material: DIAMOND_HOE
radius: 2
radius: 2.5
damage: 1
- material: NETHERITE_HOE
radius: 2
radius: 2.5
damage: 1
# Swords
- material: WOODEN_SWORD
Expand All @@ -65,7 +66,7 @@ harvest:
radius: 0
damage: 2
# Harvest radius increase from each level of the efficiency enchantment. A double whose product is rounded down.
radiusPerEfficiencyLevel: 0.4
radiusPerEfficiencyLevel: 0.3
# The unbreaking enchantment has a chance to prevent hoes from taking damage when breaking crops.
applyUnbreaking: true
# Harvesting tools only break mature crops.
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.0
version: 1.3.1
main: no.hyp.farmingupgrade.FarmingUpgradePlugin
api-version: 1.16
permissions:
Expand Down

0 comments on commit 0924256

Please sign in to comment.