9
0
mirror of https://github.com/Xiao-MoMi/Custom-Crops.git synced 2025-12-19 15:09:25 +00:00

further improve farmlands

This commit is contained in:
XiaoMoMi
2024-09-04 23:14:46 +08:00
parent 375c9c5938
commit 956d2863b3
4 changed files with 33 additions and 6 deletions

View File

@@ -205,13 +205,18 @@ public abstract class AbstractCustomEventListener implements Listener {
public void onBlockChange(BlockFadeEvent event) {
Block block = event.getBlock();
if (block.getType() == Material.FARMLAND) {
Pos3 above = Pos3.from(block.getLocation()).add(0,1,0);
BukkitCustomCropsPlugin.getInstance().getWorldManager().getWorld(block.getWorld())
.flatMap(world -> world.getBlockState(above)).ifPresent(blockState -> {
if (blockState.type() instanceof CropBlock) {
Pos3 pos3 = Pos3.from(block.getLocation());
Pos3 above = pos3.add(0,1,0);
Optional<CustomCropsWorld<?>> optionalWorld = BukkitCustomCropsPlugin.getInstance().getWorldManager().getWorld(block.getWorld());
if (optionalWorld.isPresent()) {
CustomCropsWorld<?> world = optionalWorld.get();
Optional<CustomCropsBlockState> optionalState = world.getBlockState(above);
if (optionalState.isPresent() && optionalState.get().type() instanceof CropBlock) {
event.setCancelled(true);
return;
}
});
this.itemManager.handlePhysicsBreak(block.getLocation(), "FARMLAND", event);
}
}
}

View File

@@ -62,6 +62,12 @@ public abstract class AbstractItemManager implements ItemManager {
Cancellable event
);
public abstract void handlePhysicsBreak(
Location location,
String brokenID,
Cancellable event
);
public abstract void handleEntityTrample(
Entity entity,
Location location,

View File

@@ -21,5 +21,6 @@ public enum BreakReason {
BREAK,
TRAMPLE,
EXPLODE,
ACTION
ACTION,
PHYSICS,
}

View File

@@ -474,6 +474,21 @@ public class BukkitItemManager extends AbstractItemManager {
}
}
@Override
public void handlePhysicsBreak(Location location, String brokenID, Cancellable event) {
Optional<CustomCropsWorld<?>> optionalWorld = plugin.getWorldManager().getWorld(location.getWorld());
if (optionalWorld.isEmpty()) {
return;
}
CustomCropsWorld<?> world = optionalWorld.get();
WrappedBreakEvent wrapped = new WrappedBreakEvent(null, null, world, location, brokenID, null, null, BreakReason.PHYSICS, event);
CustomCropsBlock customCropsBlock = Registries.BLOCKS.get(brokenID);
if (customCropsBlock != null) {
customCropsBlock.onBreak(wrapped);
}
}
@Override
public void handleEntityTrample(Entity entity, Location location, String brokenID, Cancellable event) {
Optional<CustomCropsWorld<?>> optionalWorld = plugin.getWorldManager().getWorld(entity.getWorld());