Skip to content

Commit

Permalink
fix: add VersionedEvent (fix #927)
Browse files Browse the repository at this point in the history
  • Loading branch information
StarWishsama committed Jul 31, 2024
1 parent 14602eb commit cb6bb12
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<maven.settings.file>${project.basedir}/.mvn/settings.xml</maven.settings.file>

<!-- Spigot properties -->
<spigot.version>1.20.6</spigot.version>
<spigot.version>1.21</spigot.version>
<spigot.javadocs>https://hub.spigotmc.org/javadocs/spigot/</spigot.javadocs>
</properties>

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/city/norain/slimefun4/SlimefunExtended.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package city.norain.slimefun4;

import city.norain.slimefun4.compatibillty.VersionedEvent;
import city.norain.slimefun4.listener.SlimefunMigrateListener;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import java.util.logging.Level;
Expand Down Expand Up @@ -47,14 +48,16 @@ public static boolean checkEnvironment(@Nonnull Slimefun sf) {
}
}

public static void register(@Nonnull Slimefun sf) {
public static void init(@Nonnull Slimefun sf) {
EnvironmentChecker.scheduleSlimeGlueCheck(sf);

checkDebug();

VaultIntegration.register(sf);

migrateListener.register(sf);

VersionedEvent.init();
}

public static void shutdown() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package city.norain.slimefun4.compatibillty;

import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import java.lang.reflect.Constructor;
import java.util.List;
import java.util.logging.Level;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import org.bukkit.ExplosionResult;
import org.bukkit.block.Block;
import org.bukkit.event.block.BlockExplodeEvent;

@UtilityClass
public class VersionedEvent {
private Constructor<BlockExplodeEvent> blockExplodeConstructor;

public void init() {
if (Slimefun.getMinecraftVersion().isBefore(MinecraftVersion.MINECRAFT_1_21)) {
try {
blockExplodeConstructor = BlockExplodeEvent.class.getConstructor(Block.class, List.class, float.class);
} catch (NoSuchMethodException e) {
Slimefun.logger().log(Level.WARNING, "无法初始化事件版本兼容模块, 部分功能可能无法正常使用", e);
}
}
}

@SneakyThrows
public BlockExplodeEvent newBlockExplodeEvent(Block block, List<Block> affectedBlock, float yield) {
if (Slimefun.getMinecraftVersion().isAtLeast(MinecraftVersion.MINECRAFT_1_21)) {
return new BlockExplodeEvent(block, block.getState(), affectedBlock, yield, ExplosionResult.DESTROY);
} else {
if (blockExplodeConstructor == null) {
throw new IllegalStateException("Unable to create BlockExplodeEvent: missing constructor");
}
return blockExplodeConstructor.newInstance(block, affectedBlock, yield);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,10 @@ private void onPluginStart() {

// All Slimefun Listeners
logger.log(Level.INFO, "正在注册监听器...");

// Inject downstream extra staff
SlimefunExtended.init(this);

registerListeners();

// Initiating various Stuff and all items with a slight delay (0ms after the Server finished
Expand Down Expand Up @@ -706,9 +710,6 @@ private void registerListeners() {
new SmithingTableListener(this);
new JoinListener(this);

// Inject downstream extra staff
SlimefunExtended.register(this);

// Item-specific Listeners
new CoolerListener(this, (Cooler) SlimefunItems.COOLER.getItem());
new SeismicAxeListener(this, (SeismicAxe) SlimefunItems.SEISMIC_AXE.getItem());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.thebusybiscuit.slimefun4.implementation.items.tools;

import city.norain.slimefun4.compatibillty.VersionedEvent;
import com.xzavier0722.mc.plugin.slimefun4.storage.util.StorageCacheUtils;
import dev.lone.itemsadder.api.CustomBlock;
import io.github.bakedlibs.dough.protection.Interaction;
Expand Down Expand Up @@ -79,7 +80,7 @@ private void breakBlocks(
List<Block> blocksToDestroy = new ArrayList<>();

if (callExplosionEvent.getValue()) {
BlockExplodeEvent blockExplodeEvent = new BlockExplodeEvent(b, blocks, 0);
BlockExplodeEvent blockExplodeEvent = VersionedEvent.newBlockExplodeEvent(b, blocks, 0);
Bukkit.getServer().getPluginManager().callEvent(blockExplodeEvent);

if (!blockExplodeEvent.isCancelled()) {
Expand Down

0 comments on commit cb6bb12

Please sign in to comment.