Skip to content

Commit

Permalink
Merge pull request #77 from patheloper/provider
Browse files Browse the repository at this point in the history
Renamed NMSInterface to ChunkDataProvider
  • Loading branch information
Metaphoriker authored Jun 13, 2024
2 parents 9d169e8 + be02c00 commit 6fb0bd3
Show file tree
Hide file tree
Showing 18 changed files with 163 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.bukkit.block.BlockState;

/** This is for internal purpose only and is used to receive a ChunkSnapshot version-independent. */
public interface NMSInterface {
public interface ChunkDataProvider {

/**
* Returns a {@link ChunkSnapshot} of the chunk at the given coordinates.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.patheloper.api.wrapper.PathEnvironment;
import org.patheloper.api.wrapper.PathPosition;
import org.patheloper.model.snapshot.world.WorldDomain;
import org.patheloper.nms.NMSUtils;
import org.patheloper.provider.ChunkDataProviderResolver;
import org.patheloper.util.BukkitVersionUtil;
import org.patheloper.util.ChunkUtils;
import org.patheloper.util.ErrorLogger;
Expand All @@ -40,11 +40,12 @@ public class FailingSnapshotManager implements SnapshotManager {

private static final Map<UUID, WorldDomain> SNAPSHOTS_MAP = new ConcurrentHashMap<>();

private static final NMSUtils NMS_UTILS;
private static final ChunkDataProviderResolver CHUNK_DATA_PROVIDER_RESOLVER;

static {
BukkitVersionUtil.Version version = BukkitVersionUtil.getVersion();
NMS_UTILS = new NMSUtils((int) version.major(), (int) version.minor());
CHUNK_DATA_PROVIDER_RESOLVER =
new ChunkDataProviderResolver((int) version.major(), (int) version.minor());
}

public static void invalidateChunk(UUID worldUUID, int chunkX, int chunkZ) {
Expand All @@ -69,7 +70,9 @@ private static Optional<PathBlock> fetchBlock(PathPosition position) {
Material material =
ChunkUtils.getMaterial(chunkSnapshotOptional.get(), x, position.getBlockY(), z);
BlockState blockState =
NMS_UTILS.getNmsInterface().getBlockState(chunkSnapshotOptional.get(), x, position.getBlockY(), z);
CHUNK_DATA_PROVIDER_RESOLVER
.getChunkDataProvider()
.getBlockState(chunkSnapshotOptional.get(), x, position.getBlockY(), z);
return Optional.of(new PathBlock(position, new BlockInformation(material, blockState)));
}

Expand Down Expand Up @@ -98,7 +101,9 @@ private static Optional<ChunkSnapshot> getChunkSnapshot(PathPosition position) {
position,
chunkX,
chunkZ,
NMS_UTILS.getNmsInterface().getSnapshot(world, chunkX, chunkZ)));
CHUNK_DATA_PROVIDER_RESOLVER
.getChunkDataProvider()
.getSnapshot(world, chunkX, chunkZ)));

return Optional.empty();
}
Expand Down Expand Up @@ -128,7 +133,9 @@ public static class RequestingSnapshotManager extends FailingSnapshotManager {
private static ChunkSnapshot retrieveChunkSnapshot(
PathEnvironment world, int chunkX, int chunkZ) {
World bukkitWorld = Bukkit.getWorld(world.getUuid());
return NMS_UTILS.getNmsInterface().getSnapshot(bukkitWorld, chunkX, chunkZ);
return CHUNK_DATA_PROVIDER_RESOLVER
.getChunkDataProvider()
.getSnapshot(bukkitWorld, chunkX, chunkZ);
}

private static ChunkSnapshot retrieveSnapshot(PathPosition position) {
Expand Down Expand Up @@ -160,7 +167,9 @@ private static PathBlock ensureBlock(PathPosition pathPosition) {

Material material = ChunkUtils.getMaterial(chunkSnapshot, x, pathPosition.getBlockY(), z);
BlockState blockState =
NMS_UTILS.getNmsInterface().getBlockState(chunkSnapshot, x, pathPosition.getBlockY(), z);
CHUNK_DATA_PROVIDER_RESOLVER
.getChunkDataProvider()
.getBlockState(chunkSnapshot, x, pathPosition.getBlockY(), z);
return new PathBlock(pathPosition, new BlockInformation(material, blockState));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.patheloper.paper;
package org.patheloper.provider.paper;

import org.bukkit.Bukkit;
import org.bukkit.ChunkSnapshot;
import org.bukkit.World;
import org.bukkit.block.BlockState;
import org.patheloper.api.snapshot.NMSInterface;
import org.patheloper.api.snapshot.ChunkDataProvider;

public class PaperNMSInterface implements NMSInterface {
public class PaperChunkDataProvider implements ChunkDataProvider {

@Override
public ChunkSnapshot getSnapshot(World world, int chunkX, int chunkZ) {
Expand Down
104 changes: 0 additions & 104 deletions pathetic-nms/src/main/java/org/patheloper/nms/NMSUtils.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package org.patheloper.provider;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.patheloper.api.snapshot.ChunkDataProvider;
import org.patheloper.provider.v1_12.OneTwelveChunkDataProviderImpl;
import org.patheloper.provider.v1_15.OneFifteenChunkDataProviderImpl;
import org.patheloper.provider.v1_16.OneSixteenChunkDataProviderImpl;
import org.patheloper.provider.v1_17.OneSeventeenChunkDataProviderImpl;
import org.patheloper.provider.v1_18.OneEighteenChunkDataProviderImpl;
import org.patheloper.provider.v1_18_R2.OneEighteenTwoChunkDataProviderImpl;
import org.patheloper.provider.v1_19_R2.OneNineteenTwoChunkDataProviderImpl;
import org.patheloper.provider.v1_19_R3.OneNineteenThreeChunkDataProviderImpl;
import org.patheloper.provider.v1_20_R1.OneTwentyOneChunkDataProviderImpl;
import org.patheloper.provider.v1_20_R2.OneTwentyTwoChunkDataProviderImpl;
import org.patheloper.provider.v1_20_R3.OneTwentyThreeChunkDataProviderImpl;
import org.patheloper.provider.v1_20_R4.OneTwentyFourChunkDataProviderImpl;
import org.patheloper.provider.v1_8.OneEightChunkDataProviderImpl;
import org.patheloper.provider.paper.PaperChunkDataProvider;

@Getter
@Slf4j
public class ChunkDataProviderResolver {

private final ChunkDataProvider chunkDataProvider;

public ChunkDataProviderResolver(int major, int minor) {
String version = "1." + major + "." + minor;

if (isPaper()) {
chunkDataProvider = new PaperChunkDataProvider();
} else {
chunkDataProvider = determineChunkDataProvider(major, minor);
}

log.info(
"Detected version v{}, using {}", version, chunkDataProvider.getClass().getSimpleName());
}

private ChunkDataProvider determineChunkDataProvider(int major, int minor) {
final ChunkDataProvider chunkDataProvider;
switch (major) {
case 20:
if (minor == 5 || minor == 6) {
chunkDataProvider = new OneTwentyFourChunkDataProviderImpl();
break;
} else if (minor == 3 || minor == 4) {
chunkDataProvider = new OneTwentyThreeChunkDataProviderImpl();
break;
} else if (minor == 2) {
chunkDataProvider = new OneTwentyTwoChunkDataProviderImpl();
break;
} else if (minor == 1) {
chunkDataProvider = new OneTwentyOneChunkDataProviderImpl();
break;
}
throw new IllegalArgumentException("Unsupported version: " + major + "." + minor);
case 19:
if (minor == 2 || minor == 3) {
chunkDataProvider = new OneNineteenTwoChunkDataProviderImpl();
break;
}
if (minor == 4) {
chunkDataProvider = new OneNineteenThreeChunkDataProviderImpl();
break;
}
throw new IllegalArgumentException("Unsupported version: " + major + "." + minor);
case 18:
if (minor == 2) {
chunkDataProvider = new OneEighteenTwoChunkDataProviderImpl();
break;
}
chunkDataProvider = new OneEighteenChunkDataProviderImpl();
break;
case 17:
chunkDataProvider = new OneSeventeenChunkDataProviderImpl();
break;
case 16:
chunkDataProvider = new OneSixteenChunkDataProviderImpl();
break;
case 15:
chunkDataProvider = new OneFifteenChunkDataProviderImpl();
break;
case 12:
chunkDataProvider = new OneTwelveChunkDataProviderImpl();
break;
case 8:
chunkDataProvider = new OneEightChunkDataProviderImpl();
break;
default:
throw new IllegalArgumentException("Unsupported version: " + major + "." + minor);
}
return chunkDataProvider;
}

private boolean isPaper() {
try {
Class.forName("io.papermc.paper.configuration.GlobalConfiguration");
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.patheloper.nms.v1_12;
package org.patheloper.provider.v1_12;

import net.minecraft.server.v1_12_R1.Chunk;
import net.minecraft.server.v1_12_R1.WorldServer;
import org.bukkit.ChunkSnapshot;
import org.bukkit.World;
import org.bukkit.block.BlockState;
import org.bukkit.craftbukkit.v1_12_R1.CraftWorld;
import org.patheloper.api.snapshot.NMSInterface;
import org.patheloper.api.snapshot.ChunkDataProvider;

public class OneTwelveNMSInterface implements NMSInterface {
public class OneTwelveChunkDataProviderImpl implements ChunkDataProvider {

@Override
public ChunkSnapshot getSnapshot(World world, int chunkX, int chunkZ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.patheloper.nms.v1_15;
package org.patheloper.provider.v1_15;

import net.minecraft.server.v1_15_R1.ChunkStatus;
import net.minecraft.server.v1_15_R1.WorldServer;
Expand All @@ -7,9 +7,9 @@
import org.bukkit.block.BlockState;
import org.bukkit.craftbukkit.v1_15_R1.CraftChunk;
import org.bukkit.craftbukkit.v1_15_R1.CraftWorld;
import org.patheloper.api.snapshot.NMSInterface;
import org.patheloper.api.snapshot.ChunkDataProvider;

public class OneFifteenNMSInterface implements NMSInterface {
public class OneFifteenChunkDataProviderImpl implements ChunkDataProvider {

@Override
public ChunkSnapshot getSnapshot(World world, int chunkX, int chunkZ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.patheloper.nms.v1_16;
package org.patheloper.provider.v1_16;

import net.minecraft.server.v1_16_R3.ChunkStatus;
import net.minecraft.server.v1_16_R3.WorldServer;
Expand All @@ -7,9 +7,9 @@
import org.bukkit.block.BlockState;
import org.bukkit.craftbukkit.v1_16_R3.CraftChunk;
import org.bukkit.craftbukkit.v1_16_R3.CraftWorld;
import org.patheloper.api.snapshot.NMSInterface;
import org.patheloper.api.snapshot.ChunkDataProvider;

public class OneSixteenNMSInterface implements NMSInterface {
public class OneSixteenChunkDataProviderImpl implements ChunkDataProvider {

@Override
public ChunkSnapshot getSnapshot(World world, int chunkX, int chunkZ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.patheloper.nms.v1_17;
package org.patheloper.provider.v1_17;

import net.minecraft.server.level.WorldServer;
import net.minecraft.world.level.block.state.IBlockData;
Expand All @@ -11,9 +11,9 @@
import org.bukkit.craftbukkit.v1_17_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_17_R1.block.CraftBlockStates;
import org.bukkit.craftbukkit.v1_17_R1.block.data.CraftBlockData;
import org.patheloper.api.snapshot.NMSInterface;
import org.patheloper.api.snapshot.ChunkDataProvider;

public class OneSeventeenNMSInterface implements NMSInterface {
public class OneSeventeenChunkDataProviderImpl implements ChunkDataProvider {

@Override
public ChunkSnapshot getSnapshot(World world, int chunkX, int chunkZ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.patheloper.nms.v1_18;
package org.patheloper.provider.v1_18;

import java.lang.reflect.Field;
import net.minecraft.server.level.WorldServer;
Expand All @@ -13,9 +13,9 @@
import org.bukkit.craftbukkit.v1_18_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_18_R1.block.CraftBlockStates;
import org.bukkit.craftbukkit.v1_18_R1.block.data.CraftBlockData;
import org.patheloper.api.snapshot.NMSInterface;
import org.patheloper.api.snapshot.ChunkDataProvider;

public class OneEighteenNMSInterface implements NMSInterface {
public class OneEighteenChunkDataProviderImpl implements ChunkDataProvider {

private static final Field blockIDField;

Expand Down
Loading

0 comments on commit 6fb0bd3

Please sign in to comment.