Jump to content

Minecraft 1.10.2 Auto Repair Problem


boyx

Recommended Posts

package boyx.arandomstuff.items;

import boyx.arandomstuff.Reference;
import boyx.arandomstuff.api.item.IModeChanger;
import boyx.arandomstuff.handlers.InternalTimers;
import boyx.arandomstuff.init.ModItems;
import boyx.arandomstuff.util.ItemHelper;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;

public class ItemRepairer extends Item {
	
public ItemRepairer(String unlocalizedName, String registryName) {
		
		this.setUnlocalizedName(unlocalizedName);
		this.setRegistryName(new ResourceLocation(Reference.MODID, registryName));
		this.setMaxStackSize(1);
		this.setMaxDamage(100);
	}

@Override
public void onUpdate(ItemStack stack, World world, Entity entity, int par4, boolean par5) 
{
	if (world.isRemote || !(entity instanceof EntityPlayer))
	{
		return;
	}
	
	EntityPlayer player = (EntityPlayer) entity;

	player.getCapability(InternalTimers.CAPABILITY, null).activateRepair();

	if (player.getCapability(InternalTimers.CAPABILITY, null).canRepair())
	{
		repairAllItems(player);
	}
}

private void repairAllItems(EntityPlayer player)
{
	IItemHandler inv = player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);

	for (int i = 0; i < inv.getSlots(); i++)
	{
		ItemStack invStack = inv.getStackInSlot(i);

		if (invStack.isEmpty || invStack.getItem() instanceof IModeChanger || !invStack.getItem().isRepairable())
		{
			continue;
		}

		if (invStack == player.getItemStackFromSlot(EntityEquipmentSlot.MAINHAND) && player.isSwingInProgress)
		{
			//Don't repair item that is currently used by the player.
			continue;
		}

		if (ItemHelper.isDamageable(invStack) && invStack.getItemDamage() > 0)
		{
			invStack.setItemDamage(invStack.getItemDamage() - 1);
		}
	}
  } 
}

This should repair every Items in my Inventory in every few seconds.

Ive got one problem whenever i get the Item in my inventory my MC crash's.

It could be that I removed invStack.isEmpty (i didnt removed it in the code above) is marked red and i dont know why.

If you want me to post the crash report just ask.

 

Thank you!

Link to comment
Share on other sites

11 minutes ago, boyx said:

(i didnt removed it in the code above) is marked red and i dont know why.

Learn java before making a mod.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Just now, boyx said:

Ok i expressed myself wrong. I know Java and i also know why its marked red but i dont understand why because i used this alot in some mods from me but only here it doesnt work.

Thats what i ment by this

What does your IDE say when you hover over the red line? Have you strictly compared this to your previous code.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

2 minutes ago, boyx said:

is not a field

This is the important part, it isn't a field. What else can return a boolean.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Just now, boyx said:

There are many things that can return a boolean but nothing i need i think.

What i need is that it returns true if the ItemStack is empty for that i always used ItemStack.Empy or ItemStack.isEmpty and both dont work.

I java there are fields, methods, classes/interfaces. What is the one that can return a boolean. you have the words correct isEmpty, but it isn't a field.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Spoiler

---- Minecraft Crash Report ----
// This doesn't make any sense!

Time: 22.09.18 21:09
Description: Ticking player

java.lang.NullPointerException: Ticking player
    at boyx.arandomstuff.items.ItemRepairer.onUpdate(ItemRepairer.java:38)
    at net.minecraft.item.ItemStack.updateAnimation(ItemStack.java:524)
    at net.minecraft.entity.player.InventoryPlayer.decrementAnimations(InventoryPlayer.java:389)
    at net.minecraft.entity.player.EntityPlayer.onLivingUpdate(EntityPlayer.java:560)
    at net.minecraft.entity.EntityLivingBase.onUpdate(EntityLivingBase.java:2225)
    at net.minecraft.entity.player.EntityPlayer.onUpdate(EntityPlayer.java:260)
    at net.minecraft.entity.player.EntityPlayerMP.onUpdateEntity(EntityPlayerMP.java:345)
    at net.minecraft.network.NetHandlerPlayServer.update(NetHandlerPlayServer.java:174)
    at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher$1.update(NetworkDispatcher.java:216)
    at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:309)
    at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:197)
    at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:807)
    at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:688)
    at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:156)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:537)
    at java.lang.Thread.run(Unknown Source)


A detailed walkthrough of the error, its code path and all known details is as follows:
---------------------------------------------------------------------------------------

-- Head --
Thread: Server thread
Stacktrace:
    at boyx.arandomstuff.items.ItemRepairer.onUpdate(ItemRepairer.java:38)
    at net.minecraft.item.ItemStack.updateAnimation(ItemStack.java:524)
    at net.minecraft.entity.player.InventoryPlayer.decrementAnimations(InventoryPlayer.java:389)
    at net.minecraft.entity.player.EntityPlayer.onLivingUpdate(EntityPlayer.java:560)
    at net.minecraft.entity.EntityLivingBase.onUpdate(EntityLivingBase.java:2225)
    at net.minecraft.entity.player.EntityPlayer.onUpdate(EntityPlayer.java:260)

-- Player being ticked --
Details:
    Entity Type: null (net.minecraft.entity.player.EntityPlayerMP)
    Entity ID: 355
    Entity Name: Player357
    Entity's Exact location: -185,70, 67,00, 211,25
    Entity's Block location: World: (-186,67,211), Chunk: (at 6,4,3 in -12,13; contains blocks -192,0,208 to -177,255,223), Region: (-1,0; contains chunks -32,0 to -1,31, blocks -512,0,0 to -1,255,511)
    Entity's Momentum: 0,00, -0,02, 0,00
    Entity's Passengers: []
    Entity's Vehicle: ~~ERROR~~ NullPointerException: null
Stacktrace:
    at net.minecraft.entity.player.EntityPlayerMP.onUpdateEntity(EntityPlayerMP.java:345)
    at net.minecraft.network.NetHandlerPlayServer.update(NetHandlerPlayServer.java:174)
    at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher$1.update(NetworkDispatcher.java:216)
    at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:309)

-- Ticking connection --
Details:
    Connection: net.minecraft.network.NetworkManager@233f833a
Stacktrace:
    at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:197)
    at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:807)
    at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:688)
    at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:156)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:537)
    at java.lang.Thread.run(Unknown Source)

-- System Details --
Details:
    Minecraft Version: 1.10.2
    Operating System: Windows 10 (amd64) version 10.0
    Java Version: 1.8.0_171, Oracle Corporation
    Java VM Version: Java HotSpot(TM) 64-Bit Server VM (mixed mode), Oracle Corporation
    Memory: 654348648 bytes (624 MB) / 1037959168 bytes (989 MB) up to 1037959168 bytes (989 MB)
    JVM Flags: 3 total; -Xincgc -Xmx1024M -Xms1024M
    IntCache: cache: 0, tcache: 0, allocated: 12, tallocated: 94
    FML: MCP 9.32 Powered by Forge 12.18.3.2185 4 mods loaded, 4 mods active
    States: 'U' = Unloaded 'L' = Loaded 'C' = Constructed 'H' = Pre-initialized 'I' = Initialized 'J' = Post-initialized 'A' = Available 'D' = Disabled 'E' = Errored
    UCHIJAAAA    mcp{9.19} [Minecraft Coder Pack] (minecraft.jar) 
    UCHIJAAAA    FML{8.0.99.99} [Forge Mod Loader] (forgeSrc-1.10.2-12.18.3.2185.jar) 
    UCHIJAAAA    Forge{12.18.3.2185} [Minecraft Forge] (forgeSrc-1.10.2-12.18.3.2185.jar) 
    UCHIJAAAA    arstuff{beta.1} [ARandomStuff] (bin) 
    Loaded coremods (and transformers): 
    GL info: ~~ERROR~~ RuntimeException: No OpenGL context found in the current thread.
    Profiler Position: N/A (disabled)
    Player Count: 1 / 8; [EntityPlayerMP['Player357'/355, l='New World', x=-185,70, y=67,00, z=211,25]]
    Type: Integrated Server (map_client.txt)
    Is Modded: Definitely; Client brand changed to 'fml,forge'

This is the crash report. Maybe it has nothing to do with the isEmpty error...

Link to comment
Share on other sites

2 minutes ago, boyx said:

Minecraft Crash Report says this line is the error?

Did you attach your capability correctly?

 

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

4 minutes ago, boyx said:

I did this with the capability.

That is not enough, read the documentation on capabilities here.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

14 hours ago, boyx said:

After i deleted the stack.isEmpty then yes it does

did you try stack.isEmpty()? how well do you know java?

Edited by Cadiboo

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

 

5 minutes ago, Cadiboo said:

did you try stack.isEmpty()?

Yes i tried that but it also doesnt work.

6 minutes ago, Cadiboo said:

how well do you know java?

I know many basic things in java. I also programmed some programs but not too complicated.

I never did something like this advanced modding.

Link to comment
Share on other sites

Just now, boyx said:

Yes i tried that but it also doesnt work.

define doesn't work

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

Just now, boyx said:

It is undefined and not working in ItemStack... 

what do you mean undefined? do you mean null? Undefined doesn't exist in Java as far as I know it only exists in JavaScript. 

1 minute ago, boyx said:

not working in ItemStack

What do you mean by not working

 

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

In Minecraft the class ItemStack doesnt own a Method called isEmpty() somehow...

I dont even know if this is the error. How i said some post above the crashlog says there too many ticks and something with the Capability doesnt work.

 

[Edit] Btw the game crashes whenever i get the Repairer Item in my inventory.

Edited by boyx
Link to comment
Share on other sites

2 minutes ago, boyx said:

In Minecraft the class ItemStack doesnt own a Method called isEmpty() somehow...

Excerpt from the actual net.minecraft.item.ItemStack.class

package net.minecraft.item;

//...

public final class ItemStack implements net.minecraftforge.common.capabilities.ICapabilitySerializable<NBTTagCompound>
{
    public static final ItemStack EMPTY = new ItemStack((Item)null);
    //...

    //...

    public ItemStack(Item itemIn, int amount)
    {
        this(itemIn, amount, 0);
    }

    //...

    public boolean isEmpty()
    {
        if (this == EMPTY)
        {
            return true;
        }
        else if (this.getItemRaw() != null && this.getItemRaw() != Items.AIR)
        {
            if (this.stackSize <= 0)
            {
                return true;
            }
            else
            {
                return this.itemDamage < -32768 || this.itemDamage > 65535;
            }
        }
        else
        {
            return true;
        }
    }
  
  //...
}

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

7 minutes ago, boyx said:

How i said some post above the crashlog says there too many ticks and something with the Capability doesnt work.

 

16 hours ago, Animefan8888 said:
16 hours ago, boyx said:


    @CapabilityInject(InternalTimers.class)
    public static final Capability<InternalTimers> CAPABILITY = null;
    public static final ResourceLocation NAME = new ResourceLocation(Reference.MODID, "internal_timers");

I did this with the capability.

That is not enough, read the documentation on capabilities here.

 

 

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

package boyx.arandomstuff.handlers;

import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.common.capabilities.ICapabilityProvider;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import boyx.arandomstuff.Reference;

public class InternalTimers
{
    @CapabilityInject(InternalTimers.class)
    public static final Capability<InternalTimers> CAPABILITY = null;
    public static final ResourceLocation NAME = new ResourceLocation(Reference.MODID, "internal_timers");

    private final Timer repair = new Timer();
    private final Timer heal = new Timer();
    private final Timer feed = new Timer();

    public void tick()
    {
        if (repair.shouldUpdate)
        {
            repair.tickCount++;
            repair.shouldUpdate = false;
        }

        if (heal.shouldUpdate)
        {
            heal.tickCount++;
            heal.shouldUpdate = false;
        }

        if (feed.shouldUpdate)
        {
            feed.tickCount++;
            feed.shouldUpdate = false;
        }
    }

    public void activateRepair()
    {
        repair.shouldUpdate = true;
    }

    public void activateHeal()
    {
        heal.shouldUpdate = true;
    }

    public void activateFeed()
    {
        feed.shouldUpdate = true;
    }

    public boolean canRepair()
    {
        if (repair.tickCount >= 19)
        {
            repair.tickCount = 0;
            repair.shouldUpdate = false;
            return true;
        }

        return false;
    }

    public boolean canHeal()
    {
        if (heal.tickCount >= 19)
        {
            heal.tickCount = 0;
            heal.shouldUpdate = false;
            return true;
        }

        return false;
    }

    public boolean canFeed()
    {
        if (feed.tickCount >= 19)
        {
            feed.tickCount = 0;
            feed.shouldUpdate = false;
            return true;
        }

        return false;
    }

    public static class Provider implements ICapabilityProvider
    {
        private final InternalTimers capInstance = new InternalTimers();

        @Override
        public boolean hasCapability(@Nonnull Capability<?> capability, @Nullable EnumFacing facing)
        {
            return capability == CAPABILITY;
        }

        @Override
        public <T> T getCapability(@Nonnull Capability<T> capability, @Nullable EnumFacing facing)
        {
            if (capability == CAPABILITY)
                return CAPABILITY.cast(capInstance);
            else return null;
        }
    }

    private static class Timer
    {
        public int tickCount = 0;
        public boolean shouldUpdate = false;
    }
}

Thats the class i did the Capabilitys in. I dont know much and i didnt understand the documentation well.

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.



×
×
  • Create New...

Important Information

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