#Trading with non-villager mobs (specifically vanilla mobs)

128 messages ยท Page 1 of 1 (latest)

jagged torrent
#

Or just open up a custom gui with an evoker is right clicked

modest matrix
#

Yes, it would be possible. I would recommend using the UseEntityCallback or whatever fapi calls it

jagged torrent
#
public static void registerEvokerInteractions() {
        UseEntityCallback.EVENT.register((player, world, hand, entity, hitResult) -> {
            if (
                    !player.isSpectator()
                    && entity.getType().equals(EntityType.EVOKER)
                    && player.hasStatusEffect(ModEffects.HERO_OF_THE_PILLAGE)
            ) {
                if (!world.isClient) {
                    NamedScreenHandlerFactory screenHandlerFactory = //createScreenHandlerFactoryImplementation();
                }
            }
            return ActionResult.SUCCESS;
        });
    }
#

this is what i have so far

vital nova
#

there is MerchantScreenHandler, which you can turn into a NamedScreenHandlerFactory using SimpleNamedScreenHandlerFactory, this is how vanilla does it.

vanilla example for villager:

player.openHandledScreen(
            new SimpleNamedScreenHandlerFactory((syncId, playerInventory, playerx) -> new MerchantScreenHandler(syncId, playerInventory, this), name)
        );

(this is at Merchant#sendOffers(), if you want to see more of how vanilla does it)

jagged torrent
candid warren
#

@jagged torrent What version are you working with? Because I'm making something similar for 1.21.4 and already have everything but the actual trades working (it had the trading screen before but I changed it, it should be pretty easy to add trades back tho)

jagged torrent
#

but it returns an Optional<Optional<EnchantmentConversionRecipe>

#

should I just use a series of .get()'s to extract the recipe from the optional?

#

that seems risky

#

but I dont see another way with my setup

#
Optional<Optional<EnchantmentConversionRecipe>> recipe = context.get((world, pos) -> world.getRecipeManager().getFirstMatch(EnchantmentConversionRecipe.Type.INSTANCE, this.input, world));

this.result.setStack(0, new ItemStack(recipe.get().get().getOutput()));
``` this seems cursed
jagged torrent
#

can you not use enchanted books in recipes?

#

does it have to be hardcoded

modest matrix
#
context.get(
  (world, pos) -> world.getRecipeManager()
      .getFirstMatch(
        RecipeType.BLASTING, 
        this.input, 
        world
      )
)
.flatMap(Function.identity())
.map(recipe -> recipe.getOutput())
.ifPresentOrElse(
        stack -> this.result.setStack(0, stack),
        () -> this.result.setStack(0, ItemStack.EMPTY)
);
``` try something like this?
jagged torrent
#

ok

jagged torrent
modest matrix
#

fixed ๐Ÿ‘

#

had to shim it to get the compiler to stop screaming at me

jagged torrent
#

oh

#

ok

modest matrix
#
var stack = context.get(
  (world, pos) -> world.getRecipeManager()
      .getFirstMatch(
        RecipeType.BLASTING, 
        this.input, 
        world
      )
)
.flatMap(Function.identity())
.map(recipe -> recipe.getOutput())
.orElse(ItemStack.EMPTY);

this.result.setStack(0, stack);
``` something like this might also work, if you like it better?
jagged torrent
#

it looks nice but im still not sure how to use enchanted books in recipes

modest matrix
#

ah

#

you said 1.20.1?

jagged torrent
#

yes

modest matrix
#

hmm.

#

rough.

jagged torrent
#

im converting a vanilla enchant to a modded enchant

#

with the recipe

#

or recipes

modest matrix
#

so both result and input?

jagged torrent
#

yes

modest matrix
#

concerning ๐Ÿ™‚

jagged torrent
#

:)! ! !! !

modest matrix
#

I haven't done this in a minute.

jagged torrent
#

o

modest matrix
#

what does your matcher look like rn?

jagged torrent
#

?

#

the match function in recipemanager for 1.20.1?

modest matrix
#

๐Ÿ‘

jagged torrent
#

not sure why its red

#

but this is vanilla

modest matrix
#

it's okay

#

I meant the one in your recipe?

#

if there is one in that version?

#

Recipe#matches

jagged torrent
modest matrix
#

okay

#

describe a recipe to me? EnchantedBook{SHARPNESS:5} -> EnchantedBook{CUSTOM:5}?

jagged torrent
#

something like that

modest matrix
jagged torrent
#

exactly liek that

modest matrix
#
var enchantment =  Enchantments.AQUA_AFFINITY;
var enchantmentResult = Enchantments.BINDING_CURSE;
var stack = Items.DIAMOND_HELMET.getDefaultStack();
int level = EnchantmentHelper.getLevel(enchantment, stack);
if (level > 0) {
    var map = EnchantmentHelper.get(stack);
    map.remove(enchantment);
    map.put(enchantmentResult, 1);
    EnchantmentHelper.set(map, stack);
}
#

something like that?

#

or what is something else the problem?

modest matrix
#

if the stack has AQUA_AFFINITY, it replaces AQUA_AFFINITY with BINDING_CURSE on the stack

jagged torrent
#

sorry that is not what I am trying to do

#

well it could work

#

but the input can only accept singular enchantment enchanted books

#

so it could replace the enchantment with the modded one

#

on a book

#

does that work

modest matrix
#

I'm not sure I understand what the poblem is?

#

the code I've provided is an example

#

it should work with all items

#

?

jagged torrent
#

but isnt that hardcoded

modest matrix
#

it doesn't have to be though

#

it's up to you to un hardcode it

#

it's hardcoded to make it easy to understand

#

if there is something you don't understand about how, I'd be glad to help with that

jagged torrent
#

so I am using slots and an update function in a screenhandler to handle the crafting

#

in the update function is where the recipe would go

#

how would I create a list of enchantments match another list of enchantments without using traditional recipes

#

because you cant use a normal json file right

#

and I dont want it to replace what is in the input

#

I want a new book in the output

#

so I already cleared the input in my code

#

using slots

jagged torrent
#

so I can make a list of "recipes"

#

without having to use a switch statement ๐Ÿ’€

#

for each enchantment pair

#

that is my problem

modest matrix
#

a Map from Enchantment to Enchantment

#

with a get Default method

jagged torrent
#

where do I store the map

#

I have never used them before

modest matrix
#

idk

jagged torrent
#

can I use a json file

lyric heath
#

If you want to

#

Maps are a basic programming concept, there's plenty of info on them.

jagged torrent
# lyric heath Maps are a basic programming concept, there's plenty of info on them.

this is my solution to the problem

private void updateResult() {
        ItemStack inputStack = this.input.getStack(0);
        ItemStack outputStack = new ItemStack(Items.ENCHANTED_BOOK);

        if (inputStack.isEmpty()) {
            this.result.setStack(0, ItemStack.EMPTY);
        } else {
            NbtList nbtList = inputStack.getEnchantments();

            for (int i = 0; i < nbtList.size(); i++) {

                NbtCompound nbtCompound = nbtList.getCompound(i);

                Identifier inputEnchantment = EnchantmentHelper.getIdFromNbt(nbtCompound);
                int level = EnchantmentHelper.getLevelFromNbt(nbtCompound);

                Enchantment outputEnchantment;
                if (inputEnchantment != null && inputEnchantment.equals(Registries.ENCHANTMENT.getId(Enchantments.SHARPNESS))) {

                    float chance = random.nextFloat();
                    if (chance < 0.1) {

                        outputEnchantment = ModEnchants.BLEEDING;

                    } else {

                        outputEnchantment = ModEnchants.DULLNESS;

                    }

                } else {

                    outputEnchantment = this.alternativeEnchantMap.get(inputEnchantment);

                }

                EnchantedBookItem.addEnchantment(outputStack, new EnchantmentLevelEntry(outputEnchantment, level));
            }

            this.result.setStack(0, outputStack);
        }

        this.sendContentUpdates();
    }
#

I just iterate over the enchantments and apply them to a map

#

then apply the new enchants to a book

#

sharpness gets special treatment with randomness

#

but I think im done

#

here is my above definition of the map

jagged torrent
jagged torrent
#

inside of useEntityCallback implementation event register

#

btw my screen does not appear when this is called

#

no crashing just no response

lyric heath
jagged torrent
#

I thought that's what openScreenHandler did

#

I looked on the documentation for what I need to do

#

how do I set the screen aswell?

lyric heath
#

Oh I'm blind I didn't see the first line

modest matrix
jagged torrent
jagged torrent
#

still no screen

jagged torrent
#

is there a problem with my screenhandler?

#

or how I register the screenhandler

jagged torrent
#

lol...

#

I never registered the actual handled screen.....

#

only the screenhandler....

#

lol i never implemented quickmove so i crashed my game

#

thats not a problem for here tho