Skip to content

Commit

Permalink
Fix 1.1.0 command & creative inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
lt-name committed Oct 20, 2024
1 parent 1146b1a commit 142f2c7
Show file tree
Hide file tree
Showing 21 changed files with 162 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,6 @@ public void setEnableClientCommand(boolean enable) {
}

public void sendCommandData() {
AvailableCommandsPacket pk = new AvailableCommandsPacket();
Map<String, CommandDataVersions> data = new HashMap<>();

for (Command command : this.server.getCommandMap().getCommands().values()) {
Expand All @@ -772,6 +771,7 @@ public void sendCommandData() {
}

if (!data.isEmpty()) {
AvailableCommandsPacket pk = new AvailableCommandsPacket();
pk.commands = data;
this.dataPacket(pk);
}
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/cn/nukkit/item/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ public static void init() {
clearCreativeItems();
}

private static final List<Item> creative113 = new ObjectArrayList<>();
private static final List<Item> creative137 = new ObjectArrayList<>();
private static final List<Item> creative274 = new ObjectArrayList<>();
private static final List<Item> creative291 = new ObjectArrayList<>();
Expand Down Expand Up @@ -507,6 +508,8 @@ public static void initCreativeItems() {
clearCreativeItems();

// Creative inventory for oldest versions
registerCreativeItems(v1_1_0);
registerCreativeItems(v1_2_0);
registerCreativeItems(v1_5_0);
registerCreativeItems(v1_7_0);
registerCreativeItems(v1_8_0);
Expand Down Expand Up @@ -582,6 +585,7 @@ private static void registerCreativeItemsNew(int protocol, int blockPaletteProto
}

public static void clearCreativeItems() {
Item.creative113.clear();
Item.creative137.clear();
Item.creative274.clear();
Item.creative291.clear();
Expand Down Expand Up @@ -626,7 +630,8 @@ public static ArrayList<Item> getCreativeItems() {

public static ArrayList<Item> getCreativeItems(int protocol) {
switch (protocol) {
case v1_1_0: //TODO check
case v1_1_0:
return new ArrayList<>(Item.creative113);
case v1_2_0:
case v1_2_5_11:
case v1_2_5:
Expand Down Expand Up @@ -749,6 +754,7 @@ public static void addCreativeItem(Item item) {

public static void addCreativeItem(int protocol, Item item) {
switch (protocol) { // NOTE: Not all versions are supposed to be here
case v1_1_0 -> Item.creative113.add(item.clone());
case v1_2_0 -> Item.creative137.add(item.clone());
case v1_5_0 -> Item.creative274.add(item.clone());
case v1_7_0 -> Item.creative291.add(item.clone());
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/cn/nukkit/network/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public void processBatch(byte[] payload, Collection<DataPacket> packets, Compres
try {
if (raknetProtocol > 8) {
pk.decode();
}else { // version < 1.6
} else { // version < 1.6
pk.setBuffer(buf, pk.protocol < ProtocolInfo.v1_2_0 ? 1 : 3);
pk.decode();
}
Expand Down Expand Up @@ -384,6 +384,7 @@ private void registerPackets() {
.registerPacket(ProtocolInfoV113.DISCONNECT_PACKET, DisconnectPacket.class)
.registerPacket(ProtocolInfoV113.DROP_ITEM_PACKET, DropItemPacketV113.class)
.registerPacket(ProtocolInfoV113.ENTITY_EVENT_PACKET, EntityEventPacket.class)
.registerPacket(ProtocolInfoV113.UPDATE_ATTRIBUTES_PACKET, UpdateAttributesPacket.class)
.registerPacket(ProtocolInfoV113.ENTITY_FALL_PACKET, EntityFallPacket.class)
.registerPacket(ProtocolInfoV113.EXPLODE_PACKET, ExplodePacketV113.class)
.registerPacket(ProtocolInfoV113.FULL_CHUNK_DATA_PACKET, LevelChunkPacket.class)
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/cn/nukkit/network/process/DataPacketManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import cn.nukkit.PlayerHandle;
import cn.nukkit.network.process.processor.common.*;
import cn.nukkit.network.process.processor.v113.ContainerSetSlotProcessor_v113;
import cn.nukkit.network.process.processor.v113.DropItemProcessor_v113;
import cn.nukkit.network.process.processor.v113.RemoveBlockProcessor_v113;
import cn.nukkit.network.process.processor.v113.UseItemProcessor_v113;
import cn.nukkit.network.process.processor.v113.*;
import cn.nukkit.network.process.processor.v137.CommandRequestProcessor_v137;
import cn.nukkit.network.process.processor.v282.SetLocalPlayerAsInitializedProcessor_v282;
import cn.nukkit.network.process.processor.v340.LecternUpdateProcessor_v340;
import cn.nukkit.network.process.processor.v422.FilterTextProcessor_v422;
Expand Down Expand Up @@ -107,7 +105,6 @@ public static void registerDefaultProcessors() {
AdventureSettingsProcessor.INSTANCE,
BookEditProcessor.INSTANCE,
ClientToServerHandshakeProcessor.INSTANCE,
CommandRequestProcessor.INSTANCE,
EmotePacketProcessor.INSTANCE,
ItemFrameDropItemProcessor.INSTANCE,
LevelSoundEventProcessor.INSTANCE,
Expand All @@ -133,12 +130,18 @@ public static void registerDefaultProcessors() {

registerProcessor(
ProtocolInfo.v1_1_0,
CommandStepProcessor_v113.INSTANCE,
ContainerSetSlotProcessor_v113.INSTANCE,
DropItemProcessor_v113.INSTANCE,
RemoveBlockProcessor_v113.INSTANCE,
UseItemProcessor_v113.INSTANCE
);

registerProcessor(
ProtocolInfo.v1_2_0,
CommandRequestProcessor_v137.INSTANCE
);

registerProcessor(
ProtocolInfo.v1_6_0_5,
SetLocalPlayerAsInitializedProcessor_v282.INSTANCE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package cn.nukkit.network.process.processor.v113;

import cn.nukkit.Player;
import cn.nukkit.PlayerHandle;
import cn.nukkit.command.Command;
import cn.nukkit.command.data.CommandParameter;
import cn.nukkit.event.player.PlayerCommandPreprocessEvent;
import cn.nukkit.network.process.DataPacketProcessor;
import cn.nukkit.network.protocol.ProtocolInfo;
import cn.nukkit.network.protocol.v113.CommandStepPacketV113;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import org.jetbrains.annotations.NotNull;

/**
* @author LT_Name
*/
public class CommandStepProcessor_v113 extends DataPacketProcessor<CommandStepPacketV113> {

public static final CommandStepProcessor_v113 INSTANCE = new CommandStepProcessor_v113();

@Override
public void handle(@NotNull PlayerHandle playerHandle, @NotNull CommandStepPacketV113 pk) {
Player player = playerHandle.player;
if (!player.spawned || !player.isAlive()) {
return;
}
player.craftingType = Player.CRAFTING_SMALL;


StringBuilder commandText = new StringBuilder(pk.command);
Command command = player.getServer().getCommandMap().getCommand(commandText.toString());
if (command != null) {
if (pk.args != null && !pk.args.isEmpty()) {
CommandParameter[] pars = command.getCommandParameters(pk.overload);
if (pars != null) {
for (CommandParameter par : pars) {
JsonElement arg = pk.args.get(par.name);
if (arg != null) {
switch (par.type) {
case TARGET:
//TODO
/*CommandArg rules = new Gson().fromJson(arg, CommandArg.class);
commandText += " " + rules.getRules()[0].getValue();*/
break;
case BLOCK_POSITION:
//TODO
/*CommandArgBlockVector bv = new Gson().fromJson(arg, CommandArgBlockVector.class);
commandText += " " + bv.getX() + " " + bv.getY() + " " + bv.getZ();*/
break;
case STRING:
case RAWTEXT:
String string = new Gson().fromJson(arg, String.class);
commandText.append(" ").append(string);
break;
default:
commandText.append(" ").append(arg);
break;
}
}
}
} else {
player.sendMessage(player.getServer().getLanguage().translateString(command.getUsage()));
}
}
}

PlayerCommandPreprocessEvent playerCommandPreprocessEvent = new PlayerCommandPreprocessEvent(player, "/" + commandText);
player.getServer().getPluginManager().callEvent(playerCommandPreprocessEvent);
if (playerCommandPreprocessEvent.isCancelled()) {
return;
}

player.getServer().dispatchCommand(playerCommandPreprocessEvent.getPlayer(), playerCommandPreprocessEvent.getMessage().substring(1));
}

@Override
public int getPacketId() {
return ProtocolInfo.toNewProtocolID(CommandStepPacketV113.NETWORK_ID);
}

@Override
public boolean isSupported(int protocol) {
return protocol < ProtocolInfo.v1_2_0;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.nukkit.network.process.processor.common;
package cn.nukkit.network.process.processor.v137;

import cn.nukkit.Player;
import cn.nukkit.PlayerHandle;
Expand All @@ -14,9 +14,9 @@
* @author LT_Name
*/
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class CommandRequestProcessor extends DataPacketProcessor<CommandRequestPacket> {
public class CommandRequestProcessor_v137 extends DataPacketProcessor<CommandRequestPacket> {

public static final CommandRequestProcessor INSTANCE = new CommandRequestProcessor();
public static final CommandRequestProcessor_v137 INSTANCE = new CommandRequestProcessor_v137();

@Override
public void handle(@NotNull PlayerHandle playerHandle, @NotNull CommandRequestPacket pk) {
Expand All @@ -32,16 +32,10 @@ public void handle(@NotNull PlayerHandle playerHandle, @NotNull CommandRequestPa
}

player.getServer().dispatchCommand(playerCommandPreprocessEvent.getPlayer(), playerCommandPreprocessEvent.getMessage().substring(1));

}

@Override
public int getPacketId() {
return ProtocolInfo.toNewProtocolID(ProtocolInfo.COMMAND_REQUEST_PACKET);
}

@Override
public boolean isSupported(int protocol) {
return protocol >= ProtocolInfo.v1_1_0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cn.nukkit.network.protocol.types.CommandParam;
import cn.nukkit.utils.BinaryStream;
import cn.nukkit.utils.SequencedHashSet;
import com.google.gson.Gson;
import lombok.ToString;
import org.cloudburstmc.protocol.common.util.TypeMap;

Expand Down Expand Up @@ -377,6 +378,12 @@ public void decode() {
public void encode() {
this.reset();

if (this.protocol < ProtocolInfo.v1_2_0) {
this.putString(new Gson().toJson(this.commands));
this.putString("");
return;
}

LinkedHashSet<String> enumValuesSet = new LinkedHashSet<>();
SequencedHashSet<String> subCommandValues = new SequencedHashSet<>();
LinkedHashSet<String> postFixesSet = new LinkedHashSet<>();
Expand Down Expand Up @@ -537,7 +544,7 @@ public void encode() {
int id = getCommandParams(protocol).getId(commandParam);
type |= id;
} catch (IllegalArgumentException e) {
// 忽略不支持的id
type |= getCommandParams(protocol).getId(CommandParam.STRING);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package cn.nukkit.network.protocol.v113;

import cn.nukkit.math.BlockVector3;
import cn.nukkit.network.protocol.DataPacket;

public class AddHangingEntityPacketV113 extends DataPacket {
public class AddHangingEntityPacketV113 extends DataPacket_v113 {
public static final byte NETWORK_ID = ProtocolInfoV113.ADD_HANGING_ENTITY_PACKET;

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package cn.nukkit.network.protocol.v113;

import cn.nukkit.item.Item;
import cn.nukkit.network.protocol.DataPacket;

public class AddItemPacketV113 extends DataPacket {
public class AddItemPacketV113 extends DataPacket_v113 {
public static final byte NETWORK_ID = ProtocolInfoV113.ADD_ITEM_PACKET;

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package cn.nukkit.network.protocol.v113;

import cn.nukkit.command.data.CommandArgs;
import cn.nukkit.network.protocol.DataPacket;
import com.google.gson.Gson;

/**
* author: MagicDroidX
* Nukkit Project
*/
public class CommandStepPacketV113 extends DataPacket {
public class CommandStepPacketV113 extends DataPacket_v113 {

public static final byte NETWORK_ID = ProtocolInfoV113.COMMAND_STEP_PACKET;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* author: MagicDroidX
* Nukkit Project
*/
public class ContainerSetContentPacketV113 extends DataPacket {
public static final byte NETWORK_ID = ProtocolInfoV113.CRAFTING_DATA_PACKET;
public class ContainerSetContentPacketV113 extends DataPacket_v113 {
public static final byte NETWORK_ID = ProtocolInfoV113.CONTAINER_SET_CONTENT_PACKET;

@Override
public byte pid() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package cn.nukkit.network.protocol.v113;

import cn.nukkit.item.Item;
import cn.nukkit.network.protocol.DataPacket;

/**
* author: MagicDroidX
* Nukkit Project
*/
public class ContainerSetSlotPacketV113 extends DataPacket {
public class ContainerSetSlotPacketV113 extends DataPacket_v113 {
public static final byte NETWORK_ID = ProtocolInfoV113.CONTAINER_SET_SLOT_PACKET;

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cn.nukkit.network.protocol.v113;

import cn.nukkit.network.protocol.DataPacket;

public abstract class DataPacket_v113 extends DataPacket {

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package cn.nukkit.network.protocol.v113;

import cn.nukkit.item.Item;
import cn.nukkit.network.protocol.DataPacket;

/**
* @author Nukkit Project Team
*/
public class DropItemPacketV113 extends DataPacket {
public class DropItemPacketV113 extends DataPacket_v113 {

public static final byte NETWORK_ID = 0x2e;
public static final byte NETWORK_ID = ProtocolInfoV113.DROP_ITEM_PACKET;

public int type;
public Item item;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
* author: MagicDroidX
* Nukkit Project
*/
public class ExplodePacketV113 extends DataPacket {
public class ExplodePacketV113 extends DataPacket_v113 {

public static final byte NETWORK_ID = 0x18;
public static final byte NETWORK_ID = ProtocolInfoV113.EXPLODE_PACKET;

public float x;
public float y;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package cn.nukkit.network.protocol.v113;

import cn.nukkit.item.Item;
import cn.nukkit.network.protocol.DataPacket;

public class InventoryActionPacketV113 extends DataPacket {
public class InventoryActionPacketV113 extends DataPacket_v113 {

public static final byte NETWORK_ID = 0x2f;
public static final byte NETWORK_ID = ProtocolInfoV113.INVENTORY_ACTION_PACKET;

public int actionId;
public Item item;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package cn.nukkit.network.protocol.v113;

import cn.nukkit.math.BlockVector3;
import cn.nukkit.network.protocol.DataPacket;

/**
* @author Nukkit Project Team
*/
public class RemoveBlockPacketV113 extends DataPacket {
public class RemoveBlockPacketV113 extends DataPacket_v113 {

public static final byte NETWORK_ID = /*ProtocolInfo.REMOVE_BLOCK_PACKET*/ 0x15;
public static final byte NETWORK_ID = ProtocolInfoV113.REMOVE_BLOCK_PACKET;

public int x;
public int y;
Expand Down
Loading

0 comments on commit 142f2c7

Please sign in to comment.