Skip to content

Commit

Permalink
some util cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Aug 2, 2024
1 parent 1dd715c commit f164217
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 78 deletions.
10 changes: 10 additions & 0 deletions shared/src/main/java/me/xginko/aef/utils/BlockUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@

public class BlockUtil {

private static final boolean WATERLOGGED_AVAILABLE;

static {
WATERLOGGED_AVAILABLE = Crafty.hasClass("org.bukkit.block.data.Waterlogged");
}

public static boolean isWaterloggedAvailable() {
return WATERLOGGED_AVAILABLE;
}

private static final Map<Material, Boolean> IS_WATERLOGGABLE_CACHE = new EnumMap<>(Material.class);
public static boolean isWaterlogged(BlockState blockState) {
return blockState != null
Expand Down
27 changes: 9 additions & 18 deletions shared/src/main/java/me/xginko/aef/utils/ChunkUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,31 @@

public class ChunkUtil {

private static boolean setForceLoadedAvailable, getInhabitedTimeAvailable;
private static final boolean SET_FORCE_LOADED_AVAILABLE, GET_INHABITED_TIME_AVAILABLE;

static {
try {
Chunk.class.getMethod("setForceLoaded", boolean.class);
setForceLoadedAvailable = true;
} catch (NoSuchMethodException e) {
setForceLoadedAvailable = false;
}

try {
Chunk.class.getMethod("getInhabitedTime");
getInhabitedTimeAvailable = true;
} catch (NoSuchMethodException e) {
getInhabitedTimeAvailable = false;
}
SET_FORCE_LOADED_AVAILABLE
= Crafty.hasMethod(Chunk.class, "setForceLoaded", boolean.class);
GET_INHABITED_TIME_AVAILABLE
= Crafty.hasMethod(Chunk.class, "getInhabitedTime");
}

public static boolean canSetChunksForceLoaded() {
return setForceLoadedAvailable;
return SET_FORCE_LOADED_AVAILABLE;
}

public static void setForceLoaded(Chunk chunk, boolean forced) {
if (setForceLoadedAvailable) {
if (SET_FORCE_LOADED_AVAILABLE) {
chunk.setForceLoaded(forced);
}
}

public static boolean canGetInhabitedTime() {
return getInhabitedTimeAvailable;
return GET_INHABITED_TIME_AVAILABLE;
}

public static long getInhabitedTime(Chunk chunk) {
return getInhabitedTimeAvailable ? chunk.getInhabitedTime() : 0L;
return GET_INHABITED_TIME_AVAILABLE ? chunk.getInhabitedTime() : 0L;
}

public static void createBedrockLayer(Chunk chunk, int y) {
Expand Down
2 changes: 0 additions & 2 deletions shared/src/main/java/me/xginko/aef/utils/Crafty.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@
* <p>This is not an official API and can break at any time. You've been warned.</p>
*/
public final class Crafty {
private Crafty() {
}

private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
private static final String PREFIX_NMS = "net.minecraft.server";
Expand Down
16 changes: 6 additions & 10 deletions shared/src/main/java/me/xginko/aef/utils/EntityUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,21 @@

public class EntityUtil {

static boolean canDisableMapPositionCursor;
private static final boolean MAP_SET_TRACKING_POS_AVAILABLE;

static {
try {
Class.forName("org.bukkit.map.MapView");
MapView.class.getMethod("setTrackingPosition", boolean.class);
canDisableMapPositionCursor = true;
} catch (ClassNotFoundException | NoSuchMethodException e) {
canDisableMapPositionCursor = false;
}
MAP_SET_TRACKING_POS_AVAILABLE
= Crafty.hasClass("org.bukkit.map.MapView")
&& Crafty.hasMethod(MapView.class, "setTrackingPosition", boolean.class);
}

public static boolean canDisableMapPositionCursor() {
return canDisableMapPositionCursor;
return MAP_SET_TRACKING_POS_AVAILABLE;
}

/**
* Disables that a position cursor will be shown when the map is near its center.
* Only available in the API starting at 1.16.
* Check {@link EntityUtil#canDisableMapPositionCursor()} before calling this method.
*
* @param itemFrame the {@link ItemFrame} entity to disable the tracking status of
*/
Expand Down
33 changes: 15 additions & 18 deletions shared/src/main/java/me/xginko/aef/utils/ItemUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import de.tr7zw.changeme.nbtapi.NBTItem;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Material;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BlockStateMeta;
Expand All @@ -17,26 +16,24 @@

public class ItemUtil {

private static boolean miniMessage_supported, bundles_suppported;
private static final boolean BUNDLES_SUPPPORTED, MINI_MESSAGE_SUPPORTED;

static {
try {
Class.forName("net.kyori.adventure.text.minimessage.MiniMessage");
MiniMessage.class.getMethod("miniMessage");
miniMessage_supported = true;
} catch (Throwable t) {
miniMessage_supported = false;
}
MINI_MESSAGE_SUPPORTED
= Crafty.hasClass("net.kyori.adventure.text.minimessage.MiniMessage")
&& Crafty.hasMethod(MiniMessage.class, "miniMessage");

try {
Class.forName("org.bukkit.inventory.meta.BundleMeta");
Material.valueOf("BUNDLE");
bundles_suppported = true;
} catch (Throwable t) {
bundles_suppported = false;
}
BUNDLES_SUPPPORTED
= XMaterial.BUNDLE.isSupported()
&& Crafty.hasClass("org.bukkit.inventory.meta.BundleMeta");
}

/**
* Gets the stored items from an ItemStack.
*
* @param itemStack ItemStack to check.
* @return Iterable containing the ItemStacks stored inside the item or {@code null} if there are none.
*/
@SuppressWarnings("UnstableApiUsage")
public static @Nullable Iterable<ItemStack> getStoredItems(@NotNull ItemStack itemStack) {
if (!itemStack.hasItemMeta()) {
Expand All @@ -50,7 +47,7 @@ public class ItemUtil {
}
}

if (bundles_suppported) {
if (BUNDLES_SUPPPORTED) {
if (itemStack.getType() == XMaterial.BUNDLE.parseMaterial()) {
return ((BundleMeta) itemStack.getItemMeta()).getItems();
}
Expand Down Expand Up @@ -83,7 +80,7 @@ public static int getApproximateByteSize(@Nullable Iterable<ItemStack> inventory
}

public static int getApproximateByteSize(@NotNull BookMeta bookMeta, boolean utf16) {
return miniMessage_supported ? getApproximateByteSizeMM(bookMeta, utf16) : getApproximateByteSizeLegacy(bookMeta, utf16);
return MINI_MESSAGE_SUPPORTED ? getApproximateByteSizeMM(bookMeta, utf16) : getApproximateByteSizeLegacy(bookMeta, utf16);
}

@SuppressWarnings("DataFlowIssue") // Legitimate because we make sure no values are null by testing .hasX()
Expand Down
2 changes: 2 additions & 0 deletions shared/src/main/java/me/xginko/aef/utils/MaterialUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public static boolean isPlayerHead(ItemStack itemStack) {
return false;
if (PlatformUtil.getMinecraftVersion() > 12)
return true;
// Legacy sums entity heads and player heads into one type: skull.
// therefore we need to make sure this is not an entity's head
return ((SkullMeta) itemStack.getItemMeta()).hasOwner();
}

Expand Down
20 changes: 4 additions & 16 deletions shared/src/main/java/me/xginko/aef/utils/PlatformUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@ public class PlatformUtil {

public static void load() {
isServerPurpur
= hasClass("org.purpurmc.purpur.PurpurConfig");
= Crafty.hasClass("org.purpurmc.purpur.PurpurConfig");
isServerPaper
= hasClass("com.destroystokyo.paper.PaperConfig")
|| hasClass("io.papermc.paper.configuration.Configuration");
= Crafty.hasClass("com.destroystokyo.paper.PaperConfig")
|| Crafty.hasClass("io.papermc.paper.configuration.Configuration");
isServerFolia
= hasClass("io.papermc.paper.threadedregions.RegionizedServer")
&& hasClass("io.papermc.paper.threadedregions.ThreadedRegionizer")
&& hasClass("io.papermc.paper.threadedregions.TickRegionScheduler")
&& hasClass("io.papermc.paper.threadedregions.TickRegions");
= Crafty.hasClass("io.papermc.paper.threadedregions.RegionizedServer");

// From PaperLib
Matcher matcher = Pattern.compile("(?i)\\(MC: (\\d)\\.(\\d+)\\.?(\\d+?)?(?: (Pre-Release|Release Candidate) )?(\\d)?\\)")
Expand Down Expand Up @@ -53,15 +50,6 @@ && hasClass("io.papermc.paper.threadedregions.TickRegionScheduler")
}
}

private static boolean hasClass(String className) {
try {
Class.forName(className);
return true;
} catch (ClassNotFoundException e) {
return false;
}
}

public static boolean isPaper() {
return isServerPaper;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.papermc.paper.threadedregions.TickRegions;
import io.papermc.paper.threadedregions.scheduler.EntityScheduler;
import io.papermc.paper.threadedregions.scheduler.RegionScheduler;
import me.xginko.aef.utils.Crafty;

import java.time.Duration;

Expand All @@ -21,6 +22,13 @@ public FoliaTickReporter(Duration cacheTime) {
this.mspt_cache = Caffeine.newBuilder().expireAfterWrite(cacheTime).build();
}

public static boolean isSupported() {
return Crafty.hasClass("io.papermc.paper.threadedregions.RegionizedServer")
&& Crafty.hasClass("io.papermc.paper.threadedregions.ThreadedRegionizer")
&& Crafty.hasClass("io.papermc.paper.threadedregions.TickRegionScheduler")
&& Crafty.hasClass("io.papermc.paper.threadedregions.TickRegions");
}

@Override
public void disable() {
tps_cache.invalidateAll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import me.xginko.aef.utils.Crafty;
import me.xginko.aef.utils.SpigotReflection;
import org.bukkit.Server;
import org.bukkit.plugin.java.JavaPlugin;
Expand All @@ -20,12 +21,7 @@ public LegacyPaperTickReporter(JavaPlugin plugin, Duration cacheDuration) {
}

public static boolean isSupported() {
try {
Server.class.getMethod("getTPS");
return true;
} catch (NoSuchMethodException e) {
return false;
}
return Crafty.hasMethod(Server.class, "getTPS");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import me.xginko.aef.utils.Crafty;
import org.bukkit.Server;
import org.bukkit.plugin.java.JavaPlugin;

Expand All @@ -19,12 +20,7 @@ public ModernPaperTickReporter(JavaPlugin plugin, Duration cacheTime) {
}

public static boolean isSupported() {
try {
Server.class.getMethod("getAverageTickTime");
return true;
} catch (NoSuchMethodException e) {
return false;
}
return Crafty.hasMethod(Server.class, "getAverageTickTime");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package me.xginko.aef.utils.tickdata;

import me.xginko.aef.utils.PlatformUtil;
import me.xginko.aef.utils.models.Disableable;
import org.bukkit.plugin.java.JavaPlugin;

Expand All @@ -14,7 +13,7 @@ public interface TickReporter extends Disableable {
double getMSPT();

static TickReporter create(JavaPlugin plugin, Duration cacheDuration) {
if (PlatformUtil.isFolia()) {
if (FoliaTickReporter.isSupported()) {
return new FoliaTickReporter(cacheDuration);
}

Expand Down

0 comments on commit f164217

Please sign in to comment.