Search in sources :

Example 16 with Identifier

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);
}
Also used : Pair(net.minecraft.util.Pair) RecipePair(top.theillusivec4.polymorph.api.common.base.RecipePair) RecipePairImpl(top.theillusivec4.polymorph.common.impl.RecipePairImpl) SortedSet(java.util.SortedSet) NbtElement(net.minecraft.nbt.NbtElement) World(net.minecraft.world.World) NbtList(net.minecraft.nbt.NbtList) Set(java.util.Set) Inventory(net.minecraft.inventory.Inventory) Recipe(net.minecraft.recipe.Recipe) PolymorphMod(top.theillusivec4.polymorph.common.PolymorphMod) AtomicReference(java.util.concurrent.atomic.AtomicReference) TreeSet(java.util.TreeSet) ItemStack(net.minecraft.item.ItemStack) NbtCompound(net.minecraft.nbt.NbtCompound) List(java.util.List) ServerPlayerEntity(net.minecraft.server.network.ServerPlayerEntity) NbtType(net.fabricmc.fabric.api.util.NbtType) RecipeData(top.theillusivec4.polymorph.api.common.component.RecipeData) Optional(java.util.Optional) Identifier(net.minecraft.util.Identifier) PolymorphApi(top.theillusivec4.polymorph.api.PolymorphApi) RecipeType(net.minecraft.recipe.RecipeType) Identifier(net.minecraft.util.Identifier) RecipePair(top.theillusivec4.polymorph.api.common.base.RecipePair) TreeSet(java.util.TreeSet) RecipePairImpl(top.theillusivec4.polymorph.common.impl.RecipePairImpl) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 17 with Identifier

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));
        }
    }
}
Also used : Identifier(net.minecraft.util.Identifier) RecipePair(top.theillusivec4.polymorph.api.common.base.RecipePair) NbtCompound(net.minecraft.nbt.NbtCompound) RecipePairImpl(top.theillusivec4.polymorph.common.impl.RecipePairImpl) NbtList(net.minecraft.nbt.NbtList) NbtElement(net.minecraft.nbt.NbtElement) ItemStack(net.minecraft.item.ItemStack)

Example 18 with Identifier

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);
}
Also used : Pair(net.minecraft.util.Pair) RecipePair(top.theillusivec4.polymorph.api.common.base.RecipePair) RecipePairImpl(top.theillusivec4.polymorph.common.impl.RecipePairImpl) SortedSet(java.util.SortedSet) NbtElement(net.minecraft.nbt.NbtElement) World(net.minecraft.world.World) NbtList(net.minecraft.nbt.NbtList) Set(java.util.Set) Inventory(net.minecraft.inventory.Inventory) Recipe(net.minecraft.recipe.Recipe) PolymorphMod(top.theillusivec4.polymorph.common.PolymorphMod) AtomicReference(java.util.concurrent.atomic.AtomicReference) TreeSet(java.util.TreeSet) ItemStack(net.minecraft.item.ItemStack) NbtCompound(net.minecraft.nbt.NbtCompound) List(java.util.List) ServerPlayerEntity(net.minecraft.server.network.ServerPlayerEntity) NbtType(net.fabricmc.fabric.api.util.NbtType) ItemComponent(dev.onyxstudios.cca.api.v3.item.ItemComponent) Optional(java.util.Optional) Identifier(net.minecraft.util.Identifier) StackRecipeData(top.theillusivec4.polymorph.api.common.component.StackRecipeData) PolymorphApi(top.theillusivec4.polymorph.api.PolymorphApi) RecipeType(net.minecraft.recipe.RecipeType) Identifier(net.minecraft.util.Identifier) RecipePair(top.theillusivec4.polymorph.api.common.base.RecipePair) TreeSet(java.util.TreeSet) RecipePairImpl(top.theillusivec4.polymorph.common.impl.RecipePairImpl) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 19 with Identifier

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;
        }
    }
}
Also used : Identifier(net.minecraft.util.Identifier) PolymorphPacketDistributor(top.theillusivec4.polymorph.api.common.base.PolymorphPacketDistributor) ScreenHandler(net.minecraft.screen.ScreenHandler) AbstractCompatibilityModule(top.theillusivec4.polymorph.common.integration.AbstractCompatibilityModule) PolymorphCommon(top.theillusivec4.polymorph.api.common.base.PolymorphCommon) SortedSet(java.util.SortedSet)

Example 20 with Identifier

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);
        }
    }
}
Also used : TrueTypeFont(net.minecraft.client.font.TrueTypeFont) STBTTFontinfo(org.lwjgl.stb.STBTTFontinfo) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) Font(net.minecraft.client.font.Font) BlankFont(net.minecraft.client.font.BlankFont) TrueTypeFont(net.minecraft.client.font.TrueTypeFont) Identifier(net.minecraft.util.Identifier) BlankFont(net.minecraft.client.font.BlankFont)

Aggregations

Identifier (net.minecraft.util.Identifier)343 NbtList (net.minecraft.nbt.NbtList)36 ItemStack (net.minecraft.item.ItemStack)31 Item (net.minecraft.item.Item)28 NbtCompound (net.minecraft.nbt.NbtCompound)22 NbtElement (net.minecraft.nbt.NbtElement)22 Inject (org.spongepowered.asm.mixin.injection.Inject)22 IOException (java.io.IOException)18 Block (net.minecraft.block.Block)18 MinecraftClient (net.minecraft.client.MinecraftClient)15 BlockItem (net.minecraft.item.BlockItem)15 BlockPos (net.minecraft.util.math.BlockPos)15 Map (java.util.Map)12 BlockState (net.minecraft.block.BlockState)12 ArrayList (java.util.ArrayList)11 VertexConsumer (net.minecraft.client.render.VertexConsumer)11 ResourceManager (net.minecraft.resource.ResourceManager)11 SoundEvent (net.minecraft.sound.SoundEvent)11 ServerPlayerEntity (net.minecraft.server.network.ServerPlayerEntity)10 LiteralText (net.minecraft.text.LiteralText)10