use of top.theillusivec4.polymorph.api.common.base.RecipePair in project Polymorph by TheIllusiveC4.
the class AbstractRecipeData method getRecipe.
@SuppressWarnings("unchecked")
@Override
public <T extends Recipe<C>, C extends Container> Optional<T> getRecipe(RecipeType<T> pType, C pInventory, Level pWorld, List<T> pRecipes) {
this.getLoadedRecipe().flatMap(id -> pWorld.getRecipeManager().byKey(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<IRecipePair> newDataset = new TreeSet<>();
List<T> recipes = pRecipes.isEmpty() ? pWorld.getRecipeManager().getRecipesFor(pType, pInventory, pWorld) : pRecipes;
if (recipes.isEmpty()) {
this.setFailing(true);
this.sendRecipesListToListeners(true);
return Optional.empty();
}
for (T entry : recipes) {
ResourceLocation id = entry.getId();
if (ref.get() == null && this.getSelectedRecipe().map(recipe -> recipe.getId().equals(id)).orElse(false)) {
ref.set(entry);
}
newDataset.add(new RecipePair(id, entry.assemble(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 AbstractRecipeData method readNBT.
@Override
public void readNBT(CompoundTag pCompound) {
if (pCompound.contains("SelectedRecipe")) {
this.loadedRecipe = new ResourceLocation(pCompound.getString("SelectedRecipe"));
}
if (pCompound.contains("RecipeDataSet")) {
Set<IRecipePair> dataset = this.getRecipesList();
dataset.clear();
ListTag list = pCompound.getList("RecipeDataSet", Tag.TAG_COMPOUND);
for (Tag inbt : list) {
CompoundTag tag = (CompoundTag) inbt;
ResourceLocation id = ResourceLocation.tryParse(tag.getString("Id"));
ItemStack stack = ItemStack.of(tag.getCompound("ItemStack"));
dataset.add(new RecipePair(id, stack));
}
}
}
use of top.theillusivec4.polymorph.api.common.base.RecipePair in project Polymorph by TheIllusiveC4.
the class PolymorphClientNetwork method sendRecipesList.
private static void sendRecipesList(MinecraftClient pClient, ClientPlayNetworkHandler pHandler, PacketByteBuf pBuf, PacketSender pSender) {
SortedSet<RecipePair> recipeDataset = new TreeSet<>();
Identifier selected = null;
if (pBuf.isReadable()) {
int size = pBuf.readInt();
for (int i = 0; i < size; i++) {
recipeDataset.add(new RecipePairImpl(pBuf.readIdentifier(), pBuf.readItemStack()));
}
if (pBuf.isReadable()) {
selected = pBuf.readIdentifier();
}
}
Identifier finalSelected = selected;
pClient.execute(() -> {
ClientPlayerEntity player = pClient.player;
if (player != null) {
Optional<RecipesWidget> maybeWidget = RecipesWidgetControl.get();
maybeWidget.ifPresent(widget -> widget.setRecipesList(recipeDataset, finalSelected));
if (!maybeWidget.isPresent()) {
RecipesWidgetControl.enqueueRecipesList(recipeDataset, finalSelected);
}
}
});
}
use of top.theillusivec4.polymorph.api.common.base.RecipePair in project Polymorph by TheIllusiveC4.
the class PolymorphPacketDistributorImpl method sendPlayerSyncS2C.
@Override
public void sendPlayerSyncS2C(ServerPlayerEntity pPlayer, SortedSet<RecipePair> pRecipesList, Identifier pSelected) {
PacketByteBuf buf = PacketByteBufs.create();
if (!pRecipesList.isEmpty()) {
buf.writeInt(pRecipesList.size());
for (RecipePair data : pRecipesList) {
buf.writeIdentifier(data.getIdentifier());
buf.writeItemStack(data.getOutput());
}
if (pSelected != null) {
buf.writeIdentifier(pSelected);
}
}
ServerPlayNetworking.send(pPlayer, PolymorphPackets.RECIPE_SYNC, buf);
}
use of top.theillusivec4.polymorph.api.common.base.RecipePair in project Polymorph by TheIllusiveC4.
the class AbstractRecipeData 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);
}
Aggregations