Skip to content

Commit

Permalink
Merge pull request #69 from BreweryTeam/paperlib
Browse files Browse the repository at this point in the history
Merge Paperlib Additions
  • Loading branch information
Jsinco authored Dec 10, 2024
2 parents 07c1bb1 + fa9a3a0 commit 45e3210
Show file tree
Hide file tree
Showing 19 changed files with 137 additions and 113 deletions.
10 changes: 7 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ val langVersion = 17
val encoding = "UTF-8"

group = "com.dre.brewery"
version = "3.4.3"
version = "3.4.4-SNAPSHOT"

repositories {
mavenCentral()
Expand All @@ -50,13 +50,17 @@ repositories {
maven("https://repo.glaremasters.me/repository/towny/") // Towny
maven("https://repo.oraxen.com/releases") // Oraxen
maven("https://storehouse.okaeri.eu/repository/maven-public/") // Okaeri Config
maven("https://papermc.io/repo/repository/maven-public/") // PaperLib
}

dependencies {
// Spigot
compileOnly("org.spigotmc:spigot-api:1.20.2-R0.1-SNAPSHOT") {
exclude("com.google.code.gson", "gson") // Implemented manually
}
// Paper Lib, performance improvements on Paper based servers and async teleporting on Folia
implementation("io.papermc:paperlib:1.0.8")

// Implemented manually mainly due to older server versions implementing versions of GSON
// Which don't support records.
implementation("com.google.code.gson:gson:2.11.0")
Expand Down Expand Up @@ -114,7 +118,7 @@ tasks {

build {
dependsOn(shadowJar)
finalizedBy("kotlinReducedJar")
//finalizedBy("kotlinReducedJar")
}

jar {
Expand All @@ -141,6 +145,7 @@ tasks {
relocate("com.github.Anon8281.universalScheduler", "com.dre.brewery.depend.universalScheduler")
relocate("eu.okaeri", "com.dre.brewery.depend.okaeri")
relocate("com.mongodb", "com.dre.brewery.depend.mongodb")
relocate("io.papermc.lib", "com.dre.brewery.depend.paperlib")

archiveClassifier.set("")
}
Expand Down Expand Up @@ -207,7 +212,6 @@ publishing {

publications {
if (user == null || pass == null) {
println("No repository credentials found, skipping publication")
return@publications
}
create<MavenPublication>("maven") {
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/dre/brewery/BDistiller.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
import com.dre.brewery.utility.MinecraftVersion;
import com.github.Anon8281.universalScheduler.UniversalRunnable;
import com.github.Anon8281.universalScheduler.scheduling.tasks.MyScheduledTask;
import io.papermc.lib.PaperLib;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.BrewingStand;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.BrewerInventory;
Expand Down Expand Up @@ -73,17 +73,17 @@ public void start() {
}

public static void distillerClick(InventoryClickEvent event) {
BrewerInventory standInv = (BrewerInventory) event.getInventory();
final Block standBlock = standInv.getHolder().getBlock();
BrewingStand standInv = (BrewingStand) PaperLib.getHolder(event.getInventory(), true).getHolder();
final Block standBlock = standInv.getBlock();

// If we were already tracking the brewer, cancel any ongoing event due to the click.
BDistiller distiller = trackedDistillers.get(standBlock);
if (distiller != null) {
distiller.cancelDistill();
standInv.getHolder().setBrewingTime(0); // Fixes brewing continuing without fuel for normal potions
standInv.getHolder().update();
standInv.setBrewingTime(0); // Fixes brewing continuing without fuel for normal potions
standInv.update();
}
final int fuel = standInv.getHolder().getFuelLevel();
final int fuel = standInv.getFuelLevel();

// Now check if we should bother to track it.
distiller = new BDistiller(standBlock, fuel);
Expand Down Expand Up @@ -204,7 +204,7 @@ public void run() {
return;
}

BrewingStand stand = (BrewingStand) standBlock.getState();
BrewingStand stand = (BrewingStand) PaperLib.getBlockState(standBlock, true).getState();
if (brewTime == -1 && !prepareForDistillables(stand)) { // check at the beginning for distillables
return;
}
Expand Down
32 changes: 17 additions & 15 deletions src/main/java/com/dre/brewery/BPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.dre.brewery.utility.MinecraftVersion;
import com.dre.brewery.utility.PermissionUtil;
import com.github.Anon8281.universalScheduler.scheduling.tasks.MyScheduledTask;
import io.papermc.lib.PaperLib;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
Expand Down Expand Up @@ -538,7 +539,7 @@ public void join(final Player player) {
if (config.isEnableWake() && !player.hasPermission("brewery.bypass.teleport")) {
Location randomLoc = Wakeup.getRandom(player.getLocation());
if (randomLoc != null) {
player.teleport(randomLoc);
PaperLib.teleportAsync(player, randomLoc);
lang.sendEntry(player, "Player_Wake");
}
}
Expand All @@ -553,20 +554,21 @@ public void disconnecting() {

public void goHome(final Player player) {
String homeType = config.getHomeType();
if (homeType != null) {
Location home = null;
if (homeType.equalsIgnoreCase("bed")) {
home = player.getBedSpawnLocation();
} else if (homeType.startsWith("cmd: ")) {
player.performCommand(homeType.substring(5));
} else if (homeType.startsWith("cmd:")) {
player.performCommand(homeType.substring(4));
} else {
Logging.errorLog("Config.yml 'homeType: " + homeType + "' unknown!");
}
if (home != null) {
player.teleport(home);
}
if (homeType == null) {
return;
}
if (homeType.equalsIgnoreCase("bed")) {
PaperLib.getBedSpawnLocationAsync(player, true).thenAcceptAsync(it -> {
if (it != null) {
PaperLib.teleportAsync(player, it);
}
});
} else if (homeType.startsWith("cmd: ")) {
player.performCommand(homeType.substring(5));
} else if (homeType.startsWith("cmd:")) {
player.performCommand(homeType.substring(4));
} else {
Logging.errorLog("Config.yml 'homeType: " + homeType + "' unknown!");
}
}

Expand Down
11 changes: 6 additions & 5 deletions src/main/java/com/dre/brewery/BSealer.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.dre.brewery.configuration.files.Lang;
import com.dre.brewery.utility.MinecraftVersion;
import com.github.Anon8281.universalScheduler.scheduling.tasks.MyScheduledTask;
import io.papermc.lib.PaperLib;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
Expand Down Expand Up @@ -61,16 +62,16 @@ public class BSealer implements InventoryHolder {
public BSealer(Player player) {
this.player = player;
if (inventoryHolderWorking) {
Inventory inv = BreweryPlugin.getInstance().getServer().createInventory(this, InventoryType.DISPENSER, lang.getEntry("Etc_SealingTable"));
Inventory inv = Bukkit.createInventory(this, InventoryType.DISPENSER, lang.getEntry("Etc_SealingTable"));
// Inventory Holder (for DISPENSER, ...) is only passed in Paper, not in Spigot. Doing inventory.getHolder() will return null in spigot :/
if (inv.getHolder() == this) {
if (PaperLib.getHolder(inv, true).getHolder() == this) {
inventory = inv;
return;
} else {
inventoryHolderWorking = false;
}
}
inventory = BreweryPlugin.getInstance().getServer().createInventory(this, 9, lang.getEntry("Etc_SealingTable"));
inventory = Bukkit.createInventory(this, 9, lang.getEntry("Etc_SealingTable"));
}

@Override
Expand Down Expand Up @@ -131,7 +132,7 @@ private void itemChecking() {

public static boolean isBSealer(Block block) {
if (BreweryPlugin.getMCVersion().isOrLater(MinecraftVersion.V1_14) && block.getType() == config.getSealingTableBlock()) {
Container container = (Container) block.getState();
Container container = (Container) PaperLib.getBlockState(block, true).getState();
if (container.getCustomName() != null) {
if (container.getCustomName().equals("§e" + lang.getEntry("Etc_SealingTable"))) {
return true;
Expand All @@ -149,7 +150,7 @@ public static void blockPlace(ItemStack item, Block block) {
assert itemMeta != null;
if ((itemMeta.hasDisplayName() && itemMeta.getDisplayName().equals("§e" + lang.getEntry("Etc_SealingTable"))) ||
itemMeta.getPersistentDataContainer().has(BSealer.TAG_KEY, PersistentDataType.BYTE)) {
Container container = (Container) block.getState();
Container container = (Container) PaperLib.getBlockState(block, true).getState();
// Rotate the Block 180° so it looks different
if (container.getBlockData() instanceof Directional dir) {
dir.setFacing(dir.getFacing().getOppositeFace());
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/dre/brewery/Barrel.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.dre.brewery.utility.BUtil;
import com.dre.brewery.utility.BoundingBox;
import com.dre.brewery.utility.Logging;
import com.dre.brewery.utility.MinecraftVersion;
import com.github.Anon8281.universalScheduler.UniversalRunnable;
import lombok.Getter;
import lombok.Setter;
Expand Down Expand Up @@ -469,7 +470,7 @@ public boolean isLarge() {
* is this a Small barrel?
*/
public boolean isSmall() {
if (!BreweryPlugin.isFolia()) {
if (!MinecraftVersion.isFolia()) {
return BarrelAsset.isBarrelAsset(BarrelAsset.SIGN, spigot.getType());
}

Expand Down
11 changes: 9 additions & 2 deletions src/main/java/com/dre/brewery/BarrelWoodType.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public enum BarrelWoodType {
MANGROVE("Mangrove", 9),
CHERRY("Cherry", 10),
BAMBOO("Bamboo", 11),
CUT_COPPER("Cut Copper", 12, Material.CUT_COPPER, Material.CUT_COPPER_STAIRS, null, null),
CUT_COPPER("Cut Copper", 12, Material.CUT_COPPER, Material.CUT_COPPER_STAIRS),
PALE_OAK("Pale Oak", 13),
// If you're adding more wood types, add them above 'NONE'
NONE("None", -1, true);
Expand All @@ -65,7 +65,6 @@ public enum BarrelWoodType {
}
}


BarrelWoodType(String formattedName, int index, Material planks, Material stairs, Material sign, Material fence) {
this.formattedName = formattedName;
this.index = index;
Expand All @@ -76,6 +75,14 @@ public enum BarrelWoodType {
BarrelAsset.addBarrelAsset(BarrelAsset.FENCE, fence);
}

BarrelWoodType(String formattedName, int index, Material planks, Material stairs) {
this.formattedName = formattedName;
this.index = index;

BarrelAsset.addBarrelAsset(BarrelAsset.PLANKS, planks);
BarrelAsset.addBarrelAsset(BarrelAsset.STAIRS, stairs);
}

@Nullable
private Material[] getStandardBarrelAssetMaterial(BarrelAsset assetType) {
try {
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/dre/brewery/Brew.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ private Brew() {
*/
@Nullable
public static Brew get(ItemMeta meta) {
if (!BreweryPlugin.isUseNBT() && !meta.hasLore()) return null;
if (!MinecraftVersion.isUseNBT() && !meta.hasLore()) return null;

Brew brew = load(meta);

Expand All @@ -170,7 +170,7 @@ public static Brew get(ItemStack item) {

ItemMeta meta = item.getItemMeta();
assert meta != null;
if (!BreweryPlugin.isUseNBT() && !meta.hasLore()) return null;
if (!MinecraftVersion.isUseNBT() && !meta.hasLore()) return null;

Brew brew = load(meta);

Expand All @@ -183,7 +183,7 @@ public static Brew get(ItemStack item) {
item.setItemMeta(meta);
} else if (brew != null && brew.needsSave) {
// Brew needs saving from a previous format
if (BreweryPlugin.isUseNBT()) {
if (MinecraftVersion.isUseNBT()) {
new BrewLore(brew, (PotionMeta) meta).removeLoreData();
Logging.debugLog("removed Data from Lore");
}
Expand Down Expand Up @@ -853,9 +853,9 @@ public static boolean isBrew(ItemStack item) {

ItemMeta meta = item.getItemMeta();
assert meta != null;
if (!BreweryPlugin.isUseNBT() && !meta.hasLore()) return false;
if (!MinecraftVersion.isUseNBT() && !meta.hasLore()) return false;

if (BreweryPlugin.isUseNBT()) {
if (MinecraftVersion.isUseNBT()) {
// Check for Data on PersistentDataContainer
if (NBTLoadStream.hasDataInMeta(meta)) {
return true;
Expand All @@ -871,7 +871,7 @@ public static boolean isBrew(ItemStack item) {

private static Brew load(ItemMeta meta) {
InputStream itemLoadStream = null;
if (BreweryPlugin.isUseNBT()) {
if (MinecraftVersion.isUseNBT()) {
// Try loading the Item Data from PersistentDataContainer
NBTLoadStream nbtStream = new NBTLoadStream(meta);
if (nbtStream.hasData()) {
Expand Down Expand Up @@ -921,7 +921,7 @@ private static Brew load(ItemMeta meta) {
// We have either enabled encode and the data was not encoded or the other way round
Logging.debugLog("Converting Brew to new encode setting");
brew.setNeedsSave(true);
} else if (BreweryPlugin.isUseNBT() && itemLoadStream instanceof Base91DecoderStream) {
} else if (MinecraftVersion.isUseNBT() && itemLoadStream instanceof Base91DecoderStream) {
// We are on a version that supports nbt but the data is still in the lore of the item
// Just save it again so that it gets saved to nbt
Logging.debugLog("Converting Brew to NBT");
Expand Down Expand Up @@ -968,7 +968,7 @@ private void loadFromStream(DataInputStream in, byte dataVersion) throws IOExcep
*/
public void save(ItemMeta meta) {
OutputStream itemSaveStream;
if (BreweryPlugin.isUseNBT()) {
if (MinecraftVersion.isUseNBT()) {
itemSaveStream = new NBTSaveStream(meta);
} else {
itemSaveStream = new Base91EncoderStream(new LoreSaveStream(meta, 0));
Expand Down
22 changes: 6 additions & 16 deletions src/main/java/com/dre/brewery/BreweryPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import com.dre.brewery.utility.UpdateChecker;
import com.github.Anon8281.universalScheduler.UniversalScheduler;
import com.github.Anon8281.universalScheduler.scheduling.schedulers.TaskScheduler;
import io.papermc.lib.PaperLib;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.Bukkit;
Expand Down Expand Up @@ -83,8 +84,6 @@ public class BreweryPlugin extends JavaPlugin {
private @Getter static BreweryPlugin instance;
private @Getter static MinecraftVersion MCVersion;
private @Getter @Setter static DataManager dataManager;
private @Getter static boolean isFolia = false;
private @Getter static boolean useNBT = false;


private final Map<String, Function<ItemLoader, Ingredient>> ingredientLoaders = new HashMap<>(); // Registrations
Expand All @@ -104,21 +103,8 @@ public void onLoad() {

@Override
public void onEnable() {
try {
Class.forName("io.papermc.paper.threadedregions.RegionizedServer");
isFolia = true;
} catch (ClassNotFoundException ignored) {}


// MC 1.13 uses a different NBT API than the newer versions.
// We decide here which to use, the new or the old or none at all
if (NBTUtil.initNbt()) {
useNBT = true;
}

if (getMCVersion().isOrLater(MinecraftVersion.V1_14)) {
// Campfires are weird
// Initialize once now so it doesn't lag later when we check for campfires under Cauldrons
// Campfires are weird, Initialize once now so it doesn't lag later when we check for campfires under Cauldrons
getServer().createBlockData(Material.CAMPFIRE);
}

Expand Down Expand Up @@ -222,6 +208,10 @@ public void onEnable() {
}

Logging.log("Using scheduler&7: &a" + scheduler.getClass().getSimpleName());
Logging.log("Environment&7: &a" + Logging.getEnvironmentAsString());
if (!PaperLib.isPaper()) {
Logging.log("&aBreweryX performs best on Paper-based servers. Please consider switching to Paper for the best experience. &7https://papermc.io");
}
Logging.log("BreweryX enabled!");
}

Expand Down
9 changes: 4 additions & 5 deletions src/main/java/com/dre/brewery/MCBarrel.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.dre.brewery.configuration.files.Config;
import com.dre.brewery.configuration.files.Lang;
import com.dre.brewery.utility.Logging;
import io.papermc.lib.PaperLib;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Barrel;
Expand Down Expand Up @@ -61,9 +62,8 @@ public MCBarrel(Inventory inv) {
// Now Opening this Barrel for a player
public void open() {
// if nobody had the inventory opened
if (inv.getViewers().size() == 1 && inv.getHolder() instanceof org.bukkit.block.Barrel) {
Barrel barrel = (Barrel) inv.getHolder();
PersistentDataContainer data = barrel.getPersistentDataContainer();
if (inv.getViewers().size() == 1 && PaperLib.getHolder(inv, true).getHolder() instanceof Barrel barrel) {
PersistentDataContainer data = barrel.getPersistentDataContainer();
NamespacedKey key = new NamespacedKey(BreweryPlugin.getInstance(), TAG);
if (!data.has(key, PersistentDataType.LONG)) {
key = new NamespacedKey("brewery", TAG.toLowerCase()); // Legacy key
Expand Down Expand Up @@ -115,8 +115,7 @@ public void close() {
if (item != null) {
if (Brew.isBrew(item)) {
// We found a brew, so set time on this Barrel
if (inv.getHolder() instanceof org.bukkit.block.Barrel) {
Barrel barrel = (Barrel) inv.getHolder();
if (PaperLib.getHolder(inv, true).getHolder() instanceof org.bukkit.block.Barrel barrel) {
PersistentDataContainer data = barrel.getPersistentDataContainer();
data.set(new NamespacedKey(BreweryPlugin.getInstance(), TAG), PersistentDataType.LONG, mcBarrelTime);
barrel.update();
Expand Down
Loading

0 comments on commit 45e3210

Please sign in to comment.