Stonecutter fixed; Loom improved

This commit is contained in:
Camotoy 2020-12-24 18:29:25 -05:00
parent 617a1216d5
commit 3d0b0a1076
No known key found for this signature in database
GPG key ID: 7EEFB66FE798081F
7 changed files with 129 additions and 42 deletions

View file

@ -53,7 +53,7 @@ import com.nukkitx.protocol.bedrock.data.command.CommandPermission;
import com.nukkitx.protocol.bedrock.packet.*;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.ints.IntSet;
import it.unimi.dsi.fastutil.ints.IntList;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectMaps;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
@ -214,10 +214,10 @@ public class GeyserSession implements CommandSender {
/**
* Saves a list of all stonecutter recipes, for use in a stonecutter inventory.
* The key is the Java ID of the item; the values are all the possible outputs' Java IDs
* The key is the Java ID of the item; the values are all the possible outputs' Java IDs sorted by their string identifier
*/
@Setter
private Int2ObjectMap<IntSet> stonecutterRecipes;
private Int2ObjectMap<IntList> stonecutterRecipes;
/**
* The current attack speed of the player. Used for sending proper cooldown timings.

View file

@ -118,6 +118,16 @@ public abstract class InventoryTranslator {
public abstract SlotType getSlotType(int javaSlot);
public abstract Inventory createInventory(String name, int windowId, WindowType windowType, PlayerInventory playerInventory);
/**
* Should be overwritten in cases where specific inventories should reject an item being in a specific spot.
* For examples, looms use this to reject items that are dyes in Bedrock but not in Java.
*
* @return true if this transfer should be rejected
*/
public boolean shouldRejectItemPlace(GeyserSession session, Inventory inventory, int javaDestinationSlot) {
return false;
}
/**
* Should be overrided if this request matches a certain criteria and shouldn't be treated normally.
* E.G. anvil renaming or enchanting
@ -178,12 +188,18 @@ public abstract class InventoryTranslator {
return rejectRequest(request);
}
int destSlot = bedrockSlotToJava(transferAction.getDestination());
if (shouldRejectItemPlace(session, inventory, destSlot)) {
// This item would not be here in Java
return rejectRequest(request, false);
}
if (isCursor(transferAction.getSource()) && isCursor(transferAction.getDestination())) { //???
return rejectRequest(request);
} else if (session.getGameMode().equals(GameMode.CREATIVE) && inventory instanceof PlayerInventory) { // TODO: does the Java server use this stuff all the time in creative?
// 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());
@ -249,7 +265,6 @@ public abstract class InventoryTranslator {
} else if (isCursor(transferAction.getSource())) { //releasing cursor
int sourceAmount = cursor.getAmount();
int destSlot = bedrockSlotToJava(transferAction.getDestination());
if (transferAction.getCount() == sourceAmount) { //release all
plan.add(Click.LEFT, destSlot);
} else { //release some
@ -279,7 +294,9 @@ public abstract class InventoryTranslator {
if (transferAction.getCount() != sourceAmount) { //TODO: handle partially picking up into non-empty cursor (temp slot)
return rejectRequest(request);
}
plan.add(Click.LEFT, sourceSlot); //release cursor onto source slot
if (getSlotType(sourceSlot).equals(SlotType.NORMAL)) {
plan.add(Click.LEFT, sourceSlot); //release cursor onto source slot
}
plan.add(Click.LEFT, sourceSlot); //pickup combined cursor and source
}
} else { //transfer from one slot to another
@ -288,7 +305,6 @@ public abstract class InventoryTranslator {
}
int sourceSlot = bedrockSlotToJava(transferAction.getSource());
int sourceAmount = plan.getItem(sourceSlot).getAmount();
int destSlot = bedrockSlotToJava(transferAction.getDestination());
if (transferAction.getCount() == sourceAmount) { //transfer all
plan.add(Click.LEFT, sourceSlot); //pickup source
plan.add(Click.LEFT, destSlot); //let go of all items and done

View file

@ -127,7 +127,7 @@ public class EnchantingInventoryTranslator extends AbstractBlockInventoryTransla
}
if (javaSlot == -1) {
// Slot should be determined as 0, 1, or 2
throw new RuntimeException("Cannot find enchant slot for item!");
return rejectRequest(request);
}
ClientClickWindowButtonPacket packet = new ClientClickWindowButtonPacket(inventory.getId(), javaSlot);
System.out.println(packet);

View file

@ -26,6 +26,8 @@
package org.geysermc.connector.network.translators.inventory.translators;
import com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientClickWindowButtonPacket;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.ListTag;
import com.nukkitx.nbt.NbtMap;
import com.nukkitx.nbt.NbtType;
import com.nukkitx.protocol.bedrock.data.inventory.ContainerSlotType;
@ -38,11 +40,15 @@ import com.nukkitx.protocol.bedrock.packet.ItemStackRequestPacket;
import com.nukkitx.protocol.bedrock.packet.ItemStackResponsePacket;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import org.geysermc.connector.inventory.GeyserItemStack;
import org.geysermc.connector.inventory.Inventory;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.inventory.BedrockContainerSlot;
import org.geysermc.connector.network.translators.inventory.updater.UIInventoryUpdater;
import org.geysermc.connector.network.translators.item.ItemRegistry;
import org.geysermc.connector.network.translators.item.translators.BannerTranslator;
import java.util.Collections;
import java.util.List;
public class LoomInventoryTranslator extends AbstractBlockInventoryTranslator {
@ -94,6 +100,20 @@ public class LoomInventoryTranslator extends AbstractBlockInventoryTranslator {
super(4, "minecraft:loom[facing=north]", ContainerType.LOOM, UIInventoryUpdater.INSTANCE);
}
@Override
public boolean shouldRejectItemPlace(GeyserSession session, Inventory inventory, int javaDestinationSlot) {
if (javaDestinationSlot != 1) {
return false;
}
GeyserItemStack itemStack = session.getPlayerInventory().getCursor();
if (itemStack.isEmpty()) {
return false;
}
// Reject the item if Bedrock is attempting to put in a dye that is not a dye in Java Edition
return !ItemRegistry.getItem(itemStack.getItemStack()).getJavaIdentifier().contains("_dye");
}
@Override
public boolean shouldHandleRequestFirst(StackRequestActionData action, Inventory inventory) {
// If the LOOM_MATERIAL slot is not empty, we are crafting a pattern that does not come from an item
@ -103,18 +123,17 @@ public class LoomInventoryTranslator extends AbstractBlockInventoryTranslator {
@Override
public ItemStackResponsePacket.Response translateSpecialRequest(GeyserSession session, Inventory inventory, ItemStackRequestPacket.Request request) {
// TODO: I anticipate this will be changed in the future to use something non-deprecated. Keep an eye out.
// Also TODO: Shift-clicking doesn't work here.
StackRequestActionData data = request.getActions()[1];
if (!(data instanceof CraftResultsDeprecatedStackRequestActionData)) {
return rejectRequest(request);
}
CraftResultsDeprecatedStackRequestActionData craftData = (CraftResultsDeprecatedStackRequestActionData) data;
// Get the patterns compound tag
List<NbtMap> blockEntityTag = craftData.getResultItems()[0].getTag().getList("Patterns", NbtType.COMPOUND);
List<NbtMap> newblockEntityTag = craftData.getResultItems()[0].getTag().getList("Patterns", NbtType.COMPOUND);
// Get the pattern that the Bedrock client requests - the last pattern in the Patterns list
String pattern = blockEntityTag.get(blockEntityTag.size() - 1).getString("Pattern");
NbtMap pattern = newblockEntityTag.get(newblockEntityTag.size() - 1);
// Get the Java index of this pattern
int index = PATTERN_TO_INDEX.getOrDefault(pattern, -1);
int index = PATTERN_TO_INDEX.getOrDefault(pattern.getString("Pattern"), -1);
if (index == -1) {
return rejectRequest(request);
}
@ -124,6 +143,30 @@ public class LoomInventoryTranslator extends AbstractBlockInventoryTranslator {
ClientClickWindowButtonPacket packet = new ClientClickWindowButtonPacket(inventory.getId(), index);
System.out.println(packet);
session.sendDownstreamPacket(packet);
GeyserItemStack inputCopy = inventory.getItem(0).copy();
inputCopy.setNetId(session.getItemNetId().incrementAndGet());
// Add the pattern manually, for better item synchronization
if (inputCopy.getNbt() == null) {
inputCopy.setNbt(new CompoundTag(""));
}
CompoundTag blockEntityTag = inputCopy.getNbt().get("BlockEntityTag");
CompoundTag javaBannerPattern = BannerTranslator.getJavaBannerPattern(pattern);
if (blockEntityTag != null) {
ListTag patternsList = blockEntityTag.get("Patterns");
if (patternsList != null) {
patternsList.add(javaBannerPattern);
} else {
patternsList = new ListTag("Patterns", Collections.singletonList(javaBannerPattern));
blockEntityTag.put(patternsList);
}
} else {
blockEntityTag = new CompoundTag("BlockEntityTag");
ListTag patternsList = new ListTag("Patterns", Collections.singletonList(javaBannerPattern));
blockEntityTag.put(patternsList);
inputCopy.getNbt().put(blockEntityTag);
}
// Set the new item as the output
inventory.setItem(3, inputCopy);
return translateRequest(session, inventory, request);
}

View file

@ -35,16 +35,15 @@ import com.nukkitx.protocol.bedrock.data.inventory.stackrequestactions.StackRequ
import com.nukkitx.protocol.bedrock.data.inventory.stackrequestactions.StackRequestActionType;
import com.nukkitx.protocol.bedrock.packet.ItemStackRequestPacket;
import com.nukkitx.protocol.bedrock.packet.ItemStackResponsePacket;
import it.unimi.dsi.fastutil.ints.IntSet;
import it.unimi.dsi.fastutil.ints.IntList;
import org.geysermc.connector.inventory.GeyserItemStack;
import org.geysermc.connector.inventory.Inventory;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.inventory.BedrockContainerSlot;
import org.geysermc.connector.network.translators.inventory.SlotType;
import org.geysermc.connector.network.translators.inventory.updater.UIInventoryUpdater;
import org.geysermc.connector.network.translators.item.ItemTranslator;
import java.util.stream.Collectors;
public class StonecutterInventoryTranslator extends AbstractBlockInventoryTranslator {
public StonecutterInventoryTranslator() {
super(2, "minecraft:stonecutter[facing=north]", ContainerType.STONECUTTER, UIInventoryUpdater.INSTANCE);
@ -66,20 +65,21 @@ public class StonecutterInventoryTranslator extends AbstractBlockInventoryTransl
// Get the ID of the item we are cutting
int id = inventory.getItem(0).getId();
// Look up all possible options of cutting from this ID
IntSet results = session.getStonecutterRecipes().get(id);
IntList results = session.getStonecutterRecipes().get(id);
if (results == null) {
return rejectRequest(request);
}
System.out.println(id + " " + results);
ItemStack javaOutput = ItemTranslator.translateToJava(craftData.getResultItems()[0]);
// TODO bruh
System.out.println(javaOutput);
// Getting the index of the item in the Java stonecutter list
// We do need to sort them by their Java ID to preserve index, though
int index = results.stream().sorted().collect(Collectors.toList()).indexOf(javaOutput.getId());
ClientClickWindowButtonPacket packet = new ClientClickWindowButtonPacket(inventory.getId(), index + 1);
ClientClickWindowButtonPacket packet = new ClientClickWindowButtonPacket(inventory.getId(), results.indexOf(javaOutput.getId()));
System.out.println(packet.toString());
session.sendDownstreamPacket(packet);
// We don't know there is an output here, so we tell ourselves that there is
inventory.setItem(1, GeyserItemStack.from(javaOutput, session.getItemNetId().incrementAndGet()));
if (inventory.getItem(1).getId() != javaOutput.getId()) {
// We don't know there is an output here, so we tell ourselves that there is
inventory.setItem(1, GeyserItemStack.from(javaOutput, session.getItemNetId().incrementAndGet()));
}
return translateRequest(session, inventory, request);
}
@ -115,4 +115,12 @@ public class StonecutterInventoryTranslator extends AbstractBlockInventoryTransl
}
return super.javaSlotToBedrock(slot);
}
@Override
public SlotType getSlotType(int javaSlot) {
if (javaSlot == 1) {
return SlotType.OUTPUT;
}
return super.getSlotType(javaSlot);
}
}

View file

@ -32,6 +32,7 @@ import com.nukkitx.nbt.NbtMap;
import com.nukkitx.nbt.NbtMapBuilder;
import com.nukkitx.nbt.NbtType;
import com.nukkitx.protocol.bedrock.data.inventory.ItemData;
import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.network.translators.ItemRemapper;
import org.geysermc.connector.network.translators.item.ItemEntry;
import org.geysermc.connector.network.translators.item.ItemRegistry;
@ -181,6 +182,7 @@ public class BannerTranslator extends ItemTranslator {
@Override
public ItemStack translateToJava(ItemData itemData, ItemEntry itemEntry) {
GeyserConnector.getInstance().getLogger().warning(itemEntry.toString());
if (itemData.getTag() == null) {
return super.translateToJava(itemData, itemEntry);
}
@ -195,13 +197,14 @@ public class BannerTranslator extends ItemTranslator {
blockEntityTag.put(OMINOUS_BANNER_PATTERN);
itemStack.getNbt().put(blockEntityTag);
} else if (nbtTag.containsKey("Patterns", NbtType.COMPOUND)) {
} else if (nbtTag.containsKey("Patterns", NbtType.LIST)) {
List<NbtMap> patterns = nbtTag.getList("Patterns", NbtType.COMPOUND);
CompoundTag blockEntityTag = new CompoundTag("BlockEntityTag");
blockEntityTag.put(convertBannerPattern(patterns));
itemStack.getNbt().put(blockEntityTag);
itemStack.getNbt().remove("Patterns"); // Remove the old Bedrock patterns list
}
return itemStack;

View file

@ -36,10 +36,7 @@ import com.nukkitx.nbt.NbtMap;
import com.nukkitx.protocol.bedrock.data.inventory.CraftingData;
import com.nukkitx.protocol.bedrock.data.inventory.ItemData;
import com.nukkitx.protocol.bedrock.packet.CraftingDataPacket;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.ints.IntSet;
import it.unimi.dsi.fastutil.ints.*;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import org.geysermc.connector.network.session.GeyserSession;
@ -58,7 +55,7 @@ public class JavaDeclareRecipesTranslator extends PacketTranslator<ServerDeclare
// Get the last known network ID (first used for the pregenerated recipes) and increment from there.
int netId = RecipeRegistry.LAST_RECIPE_NET_ID + 1;
Int2ObjectMap<Recipe> recipeMap = new Int2ObjectOpenHashMap<>();
Int2ObjectMap<IntSet> stonecutterRecipeMap = new Int2ObjectOpenHashMap<>();
Int2ObjectMap<List<StoneCuttingRecipeData>> unsortedStonecutterData = new Int2ObjectOpenHashMap<>();
CraftingDataPacket craftingDataPacket = new CraftingDataPacket();
craftingDataPacket.setCleanRecipes(true);
for (Recipe recipe : packet.getRecipes()) {
@ -141,26 +138,46 @@ public class JavaDeclareRecipesTranslator extends PacketTranslator<ServerDeclare
}
case STONECUTTING: {
StoneCuttingRecipeData stoneCuttingData = (StoneCuttingRecipeData) recipe.getData();
// As of 1.16.4, all stonecutter recipes have one ingredient option
ItemStack ingredient = stoneCuttingData.getIngredient().getOptions()[0];
ItemData input = ItemTranslator.translateToBedrock(session, ingredient);
ItemData output = ItemTranslator.translateToBedrock(session, stoneCuttingData.getResult());
UUID uuid = UUID.randomUUID();
// We need to register stonecutting recipes so they show up on Bedrock
craftingDataPacket.getCraftingData().add(CraftingData.fromShapeless(uuid.toString(),
Collections.singletonList(input), Collections.singletonList(output), uuid, "stonecutter", 0, netId++));
// Save the recipe list for reference when crafting
IntSet outputs = stonecutterRecipeMap.get(ingredient.getId());
if (outputs == null) {
outputs = new IntOpenHashSet();
stonecutterRecipeMap.put(ingredient.getId(), outputs);
List<StoneCuttingRecipeData> data = unsortedStonecutterData.get(ingredient.getId());
if (data == null) {
data = new ArrayList<>();
unsortedStonecutterData.put(ingredient.getId(), data);
}
// Add the ingredient as the key and all possible values as the value
outputs.add(stoneCuttingData.getResult().getId());
data.add(stoneCuttingData);
// Save for processing after all recipes have been received
}
}
}
craftingDataPacket.getPotionMixData().addAll(PotionMixRegistry.POTION_MIXES);
Int2ObjectMap<IntList> stonecutterRecipeMap = new Int2ObjectOpenHashMap<>();
for (Int2ObjectMap.Entry<List<StoneCuttingRecipeData>> data : unsortedStonecutterData.int2ObjectEntrySet()) {
data.getValue().sort(Comparator.comparing((stoneCuttingRecipeData ->
// Sort the list by each output item's Java identifier - this is how it's sorted on Java, and therefore
// We can get the correct order for button pressing
ItemRegistry.getItem(stoneCuttingRecipeData.getResult()).getJavaIdentifier())));
// Now that it's sorted, let's translate these recipes
for (StoneCuttingRecipeData stoneCuttingData : data.getValue()) {
// As of 1.16.4, all stonecutter recipes have one ingredient option
ItemStack ingredient = stoneCuttingData.getIngredient().getOptions()[0];
ItemData input = ItemTranslator.translateToBedrock(session, ingredient);
ItemData output = ItemTranslator.translateToBedrock(session, stoneCuttingData.getResult());
UUID uuid = UUID.randomUUID();
// We need to register stonecutting recipes so they show up on Bedrock
craftingDataPacket.getCraftingData().add(CraftingData.fromShapeless(uuid.toString(),
Collections.singletonList(input), Collections.singletonList(output), uuid, "stonecutter", 0, netId++));
// Save the recipe list for reference when crafting
IntList outputs = stonecutterRecipeMap.get(ingredient.getId());
if (outputs == null) {
outputs = new IntArrayList();
// Add the ingredient as the key and all possible values as the value
stonecutterRecipeMap.put(ingredient.getId(), outputs);
}
outputs.add(stoneCuttingData.getResult().getId());
}
}
session.sendUpstreamPacket(craftingDataPacket);
session.setCraftingRecipes(recipeMap);
session.setStonecutterRecipes(stonecutterRecipeMap);