Skip to content

Commit

Permalink
chore(env): check more type of hybrid server
Browse files Browse the repository at this point in the history
  • Loading branch information
StarWishsama committed Jan 13, 2025
1 parent f7ab813 commit a47fb8e
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 62 deletions.
31 changes: 23 additions & 8 deletions src/main/java/city/norain/slimefun4/EnvironmentChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,35 @@ static boolean checkIncompatiblePlugins(@Nonnull Logger logger) {
}

static boolean checkHybridServer() {

try {
Class.forName("cpw.mods.modlauncher.Launcher", false, ClassLoader.getSystemClassLoader());
return true;
} catch (ClassNotFoundException ignored) {}

try {
Class.forName("cpw.mods.modlauncher.Launcher");
Class.forName("net.minecraftforge.server.console.TerminalHandler");
Class.forName("net.minecraftforge.server.console.TerminalHandler", false, ClassLoader.getSystemClassLoader());

return true;
} catch (ClassNotFoundException ignored) {}

try {
Class.forName("org.cardboardpowered.mixin.CardboardMixinPlugin", false, ClassLoader.getSystemClassLoader());
return true;
} catch (ClassNotFoundException ignored) {
if (Bukkit.getPluginCommand("mohist") != null) {
return true;
}
} catch (ClassNotFoundException ignored) {}

var serverVer = Bukkit.getVersion().toLowerCase();
try {
Class.forName("net.fabricmc.loader.impl.FabricLoaderImpl", false, ClassLoader.getSystemClassLoader());
return true;
} catch (ClassNotFoundException ignored) {}

return serverVer.contains("arclight") || serverVer.contains("mohist");
if (Bukkit.getPluginCommand("mohist") != null) {
return true;
}

var serverVer = Bukkit.getVersion().toLowerCase();

return serverVer.contains("arclight") || serverVer.contains("mohist");
}

static void scheduleSlimeGlueCheck(@Nonnull Slimefun sf) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import io.github.thebusybiscuit.slimefun4.utils.compatibility.VersionedEnchantment;
import io.github.thebusybiscuit.slimefun4.utils.tags.SlimefunTag;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;

import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
Expand Down Expand Up @@ -58,7 +60,7 @@
*/
public class BlockListener implements Listener {

private static final BlockFace[] CARDINAL_BLOCKFACES = new BlockFace[] {
private static final BlockFace[] CARDINAL_BLOCKFACES = new BlockFace[]{
BlockFace.WEST, BlockFace.EAST, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.DOWN, BlockFace.UP
};

Expand Down Expand Up @@ -123,27 +125,9 @@ public void onBlockPlace(BlockPlaceEvent e) {
e.setCancelled(true);
} else {
var block = e.getBlock();
if (block.getBlockData() instanceof Rotatable rotatable
&& !(rotatable.getRotation() == BlockFace.UP || rotatable.getRotation() == BlockFace.DOWN)) {
BlockFace rotation = null;

if (sfItem instanceof NotCardinallyRotatable && sfItem instanceof NotDiagonallyRotatable) {
rotation = BlockFace.NORTH;
} else if (sfItem instanceof NotRotatable notRotatable) {
rotation = notRotatable.getRotation();
} else if (sfItem instanceof NotCardinallyRotatable notRotatable) {
rotation = notRotatable.getRotation(Location.normalizeYaw(
e.getPlayer().getLocation().getYaw()));
} else if (sfItem instanceof NotDiagonallyRotatable notRotatable) {
rotation = notRotatable.getRotation(Location.normalizeYaw(
e.getPlayer().getLocation().getYaw()));
}

if (rotation != null) {
rotatable.setRotation(rotation);
block.setBlockData(rotatable);
}
}
optimizePlacement(sfItem, block, e.getPlayer().getLocation());

var placeEvent = new SlimefunBlockPlaceEvent(e.getPlayer(), item, block, sfItem);
Bukkit.getPluginManager().callEvent(placeEvent);

Expand All @@ -156,16 +140,16 @@ public void onBlockPlace(BlockPlaceEvent e) {

if (sfItem instanceof UniversalBlock) {
var data = Slimefun.getDatabaseManager()
.getBlockDataController()
.createUniversalBlock(block.getLocation(), sfItem.getId());
.getBlockDataController()
.createUniversalBlock(block.getLocation(), sfItem.getId());

if (Slimefun.getBlockDataService().isTileEntity(block.getType())) {
Slimefun.getBlockDataService().updateUniversalDataUUID(block, data.getKey());
}
} else {
Slimefun.getDatabaseManager()
.getBlockDataController()
.createBlock(block.getLocation(), sfItem.getId());
.getBlockDataController()
.createBlock(block.getLocation(), sfItem.getId());
}

sfItem.callItemHandler(BlockPlaceHandler.class, handler -> handler.onPlayerPlace(e));
Expand All @@ -189,15 +173,15 @@ public void onBlockBreak(BlockBreakEvent e) {
var heldItem = e.getPlayer().getInventory().getItemInMainHand();
var block = e.getBlock();
var blockData = StorageCacheUtils.hasBlock(block.getLocation())
? StorageCacheUtils.getBlock(block.getLocation())
: StorageCacheUtils.getUniversalBlock(block);
? StorageCacheUtils.getBlock(block.getLocation())
: StorageCacheUtils.getUniversalBlock(block);
var sfItem = blockData == null ? null : SlimefunItem.getById(blockData.getSfId());

// If there is a Slimefun Block here, call our BreakEvent and, if cancelled, cancel this event
// and return
if (blockData != null) {
SlimefunBlockBreakEvent breakEvent =
new SlimefunBlockBreakEvent(e.getPlayer(), heldItem, e.getBlock(), sfItem);
new SlimefunBlockBreakEvent(e.getPlayer(), heldItem, e.getBlock(), sfItem);
Bukkit.getPluginManager().callEvent(breakEvent);

if (breakEvent.isCancelled()) {
Expand Down Expand Up @@ -229,18 +213,18 @@ public void onBlockBreak(BlockBreakEvent e) {
e.setDropItems(false);
var type = block.getType();
StorageCacheUtils.executeAfterLoad(
blockData,
() -> {
callBlockHandler(e, heldItem, drops);
if (e.isCancelled()) {
block.setType(type);
blockData.setPendingRemove(false);
return;
}
e.setDropItems(true);
dropItems(e, heldItem, block, sfItem, drops);
},
true);
blockData,
() -> {
callBlockHandler(e, heldItem, drops);
if (e.isCancelled()) {
block.setType(type);
blockData.setPendingRemove(false);
return;
}
e.setDropItems(true);
dropItems(e, heldItem, block, sfItem, drops);
},
true);
return;
}

Expand Down Expand Up @@ -287,7 +271,7 @@ private void callBlockHandler(BlockBreakEvent e, ItemStack item, List<ItemStack>

@ParametersAreNonnullByDefault
private void dropItems(
BlockBreakEvent e, ItemStack item, Block block, @Nullable SlimefunItem sfBlock, List<ItemStack> drops) {
BlockBreakEvent e, ItemStack item, Block block, @Nullable SlimefunItem sfBlock, List<ItemStack> drops) {
if (!drops.isEmpty()) {
// TODO: properly support loading inventories within unit tests
if (!Slimefun.instance().isUnitTest()) {
Expand All @@ -305,7 +289,7 @@ private void dropItems(
// Prevent null or air from being dropped
if (drop != null && drop.getType() != Material.AIR) {
if (e.getPlayer().getGameMode() != GameMode.CREATIVE
|| Slimefun.getCfg().getBoolean("options.drop-block-creative")) {
|| Slimefun.getCfg().getBoolean("options.drop-block-creative")) {
block.getWorld().dropItemNaturally(block.getLocation(), drop);
}
}
Expand Down Expand Up @@ -343,7 +327,7 @@ private void checkForSensitiveBlockAbove(Player player, Block block, ItemStack i
var controller = Slimefun.getDatabaseManager().getBlockDataController();
if (blockData.isDataLoaded()) {
sfItem.callItemHandler(
BlockBreakHandler.class, handler -> handler.onPlayerBreak(dummyEvent, item, drops));
BlockBreakHandler.class, handler -> handler.onPlayerBreak(dummyEvent, item, drops));
controller.removeBlock(loc);
dropItems(dummyEvent, item, block, sfItem, drops);
} else {
Expand All @@ -357,7 +341,7 @@ public boolean runOnMainThread() {
@Override
public void onResult(SlimefunBlockData result) {
sfItem.callItemHandler(
BlockBreakHandler.class, handler -> handler.onPlayerBreak(dummyEvent, item, drops));
BlockBreakHandler.class, handler -> handler.onPlayerBreak(dummyEvent, item, drops));
controller.removeBlock(loc);
dropItems(dummyEvent, item, block, sfItem, drops);
}
Expand All @@ -372,10 +356,8 @@ public void onResult(SlimefunBlockData result) {
* This method checks recursively for any sensitive blocks
* that are no longer supported due to this block breaking
*
* @param block
* The {@link Block} in question
* @param count
* The amount of times this has been recursively called
* @param block The {@link Block} in question
* @param count The amount of times this has been recursively called
*/
@ParametersAreNonnullByDefault
private void checkForSensitiveBlocks(Block block, Integer count, boolean isDropItems) {
Expand Down Expand Up @@ -407,12 +389,9 @@ private void checkForSensitiveBlocks(Block block, Integer count, boolean isDropI
* This method checks if the {@link BlockData} would be
* supported at the given {@link Block}.
*
* @param blockData
* The {@link BlockData} to check
* @param block
* The {@link Block} the {@link BlockData} would be at
* @return
* Whether the {@link BlockData} would be supported at the given {@link Block}
* @param blockData The {@link BlockData} to check
* @param block The {@link Block} the {@link BlockData} would be at
* @return Whether the {@link BlockData} would be supported at the given {@link Block}
*/
@ParametersAreNonnullByDefault
private boolean isSupported(BlockData blockData, Block block) {
Expand Down Expand Up @@ -447,4 +426,29 @@ private int getBonusDropsWithFortune(@Nullable ItemStack item, @Nonnull Block b)

return amount;
}

// 美化可旋转类 (如头颅) 物品放置
private void optimizePlacement(SlimefunItem sfItem, Block block, Location l) {
if (block.getBlockData() instanceof Rotatable rotatable
&& !(rotatable.getRotation() == BlockFace.UP || rotatable.getRotation() == BlockFace.DOWN)) {
BlockFace rotation = null;

if (sfItem instanceof NotCardinallyRotatable && sfItem instanceof NotDiagonallyRotatable) {
rotation = BlockFace.NORTH;
} else if (sfItem instanceof NotRotatable notRotatable) {
rotation = notRotatable.getRotation();
} else if (sfItem instanceof NotCardinallyRotatable notRotatable) {
rotation = notRotatable.getRotation(Location.normalizeYaw(
l.getYaw()));
} else if (sfItem instanceof NotDiagonallyRotatable notRotatable) {
rotation = notRotatable.getRotation(Location.normalizeYaw(
l.getYaw()));
}

if (rotation != null) {
rotatable.setRotation(rotation);
block.setBlockData(rotatable);
}
}
}
}

0 comments on commit a47fb8e

Please sign in to comment.