I'm somewhat new to modding and I'm trying to make a mod that has custom fish for players to catch. Before I add these fish into loot tables I'm trying to just randomize the "size" of the fish, as well as say which player it was caught by.
Here's my relevant code in the fish item so far:
private static Random randomNo = new Random();
private static int min = 32;
private static int max = 51;
private static int size = 0;
public static EntityPlayer player;
public void addInformation(ItemStack stack, @Nullable World worldIn, List<String> tooltip, ITooltipFlag flagIn)
//tooltip.add(TextFormatting.GRAY + "Caught by " + player.getName());
In addition, the second tooltip is commented out because it crashes my game. Am I not calling the player's username correctly? I've tried researching these problems, but this is the closest I could get to a solution. If anyone could educate me on what I'm doing wrong, or offer alternate solutions, that would be much appreciated!
Depending on what version of Minecraft you're writing your mod for (which you haven't specified as far as I can tell), that could be the correct method to call. However, the snippets you've included don't show you setting a value to your static "player" variable. Thus even if you use the correct method, you might be using it on an uninitialized or null variable, which won't work.
Yes, I already saw that you had declared a variable called "player" near the top of your code (although this new spoiler text of code that you say is "the whole thing" doesn't seem to include it). However, I still don't see any code assigning a value to that variable, meaning it is presumably still null (since this is Java) when your onItemPickup tries to dereference it by calling player.getName().
If this is for 1.12, I think that's the right function to call to get the player's name from an EntityPlayer. However, if you're specifically designing this for a multiplayer server, storing the player's name in a static variable seems a questionable design decision - wouldn't that introduce a possible race condition if two or more players are fishing concurrently?