use of top.theillusivec4.polymorph.api.common.base.RecipePair in project Polymorph by TheIllusiveC4.
the class AbstractStackRecipeData method readNbt.
@Override
public void readNbt(NbtCompound pCompound) {
if (pCompound.contains("SelectedRecipe")) {
this.loadedRecipe = new Identifier(pCompound.getString("SelectedRecipe"));
}
if (pCompound.contains("RecipeDataSet")) {
Set<RecipePair> dataset = this.getRecipesList();
dataset.clear();
NbtList list = pCompound.getList("RecipeDataSet", NbtType.COMPOUND);
for (NbtElement inbt : list) {
NbtCompound tag = (NbtCompound) inbt;
Identifier id = Identifier.tryParse(tag.getString("Id"));
ItemStack stack = ItemStack.fromNbt(tag.getCompound("ItemStack"));
dataset.add(new RecipePairImpl(id, stack));
}
}
}
use of top.theillusivec4.polymorph.api.common.base.RecipePair in project Polymorph by TheIllusiveC4.
the class AbstractStackRecipeData method getRecipe.
@SuppressWarnings("unchecked")
@Override
public <T extends Recipe<C>, C extends Inventory> Optional<T> getRecipe(RecipeType<T> pType, C pInventory, World pWorld, List<T> pRecipes) {
this.getLoadedRecipe().flatMap(id -> pWorld.getRecipeManager().get(id)).ifPresent(selected -> {
try {
if (selected.getType() == pType && (((T) selected).matches(pInventory, pWorld) || isEmpty(pInventory))) {
this.setSelectedRecipe(selected);
}
} catch (ClassCastException e) {
PolymorphMod.LOGGER.error("Recipe {} does not match inventory {}", selected.getId(), pInventory);
}
this.loadedRecipe = null;
});
if (this.isEmpty(pInventory)) {
this.setFailing(false);
this.sendRecipesListToListeners(true);
return Optional.empty();
}
AtomicReference<T> ref = new AtomicReference<>(null);
this.getLastRecipe().ifPresent(recipe -> {
try {
if (recipe.getType() == pType && ((T) recipe).matches(pInventory, pWorld)) {
this.getSelectedRecipe().ifPresent(selected -> {
try {
if (selected.getType() == pType && ((T) selected).matches(pInventory, pWorld)) {
ref.set((T) selected);
}
} catch (ClassCastException e) {
PolymorphMod.LOGGER.error("Recipe {} does not match inventory {}", selected.getId(), pInventory);
}
});
}
} catch (ClassCastException e) {
PolymorphMod.LOGGER.error("Recipe {} does not match inventory {}", recipe.getId(), pInventory);
}
});
T result = ref.get();
if (result != null) {
this.setFailing(false);
this.sendRecipesListToListeners(false);
return Optional.of(result);
}
SortedSet<RecipePair> newDataset = new TreeSet<>();
List<T> recipes = pRecipes.isEmpty() ? pWorld.getRecipeManager().getAllMatches(pType, pInventory, pWorld) : pRecipes;
if (recipes.isEmpty()) {
this.setFailing(true);
this.sendRecipesListToListeners(true);
return Optional.empty();
}
for (T entry : recipes) {
Identifier id = entry.getId();
if (ref.get() == null && this.getSelectedRecipe().map(recipe -> recipe.getId().equals(id)).orElse(false)) {
ref.set(entry);
}
newDataset.add(new RecipePairImpl(id, entry.craft(pInventory)));
}
this.setRecipesList(newDataset);
result = ref.get();
result = result != null ? result : recipes.get(0);
this.lastRecipe = result;
this.setSelectedRecipe(result);
this.setFailing(false);
this.sendRecipesListToListeners(false);
return Optional.of(result);
}
use of top.theillusivec4.polymorph.api.common.base.RecipePair in project Polymorph by TheIllusiveC4.
the class CommonEventsListener method openScreenHandler.
public static void openScreenHandler(ServerPlayerEntity player) {
ScreenHandler screenHandler = player.currentScreenHandler;
PolymorphCommon commonApi = PolymorphApi.common();
commonApi.getRecipeDataFromBlockEntity(screenHandler).ifPresent(recipeData -> {
PolymorphPacketDistributor packetDistributor = commonApi.getPacketDistributor();
if (recipeData.isFailing() || recipeData.isEmpty(null)) {
packetDistributor.sendRecipesListS2C(player);
} else {
Pair<SortedSet<RecipePair>, Identifier> data = recipeData.getPacketData();
packetDistributor.sendRecipesListS2C(player, data.getLeft(), data.getRight());
}
});
for (AbstractCompatibilityModule integration : PolymorphIntegrations.get()) {
if (integration.openScreenHandler(screenHandler, player)) {
return;
}
}
}
use of top.theillusivec4.polymorph.api.common.base.RecipePair in project Polymorph by TheIllusiveC4.
the class SPacketPlayerRecipeSync method decode.
public static SPacketPlayerRecipeSync decode(FriendlyByteBuf pBuffer) {
SortedSet<IRecipePair> recipeDataset = new TreeSet<>();
ResourceLocation selected = null;
if (pBuffer.isReadable()) {
int size = pBuffer.readInt();
for (int i = 0; i < size; i++) {
recipeDataset.add(new RecipePair(pBuffer.readResourceLocation(), pBuffer.readItem()));
}
if (pBuffer.isReadable()) {
selected = pBuffer.readResourceLocation();
}
}
return new SPacketPlayerRecipeSync(recipeDataset, selected);
}
use of top.theillusivec4.polymorph.api.common.base.RecipePair in project Polymorph by TheIllusiveC4.
the class AbstractHighlightedRecipeData method getPacketData.
@Override
public Pair<SortedSet<RecipePair>, Identifier> getPacketData() {
SortedSet<RecipePair> recipesList = this.getRecipesList();
Identifier selected = null;
if (!recipesList.isEmpty()) {
selected = this.getSelectedRecipe().map(Recipe::getId).orElse(recipesList.first().getIdentifier());
}
return new Pair<>(recipesList, selected);
}
Aggregations