Skip to content

Commit

Permalink
feat, wip: folia support
Browse files Browse the repository at this point in the history
  • Loading branch information
pontaoski committed Oct 31, 2023
1 parent 7eaf52c commit 889f8e8
Show file tree
Hide file tree
Showing 16 changed files with 267 additions and 99 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,9 @@
</dependency>

<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.19-R0.1-SNAPSHOT</version>
<groupId>dev.folia</groupId>
<artifactId>folia-api</artifactId>
<version>1.19.4-R0.1-SNAPSHOT</version>
<scope>provided</scope>
<exclusions>
<exclusion>
Expand Down
18 changes: 12 additions & 6 deletions src/com/dre/brewery/BCauldron.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.mini2Dx.gettext.GetText;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class BCauldron {
public enum LiquidType {
Expand Down Expand Up @@ -60,8 +61,8 @@ public static LiquidType fromString(String string) {
public static final byte EMPTY = 0, SOME = 1, FULL = 2;
public static final int PARTICLEPAUSE = 15;
public static Random particleRandom = new Random();
private static Set<UUID> plInteracted = new HashSet<>(); // Interact Event helper
public static Map<Block, BCauldron> bcauldrons = new HashMap<>(); // All active cauldrons. Mapped to their block for fast retrieve
private static Set<UUID> plInteracted = ConcurrentHashMap.newKeySet(); // Interact Event helper
public static Map<Block, BCauldron> bcauldrons = new ConcurrentHashMap<>(); // All active cauldrons. Mapped to their block for fast retrieve

private BIngredients ingredients = new BIngredients();
private final Block block;
Expand Down Expand Up @@ -294,7 +295,12 @@ public static void printTime(Player player, Block block) {
}

public void cookEffect() {
if (BUtil.isChunkLoaded(block) && LegacyUtil.isCauldronHeatsource(block.getRelative(BlockFace.DOWN))) {
P.p.scheduler.runTaskAt(block.getLocation(), sched -> {
boolean valid = BUtil.isChunkLoaded(block) && LegacyUtil.isCauldronHeatsource(block.getRelative(BlockFace.DOWN));
if (!valid) {
return;
}

Color color = getParticleColor();
// Colorable spirally spell, 0 count enables color instead of the offset variables
// Configurable RGB color. The last parameter seems to control the hue and motion, but i couldn't find
Expand Down Expand Up @@ -323,7 +329,7 @@ public void cookEffect() {
// Two hovering pixely dust clouds, a bit offset and with DustOptions to give some color and size
block.getWorld().spawnParticle(Particle.REDSTONE, particleLocation, 2, 0.15, 0.2, 0.15, new Particle.DustOptions(color, 1.5f));
}
}
}, 0);
}

private Location getRandParticleLoc() {
Expand Down Expand Up @@ -486,7 +492,7 @@ public static void clickCauldron(PlayerInteractEvent event) {
if (event.getHand() == EquipmentSlot.HAND) {
final UUID id = player.getUniqueId();
plInteracted.add(id);
P.p.getServer().getScheduler().runTask(P.p, () -> plInteracted.remove(id));
P.p.scheduler.runTaskFor(event.getPlayer(), sched -> plInteracted.remove(id), 0);
} else if (event.getHand() == EquipmentSlot.OFF_HAND) {
if (!plInteracted.remove(player.getUniqueId())) {
item = player.getInventory().getItemInMainHand();
Expand Down Expand Up @@ -608,7 +614,7 @@ public static void save(ConfigurationSection config, ConfigurationSection oldDat
// bukkit bug not updating the inventory while executing event, have to
// schedule the give
public static void giveItem(final Player player, final ItemStack item) {
P.p.getServer().getScheduler().runTaskLater(P.p, () -> player.getInventory().addItem(item), 1L);
P.p.scheduler.runTaskFor(player, sched -> player.getInventory().addItem(item), 1L);
}

}
37 changes: 22 additions & 15 deletions src/com/dre/brewery/BDistiller.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.dre.brewery;

import com.dre.brewery.lore.BrewLore;
import com.dre.brewery.utility.BTask;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
Expand All @@ -14,6 +16,8 @@

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;

/**
* Updated for 1.9 to replicate the "Brewing" process for distilling.
Expand All @@ -27,9 +31,9 @@
public class BDistiller {

private static final int DISTILLTIME = 400;
private static Map<Block, BDistiller> trackedDistillers = new HashMap<>();
private static Map<Block, BDistiller> trackedDistillers = new ConcurrentHashMap<>();

private int taskId;
private BTask task;
private int runTime = -1;
private int brewTime = -1;
private Block standBlock;
Expand All @@ -41,11 +45,11 @@ public BDistiller(Block standBlock, int fuel) {
}

public void cancelDistill() {
Bukkit.getScheduler().cancelTask(taskId); // cancel prior
task.cancel();
}

public void start() {
taskId = new DistillRunnable().runTaskTimer(P.p, 2L, 1L).getTaskId();
task = P.p.scheduler.runAsyncTaskOnTimer(new DistillRunnable(), 2L, 2L);
}

public static void distillerClick(InventoryClickEvent event) {
Expand Down Expand Up @@ -167,13 +171,20 @@ public static void showAlc(BrewerInventory inv, Brew[] contents) {
}
}

public class DistillRunnable extends BukkitRunnable {
public class DistillRunnable implements Consumer<BTask> {
private Brew[] contents = null;

@Override
public void run() {
BlockState now = standBlock.getState();
if (now instanceof BrewingStand) {
public void accept(BTask task) {
P.p.scheduler.runTaskAt(standBlock.getLocation(), sched -> {
BlockState now = standBlock.getState();
if (!(now instanceof BrewingStand)) {
task.cancel();
trackedDistillers.remove(standBlock);
P.p.debugLog("The block was replaced; not a brewing stand.");
return;
}

BrewingStand stand = (BrewingStand) now;
if (brewTime == -1) { // check at the beginning for distillables
if (!prepareForDistillables(stand)) {
Expand All @@ -189,7 +200,7 @@ public void run() {
stand.setBrewingTime(0);
stand.update();
if (!runDistill(stand.getInventory(), contents)) {
this.cancel();
task.cancel();
trackedDistillers.remove(standBlock);
P.p.debugLog("All done distilling");
} else {
Expand All @@ -199,11 +210,7 @@ public void run() {
} else {
stand.update();
}
} else {
this.cancel();
trackedDistillers.remove(standBlock);
P.p.debugLog("The block was replaced; not a brewing stand.");
}
}, 0);
}

private boolean prepareForDistillables(BrewingStand stand) {
Expand Down Expand Up @@ -233,7 +240,7 @@ private boolean prepareForDistillables(BrewingStand stand) {
}
case 0:
// No custom potion, cancel and ignore
this.cancel();
task.cancel();
trackedDistillers.remove(standBlock);
showAlc(inventory, contents);
P.p.debugLog("nothing to distill");
Expand Down
31 changes: 16 additions & 15 deletions src/com/dre/brewery/BPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.dre.brewery.lore.BrewLore;
import com.dre.brewery.recipe.BEffect;
import com.dre.brewery.recipe.BPotionEffect;
import com.dre.brewery.utility.BTask;
import com.dre.brewery.utility.BUtil;
import com.dre.brewery.utility.PermissionUtil;
import net.md_5.bungee.api.ChatMessageType;
Expand All @@ -34,7 +35,7 @@
public class BPlayer {
private static Map<String, BPlayer> players = new HashMap<>();// Players uuid and BPlayer
private static Map<Player, Integer> pTasks = new HashMap<>();// Player and count
private static int taskId;
private static BTask task;
private static Random pukeRand;

private final String uuid;
Expand Down Expand Up @@ -223,8 +224,8 @@ public void showDrunkeness(Player player) {
try {
// It this returns false, then the Action Bar is not supported. Do not repeat the message as it was sent into chat
if (sendDrunkenessMessage(player)) {
P.p.getServer().getScheduler().scheduleSyncDelayedTask(P.p, () -> sendDrunkenessMessage(player), 40);
P.p.getServer().getScheduler().scheduleSyncDelayedTask(P.p, () -> sendDrunkenessMessage(player), 80);
P.p.scheduler.runTaskFor(player, sched -> sendDrunkenessMessage(player), 40);
P.p.scheduler.runTaskFor(player, sched -> sendDrunkenessMessage(player), 80);
}
} catch (Exception e) {
e.printStackTrace();
Expand Down Expand Up @@ -308,7 +309,7 @@ public boolean sendDrunkenessMessage(Player player) {
b.append("§7]");
final String text = b.toString();
if (hangover && P.use1_11) {
P.p.getServer().getScheduler().scheduleSyncDelayedTask(P.p, () -> player.sendTitle("", text, 30, 100, 90), 160);
P.p.scheduler.runTaskFor(player, sched -> player.sendTitle("", text, 30, 100, 90), 160);
return false;
}
try {
Expand All @@ -326,7 +327,7 @@ public void drinkCap(Player player) {
drunkeness = 100;
syncToSQL(false);
if (BConfig.overdrinkKick && !player.hasPermission("brewery.bypass.overdrink")) {
P.p.getServer().getScheduler().scheduleSyncDelayedTask(P.p, () -> passOut(player), 1);
P.p.scheduler.runTaskFor(player, sched -> passOut(player), 1);
} else {
addPuke(player, 60 + (int) (Math.random() * 60.0));
P.p.msg(player, GetText.tr("You can't drink any more."));
Expand Down Expand Up @@ -472,7 +473,7 @@ public void join(final Player player) {
return;
}
// delayed login event as the player is not fully accessible pre login
P.p.getServer().getScheduler().runTaskLater(P.p, () -> login(player), 1L);
P.p.scheduler.runTaskLater((task) -> login(player), 1L);
}

// he may be having a hangover
Expand Down Expand Up @@ -578,7 +579,7 @@ public static void addPuke(Player player, int count) {
BUtil.reapplyPotionEffect(player, PotionEffectType.HUNGER.createEffect(80, 4), true);

if (pTasks.isEmpty()) {
taskId = P.p.getServer().getScheduler().scheduleSyncRepeatingTask(P.p, BPlayer::pukeTask, 1L, 1L);
task = P.p.scheduler.runAsyncTaskOnTimer(sched -> pukeTask(), 1L, 1L);
}
pTasks.put(player, event.getCount());
}
Expand All @@ -591,15 +592,15 @@ public static void pukeTask() {
if (!player.isValid() || !player.isOnline()) {
iter.remove();
}
puke(player);
P.p.scheduler.runTaskFor(player, sched -> puke(player), 0);
if (count <= 1) {
iter.remove();
} else {
entry.setValue(count - 1);
}
}
if (pTasks.isEmpty()) {
P.p.getServer().getScheduler().cancelTask(taskId);
task.cancel();
}
}

Expand Down Expand Up @@ -765,13 +766,13 @@ public static void drunkeness() {
if (bplayer.offlineDrunk == 0) {
Player player = BUtil.getPlayerfromString(name);
if (player != null) {
P.p.scheduler.runTaskFor(player, (task) -> {
bplayer.drunkEffects(player);

bplayer.drunkEffects(player);

if (BConfig.enablePuke) {
bplayer.drunkPuke(player);
}

if (BConfig.enablePuke) {
bplayer.drunkPuke(player);
}
}, 0);
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/com/dre/brewery/BSealer.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
import org.bukkit.inventory.*;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.scheduler.BukkitTask;
import org.jetbrains.annotations.NotNull;
import org.mini2Dx.gettext.GetText;

import com.dre.brewery.utility.BTask;

import java.util.Iterator;

// TODO: this class may be very bork under Folia because of inventory shenaniganery
/**
* The Sealing Inventory that is being checked for Brews and seals them after a second.
* <p>Class doesn't load in mc 1.12 and lower (Can't find RecipeChoice, BlockData and NamespacedKey)
Expand All @@ -31,7 +33,7 @@ public class BSealer implements InventoryHolder {
private final Player player;
private short[] slotTime = new short[9];
private ItemStack[] contents = null;
private BukkitTask task;
private BTask task;

public BSealer(Player player) {
this.player = player;
Expand All @@ -57,7 +59,7 @@ public BSealer(Player player) {
public void clickInv() {
contents = null;
if (task == null) {
task = P.p.getServer().getScheduler().runTaskTimer(P.p, this::itemChecking, 1, 1);
task = P.p.scheduler.runAsyncTaskOnTimer(sched -> itemChecking(), 1, 1);
}
}

Expand Down
15 changes: 9 additions & 6 deletions src/com/dre/brewery/Barrel.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import com.dre.brewery.filedata.BConfig;
import com.dre.brewery.integration.barrel.LogBlockBarrel;
import com.dre.brewery.lore.BrewLore;
import com.dre.brewery.utility.BTask;
import com.dre.brewery.utility.BUtil;
import com.dre.brewery.utility.BoundingBox;
import com.dre.brewery.utility.LegacyUtil;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
Expand All @@ -24,21 +26,22 @@
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.mini2Dx.gettext.GetText;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

/**
* A Multi Block Barrel with Inventory
*/
public class Barrel implements InventoryHolder {

public static List<Barrel> barrels = new ArrayList<>();
public static List<Barrel> barrels = Collections.synchronizedList(new ArrayList<>());
private static int check = 0; // Which Barrel was last checked

private final Block spigot;
Expand Down Expand Up @@ -108,7 +111,7 @@ public static void onUpdate() {
randomInTheBack.checked = false;
}
}
new BarrelCheck().runTaskTimer(P.p, 1, 1);
P.p.scheduler.runAsyncTaskOnTimer(new BarrelCheck(), 1, 1);
}
}

Expand Down Expand Up @@ -552,9 +555,9 @@ public static void save(ConfigurationSection config, ConfigurationSection oldDat
}
}

public static class BarrelCheck extends BukkitRunnable {
public static class BarrelCheck implements Consumer<BTask> {
@Override
public void run() {
public void accept(BTask task) {
boolean repeat = true;
while (repeat) {
if (check < barrels.size()) {
Expand All @@ -579,7 +582,7 @@ public void run() {
} else {
check = 0;
repeat = false;
cancel();
task.cancel();
}
}
}
Expand Down
Loading

0 comments on commit 889f8e8

Please sign in to comment.