Skip to content

Commit

Permalink
fix: баги
Browse files Browse the repository at this point in the history
- добавлена заглушка на getLightningLevel, setLightningLevel
- добавлен try/catch на  ConnectedClientList.send
- исправлена ошибка NativeItemInstamceExtra.asJson
- исправлено отображение прочности брони
- исправлено то, что некоторые блоки нельзя сломать
- добавлена событие explode, interactEntity
  • Loading branch information
Reider745 committed Dec 17, 2023
1 parent 4e1de2c commit 9eeadbc
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 106 deletions.
10 changes: 6 additions & 4 deletions src/main/java/com/reider745/api/CallbackHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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()) {
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/reider745/block/CustomBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ public static int getIdForText(String block) {
return customBlocks.get(block);
}

@Override
public int getToolTier() {
return 0;
}

@Override
public boolean canRandomTickBlocks() {
Expand Down
39 changes: 37 additions & 2 deletions src/main/java/com/reider745/event/EventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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());
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/reider745/item/CustomItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
94 changes: 0 additions & 94 deletions src/main/java/com/reider745/item/CustomItemArmor.java

This file was deleted.

48 changes: 48 additions & 0 deletions src/main/java/com/reider745/item/CustomItemClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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();

Expand All @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/reider745/world/BlockSourceMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/reider745/world/WorldMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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.*;
Expand Down Expand Up @@ -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<T> void send(String name, T data, Class<T> type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 9eeadbc

Please sign in to comment.