Jump to content

1.11.2 How to make a data saver?


Denisi

Recommended Posts

I have an item that uses an int value for how many times it can be used before it gets destroyed. The problem is: the int value resets every time the player leaves and re-joins. Is there a way to save that int value and re-enter (or recall whatever is the best term) it to the item?

here is my item class:

Spoiler

package com.DenisRaze.MoEquipments.items;

import java.util.List;

import com.DenisRaze.MoEquipments.init.ModItems;
import com.DenisRaze.MoEquipments.init.ModTools;
import com.ibm.icu.impl.duration.impl.Utils;

import akka.io.Tcp.WriteFile;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.MobEffects;
import net.minecraft.init.PotionTypes;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemTool;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class ItemSpeedRing extends Item {
    
    private int damage;
    
    public ItemSpeedRing(String name1, String name2) {
        setUnlocalizedName(name1);
        setRegistryName(name2);
        maxStackSize = 1;
        damage = 100;
        setMaxDamage(damage);        
    }
    
    @Override
    public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand) {
        if(player.isEntityAlive()) {
            player.addPotionEffect(new PotionEffect(Potion.getPotionById(1), 600, 0));
            player.addPotionEffect(new PotionEffect(Potion.getPotionById(3), 300, 0));
            damage--;
        }
        if(damage < 0) {
            player.getHeldItem(hand).shrink(1);
            damage = 101;
        }
        return super.onItemRightClick(world, player, hand);
    }
    
    @SideOnly(Side.CLIENT)
    @Override
    public void addInformation(ItemStack stack, EntityPlayer player, java.util.List<String> info, boolean par4) {
    if(damage < 101 && damage > 99) 
        
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "51 uses left");
    }
    if(damage < 99 && damage > 97)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "50 uses left");
    }
    if(damage < 97 && damage > 95)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "49 uses left");
    }
    if(damage < 95 && damage > 93)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "48 uses left");
    }
    if(damage < 93 && damage > 91)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "47 uses left");
    }
    if(damage < 91 && damage > 89)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "46 uses left");
    }
    if(damage < 89 && damage > 87)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "45 uses left");
    }
    if(damage < 87 && damage > 85)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "44 uses left");
    }
    if(damage < 85 && damage > 83)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "43 uses left");
    }
    if(damage < 83 && damage > 81)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "42 uses left");
    }
    if(damage < 81 && damage > 79)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "41 uses left");
    }
    if(damage < 79 && damage > 77)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "40 uses left");
    }
    if(damage < 77 && damage > 75)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "39 uses left");
    }
    if(damage < 75 && damage > 73)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "38 uses left");
    }
    if(damage < 73 && damage > 71)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "37 uses left");
    }
    if(damage < 71 && damage > 69)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "36 uses left");
    }
    if(damage < 69 && damage > 67)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "35 uses left");
    }
    if(damage < 67 && damage > 65)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "34 uses left");
    }
    if(damage < 65 && damage > 63)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "33 uses left");
    }
    if(damage < 63 && damage > 61)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "32 uses left");
    }
    if(damage < 61 && damage > 59)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "31 uses left");
    }
    if(damage < 59 && damage > 57)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "30 uses left");
    }
    if(damage < 57 && damage > 55)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "29 uses left");
    }
    if(damage < 55 && damage > 53)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "28 uses left");
    }
    if(damage < 53 && damage > 51)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "27 uses left");
    }
    if(damage < 51 && damage > 49)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "26 uses left");
    }
    if(damage < 49 && damage > 47)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "25 uses left");
    }
    if(damage < 47 && damage > 45)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "24 uses left");
    }
    if(damage < 45 && damage > 43)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "23 uses left");
    }
    if(damage < 43 && damage > 41)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "22 uses left");
    }
    if(damage < 41 && damage > 39)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "21 uses left");
    }
    if(damage < 39 && damage > 37)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "20 uses left");
    }
    if(damage < 37 && damage > 35)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "19 uses left");
    }
    if(damage < 35 && damage > 33)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "18 uses left");
    }
    if(damage < 33 && damage > 31)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "17 uses left");
    }
    if(damage < 31 && damage > 29)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "16 uses left");
    }
    if(damage < 29 && damage > 27)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "15 uses left");
    }
    if(damage < 27 && damage > 25)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "14 uses left");
    }
    if(damage < 25 && damage > 23)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "13 uses left");
    }
    if(damage < 23 && damage > 21)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "12 uses left");
    }
    if(damage < 21 && damage > 19)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "11 uses left");
    }
    if(damage < 19 && damage > 17)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "10 uses left");
    }
    if(damage < 17 && damage > 15)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "9 uses left");
    }
    if(damage < 15 && damage > 13)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "8 uses left");
    }
    if(damage < 13 && damage > 11)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "7 uses left");
    }
    if(damage < 11 && damage > 9)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "6 uses left");
    }
    if(damage < 9 && damage > 7)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "5 uses left");
    }
    if(damage < 7 && damage > 5)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "4 uses left");
    }
    if(damage < 5 && damage > 3)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "3 uses left");
    }
    if(damage < 3 && damage > 1)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "2 uses left");
    }
    if(damage < 1 && damage > -1)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "1 uses left");
    }

    
    }
}
 

I am not that good in java and I tried to figure it out myself but no luck.

Link to comment
Share on other sites

5 minutes ago, diesieben07 said:

This is the least of your problems. Are you serious about that mess of copy-pasta code?

It seems messy but it works just fine, the item is usable only for 51 times and then it vanishes from inventory. The usage display works just fine: blue text showing how many uses are there left. ( I think this is what you mean with copy-pasta code right?).

I just want a way to save the int value of that item (if is possible to re-enter it again even with the mess i did)

Edited by Denisi
not much
Link to comment
Share on other sites

7 minutes ago, diesieben07 said:

Items are singletons, you cannot just put a filed in your item class, it will be shared across all item stacks of your item.

You must store things like this in the ItemStack, look at vanilla items for examples.

Umm... is this shared to the other players too? Because if is only shared to same items on one player's inventory then is fine. I want them to get removed after 51 uses no matter if you switch to a new ring.

Edit: even if you have 2 rings and alternate the use on them when it reaches 0 only 1 is destroyed (i tested that)

Edited by Denisi
just something
Link to comment
Share on other sites

2 minutes ago, diesieben07 said:

Items behave like singletons. It will be shared across all players on the server and across server and client in singleplayer.

Ok got it this will make it more interesting... Now what about that data saver thing, can it be done or not? Thats my main problem here.

Link to comment
Share on other sites

Why aren't you using the vanilla itemstack damage/metadata for this?

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

56 minutes ago, Denisi said:

if(damage < 101 && damage > 99) 
        
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "51 uses left");
    }
    if(damage < 99 && damage > 97)
    {
        info.add(com.mojang.realmsclient.gui.ChatFormatting.BLUE + "50 uses left");
    }

    /* 49 more identical lines */

Also, seriously?

 

MATH, MOTHERFUCKER, DO YOU KNOW IT?

 

info.add(ChatFormatting.BLUE + (damage/2) + " uses left");

 

You can even get the formatting to change by doing easy checks, rather than this giant..massive...ugly...copy-pasted mess.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

6 minutes ago, Draco18s said:

Why aren't you using the vanilla itemstack damage/metadata for this?

 

4 minutes ago, Draco18s said:

Also, seriously?

 

MATH, MOTHERFUCKER, DO YOU KNOW IT?

 

info.add(ChatFormatting.BLUE + (damage/2) + " uses left");

 

You can even get the formatting to change by doing easy checks, rather than this giant..massive...ugly...copy-pasted mess.

Yes yes I'm stupid (or my brain don't work that well on 11 pm...). Anyway i solved it!

 

Edited by Denisi
the am to pm (ok i need to rest now...)...... And the 23 to 11 ( i seriously need a rest...)
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.