Skip to content

Commit

Permalink
Added a 3 tick delay to replenishing, so that it isn't immediate. (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Drachir000 committed Jan 20, 2023
1 parent b76b0e1 commit c8c945e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>de.drachir000.survival</groupId>
<artifactId>replenishenchantment</artifactId>
<version>1.2.4</version>
<version>1.2.5</version>
<packaging>jar</packaging>

<name>ReplenishEnchantment</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import de.drachir000.survival.replenishenchantment.api.ItemUtils;
import de.drachir000.survival.replenishenchantment.api.event.ReplenishEvent;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.block.Block;
Expand Down Expand Up @@ -43,7 +44,8 @@ public void onBlockBreak(BlockBreakEvent e) {
if (!inst.getMainConfiguration().getCrops().contains(e.getBlock().getType().toString()))
return;
Material material = null;
switch (e.getBlock().getType()) {
Material finalMaterial = e.getBlock().getType();
switch (finalMaterial) {
case WHEAT -> material = Material.WHEAT_SEEDS;
case CARROTS -> material = Material.CARROT;
case POTATOES -> material = Material.POTATO;
Expand Down Expand Up @@ -76,18 +78,22 @@ public void onBlockBreak(BlockBreakEvent e) {
}
}

if (!replenish && consumeItem(e.getPlayer(), 1, material))
boolean tookCropFromInventory = false;

if (!replenish && consumeItem(e.getPlayer(), 1, material)) {
replenish = true;
tookCropFromInventory = true;
}

if (replenish) {
ReplenishEvent event = new ReplenishEvent(material, drops, e.getPlayer(), e.getBlock());

if (!event.callEvent()) {
e.getBlock().getWorld().dropItemNaturally(e.getBlock().getLocation(), new ItemStack(material, 1));
if (!event.callEvent() && tookCropFromInventory) {
e.getPlayer().getInventory().addItem(new ItemStack(material, 1));
return;
}

e.setCancelled(true);
e.setDropItems(false);
for (ItemStack drop : event.getDrops()) {
e.getBlock().getWorld().dropItemNaturally(e.getBlock().getLocation(), drop);
}
Expand All @@ -97,10 +103,16 @@ public void onBlockBreak(BlockBreakEvent e) {
block.setType(Material.AIR);
block = block.getRelative(0, 1, 0);
}
Bukkit.getScheduler().runTaskLater(inst, () -> {
e.getBlock().setType(finalMaterial, true);
}, 5);
} else {
Ageable ageable = (Ageable) e.getBlock().getBlockData();
ageable.setAge(0);
e.getBlock().setBlockData(ageable);
Bukkit.getScheduler().runTaskLater(inst, () -> {
e.getBlock().setType(finalMaterial, true);
e.getBlock().setBlockData(ageable);
}, 3);
}
addAction(material, count);
}
Expand Down

0 comments on commit c8c945e

Please sign in to comment.