mirror of
https://github.com/GeyserMC/Geyser.git
synced 2024-08-14 23:57:35 +00:00
Improve creative support
This commit is contained in:
parent
f47cf32d90
commit
c6b4d163a1
3 changed files with 117 additions and 27 deletions
|
@ -31,10 +31,13 @@ import com.github.steveice10.mc.protocol.data.game.window.WindowType;
|
||||||
import com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientCreativeInventoryActionPacket;
|
import com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientCreativeInventoryActionPacket;
|
||||||
import com.nukkitx.protocol.bedrock.data.inventory.ContainerSlotType;
|
import com.nukkitx.protocol.bedrock.data.inventory.ContainerSlotType;
|
||||||
import com.nukkitx.protocol.bedrock.data.inventory.ContainerType;
|
import com.nukkitx.protocol.bedrock.data.inventory.ContainerType;
|
||||||
|
import com.nukkitx.protocol.bedrock.data.inventory.ItemData;
|
||||||
import com.nukkitx.protocol.bedrock.data.inventory.StackRequestSlotInfoData;
|
import com.nukkitx.protocol.bedrock.data.inventory.StackRequestSlotInfoData;
|
||||||
import com.nukkitx.protocol.bedrock.data.inventory.stackrequestactions.*;
|
import com.nukkitx.protocol.bedrock.data.inventory.stackrequestactions.*;
|
||||||
import com.nukkitx.protocol.bedrock.packet.ItemStackRequestPacket;
|
import com.nukkitx.protocol.bedrock.packet.ItemStackRequestPacket;
|
||||||
import com.nukkitx.protocol.bedrock.packet.ItemStackResponsePacket;
|
import com.nukkitx.protocol.bedrock.packet.ItemStackResponsePacket;
|
||||||
|
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
|
||||||
|
import it.unimi.dsi.fastutil.ints.IntSet;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import org.geysermc.connector.inventory.GeyserItemStack;
|
import org.geysermc.connector.inventory.GeyserItemStack;
|
||||||
import org.geysermc.connector.inventory.Inventory;
|
import org.geysermc.connector.inventory.Inventory;
|
||||||
|
@ -92,6 +95,7 @@ public abstract class InventoryTranslator {
|
||||||
|
|
||||||
public static final int PLAYER_INVENTORY_SIZE = 36;
|
public static final int PLAYER_INVENTORY_SIZE = 36;
|
||||||
public static final int PLAYER_INVENTORY_OFFSET = 9;
|
public static final int PLAYER_INVENTORY_OFFSET = 9;
|
||||||
|
private static final int MAX_ITEM_STACK_SIZE = 64;
|
||||||
public final int size;
|
public final int size;
|
||||||
|
|
||||||
public abstract void prepareInventory(GeyserSession session, Inventory inventory);
|
public abstract void prepareInventory(GeyserSession session, Inventory inventory);
|
||||||
|
@ -114,6 +118,7 @@ public abstract class InventoryTranslator {
|
||||||
if (firstAction.getType() == StackRequestActionType.CRAFT_RECIPE || firstAction.getType() == StackRequestActionType.CRAFT_RECIPE_AUTO) {
|
if (firstAction.getType() == StackRequestActionType.CRAFT_RECIPE || firstAction.getType() == StackRequestActionType.CRAFT_RECIPE_AUTO) {
|
||||||
responsePacket.getEntries().add(translateCraftingRequest(session, inventory, request));
|
responsePacket.getEntries().add(translateCraftingRequest(session, inventory, request));
|
||||||
} else if (firstAction.getType() == StackRequestActionType.CRAFT_CREATIVE) {
|
} else if (firstAction.getType() == StackRequestActionType.CRAFT_CREATIVE) {
|
||||||
|
// This is also used for pulling items out of creative
|
||||||
responsePacket.getEntries().add(translateCreativeRequest(session, inventory, request));
|
responsePacket.getEntries().add(translateCreativeRequest(session, inventory, request));
|
||||||
} else {
|
} else {
|
||||||
responsePacket.getEntries().add(translateRequest(session, inventory, request));
|
responsePacket.getEntries().add(translateRequest(session, inventory, request));
|
||||||
|
@ -129,6 +134,7 @@ public abstract class InventoryTranslator {
|
||||||
public ItemStackResponsePacket.Response translateRequest(GeyserSession session, Inventory inventory, ItemStackRequestPacket.Request request) {
|
public ItemStackResponsePacket.Response translateRequest(GeyserSession session, Inventory inventory, ItemStackRequestPacket.Request request) {
|
||||||
System.out.println(request);
|
System.out.println(request);
|
||||||
ClickPlan plan = new ClickPlan(session, this, inventory);
|
ClickPlan plan = new ClickPlan(session, this, inventory);
|
||||||
|
IntSet affectedSlots = new IntOpenHashSet();
|
||||||
for (StackRequestActionData action : request.getActions()) {
|
for (StackRequestActionData action : request.getActions()) {
|
||||||
GeyserItemStack cursor = session.getPlayerInventory().getCursor();
|
GeyserItemStack cursor = session.getPlayerInventory().getCursor();
|
||||||
switch (action.getType()) {
|
switch (action.getType()) {
|
||||||
|
@ -148,6 +154,73 @@ public abstract class InventoryTranslator {
|
||||||
|
|
||||||
if (isCursor(transferAction.getSource()) && isCursor(transferAction.getDestination())) { //???
|
if (isCursor(transferAction.getSource()) && isCursor(transferAction.getDestination())) { //???
|
||||||
return rejectRequest(request);
|
return rejectRequest(request);
|
||||||
|
} else if (session.getGameMode().equals(GameMode.CREATIVE) && inventory instanceof PlayerInventory) { // TODO: does the Java server use the player inventory in all instances?
|
||||||
|
// Creative acts a little differently because it just edits slots
|
||||||
|
int sourceSlot = bedrockSlotToJava(transferAction.getSource());
|
||||||
|
int destSlot = bedrockSlotToJava(transferAction.getDestination());
|
||||||
|
boolean sourceIsCursor = isCursor(transferAction.getSource());
|
||||||
|
boolean destIsCursor = isCursor(transferAction.getDestination());
|
||||||
|
|
||||||
|
GeyserItemStack sourceItem = sourceIsCursor ? session.getPlayerInventory().getCursor() :
|
||||||
|
inventory.getItem(sourceSlot);
|
||||||
|
GeyserItemStack newItem = sourceItem.copy();
|
||||||
|
if (sourceIsCursor) {
|
||||||
|
GeyserItemStack destItem = inventory.getItem(destSlot);
|
||||||
|
if (destItem.getId() == sourceItem.getId()) {
|
||||||
|
// Combining items
|
||||||
|
int itemsLeftOver = destItem.getAmount() + transferAction.getCount();
|
||||||
|
if (itemsLeftOver > MAX_ITEM_STACK_SIZE) {
|
||||||
|
// Items will remain in cursor because destination slot gets set to 64
|
||||||
|
destItem.setAmount(MAX_ITEM_STACK_SIZE);
|
||||||
|
sourceItem.setAmount(itemsLeftOver - MAX_ITEM_STACK_SIZE);
|
||||||
|
} else {
|
||||||
|
// Cursor will be emptied
|
||||||
|
destItem.setAmount(itemsLeftOver);
|
||||||
|
session.getPlayerInventory().setCursor(GeyserItemStack.EMPTY);
|
||||||
|
}
|
||||||
|
ClientCreativeInventoryActionPacket creativeActionPacket = new ClientCreativeInventoryActionPacket(
|
||||||
|
destSlot,
|
||||||
|
destItem.getItemStack()
|
||||||
|
);
|
||||||
|
session.sendDownstreamPacket(creativeActionPacket);
|
||||||
|
affectedSlots.add(destSlot);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Update the item count with however much the client took
|
||||||
|
newItem.setAmount(transferAction.getCount());
|
||||||
|
// Remove that amount from the existing item
|
||||||
|
sourceItem.setAmount(sourceItem.getAmount() - transferAction.getCount());
|
||||||
|
if (sourceItem.isEmpty()) {
|
||||||
|
// Item is basically deleted
|
||||||
|
if (sourceIsCursor) {
|
||||||
|
session.getPlayerInventory().setCursor(GeyserItemStack.EMPTY);
|
||||||
|
} else {
|
||||||
|
inventory.setItem(sourceSlot, GeyserItemStack.EMPTY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (destIsCursor) {
|
||||||
|
session.getPlayerInventory().setCursor(newItem);
|
||||||
|
} else {
|
||||||
|
inventory.setItem(destSlot, newItem);
|
||||||
|
}
|
||||||
|
GeyserItemStack itemToUpdate = destIsCursor ? sourceItem : newItem;
|
||||||
|
// The Java server doesn't care about what's in the mouse in creative mode, so we just need to track
|
||||||
|
// which inventory slot the client modified
|
||||||
|
ClientCreativeInventoryActionPacket creativeActionPacket = new ClientCreativeInventoryActionPacket(
|
||||||
|
destIsCursor ? sourceSlot : destSlot,
|
||||||
|
itemToUpdate.isEmpty() ? new ItemStack(0) : itemToUpdate.getItemStack()
|
||||||
|
);
|
||||||
|
session.sendDownstreamPacket(creativeActionPacket);
|
||||||
|
System.out.println(creativeActionPacket);
|
||||||
|
|
||||||
|
if (!sourceIsCursor) { // Cursor is always added for us as an affected slot
|
||||||
|
affectedSlots.add(sourceSlot);
|
||||||
|
}
|
||||||
|
if (!destIsCursor) {
|
||||||
|
affectedSlots.add(destSlot);
|
||||||
|
}
|
||||||
|
|
||||||
} else if (isCursor(transferAction.getSource())) { //releasing cursor
|
} else if (isCursor(transferAction.getSource())) { //releasing cursor
|
||||||
int sourceAmount = cursor.getAmount();
|
int sourceAmount = cursor.getAmount();
|
||||||
int destSlot = bedrockSlotToJava(transferAction.getDestination());
|
int destSlot = bedrockSlotToJava(transferAction.getDestination());
|
||||||
|
@ -286,37 +359,38 @@ public abstract class InventoryTranslator {
|
||||||
case CRAFT_CREATIVE: {
|
case CRAFT_CREATIVE: {
|
||||||
CraftCreativeStackRequestActionData creativeAction = (CraftCreativeStackRequestActionData) action;
|
CraftCreativeStackRequestActionData creativeAction = (CraftCreativeStackRequestActionData) action;
|
||||||
System.out.println(creativeAction.getCreativeItemNetworkId());
|
System.out.println(creativeAction.getCreativeItemNetworkId());
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
case DESTROY: {
|
case DESTROY: {
|
||||||
//TODO: Yeah this doesn't work yet.
|
|
||||||
|
|
||||||
// Only called when a creative client wants to destroy an item... I think - Camotoy
|
// Only called when a creative client wants to destroy an item... I think - Camotoy
|
||||||
DestroyStackRequestActionData destroyAction = (DestroyStackRequestActionData) action;
|
DestroyStackRequestActionData destroyAction = (DestroyStackRequestActionData) action;
|
||||||
if (session.getGameMode() == GameMode.CREATIVE) {
|
if (!session.getGameMode().equals(GameMode.CREATIVE)) {
|
||||||
if (isCursor(destroyAction.getSource())) {
|
// If this happens, let's throw an error and figure out why.
|
||||||
session.getPlayerInventory().setCursor(GeyserItemStack.EMPTY);
|
return rejectRequest(request);
|
||||||
return acceptRequest(request, makeContainerEntries(session, inventory, Collections.emptySet()));
|
}
|
||||||
} else {
|
if (!isCursor(destroyAction.getSource())) {
|
||||||
int javaSlot = bedrockSlotToJava(destroyAction.getSource());
|
int javaSlot = bedrockSlotToJava(destroyAction.getSource());
|
||||||
inventory.setItem(javaSlot, GeyserItemStack.EMPTY);
|
ClientCreativeInventoryActionPacket destroyItemPacket = new ClientCreativeInventoryActionPacket(
|
||||||
ClientCreativeInventoryActionPacket creativeActionPacket = new ClientCreativeInventoryActionPacket(
|
|
||||||
javaSlot,
|
javaSlot,
|
||||||
new ItemStack(0)
|
new ItemStack(0)
|
||||||
);
|
);
|
||||||
session.sendDownstreamPacket(creativeActionPacket);
|
session.sendDownstreamPacket(destroyItemPacket);
|
||||||
Set<Integer> affectedSlots = Collections.singleton(javaSlot);
|
System.out.println(destroyItemPacket);
|
||||||
return acceptRequest(request, makeContainerEntries(session, inventory, affectedSlots));
|
inventory.setItem(javaSlot, GeyserItemStack.EMPTY);
|
||||||
}
|
affectedSlots.add(javaSlot);
|
||||||
} else {
|
} else {
|
||||||
return rejectRequest(request);
|
// Just sync up the item on our end, since the server doesn't care what's in our cursor
|
||||||
|
session.getPlayerInventory().setCursor(GeyserItemStack.EMPTY);
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return rejectRequest(request);
|
return rejectRequest(request);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
plan.execute(false);
|
plan.execute(false);
|
||||||
return acceptRequest(request, makeContainerEntries(session, inventory, plan.getAffectedSlots()));
|
affectedSlots.addAll(plan.getAffectedSlots());
|
||||||
|
return acceptRequest(request, makeContainerEntries(session, inventory, affectedSlots));
|
||||||
}
|
}
|
||||||
|
|
||||||
public ItemStackResponsePacket.Response translateCraftingRequest(GeyserSession session, Inventory inventory, ItemStackRequestPacket.Request request) {
|
public ItemStackResponsePacket.Response translateCraftingRequest(GeyserSession session, Inventory inventory, ItemStackRequestPacket.Request request) {
|
||||||
|
@ -480,18 +554,26 @@ public abstract class InventoryTranslator {
|
||||||
if (transferAction.getSource().getContainer() != ContainerSlotType.CREATIVE_OUTPUT) {
|
if (transferAction.getSource().getContainer() != ContainerSlotType.CREATIVE_OUTPUT) {
|
||||||
return rejectRequest(request);
|
return rejectRequest(request);
|
||||||
}
|
}
|
||||||
|
// Reference the creative items list we send to the client to know what it's asking of us
|
||||||
|
ItemData creativeItem = ItemRegistry.CREATIVE_ITEMS[creativeId - 1];
|
||||||
|
// Get the correct count
|
||||||
|
creativeItem = ItemData.of(creativeItem.getId(), creativeItem.getDamage(), transferAction.getCount(), creativeItem.getTag());
|
||||||
|
ItemStack javaCreativeItem = ItemTranslator.translateToJava(creativeItem);
|
||||||
|
|
||||||
if (isCursor(transferAction.getDestination())) {
|
if (isCursor(transferAction.getDestination())) {
|
||||||
session.getPlayerInventory().setCursor(GeyserItemStack.from(ItemTranslator.translateToJava(ItemRegistry.CREATIVE_ITEMS[creativeId]), session.getItemNetId().getAndIncrement())); //TODO
|
session.getPlayerInventory().setCursor(GeyserItemStack.from(javaCreativeItem, session.getItemNetId().getAndIncrement()));
|
||||||
return acceptRequest(request, makeContainerEntries(session, inventory, Collections.emptySet()));
|
return acceptRequest(request, Collections.singletonList(
|
||||||
|
new ItemStackResponsePacket.ContainerEntry(ContainerSlotType.CURSOR,
|
||||||
|
Collections.singletonList(makeItemEntry(session, 0, session.getPlayerInventory().getCursor())))));
|
||||||
} else {
|
} else {
|
||||||
int javaSlot = bedrockSlotToJava(transferAction.getDestination());
|
int javaSlot = bedrockSlotToJava(transferAction.getDestination());
|
||||||
ItemStack javaItem = ItemTranslator.translateToJava(ItemRegistry.CREATIVE_ITEMS[creativeId - 1]); //TODO
|
inventory.setItem(javaSlot, GeyserItemStack.from(javaCreativeItem, session.getItemNetId().getAndIncrement()));
|
||||||
inventory.setItem(javaSlot, GeyserItemStack.from(javaItem, session.getItemNetId().getAndIncrement()));
|
|
||||||
ClientCreativeInventoryActionPacket creativeActionPacket = new ClientCreativeInventoryActionPacket(
|
ClientCreativeInventoryActionPacket creativeActionPacket = new ClientCreativeInventoryActionPacket(
|
||||||
javaSlot,
|
javaSlot,
|
||||||
javaItem
|
javaCreativeItem
|
||||||
);
|
);
|
||||||
session.sendDownstreamPacket(creativeActionPacket);
|
session.sendDownstreamPacket(creativeActionPacket);
|
||||||
|
System.out.println(creativeActionPacket);
|
||||||
Set<Integer> affectedSlots = Collections.singleton(javaSlot);
|
Set<Integer> affectedSlots = Collections.singleton(javaSlot);
|
||||||
return acceptRequest(request, makeContainerEntries(session, inventory, affectedSlots));
|
return acceptRequest(request, makeContainerEntries(session, inventory, affectedSlots));
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,8 @@ import com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientConfi
|
||||||
import com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientWindowActionPacket;
|
import com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientWindowActionPacket;
|
||||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
||||||
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
||||||
|
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
|
||||||
|
import it.unimi.dsi.fastutil.ints.IntSet;
|
||||||
import lombok.Value;
|
import lombok.Value;
|
||||||
import org.geysermc.connector.inventory.GeyserItemStack;
|
import org.geysermc.connector.inventory.GeyserItemStack;
|
||||||
import org.geysermc.connector.inventory.Inventory;
|
import org.geysermc.connector.inventory.Inventory;
|
||||||
|
@ -193,8 +195,11 @@ public class ClickPlan {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<Integer> getAffectedSlots() {
|
/**
|
||||||
Set<Integer> affectedSlots = new HashSet<>();
|
* @return a new set of all affected slots. This isn't a constant variable; it's newly generated each time it is run.
|
||||||
|
*/
|
||||||
|
public IntSet getAffectedSlots() {
|
||||||
|
IntSet affectedSlots = new IntOpenHashSet();
|
||||||
for (ClickAction action : plan) {
|
for (ClickAction action : plan) {
|
||||||
if (translator.getSlotType(action.slot) == SlotType.NORMAL && action.slot != Click.OUTSIDE_SLOT) {
|
if (translator.getSlotType(action.slot) == SlotType.NORMAL && action.slot != Click.OUTSIDE_SLOT) {
|
||||||
affectedSlots.add(action.slot);
|
affectedSlots.add(action.slot);
|
||||||
|
@ -206,6 +211,9 @@ public class ClickPlan {
|
||||||
@Value
|
@Value
|
||||||
private static class ClickAction {
|
private static class ClickAction {
|
||||||
Click click;
|
Click click;
|
||||||
|
/**
|
||||||
|
* Java slot
|
||||||
|
*/
|
||||||
int slot;
|
int slot;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,6 @@
|
||||||
package org.geysermc.connector.network.translators.java.window;
|
package org.geysermc.connector.network.translators.java.window;
|
||||||
|
|
||||||
import com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerSetSlotPacket;
|
import com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerSetSlotPacket;
|
||||||
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
|
|
||||||
import org.geysermc.connector.inventory.GeyserItemStack;
|
import org.geysermc.connector.inventory.GeyserItemStack;
|
||||||
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;
|
||||||
|
@ -40,6 +39,7 @@ public class JavaSetSlotTranslator extends PacketTranslator<ServerSetSlotPacket>
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void translate(ServerSetSlotPacket packet, GeyserSession session) {
|
public void translate(ServerSetSlotPacket packet, GeyserSession session) {
|
||||||
|
System.out.println(packet.toString());
|
||||||
session.addInventoryTask(() -> {
|
session.addInventoryTask(() -> {
|
||||||
if (packet.getWindowId() == 255) { //cursor
|
if (packet.getWindowId() == 255) { //cursor
|
||||||
GeyserItemStack newItem = GeyserItemStack.from(packet.getItem());
|
GeyserItemStack newItem = GeyserItemStack.from(packet.getItem());
|
||||||
|
|
Loading…
Reference in a new issue