Skip to content

Commit

Permalink
fixed: Исправлены координаты с плавающей точкой & добавлен isValid в …
Browse files Browse the repository at this point in the history
…ServerPlayerTick
  • Loading branch information
Reider745 committed Oct 29, 2024
1 parent 8cd6e51 commit f92a988
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 6 deletions.
3 changes: 0 additions & 3 deletions src/main/java/com/reider745/api/ReflectHelper.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.reider745.api;

import cn.nukkit.entity.Entity;
import cn.nukkit.level.format.FullChunk;
import cn.nukkit.nbt.tag.CompoundTag;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/reider745/entity/EntityMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ public static boolean isValid(Entity entity) {
return entity != null && entity.isValid();
}

public static boolean isValid(long entity) {
return isValid(getEntityById(entity));
}

private static <T> T validateThen(long entityUid, Function<Entity, T> then, T defaultValue) {
Entity entity = getEntityById(entityUid);
if (isValid(entity)) {
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/com/reider745/event/EventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import oshi.hardware.NetworkIF;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

import com.reider745.api.CallbackHelper;
import com.reider745.api.CustomManager;
Expand Down Expand Up @@ -96,6 +97,16 @@ protected void updateBlocks(Level level, double x, double y, double z, boolean s
level.sendBlocks(players, new Vector3[]{new Vector3(x, y, z)}, UpdateBlockPacket.FLAG_ALL);
}

private static final ConcurrentHashMap<Player, Vector3> vectorPositions = new ConcurrentHashMap<>();

public static void setPosition(Player player, Vector3 pos){
vectorPositions.put(player, pos);
}

public static void remove(Player player){
vectorPositions.remove(player);
}

@EventHandler(priority = EventPriority.HIGHEST)
public void onInteract(PlayerInteractEvent event) {
if (event.equals(dealingEvent) || Boolean.TRUE.equals(dealingEvent)) {
Expand All @@ -106,7 +117,7 @@ public void onInteract(PlayerInteractEvent event) {
final Player player = event.getPlayer();

if (event.getAction() == PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK) {
final Vector3 pos = event.getTouchVector();
final Vector3 pos = vectorPositions.get(player);

pos.x = Math.abs(pos.x) + block.x;
pos.y = Math.abs(pos.y) + block.y;
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/reider745/hooks/LevelHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import cn.nukkit.level.GameRule;
import cn.nukkit.level.Level;
import cn.nukkit.level.Position;
import cn.nukkit.level.format.LevelProvider;
import cn.nukkit.level.format.anvil.Anvil;
import cn.nukkit.level.format.generic.BaseFullChunk;
import cn.nukkit.level.format.generic.BaseLevelProvider;
import cn.nukkit.level.particle.DestroyBlockParticle;
Expand Down Expand Up @@ -411,4 +413,20 @@ public static boolean setBlock(Level self, int x, int y, int z, int layer, Block
}
return true;
}

@Inject
public static void useItemOn(Level self, Vector3 vector, Item item, BlockFace face, float fx, float fy, float fz, Player player, boolean playSound) {
// TODO: Событие нажатия на блок от Nukkit-Mot не дает координаты с плавающей точкой
EventListener.setPosition(player, new Vector3(fx, fy, fz));
}

// TODO: Удаление поддержки leveldb(не стабилен, ломает пользовательские биомы)
/*
Ломает сохранение пользовательских биомов
Имеет утечки памяти
*/
@Inject(className = "cn.nukkit.level.format.LevelProviderManager")
public static Class<? extends LevelProvider> getProviderByName(String name){
return Anvil.class;
}
}
1 change: 1 addition & 0 deletions src/main/java/com/reider745/hooks/PlayerHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public static ConnectedClient getForPlayer(Player player) {
@Inject(type = TypeHook.BEFORE_NOT_REPLACE)
public static void close(Player player, TextContainer message, String reason, boolean notify) {
NetworkHooks.close(player);
EventListener.remove(player);
ConnectedClient client = getForPlayer(player);
try {
if (client != null)
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/reider745/world/BiomesMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,13 @@ public static JSONObject getCustomBiomes() {
return json;
}

// TODO: Проблема клиент не правильно обрабатывает пользовательские биомы(вероятнее всего проблема на стороне клиента)
public static long nativeRegister(String name) {
if(AvailableIDS.isEmpty()){
throw new RuntimeException("MAX BIOMES!");
}
NukkitCustomBiome biome = new NukkitCustomBiome(null, name);
biome.reg(AvailableIDS.remove(AvailableIDS.size()-1));
biome.reg(AvailableIDS.remove(0));
customBiomes.put(biome.getId(), biome);

// без этого дерьма работает
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void onTick() {
}
}

if (!isTickCallbackDisabled) {
if (!isTickCallbackDisabled && EntityMethod.isValid(actor.getUid())) {
try {
Callback.invokeCallback(isLocal ? "LocalPlayerTick" : "ServerPlayerTick", actor.getUid(), isDead);
} catch (Throwable err) {
Expand Down

0 comments on commit f92a988

Please sign in to comment.