Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.16.4] Adding a Listener to net.minecraft.advancements.criterion.ItemDurabilityTrigger


izofar
 Share

Recommended Posts

My intent is to hijack the event when a piece of armor's durability reaches zero, and replace the item with another once this occurs. Currently I've read there is no Forge hook for this, and that I should add a listener to the ItemDurabilityTrigger. Source:

I, however, have no clue how to do this. I've found ItemDurabilityTrigger#changedDurability, which is what I want to intercept.

I've tried to look at how this method is used, and I found net.minecraft.data.advancements.NetherAdvancements, which checks if warped fungus on a stick was used. I don't believe this is the condition in which I would wish to use it, though.

While I understand and have been using events and listeners, is Draco18s instruction/advice a Java concept I am too noob to know, or are there Forge-specific  libraries and techniques that I must use to register listeners/events and all of that?

Link to comment
Share on other sites

Posted (edited)
10 minutes ago, diesieben07 said:

Is this your armor item or just any armor item?

This is my modded armor. Instantiated: Item moddedHelmet = new ArmorItem( ...);

If I'm correct, are you about to suggest I make my own armor class that inherits from ArmorItem (or something) and override some itemDamaged method? Because now that makes sense

Despite this, I do intend to do this for other armor items as well in the future

Edited by izofar
Link to comment
Share on other sites

Posted (edited)

Excellent, thanks. If I wanted, for example, gold armor to drop ingots upon breaking, how would I achieve this for vanilla armor? Would I have to duplicate the gold armor item instantiations (except inherited from the custom class) and register them under the minecraft:golden_*slot* resource? Or is writing a hook for the trigger a more desirable solution?

In addition, where is this damage item method? I can't find it in the type hierarchy. I thought this would be a member of the item stack, not the item class.

Edited by izofar
Link to comment
Share on other sites

25 minutes ago, izofar said:

If I wanted, for example, gold armor to drop ingots upon breaking, how would I achieve this for vanilla armor?

Not easily. Forge has an event for this (PlayerDestroyItemEvent), but it seems it is not implemented for armor. You could probably hack something together with CriteriaTriggers.ITEM_DURABILITY_CHANGED, but it would probably not work very well.

27 minutes ago, izofar said:

n addition, where is this damage item method? I can't find it in the type hierarchy.

It's in IForgeItem, which Item implements.

  • Thanks 1
Link to comment
Share on other sites

Posted (edited)

Is there a way to access the player owning the inventory an item stack is in? If any? Or to change the item in an itemstack?

Two ways I've looked at this is to do onArmorTick(PlayerEntity player) to store the player but I don't think this would work for multiplayer (since there is only one modded armor class instantiated?). The other means I'm trying is to set the entity representation of the item stack obtained from setDamage to the entity representation of a newly instantiated itemstack of the desired replacement material. I am not optimistic about either approach.

Edited by izofar
Link to comment
Share on other sites

I have a  ModdedArmorItem extends ArmorItem class which overrides setDamage(ItemStack stack, int damage), in which i want to check if the durability is zero, then replace the item stack with another item. Functionally, I want armor to leave an item when it breaks (to simplify, break into another armor item that resides in the same slot).

Link to comment
Share on other sites

Posted (edited)

Ah, I see. Is the <T extends LivingEntity> the player, in this case?

In addition, I'm not familiar with the <> Notation as used in `public <T extends LivingEntity> int damageItem(...)`. Does this just place restriction on T? How does one use the Consumer<T> input?

I'm looking at ItemStack#hurtAndBreak for direction on how to use the inputs. But, I've found that breaking an item occurs outside of the damageItem call. which means that giving the player the replacement item inside of damageItem will happen before the original is broken and removed.

Edited by izofar
Link to comment
Share on other sites

15 minutes ago, izofar said:

In addition, I'm not familiar with the <> Notation as used in `public <T extends LivingEntity> int damageItem(...)`. Does this just place restriction on T?

Please look up basic Java, generics in this case.

16 minutes ago, izofar said:

How does one use the Consumer<T> input?

It is to be called when the item actually breaks.

 

Link to comment
Share on other sites

6 minutes ago, diesieben07 said:

generics in this case

I'm familiar with generics, just not its use in function definitions. I appreciate your patience.

6 minutes ago, diesieben07 said:

How does one use the Consumer<T> input?

I at first failed to notice that this is a Java-specific concept not attributable to Forge. I apologize for the Java questions. Thank you for your help.

Link to comment
Share on other sites

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
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.

 Share



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • Hi, I'm one of the user who's using Custom NPCs 1.7.10. But I have one problem. I see some items are equipped to NPC rotated, not in the right position. How can I rotate the arm of NPC?
    • Hello. I decided to try to make a mod, but for some reason I stumbled at the very beginning. How do I register a block for it to appear in the inventory? It seems that I'm trying to do it according to the manual, but only the procedure for registering new blocks is described there. So I made a mod class. I registered the registration, but when I install it into the game, I do not see my block in the intent. And I will also be grateful if you give a link to some kind of detailed manual. So that step by step it was described how what can be created. Thanks.   @Mod("examplemod") public class ExampleMod { // Directly reference a log4j logger. private static final Logger LOGGER = LogManager.getLogger(); private static final String MODID = "0.1"; private static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, MODID); public static final RegistryObject<Block> MyBlock = BLOCKS.register("mysuperblockreg", () -> new SuperBlock(BlockBehaviour.Properties.of(Material.WOOD).lightLevel((s) -> { return 1; }).sound(SoundType.WOOD))); public ExampleMod() { // Register the setup method for modloading FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup); // Register the enqueueIMC method for modloading FMLJavaModLoadingContext.get().getModEventBus().addListener(this::enqueueIMC); // Register the processIMC method for modloading FMLJavaModLoadingContext.get().getModEventBus().addListener(this::processIMC); BLOCKS.register(FMLJavaModLoadingContext.get().getModEventBus()); // Register ourselves for server and other game events we are interested in MinecraftForge.EVENT_BUS.register(this); } private void setup(final FMLCommonSetupEvent event) { // some preinit code LOGGER.info("HELLO FROM PREINIT"); LOGGER.info("DIRT BLOCK >> {}", Blocks.DIRT.getRegistryName()); } private void enqueueIMC(final InterModEnqueueEvent event) { // some example code to dispatch IMC to another mod InterModComms.sendTo("examplemod", "helloworld", () -> { LOGGER.info("Hello world from the MDK"); return "Hello world"; }); } private void processIMC(final InterModProcessEvent event) { // some example code to receive and process InterModComms from other mods LOGGER.info("Got IMC {}", event.getIMCStream().map(m -> m.messageSupplier().get()).collect(Collectors.toList())); } // You can use SubscribeEvent and let the Event Bus discover methods to call @SubscribeEvent public void onServerStarting(FMLServerStartingEvent event) { // do something when the server starts LOGGER.info("HELLO from server starting"); } // You can use EventBusSubscriber to automatically subscribe events on the // contained class (this is subscribing to the MOD // Event bus for receiving Registry Events) @Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD) public static class RegistryEvents { @SubscribeEvent public static void onBlocksRegistry(final RegistryEvent.Register<Block> blockRegistryEvent) { // register a new block here LOGGER.info("HELLO from Register Block"); } } }  
    • I'm probably really stupid but how do I fix this error. It happens whenever I try to startup Minecraft forge 1.8.9 recommended [19:13:10 ERROR]: Gave up trying to download https://maven.minecraftforge.net/jline/jline/2.13/jline-2.13.jar for job 'Version & Libraries' [19:13:10 ERROR]: Job 'Version & Libraries' finished with 1 failure(s)! (took 0:00:13.982)   How do I fix this?
    • Override attackEntityFrom and do checks there, for example DamageSource#isProjectile. Or check how Enderman does it.
    • Hey, I just started modding, and especially texturing, so I'm still new to all of this. I thought a fun thing to do would be to turn the world to glass (turn blocks such as grass and stone and ores etc..) to have the grass texture, so the world would be glass. Seemed kinda fun. So I just copy-pasted the glass texture into my pack, renamed it to stone, and ran into a problem. It is rendering the glass texture, but is not transparent. Its just grey on the inside, with the glass textures on the side. I am completely lost as of what to do. Does anyone have a solution ? thank you
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.