Jump to content

specific nbt help


UltraTechX

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • WINNING303 DAFTAR SITUS JUDI SLOT RESMI TERPERCAYA 2024 Temukan situs judi slot resmi dan terpercaya untuk tahun 2024 di Winning303! Daftar sekarang untuk mengakses pengalaman berjudi slot yang aman dan terjamin. Dengan layanan terbaik dan reputasi yang kokoh, Winning303 adalah pilihan terbaik bagi para penggemar judi slot. Jelajahi berbagai pilihan permainan dan raih kesempatan besar untuk meraih kemenangan di tahun ini dengan Winning303 [[jumpuri:❱❱❱❱❱ DAFTAR DI SINI ❰❰❰❰❰ > https://w303.pink/orochimaru]] [[jumpuri:❱❱❱❱❱ DAFTAR DI SINI ❰❰❰❰❰ > https://w303.pink/orochimaru]]
    • SLOT PULSA TANPA POTONGAN : SLOT PULSA 5000 VIA INDOSAT IM3 TANPA POTONGAN - SLOT PULSA XL TELKOMSEL TANPA POTONGAN  KLIK DISINI DAFTAR DISINI SLOT VVIP << KLIK DISINI DAFTAR DISINI SLOT VVIP << KLIK DISINI DAFTAR DISINI SLOT VVIP << KLIK DISINI DAFTAR DISINI SLOT VVIP << SITUS SLOT GACOR 88 MAXWIN X500 HARI INI TERBAIK DAN TERPERCAYA GAMPANG MENANG Dunia Game gacor terus bertambah besar seiring berjalannya waktu, dan sudah tentu dunia itu terus berkembang serta merta bersamaan dengan berkembangnya SLOT GACOR sebagai website number #1 yang pernah ada dan tidak pernah mengecewakan sekalipun. Dengan banyaknya member yang sudah mempercayakan untuk terus menghasilkan uang bersama dengan SLOT GACOR pastinya mereka sudah percaya untuk bermain Game online bersama dengan kami dengan banyaknya testimoni yang sudah membuktikan betapa seringnya member mendapatkan jackpot besar yang bisa mencapai ratusan juta rupiah. Best online Game website that give you more money everyday, itu lah slogan yang tepat untuk bermain bersama SLOT GACOR yang sudah pasti menang setiap harinya dan bisa menjadikan bandar ini sebagai patokan untuk mendapatkan penghasilan tambahan yang efisien dan juga sesuatu hal yang fix setiap hari nya. Kami juga mendapatkan julukan sebagai Number #1 website bocor yang berarti terus memberikan member uang asli dan jackpot setiap hari nya, tidak lupa bocor itu juga bisa diartikan dalam bentuk berbagi promosi untuk para official member yang terus setia bermain bersama dengan kami. Berbagai provider Game terus bertambah banyak setiap harinya dan terus melakukan support untuk membuat para official member terus bisa menang dan terus maxwin dalam bentuk apapun maka itu langsung untuk feel free to try yourself, play with SLOT GACOR now or never !
    • BRI4D adalah pilihan tepat bagi Anda yang menginginkan pengalaman bermain slot dengan RTP tinggi dan transaksi yang akurat melalui Bank BRI. Berikut adalah beberapa alasan mengapa Anda harus memilih BRI4D: Tingkat Pengembalian (RTP) Tertinggi Kami bangga menjadi salah satu agen situs slot dengan RTP tertinggi, mencapai 99%! Ini berarti Anda memiliki peluang lebih besar untuk meraih kemenangan dalam setiap putaran permainan. Transaksi Melalui Bank BRI yang Akurat Proses deposit dan penarikan dana di BRI4D cepat, mudah, dan akurat. Kami menyediakan layanan transaksi melalui Bank BRI untuk kenyamanan Anda. Dengan begitu, Anda dapat melakukan transaksi dengan lancar dan tanpa khawatir. Beragam Pilihan Permainan BRI4D menyajikan koleksi permainan slot yang beragam dan menarik dari berbagai provider terkemuka. Mulai dari tema klasik hingga yang paling modern, Anda akan menemukan banyak pilihan permainan yang sesuai dengan selera dan preferensi Anda.  
    • SPARTA88 adalah pilihan tepat bagi Anda yang menginginkan agen situs slot terbaik dengan RTP tinggi dan transaksi yang mudah melalui Bank BNI. Berikut adalah beberapa alasan mengapa Anda harus memilih SPARTA88: Tingkat Pengembalian (RTP) Tinggi Kami bangga menjadi salah satu agen situs slot dengan RTP tertinggi, mencapai 98%! Ini berarti Anda memiliki peluang lebih besar untuk meraih kemenangan dalam setiap putaran permainan. Beragam Pilihan Permainan SPARTA88 menyajikan koleksi permainan slot yang beragam dan menarik dari berbagai provider terkemuka. Mulai dari tema klasik hingga yang paling modern, Anda akan menemukan banyak pilihan permainan yang sesuai dengan selera dan preferensi Anda. Kemudahan Bertransaksi Melalui Bank BNI Proses deposit dan penarikan dana di SPARTA88 cepat, mudah, dan aman. Kami menyediakan layanan transaksi melalui Bank BNI untuk kenyamanan Anda. Dengan begitu, Anda dapat melakukan transaksi dengan lancar tanpa perlu khawatir.
    • Slot Bank ALADIN atau Daftar slot Bank ALADIN bisa anda lakukan pada situs WINNING303 kapanpun dan dimanapun, Bermodalkan Hp saja anda bisa mengakses chat ke agen kami selama 24 jam full. keuntungan bergabung bersama kami di WINNING303 adalah anda akan mendapatkan bonus 100% khusus member baru yang bergabung dan deposit. Tidak perlu banyak, 5 ribu rupiah saja anda sudah bisa bermain bersama kami di WINNING303 . Tunggu apa lagi ? Segera Klik DAFTAR dan anda akan jadi Jutawan dalam semalam.
  • Topics

×
×
  • Create New...

Important Information

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