2019-11-28 03:55:58 +00:00
|
|
|
/*
|
2020-01-31 01:05:57 +00:00
|
|
|
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
|
2019-11-28 03:55:58 +00:00
|
|
|
*
|
|
|
|
* 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.java;
|
|
|
|
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.recipe.Ingredient;
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.recipe.Recipe;
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.recipe.data.ShapedRecipeData;
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.recipe.data.ShapelessRecipeData;
|
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.server.ServerDeclareRecipesPacket;
|
|
|
|
import com.nukkitx.nbt.tag.CompoundTag;
|
|
|
|
import com.nukkitx.protocol.bedrock.data.CraftingData;
|
|
|
|
import com.nukkitx.protocol.bedrock.data.ItemData;
|
2019-12-08 00:06:56 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.PotionMixData;
|
2019-11-28 03:55:58 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.CraftingDataPacket;
|
2019-12-01 00:24:01 +00:00
|
|
|
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
|
|
|
|
import it.unimi.dsi.fastutil.ints.IntSet;
|
2019-11-28 03:55:58 +00:00
|
|
|
import lombok.AllArgsConstructor;
|
|
|
|
import lombok.EqualsAndHashCode;
|
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
|
|
|
import org.geysermc.connector.network.translators.PacketTranslator;
|
2020-04-02 22:01:17 +00:00
|
|
|
import org.geysermc.connector.network.translators.Translator;
|
|
|
|
import org.geysermc.connector.network.translators.Translators;
|
2019-11-29 11:16:52 +00:00
|
|
|
import org.geysermc.connector.network.translators.item.ItemEntry;
|
|
|
|
import org.geysermc.connector.utils.Toolbox;
|
2019-11-28 03:55:58 +00:00
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
2020-04-02 22:01:17 +00:00
|
|
|
@Translator(packet = ServerDeclareRecipesPacket.class)
|
2019-11-28 03:55:58 +00:00
|
|
|
public class JavaDeclareRecipesTranslator extends PacketTranslator<ServerDeclareRecipesPacket> {
|
2020-01-31 01:05:57 +00:00
|
|
|
private static final Collection<PotionMixData> POTION_MIXES =
|
|
|
|
Arrays.stream(new int[]{372, 331, 348, 376, 289, 437, 353, 414, 382, 375, 462, 378, 396, 377, 370, 469, 470})
|
|
|
|
.mapToObj(ingredient -> new PotionMixData(0, ingredient, 0))
|
|
|
|
.collect(Collectors.toList());
|
2019-12-08 00:06:56 +00:00
|
|
|
|
2019-11-28 03:55:58 +00:00
|
|
|
@Override
|
|
|
|
public void translate(ServerDeclareRecipesPacket packet, GeyserSession session) {
|
|
|
|
CraftingDataPacket craftingDataPacket = new CraftingDataPacket();
|
|
|
|
craftingDataPacket.setCleanRecipes(true);
|
|
|
|
for (Recipe recipe : packet.getRecipes()) {
|
|
|
|
switch (recipe.getType()) {
|
|
|
|
case CRAFTING_SHAPELESS: {
|
|
|
|
ShapelessRecipeData shapelessRecipeData = (ShapelessRecipeData) recipe.getData();
|
2020-04-22 21:26:16 +00:00
|
|
|
ItemData output = Translators.getItemTranslator().translateToBedrock(session, shapelessRecipeData.getResult());
|
2020-01-31 01:05:57 +00:00
|
|
|
output = ItemData.of(output.getId(), output.getDamage(), output.getCount()); //strip NBT
|
2020-04-22 21:26:16 +00:00
|
|
|
ItemData[][] inputCombinations = combinations(session, shapelessRecipeData.getIngredients());
|
2019-11-29 11:16:52 +00:00
|
|
|
for (ItemData[] inputs : inputCombinations) {
|
2019-11-28 03:55:58 +00:00
|
|
|
UUID uuid = UUID.randomUUID();
|
|
|
|
craftingDataPacket.getCraftingData().add(CraftingData.fromShapeless(uuid.toString(),
|
|
|
|
inputs, new ItemData[]{output}, uuid, "crafting_table", 0));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CRAFTING_SHAPED: {
|
|
|
|
ShapedRecipeData shapedRecipeData = (ShapedRecipeData) recipe.getData();
|
2020-04-22 21:26:16 +00:00
|
|
|
ItemData output = Translators.getItemTranslator().translateToBedrock(session, shapedRecipeData.getResult());
|
2020-01-31 01:05:57 +00:00
|
|
|
output = ItemData.of(output.getId(), output.getDamage(), output.getCount()); //strip NBT
|
2020-04-22 21:26:16 +00:00
|
|
|
ItemData[][] inputCombinations = combinations(session, shapedRecipeData.getIngredients());
|
2019-11-29 11:16:52 +00:00
|
|
|
for (ItemData[] inputs : inputCombinations) {
|
2019-11-28 03:55:58 +00:00
|
|
|
UUID uuid = UUID.randomUUID();
|
|
|
|
craftingDataPacket.getCraftingData().add(CraftingData.fromShaped(uuid.toString(),
|
|
|
|
shapedRecipeData.getWidth(), shapedRecipeData.getHeight(), inputs,
|
|
|
|
new ItemData[]{output}, uuid, "crafting_table", 0));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-31 01:05:57 +00:00
|
|
|
craftingDataPacket.getPotionMixData().addAll(POTION_MIXES);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(craftingDataPacket);
|
2019-11-28 03:55:58 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 01:05:57 +00:00
|
|
|
//TODO: rewrite
|
2020-04-22 21:26:16 +00:00
|
|
|
private ItemData[][] combinations(GeyserSession session, Ingredient[] ingredients) {
|
2019-12-01 00:24:01 +00:00
|
|
|
Map<Set<ItemData>, IntSet> squashedOptions = new HashMap<>();
|
2019-11-28 03:55:58 +00:00
|
|
|
for (int i = 0; i < ingredients.length; i++) {
|
|
|
|
if (ingredients[i].getOptions().length == 0) {
|
2019-12-01 00:24:01 +00:00
|
|
|
squashedOptions.computeIfAbsent(Collections.singleton(ItemData.AIR), k -> new IntOpenHashSet()).add(i);
|
2019-11-28 03:55:58 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Ingredient ingredient = ingredients[i];
|
|
|
|
Map<GroupedItem, List<ItemData>> groupedByIds = Arrays.stream(ingredient.getOptions())
|
2020-04-22 21:26:16 +00:00
|
|
|
.map(item -> Translators.getItemTranslator().translateToBedrock(session, item))
|
2019-11-28 03:55:58 +00:00
|
|
|
.collect(Collectors.groupingBy(item -> new GroupedItem(item.getId(), item.getCount(), item.getTag())));
|
2019-11-29 11:16:52 +00:00
|
|
|
Set<ItemData> optionSet = new HashSet<>(groupedByIds.size());
|
2019-11-28 03:55:58 +00:00
|
|
|
for (Map.Entry<GroupedItem, List<ItemData>> entry : groupedByIds.entrySet()) {
|
|
|
|
if (entry.getValue().size() > 1) {
|
|
|
|
GroupedItem groupedItem = entry.getKey();
|
2019-11-29 11:16:52 +00:00
|
|
|
int idCount = 0;
|
|
|
|
//not optimal
|
2019-12-01 00:24:01 +00:00
|
|
|
for (ItemEntry itemEntry : Toolbox.ITEM_ENTRIES.values()) {
|
2019-11-29 11:16:52 +00:00
|
|
|
if (itemEntry.getBedrockId() == groupedItem.id) {
|
|
|
|
idCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (entry.getValue().size() < idCount) {
|
|
|
|
optionSet.addAll(entry.getValue());
|
|
|
|
} else {
|
|
|
|
optionSet.add(ItemData.of(groupedItem.id, (short) -1, groupedItem.count, groupedItem.tag));
|
|
|
|
}
|
2019-11-28 03:55:58 +00:00
|
|
|
} else {
|
|
|
|
ItemData item = entry.getValue().get(0);
|
2019-11-29 11:16:52 +00:00
|
|
|
optionSet.add(item);
|
2019-11-28 03:55:58 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-01 00:24:01 +00:00
|
|
|
squashedOptions.computeIfAbsent(optionSet, k -> new IntOpenHashSet()).add(i);
|
2019-11-28 03:55:58 +00:00
|
|
|
}
|
2019-11-29 11:16:52 +00:00
|
|
|
int totalCombinations = 1;
|
|
|
|
for (Set optionSet : squashedOptions.keySet()) {
|
|
|
|
totalCombinations *= optionSet.size();
|
2019-11-28 03:55:58 +00:00
|
|
|
}
|
2019-11-29 11:16:52 +00:00
|
|
|
if (totalCombinations > 500) {
|
2019-11-28 03:55:58 +00:00
|
|
|
ItemData[] translatedItems = new ItemData[ingredients.length];
|
|
|
|
for (int i = 0; i < ingredients.length; i++) {
|
|
|
|
if (ingredients[i].getOptions().length > 0) {
|
2020-04-22 21:26:16 +00:00
|
|
|
translatedItems[i] = Translators.getItemTranslator().translateToBedrock(session, ingredients[i].getOptions()[0]);
|
2019-11-28 03:55:58 +00:00
|
|
|
} else {
|
|
|
|
translatedItems[i] = ItemData.AIR;
|
|
|
|
}
|
|
|
|
}
|
2019-11-29 11:16:52 +00:00
|
|
|
return new ItemData[][]{translatedItems};
|
2019-11-28 03:55:58 +00:00
|
|
|
}
|
2019-11-29 11:16:52 +00:00
|
|
|
List<Set<ItemData>> sortedSets = new ArrayList<>(squashedOptions.keySet());
|
|
|
|
sortedSets.sort(Comparator.comparing(Set::size, Comparator.reverseOrder()));
|
|
|
|
ItemData[][] combinations = new ItemData[totalCombinations][ingredients.length];
|
|
|
|
int x = 1;
|
|
|
|
for (Set<ItemData> set : sortedSets) {
|
2019-12-01 00:24:01 +00:00
|
|
|
IntSet slotSet = squashedOptions.get(set);
|
2019-11-29 11:16:52 +00:00
|
|
|
int i = 0;
|
|
|
|
for (ItemData item : set) {
|
|
|
|
for (int j = 0; j < totalCombinations / set.size(); j++) {
|
|
|
|
final int comboIndex = (i * x) + (j % x) + ((j / x) * set.size() * x);
|
|
|
|
for (int slot : slotSet) {
|
|
|
|
combinations[comboIndex][slot] = item;
|
|
|
|
}
|
2019-11-28 03:55:58 +00:00
|
|
|
}
|
2019-11-29 11:16:52 +00:00
|
|
|
i++;
|
2019-11-28 03:55:58 +00:00
|
|
|
}
|
2019-11-29 11:16:52 +00:00
|
|
|
x *= set.size();
|
2019-11-28 03:55:58 +00:00
|
|
|
}
|
2019-11-29 11:16:52 +00:00
|
|
|
return combinations;
|
2019-11-28 03:55:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@EqualsAndHashCode
|
|
|
|
@AllArgsConstructor
|
|
|
|
private static class GroupedItem {
|
|
|
|
int id;
|
|
|
|
int count;
|
|
|
|
CompoundTag tag;
|
|
|
|
}
|
|
|
|
}
|