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.TranslatorsInit;
import org.geysermc.connector.network.translators.inventory.InventoryTranslator;
import org.geysermc.connector.network.translators.inventory.SlotType;
import java.util.*;
@ -237,8 +238,19 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator<Inve
if (containerAction != null) {
//left/right click
List<ClickAction> plan = new ArrayList<>();
ItemStack translatedCursor = TranslatorsInit.getItemTranslator().translateToJava(cursorAction.getFromItem());
boolean refresh = !Objects.equals(session.getInventory().getCursor(), translatedCursor.getId() == 0 ? null : translatedCursor); //refresh slot if there is a cursor mismatch
ItemStack translatedCursor = cursorAction.getFromItem().isValid() ?
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);
if (cursorAction.getFromItem().equals(containerAction.getToItem()) &&
containerAction.getFromItem().equals(cursorAction.getToItem()) &&
@ -258,7 +270,7 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator<Inve
if (containerAction.getToItem().getCount() == 0) { //pickup all
Click.LEFT.onSlot(javaSlot, plan);
} else { //pickup some
if (translator.isOutputSlot(javaSlot) ||
if (translator.getSlotType(javaSlot) == SlotType.FURNACE_OUTPUT ||
containerAction.getToItem().getCount() == containerAction.getFromItem().getCount() / 2) { //right click
Click.RIGHT.onSlot(javaSlot, plan);
} else {
@ -270,7 +282,7 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator<Inve
}
}
} else { //pickup into non-empty cursor
if (translator.isOutputSlot(javaSlot)) {
if (translator.getSlotType(javaSlot) == SlotType.FURNACE_OUTPUT) {
if (containerAction.getToItem().getCount() == 0) {
Click.LEFT.onSlot(javaSlot, plan);
} else {
@ -282,7 +294,7 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator<Inve
translator.updateInventory(session, inventory);
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);
} else {
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 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 ||
canStack(session.getInventory().getCursor(), inventory.getItem(toSlot)))) {
boolean refresh = false;
@ -360,7 +372,7 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator<Inve
Click.LEFT.onSlot(fromSlot, plan);
}
} 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(),
inventory.getTransactionId().getAndIncrement(),
fromSlot, refreshItem, WindowAction.SHIFT_CLICK_ITEM,
@ -368,7 +380,7 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator<Inve
session.getDownstream().getSession().send(shiftClickPacket);
translator.updateInventory(session, inventory);
return;
} else if ((inventory.getId() == 0 || inventory.getWindowType() == WindowType.CRAFTING) && fromSlot == 0) {
} else if (translator.getSlotType(fromSlot) == SlotType.OUTPUT) {
session.setCraftSlot(cursorSlot);
Click.LEFT.onSlot(fromSlot, plan);
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) {
if (item1 == null || item2 == null)
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) {
@ -494,13 +506,15 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator<Inve
ItemStack cursorItem = playerInventory.getCursor();
ItemStack clickedItem = inventory.getItem(action.slot);
short actionId = (short) inventory.getTransactionId().getAndIncrement();
boolean craftingOutput = (inventory.getId() == 0 || inventory.getWindowType() == WindowType.CRAFTING) && action.slot == 0;
if (craftingOutput || translator.isOutputSlot(action.slot))
boolean isOutput = translator.getSlotType(action.slot) == SlotType.OUTPUT;
if (isOutput || translator.getSlotType(action.slot) == SlotType.FURNACE_OUTPUT)
refresh = true;
ClientWindowActionPacket clickPacket = new ClientWindowActionPacket(inventory.getId(),
actionId, action.slot, !planIter.hasNext() && refresh ? refreshItem : fixStack(clickedItem),
WindowAction.CLICK_ITEM, action.click.actionParam);
if (craftingOutput) { //crafting output
if (isOutput) {
if (cursorItem == null && clickedItem != null) {
playerInventory.setCursor(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.ContainerType;
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 AnvilInventoryTranslator() {
@ -44,17 +36,8 @@ public class AnvilInventoryTranslator extends BlockInventoryTranslator {
@Override
public int bedrockSlotToJava(InventoryAction action) {
int slotnum = action.getSlot();
if (action.getSource().getContainerId() == ContainerId.INVENTORY) {
//hotbar
if (slotnum >= 9) {
return slotnum + this.size - 9;
} else {
return slotnum + this.size + 27;
}
} else {
if (action.getSource().getContainerId() == ContainerId.CURSOR) {
switch (slotnum) {
switch (action.getSlot()) {
case 1:
return 0;
case 2:
@ -63,12 +46,13 @@ public class AnvilInventoryTranslator extends BlockInventoryTranslator {
return 2;
}
}
}
return slotnum;
return super.bedrockSlotToJava(action);
}
@Override
public boolean isOutputSlot(int slot) {
return slot == 2;
public SlotType getSlotType(int javaSlot) {
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;
public class BlockInventoryTranslator extends ContainerInventoryTranslator {
protected final int blockId;
protected final ContainerType containerType;
final int blockId;
private final ContainerType containerType;
public BlockInventoryTranslator(int size, int blockId, ContainerType containerType) {
super(size);

View file

@ -25,7 +25,6 @@
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.InventoryAction;
import com.nukkitx.protocol.bedrock.packet.ContainerSetDataPacket;
@ -58,15 +57,7 @@ public class BrewingStandInventoryTranslator extends BlockInventoryTranslator {
@Override
public int bedrockSlotToJava(InventoryAction action) {
int slotnum = action.getSlot();
if (action.getSource().getContainerId() == ContainerId.INVENTORY) {
//hotbar
if (slotnum >= 9) {
return slotnum + this.size - 9;
} else {
return slotnum + this.size + 27;
}
} else {
int slotnum = super.bedrockSlotToJava(action);
switch (slotnum) {
case 0:
return 3;
@ -80,7 +71,6 @@ public class BrewingStandInventoryTranslator extends BlockInventoryTranslator {
return slotnum;
}
}
}
@Override
public int javaSlotToBedrock(int slotnum) {

View file

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

View file

@ -59,21 +59,21 @@ public class CraftingTableInventoryTranslator extends ContainerInventoryTranslat
@Override
public int bedrockSlotToJava(InventoryAction action) {
if (action.getSource().getContainerId() == ContainerId.CURSOR) {
int slotnum = action.getSlot();
if (action.getSource().getContainerId() == ContainerId.INVENTORY) {
//hotbar
if (slotnum >= 9) {
return slotnum + this.size - 9;
} else {
return slotnum + this.size + 27;
}
} else {
if (slotnum >= 32 && 42 >= slotnum) {
return slotnum - 31;
} else if (slotnum == 50) {
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;
import com.nukkitx.protocol.bedrock.data.ContainerType;
import com.nukkitx.protocol.bedrock.data.InventoryAction;
import com.nukkitx.protocol.bedrock.packet.ContainerSetDataPacket;
import org.geysermc.connector.inventory.Inventory;
import org.geysermc.connector.network.session.GeyserSession;
@ -59,7 +58,9 @@ public class FurnaceInventoryTranslator extends BlockInventoryTranslator {
}
@Override
public boolean isOutputSlot(int slot) {
return slot == 2;
public SlotType getSlotType(int javaSlot) {
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 int bedrockSlotToJava(InventoryAction action);
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;
case ContainerId.OFFHAND:
return 45;
case ContainerId.CRAFTING_ADD_INGREDIENT:
case ContainerId.CRAFTING_REMOVE_INGREDIENT:
return slotnum + 1;
case ContainerId.CURSOR:
if (slotnum >= 28 && 31 >= slotnum) {
return slotnum - 27;
} else if (slotnum == 50) {
return 0;
}
break;
}
return slotnum;
}
@ -138,8 +136,10 @@ public class PlayerInventoryTranslator extends InventoryTranslator {
}
@Override
public boolean isOutputSlot(int slot) {
return false;
public SlotType getSlotType(int javaSlot) {
if (javaSlot == 0)
return SlotType.OUTPUT;
return SlotType.NORMAL;
}
@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
}