Skip to content

Commit

Permalink
chore: major refactorings
Browse files Browse the repository at this point in the history
This change includes:
- pathetic-mapper is no more and was replaced with a specific module pathetic-bukkit
- pathetic-model is now pathetic-engine
- pathetic-engine is now independent of the bukkit api
- the required shutdown process was replaced with a shutdown hook
- the PathfinderFactory is now part of the pathetic-api and has to be instantiated with the respective PathfinderInitializer
  • Loading branch information
Metaphoriker committed Dec 15, 2024
1 parent 5398136 commit 40fc3e3
Show file tree
Hide file tree
Showing 31 changed files with 291 additions and 274 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package de.metaphoriker.pathetic.api.pathing;

import de.metaphoriker.pathetic.api.pathing.configuration.PathfinderConfiguration;

/**
* A factory class for creating {@link Pathfinder} instances.
*
* @param <I> The type of {@link PathfinderInitializer} used to initialize the pathfinder.
*/
public abstract class PathfinderFactory<I extends PathfinderInitializer> {

protected final I initializer;

/**
* Creates a new {@link PathfinderFactory} with the given initializer.
*
* @param initializer The initializer to use for initializing the pathfinder.
*/
public PathfinderFactory(I initializer) {
this.initializer = initializer;
}

/**
* Creates a new {@link Pathfinder} instance with the given configuration.
*
* @param configuration The configuration for the pathfinder.
* @return A new {@link Pathfinder} instance.
*/
public abstract Pathfinder createPathfinder(PathfinderConfiguration configuration);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package de.metaphoriker.pathetic.api.pathing;

import de.metaphoriker.pathetic.api.pathing.configuration.PathfinderConfiguration;

/**
* An interface for initializing {@link Pathfinder} instances.
*/
public interface PathfinderInitializer {

/**
* Initializes the given {@link Pathfinder} with the given {@link PathfinderConfiguration}.
*
* @param pathfinder The pathfinder to initialize.
* @param configuration The configuration for the pathfinder.
*/
void initialize(Pathfinder pathfinder, PathfinderConfiguration configuration);
}
58 changes: 48 additions & 10 deletions pathetic-mapping/pom.xml → pathetic-bukkit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,37 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>pathetic-main</artifactId>
<groupId>de.metaphoriker</groupId>
<artifactId>pathetic-main</artifactId>
<version>3.3.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>pathetic-mapping</artifactId>
<artifactId>pathetic-bukkit</artifactId>
<version>3.3.1</version>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<relocations>
<relocation>
<pattern>org.bstats</pattern>
<shadedPattern>de.metaphoriker.pathetic.shaded.bstats</shadedPattern>
</relocation>
</relocations>
</configuration>
<executions>
<execution>
<phase>package</phase>
Expand All @@ -27,16 +42,18 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>de.metaphoriker</groupId>
<artifactId>pathetic-engine</artifactId>
<version>3.3.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
Expand All @@ -45,6 +62,27 @@
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.bstats</groupId>
<artifactId>bstats-bukkit</artifactId>
<version>3.1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>de.metaphoriker</groupId>
<artifactId>pathetic-engine</artifactId>
<version>3.3.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>de.metaphoriker</groupId>
<artifactId>pathetic-provider</artifactId>
<version>3.3.1</version>
<scope>compile</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.metaphoriker.pathetic;
package de.metaphoriker.pathetic.bukkit;

import de.metaphoriker.pathetic.Pathetic;
import lombok.experimental.UtilityClass;
import org.bstats.bukkit.Metrics;
import org.bstats.charts.SimplePie;
Expand All @@ -25,14 +26,14 @@ private void init(JavaPlugin javaPlugin) {
paths = 0;
return totalPaths;
}));
metrics.addCustomChart(new SimplePie("pathetic-engine_version", Pathetic::getModelVersion));
metrics.addCustomChart(new SimplePie("pathetic-engine_version", Pathetic::getEngineVersion));
}

private void makeSureBStatsIsInitialized() {
if (!Pathetic.isInitialized())
if (!PatheticBukkit.isInitialized())
throw new IllegalStateException("Pathetic has not been initialized yet");

init(Pathetic.getPluginInstance());
init(PatheticBukkit.getPluginInstance());
}

public synchronized void increasePathCount() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package de.metaphoriker.pathetic.bukkit;

import com.google.common.eventbus.Subscribe;
import de.metaphoriker.pathetic.Pathetic;
import de.metaphoriker.pathetic.api.event.EventPublisher;
import de.metaphoriker.pathetic.api.event.PathingStartFindEvent;
import de.metaphoriker.pathetic.bukkit.listener.ChunkInvalidateListener;
import de.metaphoriker.pathetic.bukkit.util.BukkitVersionUtil;
import de.metaphoriker.pathetic.util.ErrorLogger;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

@Slf4j
@UtilityClass
public class PatheticBukkit {

private static JavaPlugin instance;

/**
* @throws IllegalStateException If an attempt is made to initialize more than 1 time
*/
public static void initialize(JavaPlugin javaPlugin) {

if (instance != null) throw ErrorLogger.logFatalError("Can't be initialized twice");

instance = javaPlugin;
Bukkit.getPluginManager().registerEvents(new ChunkInvalidateListener(), javaPlugin);

registerPathingStartListener();
Pathetic.loadEngineVersion();

if (BukkitVersionUtil.getVersion().isUnder(16, 0)
|| BukkitVersionUtil.getVersion().isEqual(BukkitVersionUtil.Version.of(16, 0))) {
log.warn(
"Pathetic is currently running in a version older than or equal to 1.16. "
+ "Some functionalities might not be accessible, such as accessing the BlockState of blocks.");
}

log.debug("Pathetic v{} initialized", Pathetic.getEngineVersion());
}

public static boolean isInitialized() {
return instance != null;
}

public static JavaPlugin getPluginInstance() {
return instance;
}

private static void registerPathingStartListener() {
EventPublisher.registerListener(
new Object() {
@Subscribe
public void onPathingStart(PathingStartFindEvent event) {
pushToBStatsIfActivated(event);
}

private void pushToBStatsIfActivated(PathingStartFindEvent event) {
if (event.getPathfinderConfiguration().isBStats()) {
BStatsHandler.increasePathCount();
}
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package de.metaphoriker.pathetic.bukkit.factory;

import de.metaphoriker.pathetic.api.pathing.Pathfinder;
import de.metaphoriker.pathetic.api.pathing.PathfinderFactory;
import de.metaphoriker.pathetic.api.pathing.configuration.PathfinderConfiguration;
import de.metaphoriker.pathetic.api.snapshot.BlockProvider;
import de.metaphoriker.pathetic.bukkit.provider.FailingBlockProvider;
import de.metaphoriker.pathetic.engine.pathfinder.AStarPathfinder;

public class BukkitPathfinderFactory extends PathfinderFactory<BukkitPathfinderInitializer> {

/**
* Creates a new {@link PathfinderFactory} with the given initializer.
*
* @param initializer The initializer to use for initializing the pathfinder.
*/
public BukkitPathfinderFactory(BukkitPathfinderInitializer initializer) {
super(initializer);
}

@Override
public Pathfinder createPathfinder(PathfinderConfiguration configuration) {
Pathfinder pathfinder = new AStarPathfinder(getBlockProvider(configuration), configuration);
initializer.initialize(pathfinder, configuration);
return pathfinder;
}

private BlockProvider getBlockProvider(PathfinderConfiguration configuration) {
return configuration.isLoadingChunks()
? new FailingBlockProvider.RequestingBlockProvider()
: new FailingBlockProvider();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package de.metaphoriker.pathetic.bukkit.factory;

import de.metaphoriker.pathetic.api.pathing.Pathfinder;
import de.metaphoriker.pathetic.api.pathing.PathfinderInitializer;
import de.metaphoriker.pathetic.api.pathing.configuration.PathfinderConfiguration;
import de.metaphoriker.pathetic.bukkit.hook.SpigotPathfindingHook;

public class BukkitPathfinderInitializer implements PathfinderInitializer {

@Override
public void initialize(Pathfinder pathfinder, PathfinderConfiguration configuration) {
pathfinder.registerPathfindingHook(new SpigotPathfindingHook());
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package de.metaphoriker.pathetic.model.pathing.pathfinder;
package de.metaphoriker.pathetic.bukkit.hook;

import de.metaphoriker.pathetic.api.pathing.hook.PathfinderHook;
import de.metaphoriker.pathetic.api.pathing.hook.PathfindingContext;
import de.metaphoriker.pathetic.api.wrapper.Depth;
import de.metaphoriker.pathetic.util.WatchdogUtil;
import de.metaphoriker.pathetic.bukkit.util.WatchdogUtil;

public class SpigotPathfindingHook implements PathfinderHook {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.metaphoriker.pathetic.bukkit.listeners;
package de.metaphoriker.pathetic.bukkit.listener;

import de.metaphoriker.pathetic.bukkit.provider.FailingBlockProvider;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
Expand All @@ -13,7 +14,6 @@
import org.bukkit.event.block.BlockPistonRetractEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.block.LeavesDecayEvent;
import de.metaphoriker.pathetic.model.snapshot.FailingBlockProvider;

public class ChunkInvalidateListener implements Listener {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.metaphoriker.pathetic.mapping.bukkit;
package de.metaphoriker.pathetic.bukkit.mapper;

import java.util.Arrays;
import lombok.NonNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
package de.metaphoriker.pathetic.model.snapshot;
package de.metaphoriker.pathetic.bukkit.provider;

import de.metaphoriker.pathetic.api.snapshot.BlockProvider;
import de.metaphoriker.pathetic.api.wrapper.BlockInformation;
import de.metaphoriker.pathetic.api.wrapper.PathBlock;
import de.metaphoriker.pathetic.api.wrapper.PathEnvironment;
import de.metaphoriker.pathetic.api.wrapper.PathPosition;
import de.metaphoriker.pathetic.bukkit.provider.world.WorldDomain;
import de.metaphoriker.pathetic.bukkit.util.BukkitVersionUtil;
import de.metaphoriker.pathetic.bukkit.util.ChunkUtil;
import de.metaphoriker.pathetic.provider.ChunkDataProviderResolver;
import de.metaphoriker.pathetic.util.ErrorLogger;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
Expand All @@ -10,16 +20,6 @@
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.BlockState;
import de.metaphoriker.pathetic.api.snapshot.BlockProvider;
import de.metaphoriker.pathetic.api.wrapper.BlockInformation;
import de.metaphoriker.pathetic.api.wrapper.PathBlock;
import de.metaphoriker.pathetic.api.wrapper.PathEnvironment;
import de.metaphoriker.pathetic.api.wrapper.PathPosition;
import de.metaphoriker.pathetic.model.snapshot.world.WorldDomain;
import de.metaphoriker.pathetic.provider.ChunkDataProviderResolver;
import de.metaphoriker.pathetic.util.BukkitVersionUtil;
import de.metaphoriker.pathetic.util.ChunkUtils;
import de.metaphoriker.pathetic.util.ErrorLogger;

/**
* The FailingBlockProvider class implements the BlockProvider interface and provides a default
Expand Down Expand Up @@ -50,7 +50,7 @@ public class FailingBlockProvider implements BlockProvider {
public static void invalidateChunk(UUID worldUUID, int chunkX, int chunkZ) {
if (SNAPSHOTS_MAP.containsKey(worldUUID)) {
WorldDomain worldDomain = SNAPSHOTS_MAP.get(worldUUID);
long chunkKey = ChunkUtils.getChunkKey(chunkX, chunkZ);
long chunkKey = ChunkUtil.getChunkKey(chunkX, chunkZ);
worldDomain.removeSnapshot(chunkKey);
}
}
Expand All @@ -66,7 +66,7 @@ private static Optional<PathBlock> fetchBlock(PathPosition position) {
int z = position.getBlockZ() - chunkZ * 16;

Material material =
ChunkUtils.getMaterial(chunkSnapshotOptional.get(), x, position.getBlockY(), z);
ChunkUtil.getMaterial(chunkSnapshotOptional.get(), x, position.getBlockY(), z);
BlockState blockState =
CHUNK_DATA_PROVIDER_RESOLVER
.getChunkDataProvider()
Expand All @@ -84,7 +84,7 @@ private static Optional<ChunkSnapshot> getChunkSnapshot(PathPosition position) {
if (SNAPSHOTS_MAP.containsKey(position.getPathEnvironment().getUuid())) {

WorldDomain worldDomain = SNAPSHOTS_MAP.get(position.getPathEnvironment().getUuid());
long chunkKey = ChunkUtils.getChunkKey(chunkX, chunkZ);
long chunkKey = ChunkUtil.getChunkKey(chunkX, chunkZ);

Optional<ChunkSnapshot> snapshot = worldDomain.getSnapshot(chunkKey);
if (snapshot.isPresent()) return snapshot;
Expand All @@ -111,7 +111,7 @@ private static ChunkSnapshot processChunkSnapshot(
WorldDomain worldDomain =
SNAPSHOTS_MAP.computeIfAbsent(
position.getPathEnvironment().getUuid(), uuid -> new WorldDomain());
worldDomain.addSnapshot(ChunkUtils.getChunkKey(chunkX, chunkZ), chunkSnapshot);
worldDomain.addSnapshot(ChunkUtil.getChunkKey(chunkX, chunkZ), chunkSnapshot);
return chunkSnapshot;
}

Expand Down Expand Up @@ -191,7 +191,7 @@ private static PathBlock ensureBlock(PathPosition pathPosition) {
int x = pathPosition.getBlockX() - chunkX * 16;
int z = pathPosition.getBlockZ() - chunkZ * 16;

Material material = ChunkUtils.getMaterial(chunkSnapshot, x, pathPosition.getBlockY(), z);
Material material = ChunkUtil.getMaterial(chunkSnapshot, x, pathPosition.getBlockY(), z);
BlockState blockState =
CHUNK_DATA_PROVIDER_RESOLVER
.getChunkDataProvider()
Expand Down
Loading

0 comments on commit 40fc3e3

Please sign in to comment.