Skip to content

Commit

Permalink
Merge pull request #93 from Th3Shadowbroker/feature/plant-growth
Browse files Browse the repository at this point in the history
Feature/plant growth
  • Loading branch information
Th3Shadowbroker authored May 11, 2023
2 parents f036831 + fec290c commit ff3a138
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>dev.th3shadowbroker.spigot</groupId>
<artifactId>OuroborosMines</artifactId>
<version>1.12.3-SNAPSHOT</version>
<version>1.13.0-SNAPSHOT</version>

<properties>
<plugin.name>${project.artifactId}</plugin.name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void onBlockBreak(BlockBreakEvent event) {
//If richness was never set or hit 0
if (!plugin.getTaskManager().hasPendingReplacementTask(event.getBlock())) {
long cooldown = minedMaterial.get().getCooldown();
new ReplacementTask(event.getBlock().getLocation(), event.getBlock().getType(), cooldown)
new ReplacementTask(event.getBlock().getLocation(), event.getBlock().getType(), cooldown, minedMaterial.get().getProperties())
.withMaterialIdentifier(minedMaterial.get().getMaterialIdentifier());

// Bamboo or Sugar cane?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.data.Ageable;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -68,8 +69,20 @@ public Optional<MineableMaterial> getMaterialProperties(Block block, Material ma
.findFirst();
Optional<MineableMaterial> regionCustomMineableMaterial = reducedMaterials(regionConfiguration.get().getMaterialList()).stream().filter(m -> validateCustom(block, m)).findFirst();

if (regionCustomMineableMaterial.isPresent()) return regionCustomMineableMaterial;
if (regionMineableMaterial.isPresent()) return regionMineableMaterial;
if (regionCustomMineableMaterial.isPresent()) {
if (!canBeHarvested(block, regionCustomMineableMaterial.get()))
return Optional.empty();

return regionCustomMineableMaterial;
}

if (regionMineableMaterial.isPresent()) {
if (!canBeHarvested(block, regionMineableMaterial.get()))
return Optional.empty();

return regionMineableMaterial;
}

if (!regionConfiguration.get().isInheritingDefaults()) return Optional.empty();
}

Expand All @@ -86,6 +99,16 @@ private boolean validateCustom(Block block, MineableMaterial mineableMaterial) {
return event.isCustom();
}

private boolean canBeHarvested(Block block, MineableMaterial material) {
if (block.getBlockData() instanceof Ageable ageable) {
if (material.hasProperties() && material.getProperties().containsKey("age")) {
var requiredAge = material.getProperties().get("age");
return ageable.getAge() >= (int) requiredAge;
}
}
return true;
}

public List<MineableMaterial> getMineableMaterials() {
return minableMaterials;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@

import dev.th3shadowbroker.ouroboros.mines.OuroborosMines;
import dev.th3shadowbroker.ouroboros.mines.events.thirdparty.itemsadder.PlaceCustomBlockEvent;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.Ageable;
import org.bukkit.block.data.BlockData;
import org.bukkit.event.EventException;
import org.bukkit.plugin.RegisteredListener;
import org.bukkit.scheduler.BukkitTask;

import java.util.Arrays;
import java.util.Map;
import java.util.Optional;

public class ReplacementTask implements Runnable {
Expand All @@ -48,24 +48,28 @@ public class ReplacementTask implements Runnable {

private MaterialIdentifier materialIdentifier;

private final Map<String, Object> properties;

private final OuroborosMines plugin = OuroborosMines.INSTANCE;

public ReplacementTask(Location blockLocation, Material material, long cooldownSeconds) {
public ReplacementTask(Location blockLocation, Material material, long cooldownSeconds, Map<String, Object> properties) {
this.location = blockLocation;
this.minedMaterial = material;
this.task = plugin.getServer().getScheduler().runTaskLater(plugin, this, cooldownSeconds);
this.plugin.getTaskManager().register(this);
this.blockData = blockLocation.getBlock().getState().getBlockData();
this.isAttachableBlock = BlockUtils.isAttachable(location.getBlock());
this.properties = properties;
}

private ReplacementTask(Location blockLocation, Material material, BlockData blockData, long cooldownSeconds) {
private ReplacementTask(Location blockLocation, Material material, BlockData blockData, long cooldownSeconds, Map<String, Object> properties) {
this.location = blockLocation;
this.minedMaterial = material;
this.task = plugin.getServer().getScheduler().runTaskLater(plugin, this, cooldownSeconds);
this.plugin.getTaskManager().register(this);
this.blockData = blockData;
this.isAttachableBlock = BlockUtils.isAttachable(location.getBlock());
this.properties = properties;
}

@Override
Expand All @@ -89,6 +93,11 @@ public void run() {
var identifier = Optional.ofNullable(materialIdentifier);
if (identifier.isEmpty() || identifier.get().isInDefaultNamespace()) {
location.getBlock().setType(minedMaterial);

if (properties != null && properties.containsKey("resetAge")) {
var resetAge = (boolean) properties.get("resetAge");
if (resetAge && blockData instanceof Ageable ageable) ageable.setAge(0);
}
location.getBlock().setBlockData(blockData);
} else {
// Call has to be forced to guarantee execution after /stop command
Expand All @@ -112,7 +121,7 @@ public void run() {
}

public void retryLater() {
new ReplacementTask(location, minedMaterial, blockData, plugin.getConfig().getInt("retryInterval", 5));
new ReplacementTask(location, minedMaterial, blockData, plugin.getConfig().getInt("retryInterval", 5), properties);
plugin.getTaskManager().unregister(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.bukkit.Material;
import org.bukkit.block.Block;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

Expand All @@ -31,7 +32,7 @@ public class WorldUtils {
public static void replaceInSequence(long offset, Material material, List<Block> blocks) {
AtomicLong currentOffset = new AtomicLong(offset);
for (Block block : blocks) {
new ReplacementTask(block.getLocation(), material, currentOffset.incrementAndGet());
new ReplacementTask(block.getLocation(), material, currentOffset.incrementAndGet(), Collections.emptyMap());
}
}

Expand Down

0 comments on commit ff3a138

Please sign in to comment.