From 118ec095123ccedf5eeb81b64f91d13b6b6c768f Mon Sep 17 00:00:00 2001 From: MaXFeeD Date: Mon, 25 Dec 2023 06:44:41 +0300 Subject: [PATCH] =?UTF-8?q?refactor:=20=D0=94=D0=BE=D0=BF=D0=BE=D0=BB?= =?UTF-8?q?=D0=BD=D0=B5=D0=BD=D1=8B=20=D0=BF=D1=80=D0=BE=D1=88=D0=BB=D1=8B?= =?UTF-8?q?=D0=B5=20=D1=81=D0=BE=D0=B1=D1=8B=D1=82=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Исправлена изначальная реализация события взрывов, теперь она вызывается для мобов и блоков. - Добавлено событие смерти еще игрока, а не только существ. - Еще раз отключена сборка на GHA. --- .github/workflows/gradle.yml | 7 --- .../java/com/reider745/InnerCoreServer.java | 20 +++++--- .../com/reider745/event/EventListener.java | 49 ++++++++++++++----- .../reider745/hooks/SnowfallEverywhere.java | 10 +++- 4 files changed, 61 insertions(+), 25 deletions(-) diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 94533846c..8d7f04482 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -28,10 +28,3 @@ jobs: uses: gradle/gradle-build-action@v2 with: arguments: assemble - - - name: Upload a Build Artifact - uses: actions/upload-artifact@v3.1.3 - with: - name: ZoteCoreLoader - path: build/libs/ZotCore-SNAPSHOT.jar - retention-days: 30 diff --git a/src/main/java/com/reider745/InnerCoreServer.java b/src/main/java/com/reider745/InnerCoreServer.java index f4764ccac..4a67906d8 100644 --- a/src/main/java/com/reider745/InnerCoreServer.java +++ b/src/main/java/com/reider745/InnerCoreServer.java @@ -2,12 +2,14 @@ import cn.nukkit.Server; import cn.nukkit.plugin.PluginDescription; +import cn.nukkit.plugin.PluginManager; import com.reider745.api.CallbackHelper; import com.reider745.block.CustomBlock; import com.reider745.commands.CommandsHelper; import com.reider745.event.EventListener; import com.reider745.event.InnerCorePlugin; +import com.reider745.hooks.SnowfallEverywhere; import com.reider745.item.CustomItem; import com.reider745.item.ItemMethod; @@ -290,8 +292,10 @@ public void preload(Server server) throws Exception { public void afterload() { server.getLogger().info("Registering Nukkit-MOT containment..."); - Logger.info("Register events ZotCoreLoader"); - InnerCoreServer.server.getPluginManager().registerEvents(new EventListener(), InnerCoreServer.plugin); + Logger.info("Registering ZoteCore events..."); + PluginManager pluginManager = server.getPluginManager(); + pluginManager.registerEvents(new EventListener(), plugin); + pluginManager.registerEvents(new SnowfallEverywhere(), plugin); CustomBlock.init(); CustomItem.postInit(); @@ -320,8 +324,9 @@ public static String getName() { } public static void useNotSupport(String name) { - if (!isDevelopMode()) + if (!isDevelopMode()) { return; + } String message = "Use not support multiplayer method " + name; if (isRuntimeException()) { throw new RuntimeException(message); @@ -331,8 +336,9 @@ public static void useNotSupport(String name) { } public static void useClientMethod(String name) { - if (!isDevelopMode()) + if (!isDevelopMode()) { return; + } String message = "Use client method " + name; if (isRuntimeException()) { throw new RuntimeException(message); @@ -342,8 +348,9 @@ public static void useClientMethod(String name) { } public static void useNotCurrentSupport(String name) { - if (!isDevelopMode()) + if (!isDevelopMode()) { return; + } String message = "The " + name + " method is currently not supported"; if (isRuntimeException()) { throw new RuntimeException(message); @@ -353,8 +360,9 @@ public static void useNotCurrentSupport(String name) { } public static void useIncomprehensibleMethod(String name) { - if (!isDevelopMode()) + if (!isDevelopMode()) { return; + } String message = "I don't really understand what this method does (" + name + "), which is why you're reading this right now"; if (isRuntimeException()) { diff --git a/src/main/java/com/reider745/event/EventListener.java b/src/main/java/com/reider745/event/EventListener.java index 68d69bfef..8129e39bb 100644 --- a/src/main/java/com/reider745/event/EventListener.java +++ b/src/main/java/com/reider745/event/EventListener.java @@ -9,24 +9,27 @@ import cn.nukkit.event.EventHandler; import cn.nukkit.event.Listener; import cn.nukkit.event.block.BlockBreakEvent; +import cn.nukkit.event.block.BlockExplosionPrimeEvent; import cn.nukkit.event.block.BlockGrowEvent; import cn.nukkit.event.block.BlockPlaceEvent; import cn.nukkit.event.entity.*; import cn.nukkit.event.entity.EntityDamageEvent.DamageCause; import cn.nukkit.event.inventory.InventoryPickupItemEvent; import cn.nukkit.event.level.ChunkPopulateEvent; +import cn.nukkit.event.player.PlayerDeathEvent; import cn.nukkit.event.player.PlayerEatFoodEvent; import cn.nukkit.event.player.PlayerExperienceChangeEvent; import cn.nukkit.event.player.PlayerInteractEntityEvent; import cn.nukkit.event.player.PlayerInteractEvent; import cn.nukkit.event.redstone.RedstoneUpdateEvent; -import cn.nukkit.inventory.Inventory; import cn.nukkit.item.Item; import cn.nukkit.item.food.Food; import cn.nukkit.level.Level; +import cn.nukkit.level.Location; import cn.nukkit.level.Position; import cn.nukkit.level.format.FullChunk; import cn.nukkit.math.Vector3; + import com.reider745.api.CallbackHelper; import com.reider745.entity.EntityMethod; import com.reider745.hooks.ItemUtils; @@ -50,11 +53,12 @@ public void onInteract(PlayerInteractEvent event) { final Block block = event.getBlock(); final Player player = event.getPlayer(); - if (event.getAction() == PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK) + if (event.getAction() == PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK) { consumeEvent(event, () -> NativeCallback.onItemUsed((int) block.x, (int) block.y, (int) block.z, event.getFace().getIndex(), (float) pos.x, (float) pos.y, (float) pos.z, true, player.getHealth() > 0, player.getId())); + } } public static void onBlockBreak(BlockBreakEvent event, boolean isNukkitPrevent) { @@ -177,7 +181,8 @@ private static int convertDamageCauseToEnum(DamageCause cause) { @EventHandler public void onEntityDamage(EntityDamageEvent event) { final Entity entity = event.getEntity(); - final Entity attacker = event instanceof EntityDamageByEntityEvent damageByEntity ? damageByEntity.getDamager() + final Entity attacker = event instanceof EntityDamageByEntityEvent damageByEntity + ? damageByEntity.getDamager() : null; if (attacker instanceof Player) { @@ -196,20 +201,39 @@ public void onEntityDeath(EntityDeathEvent event) { final Entity entity = event.getEntity(); final EntityDamageEvent damageEvent = entity.getLastDamageCause(); final Entity attacker = damageEvent instanceof EntityDamageByEntityEvent damageByEntityEvent - ? damageByEntityEvent.getDamager() - : null; + ? damageByEntityEvent.getDamager() : null; NativeCallback.onEntityDied(entity.getId(), attacker != null ? attacker.getId() : -1, convertDamageCauseToEnum(damageEvent != null ? damageEvent.getCause() : DamageCause.CUSTOM)); } @EventHandler - public void onExplosion(ExplosionPrimeEvent event) { + public void onPlayerDeath(PlayerDeathEvent event) { + final Entity entity = event.getEntity(); + final EntityDamageEvent damageEvent = entity.getLastDamageCause(); + final Entity attacker = damageEvent instanceof EntityDamageByEntityEvent damageByEntityEvent + ? damageByEntityEvent.getDamager() : null; + + consumeEvent(event, () -> NativeCallback.onEntityDied(entity.getId(), attacker != null ? attacker.getId() : -1, + convertDamageCauseToEnum(damageEvent != null ? damageEvent.getCause() : DamageCause.CUSTOM))); + } + + @EventHandler + public void onBlockExplosion(BlockExplosionPrimeEvent event) { + final Location pos = event.getBlock().getLocation(); + + consumeEvent(event, () -> NativeCallback.onExplode((float) pos.x, (float) pos.y, (float) pos.z, + (float) event.getForce(), -1, event.isIncendiary(), event.isBlockBreaking(), + (float) event.getFireChance())); + } + + @EventHandler + public void onEntityExplosion(EntityExplosionPrimeEvent event) { final Entity entity = event.getEntity(); final Position pos = entity.getPosition(); consumeEvent(event, () -> NativeCallback.onExplode((float) pos.x, (float) pos.y, (float) pos.z, - (float) event.getForce(), entity != null ? entity.getId() : -1, false, event.isBlockBreaking(), 0f)); + (float) event.getForce(), entity.getId(), false, event.isBlockBreaking(), 0f)); } @EventHandler @@ -242,9 +266,11 @@ public void onInteractEntity(PlayerInteractEntityEvent event) { @EventHandler public void onPickupItem(InventoryPickupItemEvent event) { final long dropEntity = event.getItem().getId(); - final long entity = event.getInventory() instanceof Inventory inventory - && inventory.getHolder() instanceof Entity pickupEntity ? pickupEntity.getId() : -1; - final int count = event.getItem().getItem() instanceof Item item ? item.getCount() : 0; + final long entity = event.getInventory().getHolder() instanceof Entity pickupEntity + ? pickupEntity.getId() + : -1; + final Item item = event.getItem().getItem(); + final int count = item != null ? item.getCount() : 0; consumeEvent(event, () -> NativeCallback.onEntityPickUpDrop(entity, dropEntity, count)); } @@ -256,7 +282,8 @@ public void onBlockUpdate(BlockGrowEvent event) { consumeEvent(event, () -> NativeCallback.onBlockChanged((int) block.x, (int) block.y, (int) block.z, block.getId(), - block.getDamage(), newBlock.getId(), newBlock.getDamage(), 0, 0, block.getLevel())); + block.getDamage(), newBlock.getId(), newBlock.getDamage(), block.getFullId(), + newBlock.getFullId(), block.getLevel())); } // TODO: onPathNavigationDone diff --git a/src/main/java/com/reider745/hooks/SnowfallEverywhere.java b/src/main/java/com/reider745/hooks/SnowfallEverywhere.java index 799d663d4..78df91aea 100644 --- a/src/main/java/com/reider745/hooks/SnowfallEverywhere.java +++ b/src/main/java/com/reider745/hooks/SnowfallEverywhere.java @@ -10,6 +10,9 @@ import cn.nukkit.blockentity.BlockEntity; import cn.nukkit.blockentity.BlockEntitySpawnable; +import cn.nukkit.event.EventHandler; +import cn.nukkit.event.Listener; +import cn.nukkit.event.weather.LightningStrikeEvent; import cn.nukkit.level.DimensionData; import cn.nukkit.level.biome.Biome; import cn.nukkit.level.format.ChunkSection; @@ -28,7 +31,7 @@ * Special christmas adaptation, do not use anywhere else. */ @Hooks -public class SnowfallEverywhere implements HookClass { +public class SnowfallEverywhere implements HookClass, Listener { @Inject(className = "cn.nukkit.level.format.generic.serializer.NetworkChunkSerializer") public static void serialize(IntSet protocols, BaseChunk chunk, @@ -105,4 +108,9 @@ private static byte[] serializeEntities(BaseChunk chunk, int protocol) { throw new RuntimeException(e); } } + + @EventHandler + public void onLightningStrike(LightningStrikeEvent event) { + event.setCancelled(); + } }