Skip to content
This repository has been archived by the owner on Apr 7, 2021. It is now read-only.

Commit

Permalink
Add simple inventory handling
Browse files Browse the repository at this point in the history
  • Loading branch information
HoverEpic committed Dec 24, 2017
1 parent a10f91c commit 557e35d
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static PEPacket[] sendPlayerInventory(UpstreamSession session) {
public static void open(UpstreamSession session, ServerOpenWindowPacket win) {
closeOpened(session, true);
if (TRANSLATORS.containsKey(win.getType())) {
CachedWindow cached = new CachedWindow(win.getWindowId(), win.getType(), win.getSlots());
CachedWindow cached = new CachedWindow(win.getWindowId(), win.getType(), win.getSlots() + 36/* player inventory */);
session.getWindowCache().cacheWindow(cached);
TRANSLATORS.get(win.getType()).open(session, cached);
com.github.steveice10.packetlib.packet.Packet[] items = session.getWindowCache().getCachedPackets(win.getWindowId());
Expand Down
36 changes: 15 additions & 21 deletions src/main/java/org/dragonet/proxy/network/cache/CachedWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,21 @@
import java.util.Map;

public class CachedWindow {
// vars
public final int windowId;
/**
* The type of this window on remote side, -1 for player inventory.
*/
public final WindowType pcType;
public final int size;
public String title = "Window";
public final Map<Integer, Integer> properties = Collections.synchronizedMap(new HashMap<Integer, Integer>());
public ItemStack[] slots;

// constructor
public CachedWindow(int windowId, WindowType pcType, int size) {
this.windowId = windowId;
this.pcType = pcType;
this.size = size + 36; //add player inventory slots
slots = new ItemStack[this.size];
}

// public

// private
public final int windowId;
/**
* The type of this window on remote side, -1 for player inventory.
*/
public final WindowType pcType;
public final int size;
public String title = "Window";
public final Map<Integer, Integer> properties = Collections.synchronizedMap(new HashMap<Integer, Integer>());
public ItemStack[] slots;

public CachedWindow(int windowId, WindowType pcType, int size) {
this.windowId = windowId;
this.pcType = pcType;
this.size = size;
slots = new ItemStack[this.size];
}
}
105 changes: 52 additions & 53 deletions src/main/java/org/dragonet/proxy/network/cache/WindowCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,71 +19,70 @@
import java.util.Map;

import com.github.steveice10.packetlib.packet.Packet;
import java.util.concurrent.atomic.AtomicInteger;
import org.dragonet.proxy.network.UpstreamSession;

public final class WindowCache {
// vars
private final UpstreamSession upstream;
private final Map<Integer, CachedWindow> windows = Collections
.synchronizedMap(new HashMap<Integer, CachedWindow>());
private final Map<Integer, List<Packet>> cachedItems = Collections
.synchronizedMap(new HashMap<Integer, List<Packet>>());

// constructor
public WindowCache(UpstreamSession upstream) {
this.upstream = upstream;
private final UpstreamSession upstream;
private final Map<Integer, CachedWindow> windows = Collections
.synchronizedMap(new HashMap<Integer, CachedWindow>());
private final Map<Integer, List<Packet>> cachedItems = Collections
.synchronizedMap(new HashMap<Integer, List<Packet>>());

public AtomicInteger currentTransactionId = new AtomicInteger();

CachedWindow inv = new CachedWindow(0, null, 45);
windows.put(0, inv);
}
public WindowCache(UpstreamSession upstream) {
this.upstream = upstream;

// public
public UpstreamSession getUpstream() {
return upstream;
}
CachedWindow inv = new CachedWindow(0, null, 45);
windows.put(0, inv);
}

public CachedWindow getPlayerInventory() {
return windows.get(0);
}
// public
public UpstreamSession getUpstream() {
return upstream;
}

// We do not do translations here, do it in InventoryTranslatorRegister
public void cacheWindow(CachedWindow win) {
windows.put(win.windowId, win);
}
public CachedWindow getPlayerInventory() {
return windows.get(0);
}

public CachedWindow removeWindow(int id) {
return windows.remove(id);
}
// We do not do translations here, do it in InventoryTranslatorRegister
public void cacheWindow(CachedWindow win) {
windows.put(win.windowId, win);
}

public CachedWindow get(int id) {
return windows.get(id);
}
public CachedWindow removeWindow(int id) {
return windows.remove(id);
}

public boolean hasWindow(int id) {
return windows.containsKey(id);
}
public CachedWindow get(int id) {
return windows.get(id);
}

public void newCachedPacket(int windowId, Packet packet) {
List<Packet> packets = null;
synchronized (cachedItems) {
packets = cachedItems.get(windowId);
if (packets == null) {
packets = new ArrayList<>();
cachedItems.put(windowId, packets);
}
}
packets.add(packet);
}
public boolean hasWindow(int id) {
return windows.containsKey(id);
}

public Packet[] getCachedPackets(int windowId) {
List<Packet> packets = null;
packets = cachedItems.remove(windowId);
if (packets == null) {
return null;
}
return packets.toArray(new Packet[0]);
}

// private
public void newCachedPacket(int windowId, Packet packet) {
List<Packet> packets = null;
synchronized (cachedItems) {
packets = cachedItems.get(windowId);
if (packets == null) {
packets = new ArrayList<>();
cachedItems.put(windowId, packets);
}
}
packets.add(packet);
}

public Packet[] getCachedPackets(int windowId) {
List<Packet> packets = null;
packets = cachedItems.remove(windowId);
if (packets == null) {
return null;
}
return packets.toArray(new Packet[0]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ public class ItemBlockTranslator {
swap(447, new ItemEntry(333, 4)); //acacia_boat
swap(448, new ItemEntry(333, 5)); //dark_oak_boat

toPEOverride(449, 450); //totem
toPEOverride(450, 445); //shulker_shell
swap(449, 450); //totem
swap(450, 445); //shulker_shell

swap(2256, 500); //record_13
swap(2257, 501); //record_cat
Expand Down Expand Up @@ -340,18 +340,8 @@ public static org.dragonet.proxy.data.nbt.tag.CompoundTag translateBlockEntityTo
}

public static ItemStack translateToPC(Slot slot) {
ItemStack item;
org.dragonet.proxy.data.nbt.tag.CompoundTag tag = slot.tag;
if (tag != null && tag.contains(DRAGONET_COMPOUND)) {
item = new ItemStack(tag.getCompound(DRAGONET_COMPOUND).getShort("id"),
tag.getCompound(DRAGONET_COMPOUND).getShort("amount"),
tag.getCompound(DRAGONET_COMPOUND).getShort("data"));
} else {
ItemEntry entry = translateToPC(slot.id, slot.damage);
item = new ItemStack(entry.id, slot.count, entry.damage != null ? entry.damage : slot.damage);
}

return item;
ItemEntry entry = translateToPC(slot.id, slot.damage);
return new ItemStack(entry.id, slot.count, entry.damage != null ? entry.damage : slot.damage); //TODO NBT
}

public static BlockFace translateToPC(int face) {
Expand Down Expand Up @@ -383,8 +373,8 @@ private static void toPEOverride(int fromPc, ItemEntry toPe) {
PC_TO_PE_OVERRIDE.put(fromPc, toPe);
}

private static void toPCOverride(int fromEc, int toPc) {
PE_TO_PC_OVERRIDE.put(fromEc, new ItemEntry(toPc));
private static void toPCOverride(int fromPc, int toPc) {
PE_TO_PC_OVERRIDE.put(fromPc, new ItemEntry(toPc));
}

private static void toPCOverride(int fromPc, ItemEntry toPe) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position;
import com.github.steveice10.mc.protocol.data.game.entity.player.Hand;
import com.github.steveice10.mc.protocol.data.game.entity.player.InteractAction;
import com.github.steveice10.mc.protocol.data.game.window.ClickItemParam;
import com.github.steveice10.mc.protocol.data.game.window.DropItemParam;
import com.github.steveice10.mc.protocol.data.game.window.WindowAction;
import com.github.steveice10.mc.protocol.data.game.window.WindowActionParam;
import com.github.steveice10.mc.protocol.data.game.window.WindowType;
import com.github.steveice10.mc.protocol.data.game.world.block.BlockFace;
import com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerActionPacket;
import com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerInteractEntityPacket;
Expand All @@ -22,10 +24,11 @@
import org.dragonet.proxy.network.CacheKey;
import org.dragonet.proxy.network.UpstreamSession;
import org.dragonet.proxy.network.cache.CachedEntity;
import org.dragonet.proxy.network.cache.CachedWindow;
import org.dragonet.proxy.network.cache.WindowCache;
import org.dragonet.proxy.network.translator.IPEPacketTranslator;
import org.dragonet.proxy.network.translator.ItemBlockTranslator;
import org.dragonet.proxy.protocol.packets.InventoryTransactionPacket;
import static org.dragonet.proxy.protocol.packets.InventoryTransactionPacket.*;
import org.dragonet.proxy.protocol.type.InventoryTransactionAction;
import org.dragonet.proxy.protocol.type.Slot;
import org.dragonet.proxy.protocol.type.transaction.data.ReleaseItemData;
Expand All @@ -44,18 +47,22 @@ public class PEInventoryTransactionPacketTranslator implements IPEPacketTranslat
@Override
public Packet[] translate(UpstreamSession session, InventoryTransactionPacket packet) {
//debug
System.out.println(">>>>============================");
System.out.println("InventoryTransactionPacket type: \n" + DebugTools.getAllFields(packet));
System.out.println("-------------");
for (InventoryTransactionAction action : packet.actions) {
System.out.println(DebugTools.getAllFields(action));
}
System.out.println("<<<<============================");
// if (packet.transactionType == InventoryTransactionPacket.TYPE_NORMAL) {
// System.out.println(">>>>============================");
//// System.out.println("InventoryTransactionPacket type: \n" + DebugTools.getAllFields(packet));
//// System.out.println("-------------");
// for (InventoryTransactionAction action : packet.actions) {
// System.out.println(DebugTools.getAllFields(action));
// }
// System.out.println("<<<<============================");
// }

// /!\ THIS IS A SIMPLE HANDLING WITHOUT JAVA DENY TRANSACTION
switch (packet.transactionType) {
case TYPE_NORMAL: //0
case InventoryTransactionPacket.TYPE_NORMAL: //0 INVENTORY & CHEST
System.out.println("TYPE_NORMAL");
if (packet.actions.length <= 2 && packet.actions[0].sourceType == InventoryTransactionAction.SOURCE_WORLD) //main inventory
Slot cursor = null;
if (packet.actions.length <= 2 && packet.actions[0].sourceType == InventoryTransactionAction.SOURCE_WORLD && packet.actions[1].containerId == ContainerId.INVENTORY.getId()) //main inventory
{
// drop item
ClientPlayerActionPacket act = new ClientPlayerActionPacket(
Expand All @@ -66,17 +73,51 @@ public Packet[] translate(UpstreamSession session, InventoryTransactionPacket pa
}
if (packet.actions.length == 2 && packet.actions[0].sourceType == InventoryTransactionAction.SOURCE_CONTAINER && packet.actions[1].containerId == ContainerId.CURSOR.getId()) {
// desktop version: click on an item (maybe pick/place/merge/swap)
}
// after the previous one, we can detect SHIFT click or move items on mobile devices
if (packet.actions.length == 2 && packet.actions[0].sourceType == InventoryTransactionAction.SOURCE_CONTAINER && packet.actions[2].sourceType == InventoryTransactionAction.SOURCE_CONTAINER) {
// mobile version: move item
// desktop version: SHIFT-click item

cursor = packet.actions[1].newItem; //set the cursor
int container = packet.actions[0].containerId;
int slot = packet.actions[0].slotId;
if (container == 0) { // if 0, source is player inv
if (slot > 5 && slot <= 9) {
System.out.println("interact in player inventory (stuff)");
}
if (slot > 9 && slot <= 36) {
System.out.println("interact in player inventory (inventory)");
}
if (slot > 36 && slot <= 45) {
System.out.println("interact in player inventory (hotbar)");
}
container = (int) session.getDataCache().get(CacheKey.WINDOW_OPENED_ID);
slot += 18; //normal chest 0 -> 26 + inv 27 -> 62
}
System.out.println("interact in chest (" + container + ") slot " + slot);

// send action to server
ClientWindowActionPacket windowActionPacket = new ClientWindowActionPacket(
container, //window id
session.getWindowCache().currentTransactionId.incrementAndGet(), //transaction id
slot, //slot
ItemBlockTranslator.translateToPC(cursor),
WindowAction.CLICK_ITEM,
(WindowActionParam) ClickItemParam.LEFT_CLICK
);
session.getDownstream().send(windowActionPacket);

// after the previous one, we can detect SHIFT click or move items on mobile devices
if (packet.actions.length == 2 && packet.actions[0].sourceType == InventoryTransactionAction.SOURCE_CONTAINER && packet.actions[1].sourceType == InventoryTransactionAction.SOURCE_CONTAINER) {
// mobile version: move item
// desktop version: SHIFT-click item
System.out.println("Move item from " + packet.actions[0].containerId + " slot " + packet.actions[0].slotId
+ " to " + packet.actions[1].containerId + " slot " + packet.actions[1].slotId);
}

System.out.println("Cursor set to " + cursor);
}
return null; // it's okay to return null
case TYPE_MISMATCH: //1
case InventoryTransactionPacket.TYPE_MISMATCH: //1
System.out.println("TYPE_MISMATCH");
break;
case TYPE_USE_ITEM: //2
case InventoryTransactionPacket.TYPE_USE_ITEM: //2
System.out.println("TYPE_USE_ITEM");
UseItemData useItemData = (UseItemData) packet.transactionData;
if (useItemData.blockPos.equals(new BlockPosition(0, 0, 0))) {
Expand Down Expand Up @@ -107,7 +148,7 @@ public Packet[] translate(UpstreamSession session, InventoryTransactionPacket pa
}

}
case TYPE_USE_ITEM_ON_ENTITY: //3
case InventoryTransactionPacket.TYPE_USE_ITEM_ON_ENTITY: //3
System.out.println("TYPE_USE_ITEM_ON_ENTITY");
UseItemOnEntityData useItemOnEntityData = (UseItemOnEntityData) packet.transactionData;
CachedEntity cachedEntity = session.getEntityCache().getByLocalEID(useItemOnEntityData.entityRuntimeId);
Expand All @@ -123,7 +164,7 @@ public Packet[] translate(UpstreamSession session, InventoryTransactionPacket pa
interractAction
);
return new Packet[]{interractPacket};
case TYPE_RELEASE_ITEM: //4
case InventoryTransactionPacket.TYPE_RELEASE_ITEM: //4
System.out.println("TYPE_RELEASE_ITEM");
ReleaseItemData releaseItemData = (ReleaseItemData) packet.transactionData;
// ClientPlayerActionPacket act = new ClientPlayerActionPacket(
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/dragonet/proxy/protocol/type/Slot.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,16 @@ public Slot(int id, int damage, int count, CompoundTag tag) {
public Slot clone() {
return new Slot(id, damage, count, tag);
}

public String toString()
{
StringBuffer buffer = new StringBuffer();
buffer.append("Slot{");
buffer.append("id=" + id);
buffer.append(", damage=" + damage);
buffer.append(", count=" + count);
buffer.append(", tag=" + tag);
buffer.append("}");
return buffer.toString();
}
}

0 comments on commit 557e35d

Please sign in to comment.