Cleanup and fixes

This commit is contained in:
AJ Ferguson 2019-12-01 00:26:11 -09:00
parent ba0b898da9
commit d07404406b
10 changed files with 108 additions and 87 deletions

View file

@ -55,6 +55,7 @@ import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.PacketTranslator; import org.geysermc.connector.network.translators.PacketTranslator;
import org.geysermc.connector.network.translators.TranslatorsInit; import org.geysermc.connector.network.translators.TranslatorsInit;
import org.geysermc.connector.network.translators.inventory.InventoryTranslator; import org.geysermc.connector.network.translators.inventory.InventoryTranslator;
import org.geysermc.connector.network.translators.inventory.SlotType;
import java.util.*; import java.util.*;
@ -237,8 +238,19 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator<Inve
if (containerAction != null) { if (containerAction != null) {
//left/right click //left/right click
List<ClickAction> plan = new ArrayList<>(); List<ClickAction> plan = new ArrayList<>();
ItemStack translatedCursor = TranslatorsInit.getItemTranslator().translateToJava(cursorAction.getFromItem()); ItemStack translatedCursor = cursorAction.getFromItem().isValid() ?
boolean refresh = !Objects.equals(session.getInventory().getCursor(), translatedCursor.getId() == 0 ? null : translatedCursor); //refresh slot if there is a cursor mismatch TranslatorsInit.getItemTranslator().translateToJava(cursorAction.getFromItem()) : null;
ItemStack currentCursor = session.getInventory().getCursor();
boolean refresh = false;
if (currentCursor != null) {
if (translatedCursor != null) {
refresh = !(currentCursor.getId() == translatedCursor.getId() &&
currentCursor.getAmount() == translatedCursor.getAmount());
} else {
refresh = true;
}
}
int javaSlot = translator.bedrockSlotToJava(containerAction); int javaSlot = translator.bedrockSlotToJava(containerAction);
if (cursorAction.getFromItem().equals(containerAction.getToItem()) && if (cursorAction.getFromItem().equals(containerAction.getToItem()) &&
containerAction.getFromItem().equals(cursorAction.getToItem()) && containerAction.getFromItem().equals(cursorAction.getToItem()) &&
@ -258,7 +270,7 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator<Inve
if (containerAction.getToItem().getCount() == 0) { //pickup all if (containerAction.getToItem().getCount() == 0) { //pickup all
Click.LEFT.onSlot(javaSlot, plan); Click.LEFT.onSlot(javaSlot, plan);
} else { //pickup some } else { //pickup some
if (translator.isOutputSlot(javaSlot) || if (translator.getSlotType(javaSlot) == SlotType.FURNACE_OUTPUT ||
containerAction.getToItem().getCount() == containerAction.getFromItem().getCount() / 2) { //right click containerAction.getToItem().getCount() == containerAction.getFromItem().getCount() / 2) { //right click
Click.RIGHT.onSlot(javaSlot, plan); Click.RIGHT.onSlot(javaSlot, plan);
} else { } else {
@ -270,7 +282,7 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator<Inve
} }
} }
} else { //pickup into non-empty cursor } else { //pickup into non-empty cursor
if (translator.isOutputSlot(javaSlot)) { if (translator.getSlotType(javaSlot) == SlotType.FURNACE_OUTPUT) {
if (containerAction.getToItem().getCount() == 0) { if (containerAction.getToItem().getCount() == 0) {
Click.LEFT.onSlot(javaSlot, plan); Click.LEFT.onSlot(javaSlot, plan);
} else { } else {
@ -282,7 +294,7 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator<Inve
translator.updateInventory(session, inventory); translator.updateInventory(session, inventory);
return; return;
} }
} else if ((inventory.getId() == 0 || inventory.getWindowType() == WindowType.CRAFTING) && javaSlot == 0) { //crafting output } else if (translator.getSlotType(javaSlot) == SlotType.OUTPUT) {
Click.LEFT.onSlot(javaSlot, plan); Click.LEFT.onSlot(javaSlot, plan);
} else { } else {
int cursorSlot = findTempSlot(inventory, session.getInventory().getCursor(), Collections.singletonList(javaSlot)); int cursorSlot = findTempSlot(inventory, session.getInventory().getCursor(), Collections.singletonList(javaSlot));
@ -319,7 +331,7 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator<Inve
int fromSlot = translator.bedrockSlotToJava(fromAction); int fromSlot = translator.bedrockSlotToJava(fromAction);
int toSlot = translator.bedrockSlotToJava(toAction); int toSlot = translator.bedrockSlotToJava(toAction);
if ((inventory.getId() == 0 || inventory.getWindowType() == WindowType.CRAFTING) && fromSlot == 0) { if (translator.getSlotType(fromSlot) == SlotType.OUTPUT) {
if ((craftSlot != 0 && craftSlot != -2) && (inventory.getItem(toSlot) == null || if ((craftSlot != 0 && craftSlot != -2) && (inventory.getItem(toSlot) == null ||
canStack(session.getInventory().getCursor(), inventory.getItem(toSlot)))) { canStack(session.getInventory().getCursor(), inventory.getItem(toSlot)))) {
boolean refresh = false; boolean refresh = false;
@ -360,7 +372,7 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator<Inve
Click.LEFT.onSlot(fromSlot, plan); Click.LEFT.onSlot(fromSlot, plan);
} }
} else if (canStack(fromAction.getFromItem(), toAction.getToItem())) { //partial item move } else if (canStack(fromAction.getFromItem(), toAction.getToItem())) { //partial item move
if (translator.isOutputSlot(fromSlot)) { if (translator.getSlotType(fromSlot) == SlotType.FURNACE_OUTPUT) {
ClientWindowActionPacket shiftClickPacket = new ClientWindowActionPacket(inventory.getId(), ClientWindowActionPacket shiftClickPacket = new ClientWindowActionPacket(inventory.getId(),
inventory.getTransactionId().getAndIncrement(), inventory.getTransactionId().getAndIncrement(),
fromSlot, refreshItem, WindowAction.SHIFT_CLICK_ITEM, fromSlot, refreshItem, WindowAction.SHIFT_CLICK_ITEM,
@ -368,7 +380,7 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator<Inve
session.getDownstream().getSession().send(shiftClickPacket); session.getDownstream().getSession().send(shiftClickPacket);
translator.updateInventory(session, inventory); translator.updateInventory(session, inventory);
return; return;
} else if ((inventory.getId() == 0 || inventory.getWindowType() == WindowType.CRAFTING) && fromSlot == 0) { } else if (translator.getSlotType(fromSlot) == SlotType.OUTPUT) {
session.setCraftSlot(cursorSlot); session.setCraftSlot(cursorSlot);
Click.LEFT.onSlot(fromSlot, plan); Click.LEFT.onSlot(fromSlot, plan);
int difference = toAction.getToItem().getCount() - toAction.getFromItem().getCount(); int difference = toAction.getToItem().getCount() - toAction.getFromItem().getCount();
@ -477,7 +489,7 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator<Inve
private boolean canStack(ItemStack item1, ItemStack item2) { private boolean canStack(ItemStack item1, ItemStack item2) {
if (item1 == null || item2 == null) if (item1 == null || item2 == null)
return false; return false;
return item1.getId() == item2.getId() && item1.getNbt() == item2.getNbt(); return item1.getId() == item2.getId() && Objects.equals(item1.getNbt(), item2.getNbt());
} }
private boolean canStack(ItemData item1, ItemData item2) { private boolean canStack(ItemData item1, ItemData item2) {
@ -494,13 +506,15 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator<Inve
ItemStack cursorItem = playerInventory.getCursor(); ItemStack cursorItem = playerInventory.getCursor();
ItemStack clickedItem = inventory.getItem(action.slot); ItemStack clickedItem = inventory.getItem(action.slot);
short actionId = (short) inventory.getTransactionId().getAndIncrement(); short actionId = (short) inventory.getTransactionId().getAndIncrement();
boolean craftingOutput = (inventory.getId() == 0 || inventory.getWindowType() == WindowType.CRAFTING) && action.slot == 0; boolean isOutput = translator.getSlotType(action.slot) == SlotType.OUTPUT;
if (craftingOutput || translator.isOutputSlot(action.slot))
if (isOutput || translator.getSlotType(action.slot) == SlotType.FURNACE_OUTPUT)
refresh = true; refresh = true;
ClientWindowActionPacket clickPacket = new ClientWindowActionPacket(inventory.getId(), ClientWindowActionPacket clickPacket = new ClientWindowActionPacket(inventory.getId(),
actionId, action.slot, !planIter.hasNext() && refresh ? refreshItem : fixStack(clickedItem), actionId, action.slot, !planIter.hasNext() && refresh ? refreshItem : fixStack(clickedItem),
WindowAction.CLICK_ITEM, action.click.actionParam); WindowAction.CLICK_ITEM, action.click.actionParam);
if (craftingOutput) { //crafting output
if (isOutput) {
if (cursorItem == null && clickedItem != null) { if (cursorItem == null && clickedItem != null) {
playerInventory.setCursor(clickedItem); playerInventory.setCursor(clickedItem);
} else if (canStack(cursorItem, clickedItem)) { } else if (canStack(cursorItem, clickedItem)) {

View file

@ -28,14 +28,6 @@ package org.geysermc.connector.network.translators.inventory;
import com.nukkitx.protocol.bedrock.data.ContainerId; import com.nukkitx.protocol.bedrock.data.ContainerId;
import com.nukkitx.protocol.bedrock.data.ContainerType; import com.nukkitx.protocol.bedrock.data.ContainerType;
import com.nukkitx.protocol.bedrock.data.InventoryAction; import com.nukkitx.protocol.bedrock.data.InventoryAction;
import com.nukkitx.protocol.bedrock.data.ItemData;
import com.nukkitx.protocol.bedrock.packet.InventoryContentPacket;
import com.nukkitx.protocol.bedrock.packet.InventorySlotPacket;
import org.geysermc.connector.inventory.Inventory;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.TranslatorsInit;
import java.util.Arrays;
public class AnvilInventoryTranslator extends BlockInventoryTranslator { public class AnvilInventoryTranslator extends BlockInventoryTranslator {
public AnvilInventoryTranslator() { public AnvilInventoryTranslator() {
@ -44,31 +36,23 @@ public class AnvilInventoryTranslator extends BlockInventoryTranslator {
@Override @Override
public int bedrockSlotToJava(InventoryAction action) { public int bedrockSlotToJava(InventoryAction action) {
int slotnum = action.getSlot(); if (action.getSource().getContainerId() == ContainerId.CURSOR) {
if (action.getSource().getContainerId() == ContainerId.INVENTORY) { switch (action.getSlot()) {
//hotbar case 1:
if (slotnum >= 9) { return 0;
return slotnum + this.size - 9; case 2:
} else { return 1;
return slotnum + this.size + 27; case 50:
} return 2;
} else {
if (action.getSource().getContainerId() == ContainerId.CURSOR) {
switch (slotnum) {
case 1:
return 0;
case 2:
return 1;
case 50:
return 2;
}
} }
} }
return slotnum; return super.bedrockSlotToJava(action);
} }
@Override @Override
public boolean isOutputSlot(int slot) { public SlotType getSlotType(int javaSlot) {
return slot == 2; if (javaSlot == 2)
return SlotType.OUTPUT;
return SlotType.NORMAL;
} }
} }

View file

@ -38,8 +38,8 @@ import org.geysermc.connector.network.translators.block.BlockEntry;
import org.geysermc.connector.world.GlobalBlockPalette; import org.geysermc.connector.world.GlobalBlockPalette;
public class BlockInventoryTranslator extends ContainerInventoryTranslator { public class BlockInventoryTranslator extends ContainerInventoryTranslator {
protected final int blockId; final int blockId;
protected final ContainerType containerType; private final ContainerType containerType;
public BlockInventoryTranslator(int size, int blockId, ContainerType containerType) { public BlockInventoryTranslator(int size, int blockId, ContainerType containerType) {
super(size); super(size);

View file

@ -25,7 +25,6 @@
package org.geysermc.connector.network.translators.inventory; package org.geysermc.connector.network.translators.inventory;
import com.nukkitx.protocol.bedrock.data.ContainerId;
import com.nukkitx.protocol.bedrock.data.ContainerType; import com.nukkitx.protocol.bedrock.data.ContainerType;
import com.nukkitx.protocol.bedrock.data.InventoryAction; import com.nukkitx.protocol.bedrock.data.InventoryAction;
import com.nukkitx.protocol.bedrock.packet.ContainerSetDataPacket; import com.nukkitx.protocol.bedrock.packet.ContainerSetDataPacket;
@ -58,27 +57,18 @@ public class BrewingStandInventoryTranslator extends BlockInventoryTranslator {
@Override @Override
public int bedrockSlotToJava(InventoryAction action) { public int bedrockSlotToJava(InventoryAction action) {
int slotnum = action.getSlot(); int slotnum = super.bedrockSlotToJava(action);
if (action.getSource().getContainerId() == ContainerId.INVENTORY) { switch (slotnum) {
//hotbar case 0:
if (slotnum >= 9) { return 3;
return slotnum + this.size - 9; case 1:
} else { return 0;
return slotnum + this.size + 27; case 2:
} return 1;
} else { case 3:
switch (slotnum) { return 2;
case 0: default:
return 3; return slotnum;
case 1:
return 0;
case 2:
return 1;
case 3:
return 2;
default:
return slotnum;
}
} }
} }

View file

@ -35,7 +35,7 @@ import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.TranslatorsInit; import org.geysermc.connector.network.translators.TranslatorsInit;
public abstract class ContainerInventoryTranslator extends InventoryTranslator { public abstract class ContainerInventoryTranslator extends InventoryTranslator {
public ContainerInventoryTranslator(int size) { ContainerInventoryTranslator(int size) {
super(size); super(size);
} }
@ -97,7 +97,7 @@ public abstract class ContainerInventoryTranslator extends InventoryTranslator {
} }
@Override @Override
public boolean isOutputSlot(int slot) { public SlotType getSlotType(int javaSlot) {
return false; return SlotType.NORMAL;
} }
} }

View file

@ -59,21 +59,21 @@ public class CraftingTableInventoryTranslator extends ContainerInventoryTranslat
@Override @Override
public int bedrockSlotToJava(InventoryAction action) { public int bedrockSlotToJava(InventoryAction action) {
int slotnum = action.getSlot(); if (action.getSource().getContainerId() == ContainerId.CURSOR) {
if (action.getSource().getContainerId() == ContainerId.INVENTORY) { int slotnum = action.getSlot();
//hotbar
if (slotnum >= 9) {
return slotnum + this.size - 9;
} else {
return slotnum + this.size + 27;
}
} else {
if (slotnum >= 32 && 42 >= slotnum) { if (slotnum >= 32 && 42 >= slotnum) {
return slotnum - 31; return slotnum - 31;
} else if (slotnum == 50) { } else if (slotnum == 50) {
return 0; return 0;
} }
return slotnum;
} }
return super.bedrockSlotToJava(action);
}
@Override
public SlotType getSlotType(int javaSlot) {
if (javaSlot == 0)
return SlotType.OUTPUT;
return SlotType.NORMAL;
} }
} }

View file

@ -26,7 +26,6 @@
package org.geysermc.connector.network.translators.inventory; package org.geysermc.connector.network.translators.inventory;
import com.nukkitx.protocol.bedrock.data.ContainerType; import com.nukkitx.protocol.bedrock.data.ContainerType;
import com.nukkitx.protocol.bedrock.data.InventoryAction;
import com.nukkitx.protocol.bedrock.packet.ContainerSetDataPacket; import com.nukkitx.protocol.bedrock.packet.ContainerSetDataPacket;
import org.geysermc.connector.inventory.Inventory; import org.geysermc.connector.inventory.Inventory;
import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.session.GeyserSession;
@ -59,7 +58,9 @@ public class FurnaceInventoryTranslator extends BlockInventoryTranslator {
} }
@Override @Override
public boolean isOutputSlot(int slot) { public SlotType getSlotType(int javaSlot) {
return slot == 2; if (javaSlot == 2)
return SlotType.FURNACE_OUTPUT;
return SlotType.NORMAL;
} }
} }

View file

@ -44,5 +44,5 @@ public abstract class InventoryTranslator {
public abstract void updateSlot(GeyserSession session, Inventory inventory, int slot); public abstract void updateSlot(GeyserSession session, Inventory inventory, int slot);
public abstract int bedrockSlotToJava(InventoryAction action); public abstract int bedrockSlotToJava(InventoryAction action);
public abstract int javaSlotToBedrock(int slot); public abstract int javaSlotToBedrock(int slot);
public abstract boolean isOutputSlot(int slot); public abstract SlotType getSlotType(int javaSlot);
} }

View file

@ -119,15 +119,13 @@ public class PlayerInventoryTranslator extends InventoryTranslator {
break; break;
case ContainerId.OFFHAND: case ContainerId.OFFHAND:
return 45; return 45;
case ContainerId.CRAFTING_ADD_INGREDIENT:
case ContainerId.CRAFTING_REMOVE_INGREDIENT:
return slotnum + 1;
case ContainerId.CURSOR: case ContainerId.CURSOR:
if (slotnum >= 28 && 31 >= slotnum) { if (slotnum >= 28 && 31 >= slotnum) {
return slotnum - 27; return slotnum - 27;
} else if (slotnum == 50) { } else if (slotnum == 50) {
return 0; return 0;
} }
break;
} }
return slotnum; return slotnum;
} }
@ -138,8 +136,10 @@ public class PlayerInventoryTranslator extends InventoryTranslator {
} }
@Override @Override
public boolean isOutputSlot(int slot) { public SlotType getSlotType(int javaSlot) {
return false; if (javaSlot == 0)
return SlotType.OUTPUT;
return SlotType.NORMAL;
} }
@Override @Override

View file

@ -0,0 +1,32 @@
/*
* Copyright (c) 2019 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.connector.network.translators.inventory;
public enum SlotType {
NORMAL,
OUTPUT,
FURNACE_OUTPUT
}