Skip to content

Commit

Permalink
More formatting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Camotoy committed Mar 13, 2021
1 parent 1cdd66a commit 48aa586
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ public NbtMap getLecternDataAt(GeyserSession session, int x, int y, int z, boole
NbtMap blockEntityTag = lecternTag.build();
BlockEntityUtils.updateBlockEntity(session, blockEntityTag, Vector3i.from(x, y, z));
};

if (isChunkLoad) {
// Delay to ensure the chunk is sent first, and then the lectern data
Bukkit.getScheduler().runTaskLater(this.plugin, lecternInfoGet, 5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public GeyserItemStack(int javaId, int amount, CompoundTag nbt, int netId) {
this.netId = netId;
}

public static GeyserItemStack from(ItemStack itemStack) {
return itemStack == null ? EMPTY : new GeyserItemStack(itemStack.getId(), itemStack.getAmount(), itemStack.getNbt());
}

public int getJavaId() {
return isEmpty() ? 0 : javaId;
}
Expand All @@ -74,10 +78,6 @@ public CompoundTag getNbt() {
return isEmpty() ? null : nbt;
}

public void setNetId(int netId) {
this.netId = netId;
}

public int getNetId() {
return isEmpty() ? 0 : netId;
}
Expand All @@ -90,10 +90,6 @@ public void sub(int sub) {
amount -= sub;
}

public static GeyserItemStack from(ItemStack itemStack) {
return itemStack == null ? EMPTY : new GeyserItemStack(itemStack.getId(), itemStack.getAmount(), itemStack.getNbt());
}

public ItemStack getItemStack() {
return getItemStack(amount);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public void translate(LecternUpdatePacket packet, GeyserSession session) {
session.getConnector().getLogger().debug("Expected lectern but it wasn't open!");
return;
}

LecternContainer lecternContainer = (LecternContainer) session.getOpenInventory();
if (lecternContainer.getCurrentBedrockPage() == packet.getPage()) {
// The same page means Bedrock is closing the window
Expand All @@ -76,6 +77,7 @@ public void translate(LecternUpdatePacket packet, GeyserSession session) {
// Each "page" on Java is just one page (think a spiral notebook folded back to only show one page)
int newJavaPage = (packet.getPage() * 2);
int currentJavaPage = (lecternContainer.getCurrentBedrockPage() * 2);

// Send as many click button packets as we need to
// Java has the option to specify exact page numbers by adding 100 to the number, but buttonId variable
// is a byte when transmitted over the network and therefore this stops us at 128
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ else if (javaIdentifierStripped.equals("bowl")) {
if (ridingEntity instanceof AbstractHorseEntity) {
if (ridingEntity.getMetadata().getFlags().getFlag(EntityFlag.TAMED)) {
// We should request to open the horse inventory instead
ClientPlayerStatePacket openHorseWindowPacket = new ClientPlayerStatePacket((int)session.getPlayerEntity().getEntityId(), PlayerState.OPEN_HORSE_INVENTORY);
ClientPlayerStatePacket openHorseWindowPacket = new ClientPlayerStatePacket((int) session.getPlayerEntity().getEntityId(), PlayerState.OPEN_HORSE_INVENTORY);
session.sendDownstreamPacket(openHorseWindowPacket);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.nukkitx.protocol.bedrock.packet.ItemStackResponsePacket;
import it.unimi.dsi.fastutil.ints.*;
import lombok.AllArgsConstructor;
import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.inventory.*;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.inventory.click.Click;
Expand Down Expand Up @@ -171,9 +172,12 @@ public void translateRequests(GeyserSession session, Inventory inventory, List<I
} else {
response = rejectRequest(request);
}
if (response.getResult() == ItemStackResponsePacket.ResponseStatus.ERROR) {

if (response.getResult() != ItemStackResponsePacket.ResponseStatus.OK) {
// Sync our copy of the inventory with Bedrock's to prevent desyncs
refresh = true;
}

responsePacket.getEntries().add(response);
}
session.sendUpstreamPacket(responsePacket);
Expand All @@ -198,11 +202,10 @@ public ItemStackResponsePacket.Response translateRequest(GeyserSession session,
transferAction.getSource().getSlot() >= 28 && transferAction.getSource().getSlot() <= 31) {
return rejectRequest(request, false);
}
session.getConnector().getLogger().error("DEBUG: About to reject TAKE/PLACE request made by " + session.getName());
session.getConnector().getLogger().error("Source: " + transferAction.getSource().toString() + " Result: " + checkNetId(session, inventory, transferAction.getSource()));
session.getConnector().getLogger().error("Destination: " + transferAction.getDestination().toString() + " Result: " + checkNetId(session, inventory, transferAction.getDestination()));
session.getConnector().getLogger().error("Geyser's record of source slot: " + inventory.getItem(bedrockSlotToJava(transferAction.getSource())));
session.getConnector().getLogger().error("Geyser's record of destination slot: " + inventory.getItem(bedrockSlotToJava(transferAction.getDestination())));
if (session.getConnector().getConfig().isDebugMode()) {
session.getConnector().getLogger().error("DEBUG: About to reject TAKE/PLACE request made by " + session.getName());
dumpStackRequestDetails(session, inventory, transferAction.getSource(), transferAction.getDestination());
}
return rejectRequest(request);
}

Expand Down Expand Up @@ -285,11 +288,10 @@ public ItemStackResponsePacket.Response translateRequest(GeyserSession session,
case SWAP: {
SwapStackRequestActionData swapAction = (SwapStackRequestActionData) action;
if (!(checkNetId(session, inventory, swapAction.getSource()) && checkNetId(session, inventory, swapAction.getDestination()))) {
session.getConnector().getLogger().error("DEBUG: About to reject SWAP request made by " + session.getName());
session.getConnector().getLogger().error("Source: " + swapAction.getSource().toString() + " Result: " + checkNetId(session, inventory, swapAction.getSource()));
session.getConnector().getLogger().error("Destination: " + swapAction.getDestination().toString() + " Result: " + checkNetId(session, inventory, swapAction.getDestination()));
session.getConnector().getLogger().error("Geyser's record of source slot: " + inventory.getItem(bedrockSlotToJava(swapAction.getSource())));
session.getConnector().getLogger().error("Geyser's record of destination slot: " + inventory.getItem(bedrockSlotToJava(swapAction.getDestination())));
if (session.getConnector().getConfig().isDebugMode()) {
session.getConnector().getLogger().error("DEBUG: About to reject SWAP request made by " + session.getName());
dumpStackRequestDetails(session, inventory, swapAction.getSource(), swapAction.getDestination());
}
return rejectRequest(request);
}

Expand Down Expand Up @@ -756,13 +758,22 @@ public static ItemStackResponsePacket.Response rejectRequest(ItemStackRequest re
* as bad (false).
*/
public static ItemStackResponsePacket.Response rejectRequest(ItemStackRequest request, boolean throwError) {
if (throwError) {
// Currently for debugging, but might be worth it to keep in the future if something goes terribly wrong.
if (throwError && GeyserConnector.getInstance().getConfig().isDebugMode()) {
new Throwable("DEBUGGING: ItemStackRequest rejected " + request.toString()).printStackTrace();
}
return new ItemStackResponsePacket.Response(ItemStackResponsePacket.ResponseStatus.ERROR, request.getRequestId(), Collections.emptyList());
}

/**
* Print out the contents of an ItemStackRequest, should the net ID check fail.
*/
protected void dumpStackRequestDetails(GeyserSession session, Inventory inventory, StackRequestSlotInfoData source, StackRequestSlotInfoData destination) {
session.getConnector().getLogger().error("Source: " + source.toString() + " Result: " + checkNetId(session, inventory, source));
session.getConnector().getLogger().error("Destination: " + destination.toString() + " Result: " + checkNetId(session, inventory, destination));
session.getConnector().getLogger().error("Geyser's record of source slot: " + inventory.getItem(bedrockSlotToJava(source)));
session.getConnector().getLogger().error("Geyser's record of destination slot: " + inventory.getItem(bedrockSlotToJava(destination)));
}

public boolean checkNetId(GeyserSession session, Inventory inventory, StackRequestSlotInfoData slotInfoData) {
int netId = slotInfoData.getStackNetworkId();
// "In my testing, sometimes the client thinks the netId of an item in the crafting grid is 1, even though we never said it was.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ public CraftingInventoryTranslator() {

@Override
public SlotType getSlotType(int javaSlot) {
if (javaSlot == 0)
if (javaSlot == 0) {
return SlotType.OUTPUT;
}
return SlotType.NORMAL;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,4 @@ public boolean updateSlot(InventoryTranslator translator, GeyserSession session,
session.sendUpstreamPacket(slotPacket);
return true;
}

}

0 comments on commit 48aa586

Please sign in to comment.