Skip to content

Commit

Permalink
chore: support folia
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex4386 committed Oct 30, 2024
1 parent c70a5d3 commit 4a57a57
Show file tree
Hide file tree
Showing 13 changed files with 371 additions and 168 deletions.
2 changes: 1 addition & 1 deletion dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
<dependency>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.21-R0.1-SNAPSHOT</version>
<version>1.20.6-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
<dependency>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.21-R0.1-SNAPSHOT</version>
<version>1.20.6-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/me/alex4386/plugin/typhon/TyphonMultithreading.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package me.alex4386.plugin.typhon;

import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.World;

public class TyphonMultithreading {
public static final boolean isPaperMultithread = TyphonMultithreading._isPaperMultithread();
private static Boolean _isFolia = null;

private static boolean _isPaperMultithread() {
try {
Expand All @@ -12,4 +17,19 @@ private static boolean _isPaperMultithread() {
return false;
}
}

public static boolean isFolia() {
if (_isFolia != null) {
return _isFolia;
}

try {
Class.forName("io.papermc.paper.threadedregions.RegionizedServer");
_isFolia = true;
} catch (ClassNotFoundException e) {
_isFolia = false;
}

return _isFolia;
}
}
8 changes: 6 additions & 2 deletions src/main/java/me/alex4386/plugin/typhon/TyphonPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,12 @@ public void onDisable() {
}
}

vbl.shutdown();
events.shutdown();
if (vbl != null)
vbl.shutdown();

if (events != null)
events.shutdown();

isShuttingdown = false;
}

Expand Down
88 changes: 85 additions & 3 deletions src/main/java/me/alex4386/plugin/typhon/TyphonScheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,94 @@
import io.papermc.paper.threadedregions.scheduler.ScheduledTask;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.Material;
import org.bukkit.block.Block;

import java.util.*;

public class TyphonScheduler {
public static Map<Integer, Object> tasks = new HashMap<>();
private static int taskId = 1;

private static Map<Chunk, Integer> chunkTasks = new HashMap<>();
private static Map<Chunk, Queue<Map.Entry<Block, Material>>> blockChanges = new HashMap<>();
private static Map<Chunk, Runnable> chunkRunOnceTasks = new HashMap<>();

private static Queue<Runnable> globalRunOnceTasks = new LinkedList<>();
private static int globalRunOnceTaskId = -1;

private static void runBlockUpdateTask(Chunk chunk) {
Queue<Map.Entry<Block, Material>> blockChangesQueue = blockChanges.get(chunk);
if (blockChangesQueue != null) {
while (!blockChangesQueue.isEmpty()) {
Map.Entry<Block, Material> blockChange = blockChangesQueue.poll();
blockChange.getKey().setType(blockChange.getValue());
}
}
}

private static void setupBlockUpdateTask(Chunk chunk) {
if (!blockChanges.containsKey(chunk)) {
blockChanges.put(chunk, new LinkedList<>());
}
}

public static void setBlockType(Block block, Material material) {
if (TyphonMultithreading.isPaperMultithread) {
Chunk chunk = block.getChunk();

setupBlockUpdateTask(chunk);
blockChanges.get(chunk).add(new AbstractMap.SimpleEntry<>(block, material));
} else {
block.setType(material);
}
}

private static void setupGlobalTask() {
if (globalRunOnceTaskId > 0) return;
globalRunOnceTaskId = registerGlobalTask(() -> {
while (!globalRunOnceTasks.isEmpty()) {
globalRunOnceTasks.poll().run();
}
}, 1L);
}

public static void runOnce(Runnable runnable) {
if (TyphonMultithreading.isFolia()) {
setupGlobalTask();
globalRunOnceTasks.add(runnable);
} else {
runnable.run();
}
}

private static void setupChunkTask(Chunk chunk) {
if (!chunkTasks.containsKey(chunk)) {
chunkTasks.put(chunk, registerTask(chunk, () -> {
runBlockUpdateTask(chunk);
}, 1L));
}
}

public static void runOnce(Chunk chunk, Runnable runnable) {
if (TyphonMultithreading.isFolia()) {
setupChunkTask(chunk);
chunkRunOnceTasks.put(chunk, runnable);
} else {
runnable.run();
}
}

public static void runOnce(Block block, Runnable runnable) {
if (TyphonMultithreading.isFolia()) {
block.getWorld().getChunkAtAsync(block).thenAccept(chunk -> {
runOnce(chunk, runnable);
});
} else {
runnable.run();
}
}

private static synchronized int getNewTaskId() {
while (tasks.containsKey(taskId)) {
taskId++;
Expand Down Expand Up @@ -64,14 +145,15 @@ public static int registerTask(Chunk chunk, Runnable task, long interval) {
);

if (targetId > taskId) taskId = targetId;
tasks.put(taskId, task);
}

tasks.put(taskId, task);
return taskId;
}

public static void unregisterTask(int taskId) {
if (tasks.containsKey(taskId)) {
Object value = tasks.get(taskId);
if (tasks.get(taskId) instanceof ScheduledTask) {
((ScheduledTask) tasks.get(taskId)).cancel();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,16 @@ public void unregisterEvent() {
}

public void runCraterGeothermal(VolcanoVent vent) {
this.runCraterGeothermal(vent, this.getBlockToRunCraterCycle(vent));
this.dispatchCraterGeothermal(vent, this.getBlockToRunCraterCycle(vent));
}
public void runOuterCraterGeothermal(VolcanoVent vent) {
this.runCraterGeothermal(vent, this.getBlockToRunOuterCraterCycle(vent));
this.dispatchCraterGeothermal(vent, this.getBlockToRunOuterCraterCycle(vent));
}

public void dispatchCraterGeothermal(VolcanoVent vent, Block block) {
TyphonScheduler.runOnce(block, () -> {
this.runCraterGeothermal(vent, TyphonUtils.getHighestRocklikes(block));
});
}

public void runCraterGeothermal(VolcanoVent vent, Block block) {
Expand Down Expand Up @@ -206,21 +212,21 @@ public void runVolcanoGeoThermalCycle(VolcanoVent vent) {
List<Block> targets = vent.lavaFlow.getRandomLavaBlocks(lavaFlowCount);
for (Block target: targets) {
Block actualTarget = TyphonUtils.getRandomBlockInRange(target, 1, Math.max(2, (int) (2 + (4 * vent.getHeatValue(target.getLocation())))));
this.runVolcanoGeoThermal(vent, actualTarget, false);
this.dispatchVolcanoGeoThermal(vent, actualTarget, false);
}

for (int i = 0; i < lavaFlowCount; i++) {
this.runVolcanoGeoThermal(vent, this.getVolcanoGeoThermalBlock(vent));
this.dispatchVolcanoGeoThermal(vent, this.getVolcanoGeoThermalBlock(vent));
}
} else if (vent.isCaldera()) {
for (int i = 0; i < cycleCount; i++) {
this.runVolcanoGeoThermal(vent, this.getCalderaGeoThermalBlock(vent));
this.dispatchVolcanoGeoThermal(vent, this.getCalderaGeoThermalBlock(vent));
}
}

int extraCount = (int) (cycleCount * Math.pow(vent.getStatus().getScaleFactor(), 1.5));
for (int i = 0; i < extraCount; i++) {
this.runVolcanoGeoThermal(vent, this.getVolcanoGeoThermalBlock(vent));
this.dispatchVolcanoGeoThermal(vent, this.getVolcanoGeoThermalBlock(vent));
}
}

Expand All @@ -231,15 +237,13 @@ public Block getBlockToRunOuterCraterCycle(VolcanoVent vent) {
int craterRadius = vent.getRadius();
double offset = VolcanoMath.getZeroFocusedRandom() * range;

block = TyphonUtils
.getHighestRocklikes(
TyphonUtils
.getRandomBlockInRange(
vent.getCoreBlock(),
0,
(int) (craterRadius + offset)
)
);
block =
TyphonUtils
.getRandomBlockInRange(
vent.getCoreBlock(),
0,
(int) (craterRadius + offset)
);
return block;
}

Expand All @@ -254,15 +258,13 @@ public Block getBlockToRunCraterCycle(VolcanoVent vent, double geoThermalRadius)
double range = geoThermalRadius - craterRadius;
double offset = VolcanoMath.getZeroFocusedRandom() * range;

block = TyphonUtils
.getHighestRocklikes(
block =
TyphonUtils
.getRandomBlockInRange(
vent.getCoreBlock(),
0,
(int) (craterRadius + offset)
)
);
);
return block;
}

Expand Down Expand Up @@ -315,8 +317,14 @@ public Block getVolcanoGeoThermalBlock(VolcanoVent vent) {
return block;
}

public void runVolcanoGeoThermal(VolcanoVent vent, Block block) {
this.runVolcanoGeoThermal(vent, block, true);
public void dispatchVolcanoGeoThermal(VolcanoVent vent, Block block) {
this.dispatchVolcanoGeoThermal(vent, block, true);
}

public void dispatchVolcanoGeoThermal(VolcanoVent vent, Block block, boolean allowSteam) {
TyphonScheduler.runOnce(block, () -> {
this.runVolcanoGeoThermal(vent, block, allowSteam);
});
}

public void runVolcanoGeoThermal(VolcanoVent vent, Block block, boolean allowSteam) {
Expand Down Expand Up @@ -388,6 +396,12 @@ public void runGeothermalActivity(VolcanoVent vent, Block block, boolean allowSt
}

public void createTuffRing(Block block) {
TyphonScheduler.runOnce(block, () -> {
this.runTuffRing(block);
});
}

private void runTuffRing(Block block) {
int radius = 2 + (int) (Math.random() * 3);

int deep = Math.random() < 2 ? 1 : 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ public void emergencyLand() {
}

public void land() {
TyphonScheduler.runOnce(this.block.getChunk(), this::landHandler);
}

public void landHandler() {
Volcano volcano = this.vent.getVolcano();
if (this.targetLocation != null && this.landingLocation == null) this.landingLocation = this.targetLocation;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.alex4386.plugin.typhon.volcano.intrusions;

import me.alex4386.plugin.typhon.TyphonScheduler;
import me.alex4386.plugin.typhon.TyphonUtils;
import me.alex4386.plugin.typhon.volcano.Volcano;
import me.alex4386.plugin.typhon.volcano.VolcanoComposition;
Expand Down Expand Up @@ -32,6 +33,13 @@ public void metamorphoseBlock(VolcanoVent vent, Block block) {
}

public void metamorphoseBlock(VolcanoVent vent, Block block, boolean isBomb) {
// wrapper
TyphonScheduler.runOnce(block, () -> {
runMetamorphoseBlock(vent, block, isBomb);
});
}

public void runMetamorphoseBlock(VolcanoVent vent, Block block, boolean isBomb) {
Material material = block.getType();
String blockTypeName = TyphonUtils.toLowerCaseDumbEdition(material.name());

Expand Down
Loading

0 comments on commit 4a57a57

Please sign in to comment.