From 9eeadbcab5e2cda602ff62662217b92a24c501fc Mon Sep 17 00:00:00 2001 From: Reider745 <70357814+Reider745@users.noreply.github.com> Date: Sun, 17 Dec 2023 09:37:53 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20=D0=B1=D0=B0=D0=B3=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - добавлена заглушка на getLightningLevel, setLightningLevel - добавлен try/catch на ConnectedClientList.send - исправлена ошибка NativeItemInstamceExtra.asJson - исправлено отображение прочности брони - исправлено то, что некоторые блоки нельзя сломать - добавлена событие explode, interactEntity --- .../com/reider745/api/CallbackHelper.java | 10 +- .../java/com/reider745/block/CustomBlock.java | 4 + .../com/reider745/event/EventListener.java | 39 +++++++- .../java/com/reider745/item/CustomItem.java | 2 +- .../com/reider745/item/CustomItemArmor.java | 94 ------------------- .../com/reider745/item/CustomItemClass.java | 48 ++++++++++ .../reider745/world/BlockSourceMethods.java | 4 +- .../java/com/reider745/world/WorldMethod.java | 5 +- .../multiplayer/channel/ChannelInterface.java | 1 + .../util/list/ConnectedClientList.java | 19 +++- .../innercore/api/NativeCallback.java | 1 + .../api/NativeItemInstanceExtra.java | 2 +- 12 files changed, 123 insertions(+), 106 deletions(-) delete mode 100644 src/main/java/com/reider745/item/CustomItemArmor.java diff --git a/src/main/java/com/reider745/api/CallbackHelper.java b/src/main/java/com/reider745/api/CallbackHelper.java index f10b90f36..9a9a34c60 100644 --- a/src/main/java/com/reider745/api/CallbackHelper.java +++ b/src/main/java/com/reider745/api/CallbackHelper.java @@ -89,15 +89,17 @@ public ThreadCallback(ICallbackApply apply) { private static class ThreadCallbackEvent extends ThreadCallback { protected final Event event; + protected final boolean isPrevented; - public ThreadCallbackEvent(Event event, ICallbackApply apply) { + public ThreadCallbackEvent(Event event, ICallbackApply apply, boolean isPrevented) { super(apply); this.event = event; + this.isPrevented = isPrevented; } @Override public void run() { - if (event.isCancelled()) + if (isPrevented && event.isCancelled()) return; apply.apply(); event.setCancelled(isPrevent()); @@ -125,8 +127,8 @@ public static void applyRegion(Type type, Level level, ICallbackApply apply) { threadRegion.add(apply); } - public static void apply(Event event, ICallbackApply apply) { - Thread thread = new ThreadCallbackEvent(event, apply); + public static void apply(Event event, ICallbackApply apply, boolean isPrevented) { + Thread thread = new ThreadCallbackEvent(event, apply, isPrevented); thread.start(); while (thread.isAlive()) { } diff --git a/src/main/java/com/reider745/block/CustomBlock.java b/src/main/java/com/reider745/block/CustomBlock.java index e93741b94..1eee1d0de 100644 --- a/src/main/java/com/reider745/block/CustomBlock.java +++ b/src/main/java/com/reider745/block/CustomBlock.java @@ -151,6 +151,10 @@ public static int getIdForText(String block) { return customBlocks.get(block); } + @Override + public int getToolTier() { + return 0; + } @Override public boolean canRandomTickBlocks() { diff --git a/src/main/java/com/reider745/event/EventListener.java b/src/main/java/com/reider745/event/EventListener.java index a85d2373c..3b05fcc96 100644 --- a/src/main/java/com/reider745/event/EventListener.java +++ b/src/main/java/com/reider745/event/EventListener.java @@ -12,13 +12,17 @@ import cn.nukkit.event.block.BlockBreakEvent; import cn.nukkit.event.block.BlockPlaceEvent; import cn.nukkit.event.entity.*; +import cn.nukkit.event.inventory.InventoryPickupItemEvent; import cn.nukkit.event.level.ChunkPopulateEvent; 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.item.Item; import cn.nukkit.item.food.Food; import cn.nukkit.level.Level; +import cn.nukkit.level.Position; import cn.nukkit.level.format.FullChunk; import cn.nukkit.math.BlockFace; import cn.nukkit.math.Vector3; @@ -31,7 +35,10 @@ public class EventListener implements Listener { public static void preventedCallback(Event event, CallbackHelper.ICallbackApply apply) { - CallbackHelper.apply(event, apply); + CallbackHelper.apply(event, apply, true); + } + public static void preventedCallback(Event event, CallbackHelper.ICallbackApply apply, boolean isPrevent) { + CallbackHelper.apply(event, apply, isPrevent); } @EventHandler(priority = EventPriority.LOWEST) @@ -52,7 +59,7 @@ public void use(PlayerInteractEvent event) { public void breakBlock(BlockBreakEvent event) { Block block = event.getBlock(); preventedCallback(event, () -> NativeCallback.onBlockDestroyed((int) block.x, (int) block.y, (int) block.z, - event.getFace().getIndex(), event.getPlayer().getId())); + event.getFace().getIndex(), event.getPlayer().getId()), false); } @EventHandler(priority = EventPriority.LOWEST) @@ -154,4 +161,32 @@ public void addedEntity(EntitySpawnEvent event) { public void removeEntity(EntityDespawnEvent event) { NativeCallback.onEntityRemoved(event.getEntity().getId()); } + + @EventHandler + public void explode(ExplosionPrimeEvent event){ + final Entity entity = event.getEntity(); + + if(entity != null){ + final Position pos = entity.getPosition(); + preventedCallback(event, () -> NativeCallback.onExplode((float) pos.x, (float) pos.y, (float) pos.z, (float) event.getForce(), entity.getId(), false, false, 0)); + } + } + + @EventHandler + public void expChange(PlayerExperienceChangeEvent event){ + + } + + @EventHandler + public void interactEntity(PlayerInteractEntityEvent event){ + final Vector3 position = event.getClickedPos(); + NativeCallback.onInteractWithEntity(event.getEntity().getId(), event.getPlayer().getId(), (float) position.x, (float) position.y, (float) position.z); + } + + @EventHandler + public void pickUpDrop(InventoryPickupItemEvent event){ + //event.getItem().getId(); + + //preventedCallback(event, () -> NativeCallback.onEntityPickUpDrop()); + } } diff --git a/src/main/java/com/reider745/item/CustomItem.java b/src/main/java/com/reider745/item/CustomItem.java index fdacb5009..2c067a0b8 100644 --- a/src/main/java/com/reider745/item/CustomItem.java +++ b/src/main/java/com/reider745/item/CustomItem.java @@ -135,7 +135,7 @@ public static CustomManager registerThrowableItem(String nameId, int id, String } public static CustomManager registerArmorItem(String nameId, int id, String name, int slot, int defense, int durability, float knockbackResist) { - CustomManager manager = registerItem(nameId, id, name, CustomItemArmor.class); + CustomManager manager = registerItem(nameId, id, name); manager.put(PropertiesNames.Armors.SLOT, slot); manager.put(PropertiesNames.Armors.DEFENSE, defense); manager.put(PropertiesNames.MAX_DAMAGE, durability); diff --git a/src/main/java/com/reider745/item/CustomItemArmor.java b/src/main/java/com/reider745/item/CustomItemArmor.java deleted file mode 100644 index 43257849a..000000000 --- a/src/main/java/com/reider745/item/CustomItemArmor.java +++ /dev/null @@ -1,94 +0,0 @@ -package com.reider745.item; - -import cn.nukkit.item.Item; -import cn.nukkit.item.ItemArmor; -import com.reider745.api.CustomManager; - -import com.reider745.item.ItemMethod.PropertiesNames; - -public class CustomItemArmor extends ItemArmor { - private CustomManager parameters; - private int slot; - private int defense; - private float knockbackResist; - private boolean ARMOR_DAMAGEABLE; - - @Override - public int getTier() { - return 2; - } - - @Override - public boolean isHelmet() { - return slot == 0; - } - - @Override - public boolean isChestplate() { - return slot == 1; - } - - @Override - public boolean isLeggings() { - return slot == 2; - } - - @Override - public boolean isBoots() { - return slot == 3; - } - - @Override - public int getArmorPoints() { - return defense; - } - - @Override - public int getToughness() { - return (int) knockbackResist; - } - - public CustomItemArmor(int id, Integer meta, int count){ - this(id, meta, count, CustomItem.getItemManager(id)); - } - - public CustomItemArmor(int id, Integer meta, int count, CustomManager manager) { - super(id, meta, count, manager.get(PropertiesNames.NAME, "InnerCore item")); - - parameters = manager; - this.name = parameters.get(PropertiesNames.NAME, "InnerCore item"); - this.slot = parameters.get(PropertiesNames.Armors.SLOT); - this.defense = parameters.get(PropertiesNames.Armors.DEFENSE); - this.knockbackResist = parameters.get(PropertiesNames.Armors.KNOCKBACK_RESIST); - this.ARMOR_DAMAGEABLE = parameters.get(PropertiesNames.ARMOR_DAMAGEABLE, false); - } - - @Override - public int getEnchantAbility() { - return parameters.get(PropertiesNames.ENCHANTABILITY_VALUE, 0); - } - - @Override - public int getMaxDurability() { - return parameters.get(PropertiesNames.MAX_DAMAGE); - } - - @Override - public int getMaxStackSize() { - return parameters.get(PropertiesNames.MAX_STACK); - } - - @Override - public boolean isUnbreakable() { - return ARMOR_DAMAGEABLE; - } - - @Override - public Item clone() { - CustomItemArmor item = (CustomItemArmor) super.clone(); - item.parameters = parameters; - item.name = name; - item.meta = meta; - return item; - } -} diff --git a/src/main/java/com/reider745/item/CustomItemClass.java b/src/main/java/com/reider745/item/CustomItemClass.java index d5d03c5b1..c8c0f7d10 100644 --- a/src/main/java/com/reider745/item/CustomItemClass.java +++ b/src/main/java/com/reider745/item/CustomItemClass.java @@ -16,6 +16,10 @@ public class CustomItemClass extends Item { private CustomManager parameters; private int max_damage, max_stack; private boolean use_no_target; + private int slot; + private int defense; + private float knockbackResist; + private boolean ARMOR_DAMAGEABLE; public CustomItemClass(int id, Integer meta, int count){ this(id, meta, count, CustomItem.getItemManager(id)); @@ -29,6 +33,10 @@ public CustomItemClass(int id, Integer meta, int count, CustomManager manager) { this.max_damage = parameters.get(PropertiesNames.MAX_DAMAGE); this.max_stack = parameters.get(PropertiesNames.MAX_STACK); this.use_no_target = Callback.count("ItemUseNoTarget") > 0; + this.slot = parameters.get(PropertiesNames.Armors.SLOT); + this.defense = parameters.get(PropertiesNames.Armors.DEFENSE); + this.knockbackResist = parameters.get(PropertiesNames.Armors.KNOCKBACK_RESIST); + this.ARMOR_DAMAGEABLE = parameters.get(PropertiesNames.ARMOR_DAMAGEABLE, false); CompoundTag tag = getOrCreateNamedTag(); @@ -50,6 +58,46 @@ public CustomItemClass(int id, Integer meta, int count, CustomManager manager) { setCompoundTag(tag); } + @Override + public int getTier() { + return 6; + } + + @Override + public boolean isUnbreakable() { + return ARMOR_DAMAGEABLE; + } + + @Override + public boolean isHelmet() { + return slot == 0; + } + + @Override + public boolean isChestplate() { + return slot == 1; + } + + @Override + public boolean isLeggings() { + return slot == 2; + } + + @Override + public boolean isBoots() { + return slot == 3; + } + + @Override + public int getArmorPoints() { + return defense; + } + + @Override + public int getToughness() { + return (int) knockbackResist; + } + @Override public int getMaxDurability() { return max_damage; diff --git a/src/main/java/com/reider745/world/BlockSourceMethods.java b/src/main/java/com/reider745/world/BlockSourceMethods.java index c773f98bb..d7d9052d3 100644 --- a/src/main/java/com/reider745/world/BlockSourceMethods.java +++ b/src/main/java/com/reider745/world/BlockSourceMethods.java @@ -7,6 +7,7 @@ import cn.nukkit.entity.item.EntityItem; import cn.nukkit.entity.item.EntityXPOrb; import cn.nukkit.event.entity.EntityExplosionPrimeEvent; +import cn.nukkit.event.entity.ExplosionPrimeEvent; import cn.nukkit.item.Item; import cn.nukkit.level.Explosion; import cn.nukkit.level.Level; @@ -189,7 +190,8 @@ public static void addToTickingQueue(Level pointer, int x, int y, int z, int run } public static void explode(Level pointer, float x, float y, float z, float power, boolean fire) { - EntityExplosionPrimeEvent event = new EntityExplosionPrimeEvent(null, power); + + ExplosionPrimeEvent event = new ExplosionPrimeEvent(null, power); InnerCoreServer.server.getPluginManager().callEvent(event); if (event.isCancelled()) { return; diff --git a/src/main/java/com/reider745/world/WorldMethod.java b/src/main/java/com/reider745/world/WorldMethod.java index 9b2b1a3e9..a1ae2b477 100644 --- a/src/main/java/com/reider745/world/WorldMethod.java +++ b/src/main/java/com/reider745/world/WorldMethod.java @@ -52,12 +52,13 @@ public static void setRainLevel(float v) { } public static double getLightningLevel() { - InnerCoreServer.useNotSupport("World.setGameMode"); + Level level = getLevel(); + if(level != null) + return level.isRaining() ? 8 : 15; return 0; } public static void setLightningLevel(float v) { - InnerCoreServer.useNotSupport("World.setGameMode"); } public static long getSeed() { diff --git a/src/main/java/com/zhekasmirnov/apparatus/multiplayer/channel/ChannelInterface.java b/src/main/java/com/zhekasmirnov/apparatus/multiplayer/channel/ChannelInterface.java index f9f95116f..4d26c93dd 100644 --- a/src/main/java/com/zhekasmirnov/apparatus/multiplayer/channel/ChannelInterface.java +++ b/src/main/java/com/zhekasmirnov/apparatus/multiplayer/channel/ChannelInterface.java @@ -4,6 +4,7 @@ import com.zhekasmirnov.apparatus.multiplayer.channel.codec.ChannelCodecFactory; import com.zhekasmirnov.apparatus.multiplayer.channel.data.DataChannel; import com.zhekasmirnov.apparatus.util.Java8BackComp; +import com.zhekasmirnov.innercore.api.log.ICLog; import org.json.JSONObject; import org.mozilla.javascript.Scriptable; diff --git a/src/main/java/com/zhekasmirnov/apparatus/multiplayer/util/list/ConnectedClientList.java b/src/main/java/com/zhekasmirnov/apparatus/multiplayer/util/list/ConnectedClientList.java index d974b7aa7..4aead3e0c 100644 --- a/src/main/java/com/zhekasmirnov/apparatus/multiplayer/util/list/ConnectedClientList.java +++ b/src/main/java/com/zhekasmirnov/apparatus/multiplayer/util/list/ConnectedClientList.java @@ -1,11 +1,14 @@ package com.zhekasmirnov.apparatus.multiplayer.util.list; +import cn.nukkit.Player; +import com.reider745.entity.EntityMethod; import com.zhekasmirnov.apparatus.adapter.innercore.game.entity.StaticEntity; import com.zhekasmirnov.apparatus.adapter.innercore.game.common.Vector3; import com.zhekasmirnov.apparatus.job.JobExecutor; import com.zhekasmirnov.apparatus.multiplayer.Network; import com.zhekasmirnov.apparatus.multiplayer.server.ConnectedClient; import com.zhekasmirnov.apparatus.util.Java8BackComp; +import com.zhekasmirnov.innercore.api.log.ICLog; import java.lang.ref.WeakReference; import java.util.*; @@ -238,7 +241,21 @@ public void dropPoliciesAndClear() { } public void send(String name, Object data) { - forEach(client -> client.send(name, data)); + forEach(client -> { + try{ + client.send(name, data); + }catch (Exception e){ + final long playerUid = client.getPlayerUid(); + ICLog.e("Network", "error send "+playerUid, e); + + final Player player = EntityMethod.getPlayerToLong(playerUid); + if(player != null) + player.kick(); + else + ICLog.i("Network", "error kick player"); + } + + }); } public void send(String name, T data, Class type) { diff --git a/src/main/java/com/zhekasmirnov/innercore/api/NativeCallback.java b/src/main/java/com/zhekasmirnov/innercore/api/NativeCallback.java index 6f9358188..cc4a07583 100644 --- a/src/main/java/com/zhekasmirnov/innercore/api/NativeCallback.java +++ b/src/main/java/com/zhekasmirnov/innercore/api/NativeCallback.java @@ -669,6 +669,7 @@ public static void onPlayerLevelAdded(int level, long player) { Callback.invokeAPICallback("ExpLevelAdd", level, player); } + @Deprecated public static void onCommandExec() { String command = getStringParam("command"); Callback.invokeAPICallback("NativeCommand", command == null ? null : command.trim()); diff --git a/src/main/java/com/zhekasmirnov/innercore/api/NativeItemInstanceExtra.java b/src/main/java/com/zhekasmirnov/innercore/api/NativeItemInstanceExtra.java index 5ea06391f..0a25419ea 100644 --- a/src/main/java/com/zhekasmirnov/innercore/api/NativeItemInstanceExtra.java +++ b/src/main/java/com/zhekasmirnov/innercore/api/NativeItemInstanceExtra.java @@ -185,7 +185,7 @@ public JSONObject asJson() { if (data != null && data.length() > 0) { json.put("data", data); } - } catch (JSONException ignore) { + } catch (Exception ignore) { } return json.length() > 0 ? json : null; }