9
0
mirror of https://github.com/Xiao-MoMi/Custom-Crops.git synced 2025-12-29 11:59:15 +00:00

optimize events

This commit is contained in:
XiaoMoMi
2024-11-10 18:56:45 +08:00
parent 53a77d16bd
commit f2739391e5
5 changed files with 28 additions and 6 deletions

View File

@@ -232,7 +232,7 @@ public abstract class AbstractCustomEventListener implements Listener {
Optional<CustomCropsWorld<?>> optionalWorld = BukkitCustomCropsPlugin.getInstance().getWorldManager().getWorld(block.getWorld());
if (optionalWorld.isPresent()) {
CustomCropsWorld<?> world = optionalWorld.get();
Optional<CustomCropsBlockState> optionalState = world.getBlockState(above);
Optional<CustomCropsBlockState> optionalState = world.getLoadedBlockState(above);
if (optionalState.isPresent() && optionalState.get().type() instanceof CropBlock) {
event.setCancelled(true);
return;
@@ -281,7 +281,8 @@ public abstract class AbstractCustomEventListener implements Listener {
}
CustomCropsWorld<?> w = world.get();
for (Block block : event.getBlocks()) {
if (w.getBlockState(Pos3.from(block.getLocation())).isPresent()) {
Pos3 pos3 = Pos3.from(block.getLocation());
if (w.getLoadedBlockState(pos3).isPresent()) {
event.setCancelled(true);
return;
}
@@ -296,7 +297,8 @@ public abstract class AbstractCustomEventListener implements Listener {
}
CustomCropsWorld<?> w = world.get();
for (Block block : event.getBlocks()) {
if (w.getBlockState(Pos3.from(block.getLocation())).isPresent()) {
Pos3 pos3 = Pos3.from(block.getLocation());
if (w.getLoadedBlockState(pos3).isPresent()) {
event.setCancelled(true);
return;
}
@@ -362,7 +364,7 @@ public abstract class AbstractCustomEventListener implements Listener {
Block relative = block.getRelative(directional.getFacing());
Location location = relative.getLocation();
Pos3 pos3 = Pos3.from(location);
world.getBlockState(pos3).ifPresent(state -> {
world.getLoadedBlockState(pos3).ifPresent(state -> {
if (state.type() instanceof CropBlock cropBlock) {
ItemStack itemStack = event.getItem();
String itemID = itemManager.id(itemStack);

View File

@@ -163,6 +163,15 @@ public interface CustomCropsWorld<W> {
*/
CustomCropsRegion[] loadedRegions();
/**
* Gets the block state in a loaded chunk
*
* @param location location
* @return the optional block state
*/
@NotNull
Optional<CustomCropsBlockState> getLoadedBlockState(Pos3 location);
/**
* Gets the block state at a specific location.
*

View File

@@ -147,6 +147,18 @@ public class CustomCropsWorldImpl<W> implements CustomCropsWorld<W> {
return loadedRegions.values().toArray(new CustomCropsRegion[0]);
}
@Override
public @NotNull Optional<CustomCropsBlockState> getLoadedBlockState(Pos3 location) {
ChunkPos pos = location.toChunkPos();
Optional<CustomCropsChunk> chunk = getLoadedChunk(pos);
if (chunk.isEmpty()) {
return Optional.empty();
} else {
CustomCropsChunk customChunk = chunk.get();
return customChunk.getBlockState(location);
}
}
@NotNull
@Override
public Optional<CustomCropsBlockState> getBlockState(Pos3 location) {