Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Renaming a block? client sided..?

Comments in 'Plugin Development' started by dxm_hippie, Jan 26, 2016.

  1. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    I saw that in the give command..thats why i decided to try it out but i thought i saw somewhere that is will create memory leaks..maybe if not used correctly?
  2. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    The whole iteminhand/additem thing was my first and failed attempt to stop it from removing it from your hand..if i just use setiteminhand it will delete whatever was in that hotbar slot before
  3. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    I can't fricking remember where i saw that with CustomName
    something like $nbt->String("CustomName":[String("blaa")]); or so.. maybe in SpawnEgg.php, not sure
  4. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    So what would be the difference? would setting a custom name rename all the blocks in game if i ran it on load? like all the specified blocks i should say..bc right now im running it on playerhelditemevent.
  5. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    Well, for this you won't have to care about a player selecting the item. The name will always be the one you want.
  6. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    So i would be able to run it at load then?
  7. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    You wanna set custom item name? This is how I do it.
    PHP:
    public function onJoin(PlayerJoinEvent $e){
            
    $p $e->getPlayer();
            
    $item Item::get(Item::APPLE);
            
    $item->setCount(1);
            
    $tag "{display:{Name:\"My custom item name!\"}}";
            
    $tag NBT::parseJSON($tag);
            if(
    $tag instanceof Compound){
                
    $item->setNamedTag($tag);
            }
            
    $p->getInventory()->addItem($item);
        }
  8. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    Well i had some issues using that style with json..had to move the json format to a file the read it from there i have no idea why. Also i am calling it on player held item event so ik that they already have the item and how many they have. I was kinda wondering if there is a way to rename a block or item compleatly..so once the code is ran once (on startup) any spesified item in the game will have the custom name
  9. Samueljh1H8sLag
    Offline

    Samueljh1H8sLag Active Member

    Joined:
    Jun 20, 2015
    Posts:
    168
    Minecraft User:
    Samueljh1_
    More tags can be found here: http://minecraft.gamepedia.com/Tutorials/Command_NBT_tags
    also note that using NBT tags can be a good way to store data within an item, for example, my server uses powerups that store the owner's username. This then only allows the owner to use that item. (Check the data matches the username in PlayerInteractEvent).

    Here is a better way to set nbt tags, rather than JSON:

    PHP:
    $item->setCompoundTag(new Compound("",
         
    "display" => new Compound("display", [
             
    "Name" => new String("Name"$customName),
          ])
    ]));
    PocketKiller likes this.
  10. Samueljh1H8sLag
    Offline

    Samueljh1H8sLag Active Member

    Joined:
    Jun 20, 2015
    Posts:
    168
    Minecraft User:
    Samueljh1_
    just do

    PHP:
    $item Item::get(Item::APPLE01);
    $tag NBT::parseJSON("{display:{Name:\"My custom item name!\"}}");
    $item->setNamedTag($tag);
    There's no need to use an instanceof
  11. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    Bad syntax.
    I copied that from @shoghicp
  12. Samueljh1H8sLag
    Offline

    Samueljh1H8sLag Active Member

    Joined:
    Jun 20, 2015
    Posts:
    168
    Minecraft User:
    Samueljh1_
    Why is that bad syntax?
  13. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    You need instanceof as NBT::parseJSON will return null if JSON syntax is invalid
  14. Samueljh1H8sLag
    Offline

    Samueljh1H8sLag Active Member

    Joined:
    Jun 20, 2015
    Posts:
    168
    Minecraft User:
    Samueljh1_
    Well that doesn't look invalid, so there's no need. I would only do this if the name was defined by user input. Also my method is faster, easier to read and doesn't need any instanceof calls.
    dxm_hippie likes this.
  15. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    I dont think you need the instanceof..bc ik i had a json format wrong a few times while troubleshooting but i got no error or crash. I do have it working tho but thanks for your guy's suggestions. The way i have done it tho is sort of buggy..well not buggy but i dont like it lol i have to remove the item from there hand then clone the item then put the cloned 'named' item back in the inventory..then they may hold the item after that /: will setCompountTag rename it without having to do this?
  16. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    It won't do anything if the JSON format is invalid.
  17. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Better to use assertions for programmer mistakes.
  18. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    i have a fix for setCustomName() method

    PHP:
    public function setCustomName($name){
            if((string) 
    $name === ""){
                
    $this->clearCustomName();
            }

            if(!
    $this->hasCompound()){
                
    $tag = new Compound("", []);
            }else{
                
    $tag $this->getNamedTag();
            }

            if(isset(
    $tag->display) and $tag->display instanceof Compound){
                
    $tag->display->Name = new StringTag("Name"$name);
            }else{
                
    $tag->display = new Compound("display", [
                    
    "Name" => new StringTag("Name"$name)
                ]);
            }

            
    $this->setNamedTag($tag); //this line is missing

            
    return $this;
        }

Share This Page

Advertisement