Jump to content

Making a delay in method


Rex_Technology

Recommended Posts

I would suggest learning Java before modding. It's an absolutely terrible idea to learn it at the same time because Minecraft contains numerous bad practices and you are expected to have an intermediate level of Java knowledge beforehand since we do not explain Java concepts, only answer Minecraft related questions. It's like trying to run before figuring out how to stand.

Link to comment
Share on other sites

  • 2 weeks later...
4 hours ago, Rex_Technology said:

I already learned java, I followed a java tutorial made by Kaupenjoe that is made for minecraft modding

Following a tutorial is not the same as learning a language, but your choice.

Regardless, I gave the answer above, use a capability on the level and create a timer method to handle the destroying. If you are using a block entity to destroy the block, then use that instead of a capability on the level. There are docs, wikis, and tutorials on capabilities if you are unsure of their system.

  • Thanks 1
Link to comment
Share on other sites

On 2/15/2023 at 12:36 PM, Alpvax said:

You need to make your own capability, and you can give it whatever methods you want.

well yes but not quite

short ago i have to made a capability extending itemhandler to make a custome drill and i need to recalculate the value of tick to calculate animation frame and fuel comsumtion but i cannot make it 

so i end quickitng an made just a 

	    public int get_tick() {
        if( this.tick > 0) {
        } 
        else if( this.action > 0) {
            if( this.start > 0L ) {
            //System.out.println( "Calcular_tick()\n" );
            this.tick = (int) ((System.currentTimeMillis() - this.start) / 50); // 50
            this.tick = (this.tick > this.limit)? this.limit : this.tick;
            }
        }        
        return this.tick;
    }
	


 

so everytime the code ask for that variable it recalculate it 

coze idont find any ontick() or public void tick()  that works inside the capability 

 

 

Spoiler

				
			package merctool.item.tool;				
			import net.minecraft.core.NonNullList;				
			import net.minecraft.nbt.CompoundTag;
		import net.minecraft.nbt.ListTag;
		import net.minecraft.nbt.Tag;
		import net.minecraft.world.item.ItemStack;
		import net.minecraftforge.common.capabilities.ForgeCapabilities;
		import net.minecraftforge.items.ItemStackHandler;
		import org.jetbrains.annotations.NotNull;
		import org.jetbrains.annotations.Nullable;				
			/*
		helditem.getCapability(ForgeCapabilities.ITEM_HANDLER).ifPresent(handler00 -> {
		    item_handler ih = (item_handler)handler00;
		    System.out.println(warudo.isClientSide + " ih.fuel => " + ih.fuel );                
		   });
		*/				
			public class item_handler extends ItemStackHandler {
		    public ItemStack container = null;				
			    public float fuel   = 0;
		    public int munition = 0;				
			    public long start   = 0;
		    public int action   = 0;
		    public int tick     = 0;
		    public int limit    = 1200;				
			    public item_handler(ItemStack container) {
		        super(0);
		        this.container = container;
		    }				
			    public void print(String mensaje) {
		        System.out.println("\n" + mensaje);
		        System.out.println("this.fuel     = " + this.fuel);
		        System.out.println("this.munition = " + this.munition);
		        System.out.println("this.start    = " + this.start);
		        System.out.println("this.action   = " + this.action);
		        System.out.println("this.tick     = " + this.tick);
		        System.out.println("this.limit    = " + this.limit);
		        System.out.println("\n");
		    }				
			    @Override
		    public int getSlotLimit(int slot) {
		        return 64;
		    }				
			    @Override
		    public boolean isItemValid(int slot, @NotNull ItemStack stack) {
		        if (stack == null || stack.isEmpty()) // || stack.getItem() instanceof BasketBlockItem
		        {
		            return false;
		        } else {
		            return super.isItemValid(slot, stack);
		        }
		    }				
			    @Override
		    public void setStackInSlot(int slot, @NotNull ItemStack stack) {				
			        //// System.out.println("setStackInSlot(" + slot + ")");
		        validateSlotIndex(slot);
		        this.stacks.set(slot, stack);
		        onContentsChanged(slot);				
			        if (this.container != null) {
		            // write_item_to_slot(this.container, slot, stack);
		        }
		    }				
			    // ########## ########## ########## ##########
		    // @Override
		    public static NonNullList<ItemStack> read_items_from(ItemStack itemstack) {				
			        NonNullList<ItemStack> contained_items = NonNullList.withSize(9, ItemStack.EMPTY);// this.getContainerSize()				
			        if (itemstack.hasTag()) {
		            CompoundTag compoundtag = itemstack.getTag();
		            ListTag listtag = null;
		            int size = 0;				
			            if (compoundtag.contains("Items")) {
		                // ListTag listtag = new ListTag();
		                listtag = compoundtag.getList("Items", 10);
		                size = listtag.size();
		                // contained_items = NonNullList.withSize(size, ItemStack.EMPTY);				
			                for (int i = 0; i < listtag.size(); ++i) {
		                    CompoundTag itemstacktag = listtag.getCompound(i);
		                    int j = compoundtag.getByte("Slot") & 255;
		                    if (j >= 0 && j < contained_items.size()) {
		                        contained_items.set(j, ItemStack.of(itemstacktag));
		                    }
		                }
		            }
		        }
		        return contained_items;
		    }				
			    // ########## ########## ########## ##########
		    // @Override
		    public void write_item_to_slot(ItemStack container, int slot, @NotNull ItemStack stack) {
		        // , NonNullList<ItemStack> contained_items				
			        CompoundTag compoundtag = null;				
			        if (container.hasTag()) {
		            compoundtag = container.getTag();
		        } else {
		            compoundtag = new CompoundTag();
		        }				
			        ListTag listtag = null;
		        if (compoundtag.contains("Items")) {
		            listtag = compoundtag.getList("Items", 10);
		        } else {
		            listtag = new ListTag();
		        }				
			        CompoundTag itemstacktag = null;				
			        if (slot < listtag.size()) {
		            itemstacktag = listtag.getCompound(slot);
		        } else {
		            itemstacktag = new CompoundTag();
		        }				
			        itemstacktag.putByte("Slot", (byte) slot);
		        stack.save(itemstacktag);
		        listtag.add(itemstacktag); // aqui tengi una duda, se sobreescrive o crea otra ??				
			        compoundtag.put("Items", listtag);
		        container.setTag(compoundtag);
		    }				
			    @Override
		    public CompoundTag serializeNBT() {
		        ListTag nbtTagList = new ListTag();
		        for (int i = 0; i < stacks.size(); i++) {
		            if (!stacks.get(i).isEmpty()) {
		                CompoundTag itemTag = new CompoundTag();
		                itemTag.putInt("Slot", i);
		                stacks.get(i).save(itemTag);
		                nbtTagList.add(itemTag);
		            }
		        }
		        CompoundTag nbt = new CompoundTag();
		        nbt.put("Items", nbtTagList);
		        nbt.putInt("Size", stacks.size());				
			        nbt.putInt("munition", this.munition);
		        nbt.putFloat("fuel",   this.fuel);
		        nbt.putLong("start",   this.start);
		        nbt.putShort("action", (short) this.action);
		        nbt.putShort("limit",  (short) this.limit);
		        this.tick = 0;
		        
		        //print("serializeNBT()");
		        return nbt;
		    }				
			    @Override
		    public void deserializeNBT(CompoundTag nbt) {
		        setSize(nbt.contains("Size", Tag.TAG_INT) ? nbt.getInt("Size") : stacks.size());
		        ListTag tagList = nbt.getList("Items", Tag.TAG_COMPOUND);
		        for (int i = 0; i < tagList.size(); i++) {
		            CompoundTag itemTags = tagList.getCompound(i);
		            int slot = itemTags.getInt("Slot");				
			            if (slot >= 0 && slot < stacks.size()) {
		                stacks.set(slot, ItemStack.of(itemTags));
		            }
		        }				
			        this.munition = nbt.getInt("munition");
		        this.fuel = nbt.getFloat("fuel");				
			        this.limit = nbt.getShort("limit");
		        this.start = nbt.getLong("start");
		        this.action = nbt.getShort("action");				
			        //System.out.println( "DeserializeNBT()" );
		        //print("DeserializeNBT()");
		        onLoad();
		    }				
			    public void start(int action) {				
			        if ( this.action != action ) {
		            this.action = action;
		            this.start = System.currentTimeMillis(); // 50
		            this.tick = 0;
		        }				
			    }				
			    public void stop() {
		        this.action = 0;
		        this.start = 0; // 50
		        this.tick = 0;
		        this.limit = 1200;
		    }				
			    
		    public int get_tick() {
		        if( this.tick > 0) {
		        } 
		        else if( this.action > 0) {
		            if( this.start > 0L ) {
		            //System.out.println( "Calcular_tick()\n" );
		            this.tick = (int) ((System.currentTimeMillis() - this.start) / 50); // 50 = 20ticksxsecond
		            this.tick = (this.tick > this.limit)? this.limit : this.tick;
		            }
		        }        
		        return this.tick;
		    }
		    
		}
		 				
			

 
 

 

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

On 2/17/2023 at 9:08 AM, perromercenary00 said:

well yes but not quite

short ago i have to made a capability extending itemhandler to make a custome drill and i need to recalculate the value of tick to calculate animation frame and fuel comsumtion but i cannot make it 

Animation frame rendering can be lerped using the current tick data and the previous tick data. So, there's no 'not quite'. You can make an method and implement it like any other method in java.

Link to comment
Share on other sites

oo that 
when i made the animations using the tick provided by onUseTick 
causes chopping animations when the game start skiping frames 
an sometimes the animation locks in a cicle and dont let the item continue whit is normal use 

then i just begin to store the statr time as epoch and calculate the frame from it and fix the problem 

 

Link to comment
Share on other sites

if theres is a work around to make the logic inside the capability it would make things easier 
i try puting and onload method and launchi it from 

 

Spoiler

				
			    @Override
		    public CompoundTag serializeNBT() {
		        ListTag nbtTagList = new ListTag();
		        for (int i = 0; i < stacks.size(); i++) {
		            if (!stacks.get(i).isEmpty()) {
		                CompoundTag itemTag = new CompoundTag();
		                itemTag.putInt("Slot", i);
		                stacks.get(i).save(itemTag);
		                nbtTagList.add(itemTag);
		            }
		        }
		        CompoundTag nbt = new CompoundTag();
		        nbt.put("Items", nbtTagList);
		        nbt.putInt("Size", stacks.size());				
			        nbt.putInt("munition", this.munition);
		        nbt.putFloat("fuel",   this.fuel);
		        nbt.putLong("start",   this.start);
		        nbt.putShort("action", (short) this.action);
		        nbt.putShort("limit",  (short) this.limit);
		        this.tick = 0;
		        
		        //print("serializeNBT()");
		        
		        onSave();
		        
		        return nbt;
		    }				
			    
		    //@Override
		    protected void onSave()
		    {
		        //System.out.println("item_handler __ onSave() ");
		        
		        if(this.action > 0) {
		            //this.tick = get_tick();
		            
		            if( this.action > 0) {
		                if( this.start > 0L ) {
		                //System.out.println( "Calcular_tick()\n" );
		                this.tick = (int) ((System.currentTimeMillis() - this.start) / 50); // 50
		                this.tick = (this.tick > this.limit)? this.limit : this.tick;
		                }
		            }    
		        }
		        
		        System.out.println("item_handler __ onSave() " + this.tick );				
			    }				
			

i notice this methods ticks for a while afther you read the capability from the item and lives while the item is in the hotbar  but if you put it on the inventory and close the menu it dies so is not 

is not reliable becoze it only ticks a few times and stop so is unfited to make a long count and do something after like finishing the drilling action and consume the fuel value saving the numbers back to item 

the other option would be to call the capability once and again from the itemstack 
in older version there was a onupdate() method that constantly tick even when you toss the item into the world i dot find it in 1.19.2 surely the name has change 


 

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



×
×
  • Create New...

Important Information

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