Jump to content

Recommended Posts

Posted

How can I have nbt data specific to a block, I mean like if i place two of the same block, I want each to have its own nbt compound, the reason I am asking this is because I was able to pull it off, but both of the blocks would share the same nbt tag, how can I fix this? Thanks in advance!

 

 

I'm working on something big!  As long as there arent alot of big issues... [D

Posted

oh right! my code!

 

genericTileEntity.java:

 

package tutorial.generic;

import net.minecraft.block.state.IBlockState;

import net.minecraft.entity.EntityLivingBase;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.inventory.IInventory;

import net.minecraft.inventory.InventoryHelper;

import net.minecraft.item.ItemStack;

import net.minecraft.nbt.NBTTagCompound;

import net.minecraft.nbt.NBTTagList;

import net.minecraft.server.gui.IUpdatePlayerListBox;

import net.minecraft.tileentity.TileEntity;

import net.minecraft.util.BlockPos;

import net.minecraft.util.ChatComponentText;

import net.minecraft.util.ChatComponentTranslation;

import net.minecraft.util.IChatComponent;

import net.minecraft.world.World;

 

public class genericTileEntity extends TileEntity implements IInventory {

private ItemStack[] inventory;

public static String name = "";

public static float maxSpeed = 0;

    private String customName;

    public static NBTTagCompound maxSpeedTag = new NBTTagCompound();

public static NBTTagCompound nameTag = new NBTTagCompound();

   

 

    public genericTileEntity() {

        this.inventory = new ItemStack[this.getSizeInventory()];

    }

 

    public String getCustomName() {

        return this.customName;

    }

 

    public void setCustomName(String customName) {

        this.customName = customName;

    }

   

    @Override

    public String getName() {

        return this.hasCustomName() ? this.customName : "container.tutorial_tile_entity";

    }

 

    @Override

    public boolean hasCustomName() {

        return this.customName != null && !this.customName.equals("");

    }

 

    @Override

    public IChatComponent getDisplayName() {

        return this.hasCustomName() ? new ChatComponentText(this.getName()) : new ChatComponentTranslation(this.getName());

    }

   

    @Override

    public int getSizeInventory() {

        return 9;

    }

   

    @Override

    public ItemStack getStackInSlot(int index) {

        if (index < 0 || index >= this.getSizeInventory())

            return null;

        return this.inventory[index];

    }

 

    @Override

    public ItemStack decrStackSize(int index, int count) {

        if (this.getStackInSlot(index) != null) {

            ItemStack itemstack;

 

            if (this.getStackInSlot(index).stackSize <= count) {

                itemstack = this.getStackInSlot(index);

                this.setInventorySlotContents(index, null);

                this.markDirty();

                return itemstack;

            } else {

                itemstack = this.getStackInSlot(index).splitStack(count);

 

                if (this.getStackInSlot(index).stackSize <= 0) {

                    this.setInventorySlotContents(index, null);

                } else {

                    //Just to show that changes happened

                    this.setInventorySlotContents(index, this.getStackInSlot(index));

                }

 

                this.markDirty();

                return itemstack;

            }

        } else {

            return null;

        }

    }

 

    @Override

    public ItemStack getStackInSlotOnClosing(int index) {

        ItemStack stack = this.getStackInSlot(index);

        this.setInventorySlotContents(index, null);

        return stack;

    }

 

    @Override

    public void setInventorySlotContents(int index, ItemStack stack) {

        if (index < 0 || index >= this.getSizeInventory())

            return;

 

        if (stack != null && stack.stackSize > this.getInventoryStackLimit())

            stack.stackSize = this.getInventoryStackLimit();

           

        if (stack != null && stack.stackSize == 0)

            stack = null;

 

        this.inventory[index] = stack;

        this.markDirty();

    }

   

    @Override

    public int getInventoryStackLimit() {

        return 64;

    }

   

    @Override

    public boolean isUseableByPlayer(EntityPlayer player) {

        return this.worldObj.getTileEntity(this.getPos()) == this && player.getDistanceSq(this.pos.add(0.5, 0.5, 0.5)) <= 64;

    }

   

    @Override

    public void openInventory(EntityPlayer player) {

    }

 

    @Override

    public void closeInventory(EntityPlayer player) {

    }

   

    @Override

    public boolean isItemValidForSlot(int index, ItemStack stack) {

        return true;

    }

   

    @Override

    public int getField(int id) {

        return 0;

    }

 

    @Override

    public void setField(int id, int value) {

    }

 

    @Override

    public int getFieldCount() {

        return 0;

    }

   

    @Override

    public void clear() {

        for (int i = 0; i < this.getSizeInventory(); i++)

            this.setInventorySlotContents(i, null);

    }

   

    public static void processActivate(EntityPlayer par5EntityPlayer, World world) {

        name = guiGenericTileEntity.textField.getText();

        maxSpeed = guiGenericTileEntity.mySlider.sliderValue;

}

   

   

   

    @Override

    public void writeToNBT(NBTTagCompound nbt) {

        super.writeToNBT(nbt);

 

        nbt.setString("name",name);

        nbt.setFloat("maxSpeed", maxSpeed);

        System.out.println("saved!");

    }

 

 

    @Override

    public void readFromNBT(NBTTagCompound nbt) {

        super.readFromNBT(nbt);

 

        name = nbt.getString("name");

        maxSpeed = nbt.getFloat("maxSpeed");

        System.out.println("loaded!");

    }

   

   

   

    public void setName(String name)

    {

        this.name = name;

        this.markForUpdate();

    }

   

    public void setMaxSpeed(float maxSpeed)

    {

        this.maxSpeed = maxSpeed;

        this.markForUpdate();

    }

   

    public void markForUpdate()

    {

    BlockPos pos = this.getPos();

        this.worldObj.markBlockForUpdate(pos);

        this.markDirty();

    }

   

   

 

 

   

}

 

 

 

 

 

guiGenericTileEntity.java:

 

 

 

 

 

package tutorial.generic;

 

import java.awt.Color;

import java.io.IOException;

 

import net.minecraft.client.gui.GuiButton;

import net.minecraft.client.gui.GuiScreen;

import net.minecraft.client.gui.GuiSlider;

import net.minecraft.client.gui.GuiTextField;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.nbt.NBTTagCompound;

import net.minecraft.util.ChatComponentText;

import net.minecraft.util.ResourceLocation;

import net.minecraft.util.StatCollector;

import net.minecraft.world.World;

 

public class guiGenericTileEntity extends GuiScreen{

 

public static NBTTagCompound nameTag = new NBTTagCompound();

private int x, y, z;

private EntityPlayer player;

private World world;

private int xSize, ySize;

public static GuiTextField textField;

public static GuiSliderFixed mySlider;

public static NBTTagCompound maxSpeedTag = new NBTTagCompound();

public guiGenericTileEntity(EntityPlayer player, World world, int x, int y, int z) {

 

this.x = x;

this.y = y;

this.z = z;

this.player = player;

this.world = world;

xSize = 176;

ySize = 214;

}

 

 

 

private ResourceLocation backgroundimage = new ResourceLocation(Generic.MODID.toLowerCase() + ":" + "textures/gui/guiBackGenericTileEntity.png");

 

@Override

public void drawScreen(int mouseX, int mouseY, float renderPartialTicks) {

this.mc.getTextureManager().bindTexture(backgroundimage);

int x = (this.width - xSize) / 2;

int y = (this.height - ySize) / 2;

drawTexturedModalRect(x, y, 0, 0, xSize,  ySize);

 

fontRendererObj.drawString("MTTA System", (int) (width / 2 - (width / 13)), height / 15, 8);

fontRendererObj.drawString("Train Name :", (int) (width / 2 - (width / 13)), (int) (height / 7.8), 8);

fontRendererObj.drawString("Max Speed :", (int) (width / 2 - (width / 13)), (int) (height / 3.5), 8);

 

 

 

textField.drawTextBox();

 

super.drawScreen(mouseX, mouseY, renderPartialTicks);

}

 

@Override

public boolean doesGuiPauseGame() {

return false;

}

 

@Override

public void updateScreen(){

textField.updateCursorCounter();

 

super.updateScreen();

}

 

@Override

protected void keyTyped(char typedChar, int keyCode) throws IOException{

textField.textboxKeyTyped(typedChar, keyCode);

super.keyTyped(typedChar, keyCode);

}

 

@Override

public void initGui(){

 

 

buttonList.add(new GuiButton(1, (int) (xSize / 3 * 2.35), ySize - (ySize / 18), xSize - 20, height / 12, "Save And Close"));

 

mySlider = new GuiSliderFixed(3, width / 2 - 75, height / 3, "MPH ", 1.0F, 100.0F, 1.0F);

buttonList.add(mySlider);

mySlider.sliderValue = maxSpeedTag.getFloat("MaxSpeed");

 

textField = new GuiTextField(width / 2, fontRendererObj, width / 2 - 50, (int) (height /  6), 100, 20);

textField.setMaxStringLength(30);

textField.setText(nameTag.getString("Name"));

textField.setFocused(true);

textField.setCanLoseFocus(false);

super.initGui();

}

 

@Override

protected void actionPerformed(GuiButton guibutton) {

//id is the id you give your button

switch(guibutton.id) {

case 1:

player.closeScreen();

nameTag.setString("Name", textField.getText());

maxSpeedTag.setFloat("MaxSpeed", mySlider.sliderValue);

genericTileEntity.processActivate(player, world);

player.addChatMessage(new ChatComponentText("Saved the current settings for " + textField.getText() + "!"));

break;

}

//Packet code here

//PacketDispatcher.sendPacketToServer(packet); //send packet

}

 

 

 

 

 

}

 

 

Hope this helps! ;D

I'm working on something big!  As long as there arent alot of big issues... [D

Posted

You don't need NBT data here.  Just use a private float property.

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.

Posted

ok, but what if I created another entity later that needed this data? then what would I do?

I'm working on something big!  As long as there arent alot of big issues... [D

Posted

Define "later needs this data."

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.

Posted

He needs that because he is using Static variables and all tileEntities are overriding each other.

Remove the static from your variables (speed and the other one) and you will not have those problems anymore.

 

For explaining:

Static means no matter how often that class gets recreated that variable will be always the same for all of them.

A none static version will be recreated and shares the data only with the current instance which created it.

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • ahh yes I mean waystone. Thank you very much helped alot  cheers mate!
    • Do you mean waystones?   You can use KubeJS to remove and add a changed recipe for it: https://www.curseforge.com/minecraft/mc-mods/kubejs   Add KubeJS and start the modpack once to generate the files for the mod   In your modpack folder, you will find now a kubejs folder Go to server_scripts (create the folder if missing) - yes, also for singleplayer, use the server_scripts folder Create a new file "reciperemove.js" Edit this file and add the recipe for the waypoint mod: ServerEvents.recipes(event => { let toRemove = [ {output: 'waystones:example_item'}, ]; for (const remove of toRemove) { event.remove(remove); } }) Just make sure you are using the correct item ID   Create another file "recipes.js" in the server_scripts folder and use: ServerEvents.recipes(event => { event.shaped( Item.of('waystones:example_item'), [ 'AAA', 'BCB', 'AAA' ], { A: 'minecraft:stone', B: 'waystones:item_1', C: 'waystones:item_2' } ) })   The Letter Block with AAA etc is a crafting table - so there you define the recipe and the items      
    • I am currently making a modpack for myself and I want to change the recipe for waypoints*. How do I do this? Is there a simply way to change an existing recipe (or to delete the old one and make a new one). I only need a for 1 single player world. My coding Knowledge is limited to arduinos but I feel I could teach myself some Java if needed *waypoint mod by BlayTheNinth
    • Tired of the same old iron sword and pickaxe? Dive into a world of enhanced possibilities with Droid's Implements! This exciting addition to your Minecraft experience introduces:   Four Unique Weapons:    Daggers: Quick, cheap, and effective, great for agility. Applies Weakness for one minute. Warhammers: A powerful option for defeating strong enemies. Applies Harming for a quarter of a second, making it ineffective against the undead. Spears: A balanced weapon with fair damage and speed. Applies Mining Fatigue, making escape difficult for cornered enemies. Clubs: Hard-hitting yet slow, the club is a robust alternative to the sword. Applies Slowness, increasing the chance of catching your enemy.   Two Handy Tools:   The Mining Hammer: A worthwhile alternative to the pickaxe, the Mining Hammer mines in a 3x3 area, making mining stone more efficient. The Heavy Shovel: A beneficial substitute to the shovel, the Mining Hammer mines in a 3x3 area, making mining sand and gravel more efficient.   Damage Comparison: Dagger > Spear > Sword = Club > Warhammer     Check it out now!
  • Topics

×
×
  • Create New...

Important Information

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