use of net.minecraft.util.Identifier 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);
}
use of net.minecraft.util.Identifier 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 net.minecraft.util.Identifier 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 net.minecraft.util.Identifier 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 net.minecraft.util.Identifier in project SpeedRunIGT by RedLime.
the class FontUtils method addFont.
public static void addFont(HashMap<Identifier, List<Font>> map, File file, File configFile) {
FileInputStream fileInputStream = null;
STBTTFontinfo sTBTTFontinfo = null;
ByteBuffer byteBuffer = null;
Throwable throwable = null;
try {
fileInputStream = new FileInputStream(file);
sTBTTFontinfo = STBTTFontinfo.malloc();
byteBuffer = TextureUtil.readAllToByteBuffer(fileInputStream);
byteBuffer.flip();
if (!STBTruetype.stbtt_InitFont(sTBTTFontinfo, byteBuffer)) {
return;
}
Identifier fontIdentifier = new Identifier(SpeedRunIGT.MOD_ID, file.getName().toLowerCase(Locale.ROOT).replace(".ttf", "").replaceAll(" ", "_").replaceAll("[^a-z0-9/._-]", ""));
ArrayList<Font> fontArrayList = new ArrayList<>();
FontConfigure fontConfigure;
if (configFile != null && configFile.exists()) {
fontConfigure = FontConfigure.fromJson(FileUtils.readFileToString(configFile, StandardCharsets.UTF_8));
} else {
fontConfigure = FontConfigure.create();
}
fontArrayList.add(new TrueTypeFont(byteBuffer, sTBTTFontinfo, fontConfigure.size, fontConfigure.oversample, fontConfigure.shift[0], fontConfigure.shift[1], fontConfigure.skip));
SpeedRunIGT.FONT_MAPS.put(fontIdentifier, new FontIdentifier(file, fontIdentifier, fontConfigure));
fontArrayList.add(new BlankFont());
map.put(fontIdentifier, fontArrayList);
} catch (FileNotFoundException e) {
if (sTBTTFontinfo != null)
sTBTTFontinfo.free();
MemoryUtil.memFree(byteBuffer);
} catch (IOException throwable1) {
throwable = throwable1;
} finally {
try {
if (fileInputStream != null)
fileInputStream.close();
} catch (IOException e) {
if (throwable != null)
throwable.addSuppressed(e);
}
}
}
Aggregations