Skip to content

Commit

Permalink
Version 1.3.7
Browse files Browse the repository at this point in the history
- Call BlockPlaceEvent for replanting of crops.
  • Loading branch information
Torm committed Nov 24, 2022
1 parent 5d66100 commit ea1b4a3
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 59 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Upgraded farming mechanics.

### Compatibility

Tested for Spigot 1.17.1.
Tested for Spigot 1.19.2.

### Features

Expand Down
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.3.6'
version '1.3.7'

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

dependencies {
compileOnly 'org.spigotmc:spigot-api:1.17.1-R0.1-SNAPSHOT'
implementation 'org.jetbrains:annotations:15.0'
compileOnly 'org.spigotmc:spigot-api:1.19.2-R0.1-SNAPSHOT'
implementation 'org.jetbrains:annotations:23.0.0'
}
144 changes: 91 additions & 53 deletions src/main/java/no/hyp/farmingupgrade/FarmingUpgradePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.bukkit.event.*;
import org.bukkit.event.block.*;
import org.bukkit.event.player.*;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
Expand Down Expand Up @@ -496,59 +497,27 @@ boolean harvestCrop(Player player, Block block, ItemStack tool, boolean replant,
FarmingUpgradePlugin.breakBlockEffect(block, state, Sound.BLOCK_CROP_BREAK, particleScale);
block.setType(Material.AIR);

// Whether a seed has been collected for replanting.
boolean seedFound = false;
// A seed that can replant the crop.
@Nullable ItemStack foundSeed = null;
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, 0.75f + random.nextFloat() * 0.5f);
}
// Calculate the dropped item entities.
List<Item> drops = new ArrayList<>();
// If event disallows drops, do not drop anything, nor a replanting seed.
if (!upgradedEvent.isDropItems()) itemDrops.clear();
// Search for a seed to replant the crop with if replanting is enabled.
if (replant) {
for (ItemStack itemDrop : itemDrops) {
if (itemDrop.getType().isItem() && itemDrop.getType() != Material.AIR && itemDrop.getAmount() >= 1) {
drops.add(block.getWorld().dropItemNaturally(block.getLocation(), itemDrop));
}
}
// 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 replanting is enabled, and a seed is not found yet, search for one in this ItemStack.
if (foundSeed == null) {
int amount = itemDrop.getAmount();
if (itemDrop.getType() == seed && amount >= 1) {
foundSeed = itemDrop.clone();
foundSeed.setAmount(1);
itemDrop.setAmount(amount - 1);
}
}
}
}
if (mode == GameMode.CREATIVE && replant) seedFound = true; // A crop is always replanted in creative mode.
// Replant the crop if a seed was found or the player is in creative mode.
if (seedFound) {
// Replant the crop if a seed was found.
if (foundSeed != null) {
var delayRange = toolUpgrade.maximumReplantDelay - toolUpgrade.minimumReplantDelay;
int delay;
if (delayRange == 0) {
Expand All @@ -558,17 +527,86 @@ boolean harvestCrop(Player player, Block block, ItemStack tool, boolean replant,
}
var replantParticlesMultiplier = toolUpgrade.replantParticleMultiplier;
if (delay == 0) {
// Send a BlockPlaceEvent for the replanted crop.
var replacedState = block.getState();
block.setType(state.getType());
if (replantParticlesMultiplier > 0.0) fertiliseEffect(block, replantParticlesMultiplier);
var plantEvent = new BlockPlaceEvent(block, replacedState, block.getRelative(0, -1, 0), foundSeed.clone(), player, true, EquipmentSlot.HAND);
callingBlockPlaceEvent = true;
this.getServer().getPluginManager().callEvent(plantEvent);
callingBlockPlaceEvent = false;
if (plantEvent.isCancelled() || !plantEvent.canBuild()) {
// If cancelled, revert block state to air and add seed to drops.
replacedState.update();
itemDrops.add(foundSeed);
} else {
if (replantParticlesMultiplier > 0.0) fertiliseEffect(block, replantParticlesMultiplier);
}
} else {
final @Nullable var finalFoundSeed = foundSeed;
boolean drop = upgradedEvent.isDropItems() || mode != GameMode.CREATIVE;
getServer().getScheduler().runTaskLater(this, () -> {
if (block.isEmpty()) {
block.setType(state.getType());
if (replantParticlesMultiplier > 0.0) fertiliseEffect(block, replantParticlesMultiplier);
// Player must be online for a BlockPlaceEvent to happen, otherwise weird stuff could happen.
if (player.isOnline()) {
if (block.isEmpty()) {
var replacedState = block.getState();
block.setType(state.getType());
var plantEvent = new BlockPlaceEvent(block, replacedState, block.getRelative(0, -1, 0), finalFoundSeed.clone(), player, true, EquipmentSlot.HAND);
callingBlockPlaceEvent = true;
this.getServer().getPluginManager().callEvent(plantEvent);
callingBlockPlaceEvent = false;
if (plantEvent.isCancelled() || !plantEvent.canBuild()) {
replacedState.update();
if (drop) block.getWorld().dropItemNaturally(block.getLocation(), finalFoundSeed);
} else {
if (replantParticlesMultiplier > 0.0) fertiliseEffect(block, replantParticlesMultiplier);
}
} else {
if (drop) block.getWorld().dropItemNaturally(block.getLocation(), finalFoundSeed);
}
} else {
if (block.isEmpty()) {
block.setType(state.getType());
if (replantParticlesMultiplier > 0.0) fertiliseEffect(block, replantParticlesMultiplier);
} else {
if (drop) block.getWorld().dropItemNaturally(block.getLocation(), finalFoundSeed);
}
}
}, delay);
}
}
// Clear all drops if in creative.
if (mode == GameMode.CREATIVE) itemDrops.clear();
// 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, 0.75f + random.nextFloat() * 0.5f);
}
// 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));
}
}
// 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();
}
}
return true;
}

Expand Down Expand Up @@ -630,9 +668,9 @@ void onPlant(BlockPlaceEvent event) {
var aboveFarmland = farmland.getRelative(0, 1, 0);
if (aboveFarmland.getType() != Material.AIR) continue;
var placeEvent = new BlockPlaceEvent(aboveFarmland, aboveFarmland.getState(), farmland, event.getItemInHand(), player, event.canBuild()); //TODO canBuild
callingBlockPlaceEvent = true;
var savedState = aboveFarmland.getState();
aboveFarmland.setType(cropType);
callingBlockPlaceEvent = true;
getServer().getPluginManager().callEvent(placeEvent);
callingBlockPlaceEvent = false;
if (placeEvent.isCancelled() || !placeEvent.canBuild()) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ bonemealUpgrade:
# Radius of upgraded bonemeal. Make sure to also adjust plant growth rate when changing the radius.
radius: 1
# Number of trials to run for each crop. Adjust growth chance during each trial below.
# If you increase one of radius, trials, or growth chance, you can balance by decreasing the others.
trials: 8
# Target growth stages for one bonemeal. This determines the maximum growth per bonemeal.
targetGrowthStages: 8
# Ticks until the bonemeal affects a fertilised crop.
minimumDelay: 400
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.6
version: 1.3.7
main: no.hyp.farmingupgrade.FarmingUpgradePlugin
api-version: 1.16
permissions:
Expand Down

0 comments on commit ea1b4a3

Please sign in to comment.