Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,4 @@ build/

.DS_Store
# Ignore run folders
run-[0-9].[0-9][0-9]/
run-[0-9].[0-9][0-9].[0-9]/
run-*/
14 changes: 10 additions & 4 deletions Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
import com.plotsquared.core.setup.SettingsNodesWrapper;
import com.plotsquared.core.util.EventDispatcher;
import com.plotsquared.core.util.FileUtils;
import com.plotsquared.core.util.MinecraftVersion;
import com.plotsquared.core.util.PlatformWorldManager;
import com.plotsquared.core.util.PlayerManager;
import com.plotsquared.core.util.PremiumVerification;
Expand Down Expand Up @@ -237,14 +238,19 @@ public final class BukkitPlatform extends JavaPlugin implements Listener, PlotPl
return this.version;
}

@Override
public MinecraftVersion minecraftVersion() {
return MinecraftVersion.current();
}

@Override
public int versionMinHeight() {
return serverVersion()[1] >= 18 ? -64 : 0;
return minecraftVersion().isNewerOrEqualThan(MinecraftVersion.CAVES_AND_CLIFFS_2) ? -64 : 0;
}

@Override
public int versionMaxHeight() {
return serverVersion()[1] >= 18 ? 319 : 255;
return minecraftVersion().isNewerOrEqualThan(MinecraftVersion.CAVES_AND_CLIFFS_2) ? 319 : 255;
}

@Override
Expand Down Expand Up @@ -364,14 +370,14 @@ public void onEnable() {

if (Settings.Enabled_Components.EVENTS) {
getServer().getPluginManager().registerEvents(injector().getInstance(PlayerEventListener.class), this);
if ((serverVersion()[1] == 20 && serverVersion()[2] >= 1) || serverVersion()[1] > 20) {
if (minecraftVersion().isNewerOrEqualThan(20, 1)) {
getServer().getPluginManager().registerEvents(injector().getInstance(PlayerEventListener1201.class), this);
}
getServer().getPluginManager().registerEvents(injector().getInstance(BlockEventListener.class), this);
if (Settings.HIGH_FREQUENCY_LISTENER) {
getServer().getPluginManager().registerEvents(injector().getInstance(HighFreqBlockEventListener.class), this);
}
if (serverVersion()[1] >= 17) {
if (minecraftVersion().isNewerOrEqualThan(MinecraftVersion.CAVES_AND_CLIFFS)) {
getServer().getPluginManager().registerEvents(injector().getInstance(BlockEventListener117.class), this);
}
getServer().getPluginManager().registerEvents(injector().getInstance(EntityEventListener.class), this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.plotsquared.core.plot.world.PlotAreaManager;
import com.plotsquared.core.queue.ZeroedDelegateScopedQueueCoordinator;
import com.plotsquared.core.util.ChunkManager;
import com.plotsquared.core.util.MinecraftVersion;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.math.BlockVector2;
import com.sk89q.worldedit.math.BlockVector3;
Expand Down Expand Up @@ -90,14 +91,13 @@ public BukkitPlotGenerator(
this.plotGenerator = generator;
this.platformGenerator = this;
this.populators = new ArrayList<>();
int minecraftMinorVersion = PlotSquared.platform().serverVersion()[1];
if (minecraftMinorVersion >= 17) {
if (MinecraftVersion.current().isNewerOrEqualThan(MinecraftVersion.CAVES_AND_CLIFFS)) {
this.populators.add(new BlockStatePopulator(this.plotGenerator));
} else {
this.populators.add(new LegacyBlockStatePopulator(this.plotGenerator));
}
this.full = true;
this.useNewGenerationMethods = PlotSquared.platform().serverVersion()[1] >= 19;
this.useNewGenerationMethods = MinecraftVersion.current().isNewerOrEqualThan(MinecraftVersion.THE_WILD_UPDATE);
this.biomeProvider = new BukkitPlotBiomeProvider();
}

Expand All @@ -112,7 +112,7 @@ public BukkitPlotGenerator(final String world, final ChunkGenerator cg, final @N
this.full = false;
this.platformGenerator = cg;
this.plotGenerator = new DelegatePlotGenerator(cg, world);
this.useNewGenerationMethods = PlotSquared.platform().serverVersion()[1] >= 19;
this.useNewGenerationMethods = MinecraftVersion.current().isNewerOrEqualThan(MinecraftVersion.THE_WILD_UPDATE);
this.biomeProvider = null;
}

Expand Down Expand Up @@ -446,7 +446,7 @@ private final class BukkitPlotBiomeProvider extends BiomeProvider {

static {
Set<Biome> disabledBiomes = new HashSet<>(List.of(Biome.CUSTOM));
if (PlotSquared.platform().serverVersion()[1] <= 19) {
if (MinecraftVersion.current().isOlderOrEqualThan(MinecraftVersion.THE_WILD_UPDATE)) {
final Biome cherryGrove = Registry.BIOME.get(NamespacedKey.minecraft("cherry_grove"));
if (cherryGrove != null) {
disabledBiomes.add(cherryGrove);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
package com.plotsquared.bukkit.listener;

import com.google.inject.Inject;
import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.location.Location;
import com.plotsquared.core.plot.Plot;
import com.plotsquared.core.plot.PlotArea;
import com.plotsquared.core.plot.world.PlotAreaManager;
import com.plotsquared.core.plot.world.SinglePlotArea;
import com.plotsquared.core.util.MinecraftVersion;
import com.plotsquared.core.util.ReflectionUtils;
import com.plotsquared.core.util.ReflectionUtils.RefClass;
import com.plotsquared.core.util.ReflectionUtils.RefField;
Expand All @@ -34,59 +34,48 @@
import com.plotsquared.core.util.task.TaskManager;
import com.plotsquared.core.util.task.TaskTime;
import io.papermc.lib.PaperLib;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.BlockState;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPhysicsEvent;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.entity.EntitySpawnEvent;
import org.bukkit.event.entity.ItemSpawnEvent;
import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.event.world.ChunkUnloadEvent;
import org.checkerframework.checker.nullness.qual.NonNull;

import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Objects;

import static com.plotsquared.core.util.ReflectionUtils.getRefClass;

@SuppressWarnings("unused")
public class ChunkListener implements Listener {

private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + ChunkListener.class.getSimpleName());

private final PlotAreaManager plotAreaManager;
private final int version;

private RefMethod methodSetUnsaved;
private RefMethod methodGetHandleChunk;
private RefMethod methodGetHandleWorld;
private RefField mustNotSave;
private Object objChunkStatusFull = null;
/*
private RefMethod methodGetFullChunk;
private RefMethod methodGetBukkitChunk;
private RefMethod methodGetChunkProvider;
private RefMethod methodGetVisibleMap;
private RefField worldServer;
private RefField playerChunkMap;
private RefField updatingChunks;
private RefField visibleChunks;
*/
private Chunk lastChunk;
private boolean ignoreUnload = false;

@Inject
public ChunkListener(final @NonNull PlotAreaManager plotAreaManager) {
this.plotAreaManager = plotAreaManager;
version = PlotSquared.platform().serverVersion()[1];
if (!Settings.Chunk_Processor.AUTO_TRIM) {
return;
}
Expand All @@ -98,76 +87,24 @@ public ChunkListener(final @NonNull PlotAreaManager plotAreaManager) {
try {
this.methodGetHandleChunk = classCraftChunk.getMethod("getHandle");
} catch (NoSuchMethodException ignored) {
try {
RefClass classChunkStatus = getRefClass("net.minecraft.world.level.chunk.ChunkStatus");
this.objChunkStatusFull = classChunkStatus.getRealClass().getField("n").get(null);
this.methodGetHandleChunk = classCraftChunk.getMethod("getHandle", classChunkStatus.getRealClass());
} catch (NoSuchMethodException ex) {
throw new RuntimeException(ex);
}
RefClass classChunkStatus = getRefClass("net.minecraft.world.level.chunk.ChunkStatus");
this.objChunkStatusFull = classChunkStatus.getRealClass().getField("n").get(null);
this.methodGetHandleChunk = classCraftChunk.getMethod("getHandle", classChunkStatus.getRealClass());
}
try {
if (version < 17) {
RefClass classChunk = getRefClass("{nms}.Chunk");
this.mustNotSave = classChunk.getField("mustNotSave");
} else {
RefClass classChunk = getRefClass("net.minecraft.world.level.chunk.Chunk");
this.mustNotSave = classChunk.getField("mustNotSave");
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
if (MinecraftVersion.current().isNewerOrEqualThan(MinecraftVersion.CAVES_AND_CLIFFS)) {
RefClass classChunk = getRefClass("net.minecraft.world.level.chunk.Chunk");
this.mustNotSave = classChunk.getField("mustNotSave");
} else {
RefClass classChunk = getRefClass("{nms}.Chunk");
this.mustNotSave = classChunk.getField("mustNotSave");
}
} catch (Throwable ignored) {
} catch (Throwable t) {
Settings.Chunk_Processor.AUTO_TRIM = false;
LOGGER.error("Failed to initialize NMS access for auto-trimming capabilities. Disabling auto trim", t);
}
for (World world : Bukkit.getWorlds()) {
world.setAutoSave(false);
}
if (version > 13) {
return;
}
TaskManager.runTaskRepeat(() -> {
try {
HashSet<Chunk> toUnload = new HashSet<>();
for (World world : Bukkit.getWorlds()) {
String worldName = world.getName();
if (!this.plotAreaManager.hasPlotArea(worldName)) {
continue;
}
Object craftWorld = methodGetHandleWorld.of(world).call();
if (version == 13) {
Object chunkMap = craftWorld.getClass().getDeclaredMethod("getPlayerChunkMap").invoke(craftWorld);
Method methodIsChunkInUse =
chunkMap.getClass().getDeclaredMethod("isChunkInUse", int.class, int.class);
Chunk[] chunks = world.getLoadedChunks();
for (Chunk chunk : chunks) {
if ((boolean) methodIsChunkInUse.invoke(chunkMap, chunk.getX(), chunk.getZ())) {
continue;
}
int x = chunk.getX();
int z = chunk.getZ();
if (!shouldSave(worldName, x, z)) {
unloadChunk(worldName, chunk, false);
continue;
}
toUnload.add(chunk);
}
}
}
if (toUnload.isEmpty()) {
return;
}
long start = System.currentTimeMillis();
for (Chunk chunk : toUnload) {
if (System.currentTimeMillis() - start > 5) {
return;
}
chunk.unload(true);
}
} catch (Throwable e) {
e.printStackTrace();
}
}, TaskTime.ticks(1L));
}

public boolean unloadChunk(String world, Chunk chunk, boolean safe) {
Expand Down Expand Up @@ -263,25 +200,7 @@ public void onChunkLoad(ChunkLoadEvent event) {

@EventHandler(priority = EventPriority.LOWEST)
public void onItemSpawn(ItemSpawnEvent event) {
Item entity = event.getEntity();
PaperLib.getChunkAtAsync(event.getLocation()).thenAccept(chunk -> {
if (chunk == this.lastChunk) {
event.getEntity().remove();
event.setCancelled(true);
return;
}
if (!this.plotAreaManager.hasPlotArea(chunk.getWorld().getName())) {
return;
}
Entity[] entities = chunk.getEntities();
if (entities.length > Settings.Chunk_Processor.MAX_ENTITIES) {
event.getEntity().remove();
event.setCancelled(true);
this.lastChunk = chunk;
} else {
this.lastChunk = null;
}
});
this.onInternalEntitySpawn(event);
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
Expand All @@ -293,7 +212,10 @@ public void onBlockPhysics(BlockPhysicsEvent event) {

@EventHandler(priority = EventPriority.LOWEST)
public void onEntitySpawn(CreatureSpawnEvent event) {
LivingEntity entity = event.getEntity();
this.onInternalEntitySpawn(event);
}

private void onInternalEntitySpawn(EntitySpawnEvent event) {
PaperLib.getChunkAtAsync(event.getLocation()).thenAccept(chunk -> {
if (chunk == this.lastChunk) {
event.getEntity().remove();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import com.plotsquared.core.plot.world.PlotAreaManager;
import com.plotsquared.core.util.EventDispatcher;
import com.plotsquared.core.util.MathMan;
import com.plotsquared.core.util.MinecraftVersion;
import com.plotsquared.core.util.PlotFlagUtil;
import com.plotsquared.core.util.PremiumVerification;
import com.plotsquared.core.util.entity.EntityCategories;
Expand Down Expand Up @@ -216,8 +217,7 @@ public class PlayerEventListener implements Listener {
"PINK_DYE",
"GLOW_INK_SAC"
));
int[] version = PlotSquared.platform().serverVersion();
if (version[1] >= 20) {
if (MinecraftVersion.current().isNewerOrEqualThan(MinecraftVersion.TRAILS_AND_TALES)) {
mutableDyes.add("HONEYCOMB");
}
DYES = Set.copyOf(mutableDyes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.plotsquared.core.plot.world.PlotAreaManager;
import com.plotsquared.core.plot.world.SinglePlotArea;
import com.plotsquared.core.plot.world.SinglePlotAreaManager;
import com.plotsquared.core.util.MinecraftVersion;
import com.plotsquared.core.util.ReflectionUtils;
import org.bukkit.Chunk;
import org.bukkit.World;
Expand Down Expand Up @@ -49,7 +50,7 @@ public SingleWorldListener() throws Exception {
this.methodGetHandleChunk = classCraftChunk.getMethod("getHandle").getRealMethod();
} catch (NoSuchMethodException ignored) {
try {
String chunkStatus = PlotSquared.platform().serverVersion()[1] < 21
String chunkStatus = MinecraftVersion.current().isOlderThan(MinecraftVersion.TRICKY_TRIALS)
? "net.minecraft.world.level.chunk" + ".ChunkStatus"
: "net.minecraft.world.level.chunk.status.ChunkStatus";
ReflectionUtils.RefClass classChunkStatus = getRefClass(chunkStatus);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.plotsquared.core.plot.world.PlotAreaManager;
import com.plotsquared.core.util.EventDispatcher;
import com.plotsquared.core.util.MathMan;
import com.plotsquared.core.util.MinecraftVersion;
import com.plotsquared.core.util.WorldUtil;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.extension.platform.Actor;
Expand Down Expand Up @@ -313,7 +314,7 @@ public void setFlight(boolean fly) {
@Override
public void playMusic(final @NonNull Location location, final @NonNull ItemType id) {
if (id == ItemTypes.AIR) {
if (PlotSquared.platform().serverVersion()[1] >= 19) {
if (MinecraftVersion.current().isOlderOrEqualThan(MinecraftVersion.THE_WILD_UPDATE)) {
player.stopSound(SoundCategory.MUSIC);
return;
}
Expand Down
Loading
Loading