Jump to content

Recommended Posts

Posted

I've recently taken the task of trying to create an uncrafting enchantment where a block is mined and then disassembles itself according to its crafting recipe. I've created the code that allows this to work, however, there are a few problems I can't seem to figure out. First, the items outputted don't always come out in the correct amounts. For example, mining a diamond block outputs three stacks instead of just nine diamonds. This occurs semi-randomly based on the number of items. Second, some blocks when broken throw an ArrayOutOfBoundsException such as the chest when breaking. I'm not sure how this is possible as the index pulled out of the getMatchingStacks is only as large as the size of the array. If you could provide a solution to either problem, it would be greatly appreciated.

 

Here's the code:

Spoiler

@SubscribeEvent
    public static void onBlockHarvested(BlockEvent.HarvestDropsEvent event) {
        Block block = event.getState().getBlock();
        if(event.getHarvester() != null && !event.isSilkTouching()) {
            if(EnchantmentHelper.getEnchantmentLevel(CustomEnchantments.UNCRAFTING, event.getHarvester().getHeldItemMainhand()) > 0) {
                Item item = Item.getItemFromBlock(block);
                ItemStack stack = ItemStack.EMPTY;
                if(item == null) return;
                if(item.getHasSubtypes()) stack = new ItemStack(item, 1, block.damageDropped(event.getState()));
                else stack = new ItemStack(item, 1, 0);
                
                for(Iterator<IRecipe> it= GameRegistry.findRegistry(IRecipe.class).getValuesCollection().iterator(); it.hasNext(); ) {
                    IRecipe recipe = it.next();
                    if(ItemStack.areItemsEqual(stack, recipe.getRecipeOutput())) {
                        event.getDrops().clear();
                        for(Ingredient ingredient : recipe.getIngredients()) {
                            event.getDrops().add(ingredient.getMatchingStacks()[(new Random()).nextInt(ingredient.getMatchingStacks().length)]);
                        }
                        break;
                    }
                }
            }
        }
    }

 

Posted (edited)
19 hours ago, diesieben07 said:

You can call Block#getPickBlock, that should give you the best possible result.

That would make sense, I'm assuming that the RayTraceResult can just be gathered by the position of the block and a insignificant EnumFacing.

 

19 hours ago, diesieben07 said:

getRecipeOutput is not guaranteed to be correct, or even present at all. The only way to correctly determine a recipe's output is to call IRecipe#getCraftingResult, which requires the actual inputs.

Can that be obtained by going through getIngredients()? Otherwise, I do not know how to get an instance of InventoryCrafting for the IRecipe.

 

19 hours ago, diesieben07 said:

Just comparing the Item and damage value (which is what areItemsEqual does) is not enough.

Switching to ItemStack#areItemStacksEqual then.

 

19 hours ago, diesieben07 said:

As for your error: Please show the actual stack trace. Like you said, this should not be possible.

Spoiler

[16:42:18] [Server thread/ERROR] [FML]: Index: 1 Listeners:
[16:42:18] [Server thread/ERROR] [FML]: 0: NORMAL
[16:42:18] [Server thread/ERROR] [FML]: 1: ASM: class com.championash5357.custom.client.event.CustomEvents onBlockHarvested(Lnet/minecraftforge/event/world/BlockEvent$HarvestDropsEvent;)V
[16:42:18] [Server thread/FATAL] [minecraft/MinecraftServer]: Error executing task
java.util.concurrent.ExecutionException: java.lang.IllegalArgumentException: bound must be positive
    at java.util.concurrent.FutureTask.report(Unknown Source) ~[?:1.8.0_201]
    at java.util.concurrent.FutureTask.get(Unknown Source) ~[?:1.8.0_201]
    at net.minecraft.util.Util.runTask(Util.java:54) [Util.class:?]
    at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:798) [MinecraftServer.class:?]
    at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:743) [MinecraftServer.class:?]
    at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:192) [IntegratedServer.class:?]
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:592) [MinecraftServer.class:?]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_201]
Caused by: java.lang.IllegalArgumentException: bound must be positive
    at java.util.Random.nextInt(Unknown Source) ~[?:1.8.0_201]
    at com.championash5357.custom.client.event.CustomEvents.onBlockHarvested(CustomEvents.java:257) ~[CustomEvents.class:?]
    at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_31_CustomEvents_onBlockHarvested_HarvestDropsEvent.invoke(.dynamic) ~[?:?]
    at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:90) ~[ASMEventHandler.class:?]
    at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:182) ~[EventBus.class:?]
    at net.minecraftforge.event.ForgeEventFactory.fireBlockHarvesting(ForgeEventFactory.java:321) ~[ForgeEventFactory.class:?]
    at net.minecraft.block.Block.dropBlockAsItemWithChance(Block.java:721) ~[Block.class:?]
    at net.minecraft.block.Block.dropBlockAsItem(Block.java:710) ~[Block.class:?]
    at net.minecraft.block.Block.harvestBlock(Block.java:929) ~[Block.class:?]
    at net.minecraft.server.management.PlayerInteractionManager.tryHarvestBlock(PlayerInteractionManager.java:353) ~[PlayerInteractionManager.class:?]
    at net.minecraft.server.management.PlayerInteractionManager.blockRemoving(PlayerInteractionManager.java:261) ~[PlayerInteractionManager.class:?]
    at net.minecraft.network.NetHandlerPlayServer.processPlayerDigging(NetHandlerPlayServer.java:732) ~[NetHandlerPlayServer.class:?]
    at net.minecraft.network.play.client.CPacketPlayerDigging.processPacket(CPacketPlayerDigging.java:56) ~[CPacketPlayerDigging.class:?]
    at net.minecraft.network.play.client.CPacketPlayerDigging.processPacket(CPacketPlayerDigging.java:12) ~[CPacketPlayerDigging.class:?]
    at net.minecraft.network.PacketThreadUtil$1.run(PacketThreadUtil.java:21) ~[PacketThreadUtil$1.class:?]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) ~[?:1.8.0_201]
    at java.util.concurrent.FutureTask.run(Unknown Source) ~[?:1.8.0_201]
    at net.minecraft.util.Util.runTask(Util.java:53) ~[Util.class:?]
    ... 5 more
[16:42:34] [Server thread/ERROR] [FML]: Exception caught during firing event net.minecraftforge.event.world.BlockEvent$HarvestDropsEvent@38d72c8e:
java.lang.IllegalArgumentException: bound must be positive
    at java.util.Random.nextInt(Unknown Source) ~[?:1.8.0_201]
    at com.championash5357.custom.client.event.CustomEvents.onBlockHarvested(CustomEvents.java:257) ~[CustomEvents.class:?]
    at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_31_CustomEvents_onBlockHarvested_HarvestDropsEvent.invoke(.dynamic) ~[?:?]
    at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:90) ~[ASMEventHandler.class:?]
    at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:182) [EventBus.class:?]
    at net.minecraftforge.event.ForgeEventFactory.fireBlockHarvesting(ForgeEventFactory.java:321) [ForgeEventFactory.class:?]
    at net.minecraft.block.Block.dropBlockAsItemWithChance(Block.java:721) [Block.class:?]
    at net.minecraft.block.Block.dropBlockAsItem(Block.java:710) [Block.class:?]
    at net.minecraft.block.Block.harvestBlock(Block.java:929) [Block.class:?]
    at net.minecraft.block.BlockContainer.harvestBlock(BlockContainer.java:92) [BlockContainer.class:?]
    at net.minecraft.server.management.PlayerInteractionManager.tryHarvestBlock(PlayerInteractionManager.java:353) [PlayerInteractionManager.class:?]
    at net.minecraft.server.management.PlayerInteractionManager.blockRemoving(PlayerInteractionManager.java:261) [PlayerInteractionManager.class:?]
    at net.minecraft.network.NetHandlerPlayServer.processPlayerDigging(NetHandlerPlayServer.java:732) [NetHandlerPlayServer.class:?]
    at net.minecraft.network.play.client.CPacketPlayerDigging.processPacket(CPacketPlayerDigging.java:56) [CPacketPlayerDigging.class:?]
    at net.minecraft.network.play.client.CPacketPlayerDigging.processPacket(CPacketPlayerDigging.java:12) [CPacketPlayerDigging.class:?]
    at net.minecraft.network.PacketThreadUtil$1.run(PacketThreadUtil.java:21) [PacketThreadUtil$1.class:?]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_201]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_201]
    at net.minecraft.util.Util.runTask(Util.java:53) [Util.class:?]
    at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:798) [MinecraftServer.class:?]
    at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:743) [MinecraftServer.class:?]
    at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:192) [IntegratedServer.class:?]
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:592) [MinecraftServer.class:?]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_201]

The line referred to is "ItemStack ing = ingredient.getMatchingStacks()[(new Random()).nextInt(ingredient.getMatchingStacks().length)];" I misread the error. Apologies. Fixed the issue.

 

Here is the rearranged code (getRecipeOutput not changed yet as for lack of knowledge of how to):

Spoiler

                ItemStack stack = block.getPickBlock(event.getState(), new RayTraceResult(new Vec3d(event.getPos().getX(), event.getPos().getY(), event.getPos().getZ()), EnumFacing.NORTH), event.getWorld(), event.getPos(), event.getHarvester());
                List<ItemStack> drops = Lists.newArrayList();
                
                for(Iterator<IRecipe> it= GameRegistry.findRegistry(IRecipe.class).getValuesCollection().iterator(); it.hasNext(); ) {
                    IRecipe recipe = it.next();
                    if(ItemStack.areItemStacksEqual(stack, recipe.getRecipeOutput())) {
                        event.getDrops().clear();
                        for(Ingredient ingredient : recipe.getIngredients()) {

                            if(ingredient.getMatchingStacks().length == 0) continue;
                            ItemStack ing = ingredient.getMatchingStacks()[(new Random()).nextInt(ingredient.getMatchingStacks().length)];
                            boolean is_drop = false;
                            for(ItemStack drop : drops) {
                                if(ItemStack.areItemsEqual(drop, ing) && drop.getCount() != drop.getMaxStackSize()) {
                                    is_drop = true;
                                    drop.grow(1);
                                }
                            }
                            if(!is_drop) drops.add(ing);
                        }
                        for(ItemStack drop : drops) {
                            event.getDrops().add(drop);
                        }
                        break;
                    }
                }

 

Edited by ChampionAsh5357
Posted
7 hours ago, diesieben07 said:

Like I said, you would need to have the inputs that were originally used to craft the item that was just mined. But that's impossible to keep track of.

 

getRecipeOutput will work for some things, but it might produce strange results for others.

The issue is here that getRecipeOutput() will work for and Shaped or Shapeless recipe defined in the json files since they all have getRecipeOutput() as the main output. Anything pertaining to banners would not work with the current setup due to patterns. That would require complete markings of all banner inputs with IRecipe which I don't believe is possible without freezing or lagging out the game. If we were to ignore those custom IRecipes, that would just make it impossible to uncraft anything customly defined. However, this still leaves the question of multiblock structures being mined. The bed, for example, should output its contents but instead returns the block itself.

Posted (edited)

After further research, it seems as there 4 3 main problems excluding the banner nbt one.

1. The piston if mined when extended at the head will drop the block and the uncrafted recipe.

2. The bed drops the block instead of the recipe.

3. The shulker box drops the block with the items still inside instead of the recipe.

4. There seems to be some sort of item duplication that increases over each block mined with recipes having more than one of the same block.

The first three seem to originate from Block#dropBlockAsItemWithChance while I can't seem to find the cause of the fourth problem.

Edited by ChampionAsh5357
Found the issue and resolved it for the item dupe.
Posted (edited)
On 8/4/2019 at 5:46 AM, diesieben07 said:

Like I said, you cannot use block drops. They are highly inaccurate (diamond ore drops diamonds instead of diamond ore blocks). You need to use getPickBlock.

If you looked at the above code, you can see that I have been using Block#getPickBlock since you've suggested it. The issue is with actual Minecraft programming itself it seems as the Bed (BlockBed) and Shulker Box (BlockShulkerBox) has no call to triggering the BlockEvent#HarvestDropsEvent within it. For the Piston Extension (BlockPistonMoving or BlockPistonExtension), it calls the command twice first dropping the block and then the recipe. The reason why I mentioned Block#dropBlockAsItemWithChance is because that is the only place the trigger fires when a block is broken in this way. The other trigger provides no use. The above three classes do not have access to the trigger or accesses it abnormally.

Edited by ChampionAsh5357
Posted

Backing up to the banner, could you instead reverse the patterns with the colors and calculate the output that way? Basically, read the itemstack nbt if it is a banner and drop items corresponding to the color and the pattern used?

Posted (edited)

I was able to find a decomposition using BannerPattern and EnumDyeColor by reading the nbt. However, the same issue as before persists where HarvestDropsEvent is not triggered. The other three mentioned above do not seem to not trigger the event either. I'm not exactly sure where the method needed to use it was overwritten, but the trigger is never called to allow the event to happen at least in these four cases.

Edited by ChampionAsh5357
Posted

Okay, I've managed to work around three of the four blocks using event canceling and triggering the HarvestDropsEvent in the BreakEvent. However, I still haven't managed to get the shulker box to disappear without having it drop its item. Is there any way to set a block to air within BreakEvent without having it drop its item?

Posted
6 hours ago, diesieben07 said:

You would have to empty the shulker box inventory before breaking the block.

I've already tried that, but your tip gave me the solution. I had to remove the tile entity before setting the block to air and canceling since the shulker box only drops in the normal way as long as the tile entity exists. I just have to create a player capability to allow storage of blocks to reverse recipes with more than one output. Thank you for the all the help you have provided.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • So am trying to make a custom 1.19.2 modpack and everything works until I add Oculus. I have tried Oculus+Embedium and Oculus+Rubdium and by themselves they work but as soon as I add anything it crashes no matter what it is. The modpack works fine with just Embedium and Rubdium. Can you help me to see if this is something i can fix or do i just have to deal with not having shaders. Here is the crash log. Thank you for your time. https://paste.ee/p/WXfNZ24K
    • New users at Temureceive a 40 Off discount on orders over 40 Off Use the code [{acx318439}]] during checkout to get TemuDiscount 40 Off For New Users. You n save 40 Off off your first order with the Promo Code available for a limited time only. Extra 30% off for new and existing customers + Up to $40 Off % off & more. Temu Promo Codes for New users- [{acx318439}]] Temudiscount code for New customers- [{acx318439}]] Temu $40 Off Promo Code- [{acx318439}]] what are Temu codes- acx318439 does Temu give you $40 Off - [{acx318439}]] Yes Verified Temu Promo Code january 2025- {acx318439} TemuNew customer offer {acx318439} Temudiscount codejanuary 2025 {acx318439} 40 off Promo Code Temu {acx318439} Temu 40% off any order {acx318439} 40 dollar off Temu code {acx318439} TemuCoupon $40 Off off for New customers There are a number of discounts and deals shoppers n take advantage of with the Teemu Coupon Bundle [{acx318439}]]. TemuCoupon $40 Off off for New customers [{acx318439}]] will save you $40 Off on your order. To get a discount, click on the item to purchase and enter the code. You n think of it as a supercharged savings pack for all your shopping needs Temu Promo Code 80% off – [{acx318439}]] Free Temu codes 50% off – [{acx318439}]] TemuCoupon $40 Off off – [{acx318439}]] Temu buy to get ₱39 – [{acx318439}]] Temu 129 coupon bundle – [{acx318439}]] Temu buy 3 to get €99 – [{acx318439}]] Exclusive $40 Off Off TemuDiscount Code Temu $40 Off Off Promo Code : (acx318439) Temu Discount Code $40 Off Bundle acx318439) acx318439 Temu $40 Off off Promo Code for Exsting users : acx318439) Temu Promo Code $40 Off off Temu 40 Off coupon code (acx318439) will save you 40 Off on your order. To get a discount, click on the item to purchase and enter the code. Yes, Temu offers 40 Off Coupon Code “acx318439” for Existing Customers.  You can get a 40 Off bonus plus 30% off any purchase at Temu with the 40 Off Coupon Bundle at Temu if you sign up with the referral code [{acx318439}]] and make a first purchase of $40 Off or more. Temu Promo Code 40 off-{acx318439} Temu Promo Code -{acx318439} Temu Promo Code $40 Off off-{acx318439} kubonus code -{acx318439} Get ready to unlock a world of savings with our free Temu UK coupons! We’ve got you covered with a wide range of Temu UK coupon code options that will help you maximize your shopping experience.30% Off Temu UK Coupons, Promo Codes + 25% Cash Back [ acx318439]   Yes, Temu offers 40 off coupon code {acx318439} for first-time users. You can get a $40 bonus plus 40% off any purchase at Temu with the $40 Coupon Bundle if you sign up with the referral code [{acx318439}]] and make a first purchase of $40 or more. If you are who wish to join Temu, then you should use this exclusive TemuCoupon code 40 off (acx318439) and get 40 off on your purchase with Temu. You can get a 40% discount with TemuCoupon code {acx318439}. This exclusive offer is for existing customers and can be used for a 40 reduction on your total purchase. Enter coupon code {acx318439} at checkout to avail of the discount. You can use the code {acx318439} to get a 40 off TemuCoupon as a new customer. Apply this TemuCoupon code $40 off (acx318439) to get a $40 discount on your shopping with Temu. If you’re a first-time user and looking for a TemuCoupon code $40 first time user(acx318439) then using this code will give you a flat $40 Off and a 90% discount on your Temu shopping.     •    acx318439: Enjoy flat 40% off on your first Temu order.     •    acx318439: Download the Temu app and get an additional 40% off.     •    acx318439: Celebrate spring with up to 90% discount on selected items.     •    acx318439: Score up to 90% off on clearance items.     •    acx318439: Beat the heat with hot summer savings of up to 90% off.     •    acx318439: Temu UK Coupon Code to 40% off on Appliances at Temu. How to Apply Temu Coupon Code? Using the TemuCoupon code $40 off is a breeze. All you need to do is follow these simple steps:     1    Visit the Temu website or app and browse through the vast collection of products.     2    Once you’ve added the items you wish to purchase to your cart, proceed to the checkout page.     3    During the checkout process, you’ll be prompted to enter a coupon code or promo code.     4    Type in the coupon code: [{acx318439}]] and click “Apply.”     5    Voila! You’ll instantly see the $40 discount reflected in your total purchase amount. Temu New User Coupon: Up To 90% OFF For Existing Customers Temu Existing customer’s coupon codes are designed just for new customers, offering the biggest discounts 90% and the best deals currently available on Temu. To maximize your savings, download the Temu app and apply our Temu new user coupon during checkout.     •    acx318439: New users can get up to 80% extra off.     •    acx318439: Get a massive 40% off your first order!     •    acx318439: Get 20% off on your first order; no minimum spending required.     •    acx318439: Take an extra 15% off your first order on top of existing discounts.     •    acx318439: Temu UK Enjoy a 40% discount on your entire first purchase.  
    • What do I do now when it says "1 error"?
    • Hello everyone new here how are you all?
    • I haven't tested it but under https://minecraft.wiki/w/Items_model_definition it says now:   So I guess the resource location must have changed with 1.24.4, which means you need to move your models/item/ to the new source. But as I said I haven't tested this so it also may be that this wont work. Nevertheless give it a try      EDIT (important) So now I tested it and found out how it works   Let the model files (e.g. the .json from blockbench) within "assets/<your_mod_id>/models/item" In addition to that do the following: Every model you added will need a new file under "assets/<your_mod_id>/items" That file is also a JSON and looks like this: { "model": { "type": "minecraft:model", "model": "your_mod_id:item/custom_item" } } - "type" can be minecraft:model, minecraft:composite, minecraft:condition, minecraft:select, minecraft:range_dispatch, minecraft:empty, minecraft:bundle/selected_item or minecraft:special. (In most cases you would need minecraft:model) - "model" is the path to your actual model for this item. For example the value above would point to "assets/your_mod_id/models/item/custom_item"
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.