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

(1.15.1) How to display heads of online Players in new creative Tab


anonymusdennis
 Share

Recommended Posts

Minecraft version: 1.15.1

 

So I want my mod to add an Creative Tab in which one can use all the heads of the current online Players.

 

so far I got this:

public static ArrayList<String> online_player_names = new ArrayList<String>() {{
        add("Steve");
        add("my_name");
        add("Alex");
        add("Herobrine");
        add("Cake");
        add("CaptainSparkles");
        add("Steve");
        add("Steve");
        add("Steve");
        add("Steve");
        add("Steve");
        add("Steve");
        add("Steve");
        add("Steve");
        add("Steve");
    }};
//this is a list of predefined heads

@Mod.EventBusSubscriber(bus=Mod.EventBusSubscriber.Bus.MOD)
    public static class RegistryEvents {
	@SubscribeEvent
        public static void updateHeads(final GuiOpenEvent event){
            Collection<NetworkPlayerInfo> playersC= Minecraft.getInstance().getConnection().getPlayerInfoMap();
            playersC.forEach((loadedPlayer)->{
                String LoadedPlayerName = loadedPlayer.getGameProfile().getName();
                if(!online_player_names.contains(LoadedPlayerName)){
                    online_player_names.add(LoadedPlayerName);

                }

            });
            online_player_names.forEach((playername)->{
                ItemStack head = (new ItemStack(Blocks.PLAYER_HEAD));
                CompoundNBT nbt = head.getTag();
                nbt.putString("Skullowner", playername);
                head.setTag(nbt);
                Item heads = head.getItem();
              //Put Itemstack to Creative Tab
                ModItemGroups.MOD_ITEM_GROUP_Skulls.fill(new NonNullList<ItemStack>() {{add(head);}});
            });
	}

 

 

this code gives an error when it tries to put the ItemStack into a new Creative Tab:

 

Error Code From pastebin here

 

I don't know how to solve this 

I didn't fin any references on how to add Already exsisting Items to a new CreativeTab but with changed NBT

 

here is my code for the creative Tabs:

package com.anonymusdennis.annomod.init;

import com.anonymusdennis.annomod.annomod;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.util.NonNullList;
import net.minecraft.util.registry.Registry;
import java.util.function.Supplier;

public class ModItemGroups  {
    public static class ModItemGroup extends ItemGroup {

        private final Supplier<ItemStack> iconSupplier;

        public ModItemGroup(final String name, final Supplier<ItemStack> iconSupplier) {
            super(name);
            this.iconSupplier = iconSupplier;
        }

        @Override
        public ItemStack createIcon() {
            return iconSupplier.get();
        }
        @Override
        public void fill(NonNullList<ItemStack> items) {
            for(Item item : Registry.ITEM) {
                item.fillItemGroup(this, items);
            }
        }
    }
    public static final ItemGroup MOD_ITEM_GROUP_Banner = new ModItemGroup("jolo", () -> new ItemStack(Items.LIGHT_BLUE_BANNER));
    public static ItemGroup MOD_ITEM_GROUP_Skulls = (new ModItemGroup("Heads", () -> new ItemStack(Items.LIGHT_BLUE_BANNER)));
    public static final ItemGroup MOD_ITEM_GROUP_Standart = new ModItemGroup(annomod.MOD_ID, () -> new ItemStack(ItemInit.erstes_item));
    public static final ItemGroup MOD_ITEM_GROUP_Rest = new ModItemGroup("Rest", () -> new ItemStack(Items.LIGHT_BLUE_BANNER));

}

or here from pastebin:

https://pastebin.com/EMm8BqyA

 

Edited by anonymusdennis
bad Internet
Link to comment
Share on other sites

public static class ModItemGroup extends ItemGroup {
    public String name = "";
    public ItemGroup UpdateName(String name)
    {
        this.name = "";
        return this;
    }

    public static ArrayList<String> player_names = new ArrayList<String>() {{}};
    private final Supplier<ItemStack> iconSupplier;

    public ModItemGroup(final String name, final Supplier<ItemStack> iconSupplier) {
        super(name);
        this.iconSupplier = iconSupplier;
    }
    @Override
    public void fill(NonNullList<ItemStack> items) {
        LOGGER.info(this.name);

        if(this.name == "skull"){
            LOGGER.info(this.name + "success");
            items = new NonNullList<ItemStack>() {{
                player_names.forEach((playername)->{
                    ItemStack head = (new ItemStack(Blocks.PLAYER_HEAD));
                    CompoundNBT nbt = head.getTag();
                    nbt.putString("Skullowner", playername);
                    head.setTag(nbt);
                    Item heads = head.getItem();
                    add(head);
                });}};
        }
    }
    @Override
    public ItemStack createIcon() {
        return iconSupplier.get();
    }

}

Now I got this is this better?

I also removed the call of the fill function



Now I got another error :

[10:34:56] [Render thread/ERROR] [ne.mi.fm.ja.FMLModContainer/]: Exception caught during firing event: Unable to have damage AND stack.
    Index: 2

 

And below there is this error:


 Caught exception during event RegistryEvent.Register<minecraft:item> dispatch for modid annomod


here is the complete Error

 

https://pastebin.com/4W14KiMv

Edited by anonymusdennis
Link to comment
Share on other sites

okay i think I got it started without any errors:

 

package com.anonymusdennis.annomod.init;

import com.anonymusdennis.annomod.annomod;
import net.minecraft.block.Blocks;
import net.minecraft.client.gui.screen.inventory.CreativeScreen;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.container.Container;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.NonNullList;
import net.minecraft.util.registry.Registry;

import java.util.ArrayList;
import java.util.function.Supplier;
import net.minecraft.client.gui.screen.inventory.CreativeScreen;

import static net.minecraft.world.biome.Biome.LOGGER;

public class ModItemGroups  {

    public static class ModItemGroup extends ItemGroup {
        public String name = "";
        public ItemGroup UpdateName(String name)
        {
            this.name = "";
            return this;
        }

        public static ArrayList<String> player_names = new ArrayList<String>() {{}};
        private final Supplier<ItemStack> iconSupplier;

        public ModItemGroup(final String name, final Supplier<ItemStack> iconSupplier) {
            super(name);
            this.iconSupplier = iconSupplier;
        }
        @Override
        public void fill(NonNullList<ItemStack> items) {
            LOGGER.info(this.name);

            if(this.name == "skull"){
                LOGGER.info(this.name + "success");

                    player_names.forEach((playername)-> {
                        ItemStack head = (new ItemStack(Blocks.PLAYER_HEAD));
                        CompoundNBT nbt = head.getTag();
                        nbt.putString("Skullowner", playername);
                        head.setTag(nbt);
                        Item heads = head.getItem();
                        items.add(head);
                    });
                for(Item item : Registry.ITEM) {
                    item.fillItemGroup(this, items);
                }
            }
        }
        @Override
        public ItemStack createIcon() {
            return iconSupplier.get();
        }

    }
    public static final ItemGroup MOD_ITEM_GROUP_Banner = new ModItemGroup("jolo", () -> new ItemStack(Items.LIGHT_BLUE_BANNER));
    public ItemGroup MOD_ITEM_GROUP_Skulls = ((new ModItemGroup("Heads", () -> new ItemStack(Items.LIGHT_BLUE_BANNER))).UpdateName("skull"));
    public static final ItemGroup MOD_ITEM_GROUP_Standart = new ModItemGroup(annomod.MOD_ID, () -> new ItemStack(ItemInit.erstes_item));
    public static final ItemGroup MOD_ITEM_GROUP_Rest = new ModItemGroup("Rest", () -> new ItemStack(Items.LIGHT_BLUE_BANNER));

}

 

this does somehow cause the creative Tab "Heads" not to show up why?

 

I am truly sorry If I am stressing you..

 

Link to comment
Share on other sites

1 hour ago, anonymusdennis said:

whyyyyy?

Why indeed. What is this post for?

 

2 hours ago, anonymusdennis said:

this does somehow cause the creative Tab "Heads" not to show up why?

The field MOD_ITEM_GROUP_Skulls (a truly terrible field name, by the way, please refer to the recommended java naming scheme) is an instance field. Since you (probably) never make an instance of the containing class ModItemGroups, it is never initialized and thus your ItemGroup instance is never created.

  • Thanks 1
Link to comment
Share on other sites

okay i got it now and here is my code for anyone who is interested:

 

 

ModItemGroups.java

package com.anonymusdennis.annomod.init;

import com.anonymusdennis.annomod.annomod;
import net.minecraft.block.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.NonNullList;
import net.minecraft.util.registry.Registry;

import java.util.ArrayList;
import java.util.function.Supplier;
import java.util.logging.Logger;

import static net.minecraft.world.biome.Biome.LOGGER;

public class ModItemGroups  {

    public static class ModItemGroup extends ItemGroup {
        public String namer = "";

        private final Supplier<ItemStack> iconSupplier;

        public ModItemGroup(final String name, final Supplier<ItemStack> iconSupplier, String skull) {
            super(name);
            this.namer = skull;
            namer = skull;
            this.iconSupplier = iconSupplier;
        }
        @Override
        public void fill(NonNullList<ItemStack> items) {
            if(namer == "skull"){
                annomod.online_player_names.forEach((playername)-> {
                    ItemStack head = (new ItemStack(Blocks.PLAYER_HEAD));
                    CompoundNBT nbt = head.getOrCreateTag();
                    nbt.putString("SkullOwner", playername);
                    nbt.put("display", new CompoundNBT() {{
                        putString("text", playername);
                    }});
                    head.setTag(nbt);
                    Item heads = head.getItem();
                    items.add(head);
                });

            }
            for(Item item : Registry.ITEM) {
                item.fillItemGroup(this, items);
            }
        }
        @Override
        public ItemStack createIcon() {
            return iconSupplier.get();
        }

    }
    public static final ItemGroup MOD_ITEM_GROUP_Blocks = new ModItemGroup("jolo", () -> new ItemStack(Items.LIGHT_BLUE_BANNER), "banner");
    public static final ItemGroup MOD_ITEM_GROUP_Skulls = new ModItemGroup("Heads", () -> new ItemStack(Items.PLAYER_HEAD),"skull");
    public static final ItemGroup MOD_ITEM_GROUP_Standart = new ModItemGroup(annomod.MOD_ID, () -> new ItemStack(ItemInit.erstes_item), "standard");
    public static final ItemGroup MOD_ITEM_GROUP_Food = new ModItemGroup("Rest", () -> new ItemStack(Items.LIGHT_BLUE_BANNER), "rest");

}

 

annomod.java

 

package com.anonymusdennis.annomod;

import com.anonymusdennis.annomod.init.ModItemGroups;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.block.SkullBlock;
import net.minecraft.block.SkullPlayerBlock;
import net.minecraft.client.Minecraft;
import net.minecraft.client.network.play.NetworkPlayerInfo;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.NonNullList;
import net.minecraftforge.client.event.GuiOpenEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.Tags;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.event.server.FMLServerStartingEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.fml.network.NetworkRegistry;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.ArrayList;

// The value here should match an entry in the META-INF/mods.toml file
@Mod("annomod")
public class annomod
{

private int online_players = 0;
    // Directly reference a log4j logger.
    public static ArrayList<String> online_player_names = new ArrayList<String>() {{
        add("anonymusdennis");
        add("Steve");
        //List of heads to be added by default
    }};

    private static final Logger LOGGER = LogManager.getLogger();
    public static final String MOD_ID = "annomod";
    public static annomod instance;
    public annomod() {
        // 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);
        // Register the doClientStuff method for modloading
        FMLJavaModLoadingContext.get().getModEventBus().addListener(this::doClientStuff);
        instance = this;
        // 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
    }

    private void doClientStuff(final FMLClientSetupEvent event) {
        // do something that can only be done on the client
    }

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

    // 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");
        }
        @SubscribeEvent
        public static void updateHeads(final GuiOpenEvent event){

            Collection<NetworkPlayerInfo> playersC= Minecraft.getInstance().getConnection().getPlayerInfoMap();
            playersC.forEach((loadedPlayer)->{
                String LoadedPlayerName = loadedPlayer.getGameProfile().getName();
                if(!online_player_names.contains(LoadedPlayerName)){
                    online_player_names.add(LoadedPlayerName);
                }
                LoadedPlayerName = loadedPlayer.getDisplayName().getString();
                if(!online_player_names.contains(LoadedPlayerName)) {
                    online_player_names.add(LoadedPlayerName);
                }
            });
            String LoadedPlayerName = Minecraft.getInstance().player.getDisplayName().getString();
            if(!online_player_names.contains(LoadedPlayerName)) {
                online_player_names.add(LoadedPlayerName);
            }


        }
    }
}

 

 

 

 

 

 

Thanks to everyone !!!!!!!!

Link to comment
Share on other sites

Several comments:

  1. You are still comparing strings using ==. This will fail!
  2. The new ArrayList {{ inner class hack is ugly and causes unnecessary class files. Use a utility method like Lists.newArrayList from Guava instead if you want a one liner.
  3. Your updateHeads method will crash servers because it references client-only classes from common code. Refer to the documentation for how to properly isolate distribution-specific code.
  4. Your updateHeads method is inefficient and unnecessary. Just iterate over the online players in the fill method instead of keeping a separate ArrayList.
Link to comment
Share on other sites

  • 2 months later...

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

×
×
  • Create New...

Important Information

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