Skip to content

Commit

Permalink
fix: PlayerActor, исправлен выход, feat: добавлена комманда
Browse files Browse the repository at this point in the history
- добавлена комманда inner_core_network(отображает список подключённых игроков)
- исправлена ошибка null при использовании PlayerActor после выхода игрока
- удалено сообщение, что Entity.getAll устарел
  • Loading branch information
Reider745 committed Dec 6, 2023
1 parent 1617db7 commit dff03da
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/reider745/commands/CommandsHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public static void init(){
map.register(fallbackPrefix, new CustomBlocksCommands());
map.register(fallbackPrefix, new CustomItemsCommands());
map.register(fallbackPrefix, new StateCommand());
map.register(fallbackPrefix, new InnerCoreNetworkCommand());
}
}
37 changes: 37 additions & 0 deletions src/main/java/com/reider745/commands/InnerCoreNetworkCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.reider745.commands;

import cn.nukkit.command.Command;
import cn.nukkit.command.CommandSender;
import cn.nukkit.command.data.CommandParamType;
import cn.nukkit.command.data.CommandParameter;
import com.reider745.block.BlockStateRegisters;
import com.reider745.block.CustomBlock;
import com.reider745.item.ItemMethod;
import com.zhekasmirnov.apparatus.multiplayer.Network;
import com.zhekasmirnov.apparatus.multiplayer.server.ConnectedClient;

import java.util.ArrayList;
import java.util.List;

public class InnerCoreNetworkCommand extends Command {
public InnerCoreNetworkCommand() {
super("inner_core_network", "debug network command");
}

@Override
public boolean execute(CommandSender commandSender, String s, String[] args) {
if(!commandSender.isOp()) return false;

StringBuilder message = new StringBuilder("===Network===");
List<ConnectedClient> clients = Network.getSingleton().getServer().getConnectedClients();
for(ConnectedClient client : clients)
message.append("\n"+client.getPlayerUid()+" "+client.getClientState().name());

clients = Network.getSingleton().getServer().getInitializingClients();
for(ConnectedClient client : clients)
message.append("\n"+client.getPlayerUid()+" "+client.getClientState().name());

commandSender.sendMessage(message.toString());
return true;
}
}
42 changes: 38 additions & 4 deletions src/main/java/com/reider745/entity/PlayerActorMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

import cn.nukkit.Player;
import cn.nukkit.PlayerFood;
import cn.nukkit.inventory.PlayerInventory;
import cn.nukkit.item.Item;
import cn.nukkit.level.Level;
import cn.nukkit.math.Vector3;
import com.reider745.InnerCoreServer;
import com.reider745.hooks.ItemUtils;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;

public class PlayerActorMethods {
@Nullable
public static Player constructNew(long entity){
Collection<Player> collection = InnerCoreServer.server.getOnlinePlayers().values();
for(Player player : collection)
Expand All @@ -24,46 +28,59 @@ public static void invokeUseItemNoTarget(Player pointer, int id, int count, int
//pointer.getLevel()
}
public static void addItemToInventory(Player pointer, int id, int count, int data, long extra, boolean dropLeft){
if(pointer == null || !pointer.isValid()) return;
Item[] items = pointer.getInventory().addItem(ItemUtils.get(id, count, data, extra));
if(dropLeft)
for(Item item : items)
if (dropLeft)
for (Item item : items)
pointer.dropItem(item);
}
public static void addItemToInventoryPtr(Player pointer, long itemStack, boolean dropLeft){

}
public static void addExperience(Player pointer, int amount){
if(pointer == null || !pointer.isValid()) return;
pointer.addExperience(amount);
}
public static int getDimension(Player pointer){
return pointer.getLevel().getDimension();
if(pointer == null || !pointer.isValid()) return 0;
Level level = pointer.getLevel();
if(level != null)
return pointer.getLevel().getDimension();
return 0;
}
public static int getGameMode(Player pointer){
if(pointer == null || !pointer.isValid()) return 0;
return pointer.getGamemode();
}
public static Item getInventorySlot(Player pointer, int slot){
if(pointer == null || !pointer.isValid()) return Item.AIR_ITEM;
return pointer.getInventory().getItem(slot);
}
public static Item getArmor(Player pointer, int slot){
if(pointer == null || !pointer.isValid()) return Item.AIR_ITEM;
return pointer.getInventory().getArmorItem(slot);
}
public static float getExhaustion(Player pointer){
return 0;
//return pointer.get
}
public static float getExperience(Player pointer){
if(pointer == null || !pointer.isValid()) return 0;
return pointer.getExperience();
}
public static float getHunger(Player pointer){
if(pointer == null || !pointer.isValid()) return 0;
PlayerFood food = pointer.getFoodData();
if(food != null)
return food.getLevel();
return 0;
}
public static float getLevel(Player pointer){
if(pointer == null || !pointer.isValid()) return 0;
return pointer.getExperienceLevel();
}
public static float getSaturation(Player pointer){
if(pointer == null || !pointer.isValid()) return 0;
PlayerFood food = pointer.getFoodData();
if(food != null)
return food.getFoodSaturationLevel();
Expand All @@ -73,55 +90,72 @@ public static int getScore(Player pointer){
return 0;
}
public static int getSelectedSlot(Player pointer){
if(pointer == null || !pointer.isValid()) return 0;
return pointer.getInventory().getHeldItemSlot();
}
public static void setInventorySlot(Player pointer, int slot, int id, int count, int data, long extra){
if(pointer == null || !pointer.isValid()) return;
pointer.getInventory().setItem(slot, ItemUtils.get(id, count, data, extra));
}
public static void setArmor(Player pointer, int slot, int id, int count, int data, long extra){
if(pointer == null || !pointer.isValid()) return;
pointer.getInventory().setArmorItem(slot, ItemUtils.get(id, count, data, extra));
}
public static void setExhaustion(Player pointer, float value){

}
public static void setExperience(Player pointer, float value){
if(pointer == null || !pointer.isValid()) return;
pointer.setExperience((int) value);
}
public static void setHunger(Player pointer, float value){
if(pointer == null || !pointer.isValid()) return;
PlayerFood food = pointer.getFoodData();
if(food != null)
food.setLevel((int) value);
}
public static void setLevel(Player pointer, float value){
if(pointer == null || !pointer.isValid()) return;
pointer.setExperience(0, (int) value);
}
public static void setSaturation(Player pointer, float value){
if(pointer == null || !pointer.isValid()) return;
PlayerFood food = pointer.getFoodData();
if(food != null)
food.setFoodSaturationLevel(value);
}
public static void setSelectedSlot(Player pointer, int slot){
if(pointer == null || !pointer.isValid()) return;
pointer.getInventory().setHeldItemSlot(slot);
}
public static void setRespawnCoords(Player pointer, int x, int y, int z){
if(pointer == null || !pointer.isValid()) return;
pointer.setSpawn(new Vector3(x, y, z));
}
public static void spawnExpOrbs(Player pointer, float x, float y, float z, int amount){
pointer.getLevel().dropExpOrb(new Vector3(x, y, z), amount);
if(pointer == null) return;
Level level = pointer.getLevel();
if(level != null)
level.dropExpOrb(new Vector3(x, y, z), amount);
}
public static boolean isSneaking(Player pointer){
if(pointer == null || !pointer.isValid()) return false;
return pointer.isSneaking();
}
public static void setSneaking(Player pointer, boolean sneaking){
if(pointer == null || !pointer.isValid()) return;
pointer.setSneaking(sneaking);
}
public static int getItemUseDuration(Player pointer){
if(pointer == null || !pointer.isValid()) return 0;
return pointer.getInventory().getItemInHand().getMaxDurability();
}
public static float getItemUseIntervalProgress(Player pointer){
if(pointer == null || !pointer.isValid()) return 0;
return pointer.getInventory().getItemInHand().getMaxDurability();
}
public static float getItemUseStartupProgress(Player pointer){
if(pointer == null || !pointer.isValid()) return 0;
return pointer.getInventory().getItemInHand().getMaxDurability();
}
}
13 changes: 8 additions & 5 deletions src/main/java/com/reider745/hooks/PlayerHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,13 @@ public static void completeLoginSequence(Player self) {

@Inject
public static void close(Player self, TextContainer message, String reason, boolean notify) {
/*
* ConnectedClient client = getForPlayer(self);
* if(client != null)
* client.getChannelInterface().close();
*/
ConnectedClient client = getForPlayer(self);
try{
if(client != null)
client.getChannelInterface().close();
}catch (Exception e){
e.printStackTrace();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public boolean isValid() {
try {
return PlayerActorMethods.isValid(pointer);
} catch (NoSuchMethodError e) {
return pointer != null;
return pointer != null && pointer.isValid();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private synchronized void channelBroke(IOException exception) {
}
isPanic = true;
if (exception != null) {
exception.printStackTrace();
//exception.printStackTrace();
}
if (!isClosed) {
if (EngineConfig.isDeveloperMode()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,6 @@ public static ArrayList<Long> getAllArrayList() {

@JSStaticFunction
public static NativeArray getAll() {
logDeprecation("Entity.getAll()");
return new NativeArray(NativeCallback.getAllEntities().toArray());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public static void loadNativeRecipesIfNeeded() {
} try {
FileTools.unpackAsset("innercore/recipes/furnace_fuel.json", FileTools.DIR_WORK + "furnace.json");
} catch (IOException e) {
ICLog.e("RECIPES", "failed to unpack recipes", e);
//ICLog.e("RECIPES", "failed to unpack recipes", e);
return;
}

Expand Down

0 comments on commit dff03da

Please sign in to comment.