Skip to content

Commit

Permalink
refactor: Дополнены прошлые события
Browse files Browse the repository at this point in the history
- Исправлена изначальная реализация события взрывов, теперь она вызывается для мобов и блоков.
- Добавлено событие смерти еще игрока, а не только существ.
- Еще раз отключена сборка на GHA.
  • Loading branch information
MaXFeeD committed Dec 25, 2023
1 parent b45ffd1 commit 118ec09
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 25 deletions.
7 changes: 0 additions & 7 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,3 @@ jobs:
uses: gradle/gradle-build-action@v2
with:
arguments: assemble

- name: Upload a Build Artifact
uses: actions/[email protected]
with:
name: ZoteCoreLoader
path: build/libs/ZotCore-SNAPSHOT.jar
retention-days: 30
20 changes: 14 additions & 6 deletions src/main/java/com/reider745/InnerCoreServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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()) {
Expand Down
49 changes: 38 additions & 11 deletions src/main/java/com/reider745/event/EventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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));
}
Expand All @@ -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
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/reider745/hooks/SnowfallEverywhere.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -105,4 +108,9 @@ private static byte[] serializeEntities(BaseChunk chunk, int protocol) {
throw new RuntimeException(e);
}
}

@EventHandler
public void onLightningStrike(LightningStrikeEvent event) {
event.setCancelled();
}
}

0 comments on commit 118ec09

Please sign in to comment.