Skip to content

Commit 5cfeec7

Browse files
committed
Added small how to use guide
1 parent 3d8baae commit 5cfeec7

File tree

13 files changed

+288
-19
lines changed

13 files changed

+288
-19
lines changed

src/main/java/net/lenni0451/miniconnect/server/protocol/LobbyPacketRegistry.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import net.lenni0451.miniconnect.server.protocol.packets.config.S2CConfigRegistryDataPacket;
44
import net.lenni0451.miniconnect.server.protocol.packets.config.S2CConfigUpdateTagsPacket;
55
import net.lenni0451.miniconnect.server.protocol.packets.play.c2s.C2SChatPacket;
6+
import net.lenni0451.miniconnect.server.protocol.packets.play.c2s.C2SContainerButtonClickPacket;
67
import net.lenni0451.miniconnect.server.protocol.packets.play.c2s.C2SContainerClickPacket;
78
import net.lenni0451.miniconnect.server.protocol.packets.play.c2s.C2SContainerClosePacket;
89
import net.lenni0451.miniconnect.server.protocol.packets.play.s2c.*;
@@ -21,6 +22,7 @@ public LobbyPacketRegistry() {
2122
this.registerPacket(MCPackets.C2S_CHAT, C2SChatPacket::new);
2223
this.registerPacket(MCPackets.C2S_CONTAINER_CLOSE, C2SContainerClosePacket::new);
2324
this.registerPacket(MCPackets.C2S_CONTAINER_CLICK, C2SContainerClickPacket::new);
25+
this.registerPacket(MCPackets.C2S_CONTAINER_BUTTON_CLICK, C2SContainerButtonClickPacket::new);
2426

2527
this.registerPacket(MCPackets.S2C_LOGIN, S2CLoginPacket::new);
2628
this.registerPacket(MCPackets.S2C_KEEP_ALIVE, S2CKeepAlivePacket::new);
@@ -32,6 +34,9 @@ public LobbyPacketRegistry() {
3234
this.registerPacket(MCPackets.S2C_OPEN_SCREEN, S2COpenScreenPacket::new);
3335
this.registerPacket(MCPackets.S2C_TRANSFER, S2CTransferPacket::new);
3436
this.registerPacket(MCPackets.S2C_CONTAINER_SET_CONTENT, S2CContainerSetContentPacket::new);
37+
this.registerPacket(MCPackets.S2C_OPEN_BOOK, S2COpenBookPacket::new);
38+
this.registerPacket(MCPackets.S2C_SET_EQUIPMENT, S2CSetEquipmentPacket::new);
39+
this.registerPacket(MCPackets.S2C_CONTAINER_SET_DATA, S2CContainerSetDataPacket::new);
3540
}
3641

3742
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package net.lenni0451.miniconnect.server.protocol.packets.play.c2s;
2+
3+
import io.netty.buffer.ByteBuf;
4+
import lombok.AllArgsConstructor;
5+
import lombok.NoArgsConstructor;
6+
import net.raphimc.netminecraft.packet.Packet;
7+
import net.raphimc.netminecraft.packet.PacketTypes;
8+
9+
@NoArgsConstructor
10+
@AllArgsConstructor
11+
public class C2SContainerButtonClickPacket implements Packet {
12+
13+
public int syncId;
14+
public int buttonId;
15+
16+
@Override
17+
public void read(ByteBuf byteBuf, int protocolVersion) {
18+
this.syncId = PacketTypes.readVarInt(byteBuf);
19+
this.buttonId = PacketTypes.readVarInt(byteBuf);
20+
}
21+
22+
@Override
23+
public void write(ByteBuf byteBuf, int protocolVersion) {
24+
throw new UnsupportedOperationException();
25+
}
26+
27+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package net.lenni0451.miniconnect.server.protocol.packets.play.s2c;
2+
3+
import io.netty.buffer.ByteBuf;
4+
import lombok.AllArgsConstructor;
5+
import lombok.NoArgsConstructor;
6+
import net.raphimc.netminecraft.packet.Packet;
7+
import net.raphimc.netminecraft.packet.PacketTypes;
8+
9+
@NoArgsConstructor
10+
@AllArgsConstructor
11+
public class S2CContainerSetDataPacket implements Packet {
12+
13+
public int syncId;
14+
public int propertyId;
15+
public int value;
16+
17+
@Override
18+
public void read(ByteBuf byteBuf, int protocolVersion) {
19+
throw new UnsupportedOperationException();
20+
}
21+
22+
@Override
23+
public void write(ByteBuf byteBuf, int protocolVersion) {
24+
PacketTypes.writeVarInt(byteBuf, this.syncId);
25+
byteBuf.writeShort(this.propertyId);
26+
byteBuf.writeShort(this.value);
27+
}
28+
29+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package net.lenni0451.miniconnect.server.protocol.packets.play.s2c;
2+
3+
import io.netty.buffer.ByteBuf;
4+
import lombok.AllArgsConstructor;
5+
import lombok.NoArgsConstructor;
6+
import net.raphimc.netminecraft.packet.Packet;
7+
import net.raphimc.netminecraft.packet.PacketTypes;
8+
9+
@NoArgsConstructor
10+
@AllArgsConstructor
11+
public class S2COpenBookPacket implements Packet {
12+
13+
public int hand;
14+
15+
@Override
16+
public void read(ByteBuf byteBuf, int protocolVersion) {
17+
throw new UnsupportedOperationException();
18+
}
19+
20+
@Override
21+
public void write(ByteBuf byteBuf, int protocolVersion) {
22+
PacketTypes.writeVarInt(byteBuf, this.hand);
23+
}
24+
25+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package net.lenni0451.miniconnect.server.protocol.packets.play.s2c;
2+
3+
import com.viaversion.viaversion.api.minecraft.item.Item;
4+
import com.viaversion.viaversion.api.type.types.version.Types1_21_4;
5+
import io.netty.buffer.ByteBuf;
6+
import lombok.AllArgsConstructor;
7+
import lombok.NoArgsConstructor;
8+
import net.raphimc.netminecraft.packet.Packet;
9+
import net.raphimc.netminecraft.packet.PacketTypes;
10+
import org.apache.commons.lang3.tuple.Pair;
11+
12+
import java.util.List;
13+
14+
@NoArgsConstructor
15+
@AllArgsConstructor
16+
public class S2CSetEquipmentPacket implements Packet {
17+
18+
public int entityId;
19+
public List<Pair<Integer, Item>> equipment;
20+
21+
@Override
22+
public void read(ByteBuf byteBuf, int protocolVersion) {
23+
throw new UnsupportedOperationException();
24+
}
25+
26+
@Override
27+
public void write(ByteBuf byteBuf, int protocolVersion) {
28+
PacketTypes.writeVarInt(byteBuf, this.entityId);
29+
int count = this.equipment.size();
30+
for (int i = 0; i < count; i++) {
31+
boolean last = i == count - 1;
32+
Pair<Integer, Item> pair = this.equipment.get(i);
33+
int slotMask = pair.getLeft();
34+
byteBuf.writeByte(!last ? slotMask | -128 : slotMask);
35+
Types1_21_4.ITEM.write(byteBuf, pair.getRight());
36+
}
37+
}
38+
39+
}

src/main/java/net/lenni0451/miniconnect/server/states/StateHandler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public Channel getChannel() {
3535
return this.channel;
3636
}
3737

38+
public LambdaManager getHandlerManager() {
39+
return this.handlerManager;
40+
}
41+
3842
public void tick() {
3943
}
4044

src/main/java/net/lenni0451/miniconnect/server/states/play/screen/ItemBuilder.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
import net.lenni0451.miniconnect.server.protocol.ProtocolConstants;
1111
import net.lenni0451.miniconnect.utils.ViaUtils;
1212

13-
import java.util.ArrayList;
14-
import java.util.Collections;
15-
import java.util.List;
13+
import java.util.*;
1614
import java.util.function.Consumer;
1715

1816
public class ItemBuilder {
@@ -25,7 +23,8 @@ public static ItemBuilder item(final String id) {
2523
private final String id;
2624
private ATextComponent name;
2725
private final List<ATextComponent> lore = new ArrayList<>();
28-
private boolean glint = false;
26+
private Boolean glint;
27+
private final Map<StructuredDataKey, Object> structuredData = new HashMap<>();
2928

3029
private ItemBuilder(final String id) {
3130
this.id = id;
@@ -46,6 +45,11 @@ public ItemBuilder setGlint(final boolean state) {
4645
return this;
4746
}
4847

48+
public <T> ItemBuilder data(final StructuredDataKey<T> key, final T value) {
49+
this.structuredData.put(key, value);
50+
return this;
51+
}
52+
4953
public ItemBuilder calculate(final Consumer<ItemBuilder> consumer) {
5054
consumer.accept(this);
5155
return this;
@@ -66,8 +70,11 @@ public Item get() {
6670
}
6771
item.dataContainer().set(StructuredDataKey.LORE, lore);
6872
}
69-
if (this.glint) {
70-
item.dataContainer().set(StructuredDataKey.ENCHANTMENT_GLINT_OVERRIDE, true);
73+
if (this.glint != null) {
74+
item.dataContainer().set(StructuredDataKey.ENCHANTMENT_GLINT_OVERRIDE, this.glint);
75+
}
76+
for (Map.Entry<StructuredDataKey, Object> entry : this.structuredData.entrySet()) {
77+
item.dataContainer().set(entry.getKey(), entry.getValue());
7178
}
7279
return item;
7380
}

src/main/java/net/lenni0451/miniconnect/server/states/play/screen/Items.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ public class Items {
44

55
public static final String OAK_DOOR = "oak_door";
66
public static final String BARRIER = "barrier";
7-
public static final String WRITABLE_BOOK = "writable_book";
7+
public static final String ANVIL = "anvil";
8+
public static final String WRITTEN_BOOK = "written_book";
89
public static final String NAMETAG = "name_tag";
910
public static final String ARROW = "arrow";
1011
public static final String BEDROCK = "bedrock";
@@ -14,5 +15,6 @@ public class Items {
1415
public static final String GRAY_STAINED_GLASS_PANE = "gray_stained_glass_pane";
1516
public static final String TRIAL_KEY = "trial_key";
1617
public static final String LEVER = "lever";
18+
public static final String ENDER_PEARL = "ender_pearl";
1719

1820
}

src/main/java/net/lenni0451/miniconnect/server/states/play/screen/Screen.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,31 @@
55
public abstract class Screen {
66

77
private final ATextComponent title;
8-
private final int rows;
8+
private final int slotCount;
9+
private final int inventoryType;
910

1011
public Screen(final ATextComponent title, final int rows) {
1112
this.title = title;
12-
this.rows = rows;
13+
this.slotCount = rows * 9;
14+
this.inventoryType = rows - 1;
15+
}
16+
17+
public Screen(final ATextComponent title, final int inventoryType, final int slotCount) {
18+
this.title = title;
19+
this.slotCount = slotCount;
20+
this.inventoryType = inventoryType;
1321
}
1422

1523
public ATextComponent getTitle() {
1624
return this.title;
1725
}
1826

19-
public int getRows() {
20-
return this.rows;
27+
public int getSlotCount() {
28+
return this.slotCount;
29+
}
30+
31+
public int getInventoryType() {
32+
return this.inventoryType;
2133
}
2234

2335
public abstract void init(final ScreenHandler screenHandler, final ItemList itemList);

src/main/java/net/lenni0451/miniconnect/server/states/play/screen/ScreenHandler.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,18 @@ public StateHandler getStateHandler() {
2424
}
2525

2626
public void openScreen(final Screen screen) {
27-
this.currentItemList = new ItemList(screen.getRows() * 9);
27+
if (this.currentScreen != null) this.stateHandler.getHandlerManager().unregister(this.currentScreen);
28+
this.currentItemList = new ItemList(screen.getSlotCount());
2829
screen.init(this, this.currentItemList);
29-
this.stateHandler.send(new S2COpenScreenPacket(1, screen.getRows() - 1, screen.getTitle()));
30+
this.stateHandler.send(new S2COpenScreenPacket(1, screen.getInventoryType(), screen.getTitle()));
3031
this.stateHandler.send(new S2CContainerSetContentPacket(1, 0, this.currentItemList.getItems(), StructuredItem.empty()));
3132
this.currentScreen = screen;
33+
this.stateHandler.getHandlerManager().register(this.currentScreen);
3234
}
3335

3436
public void closeScreen() {
37+
if (this.currentScreen == null) return;
38+
this.stateHandler.getHandlerManager().unregister(this.currentScreen);
3539
this.currentScreen = null;
3640
this.stateHandler.send(new S2CContainerClosePacket(1));
3741
}
@@ -48,6 +52,7 @@ public void handle(final C2SContainerClickPacket packet) {
4852
@EventHandler
4953
public void handle(final C2SContainerClosePacket packet) {
5054
Screen currentScreen = this.currentScreen;
55+
this.stateHandler.getHandlerManager().unregister(this.currentScreen);
5156
this.currentScreen = null; //First set the screen to null because the close logic could open a new screen
5257
currentScreen.close(this);
5358
}

0 commit comments

Comments
 (0)