Help with a bit of code

Status
This thread has been locked.

chelsea1124

⚔️professional config and server creator⚔️
Premium
Feedback score
17
Posts
423
Reactions
53
Resources
3
Hello mc-market developers to day I would like to know how to do 2 things

One is how to add custom name to my items when a player joins the server

Code for that:
Code:
    // Armor on join \\
    @EventHandler
    public void onJoin(PlayerJoinEvent e) {

        e.getPlayer().getInventory().setHelmet(new ItemStack(Material.STAINED_GLASS, 1));
        e.getPlayer().getInventory().setChestplate(new ItemStack(Material.IRON_CHESTPLATE, 1));
        e.getPlayer().getInventory().setLeggings(new ItemStack(Material.IRON_LEGGINGS, 1));
        e.getPlayer().getInventory().setBoots(new ItemStack(Material.IRON_BOOTS, 1));

        e.getPlayer().setMaxHealth(2.0);
        e.getPlayer().setHealth(2.0);
        e.getPlayer().setFoodLevel(20);
    }

Second, how would I make it in the config to when a player hits y 0 they get teleport back to the cords but they get put back to the cords they put in the config

He is the code I have so far:


Code:
@EventHandler
        public void onPlayerMove (PlayerMoveEvent e){
            Player p = e.getPlayer();
            Location loc = p.getLocation();
            World world = Bukkit.getServer().getWorld("world");

            if (loc.getBlockY() <= 0) {

                World w = Bukkit.getServer().getWorld(getConfig().getString("location"));
                double x = getConfig().getDouble("loc.x");
                double y = getConfig().getDouble("loc.y");
                double z = getConfig().getDouble("loc.z");
                p.teleport(new Location(w, x, y, z));
            }
        }
 
PebbleHost
High performance, consistent uptime and fast support. Minecraft hosting that just works.

Method

Freelance Java Dev
Premium
Feedback score
5
Posts
47
Reactions
15
Resources
0
Gonna try and help you out without explicitly writing down the code for you.

For the first one:

Create an ItemStack object as you're already doing. Then create an ItemMeta object using getItemMeta(). On the ItemMeta object, do setDisplayName() to set its name. After that set the ItemStack's meta to the ItemMeta object. Then set the item in the player's inventory to the new ItemStack.

For the second one:
Theoretically this code should work, but it might just be a small technicality with the methods you're using. I would double check the logic of your if-statement. Maybe add a print-statement above and below it letting you know what their y-coordinate is.
 
Last edited:

Nerm

Deactivated
Feedback score
21
Posts
851
Reactions
714
Resources
0
For the first part, You have to make an ItemStack, which is
Code:
ItemStack helmetItem = new ItemStack(Material.GLASS);
then you have to get the ItemMeta, which is
Code:
ItemMeta helmetItemMeta = helmetItem.getItemMeta();
after that you can set the name also known as the Display Name to
Code:
helmetItemMeta.setDisplayName("Whatever");
then finally you have to set the Item Meta.
Code:
helmetItem.setItemMeta(helmetItemMeta);

then you give the player 'helmetItem' the same applies for every Item Stack unless you have an item stack builder.
 

chelsea1124

⚔️professional config and server creator⚔️
Premium
Feedback score
17
Posts
423
Reactions
53
Resources
3
For the first part, You have to make an ItemStack, which is
Code:
ItemStack helmetItem = new ItemStack(Material.GLASS);
then you have to get the ItemMeta, which is
Code:
ItemMeta helmetItemMeta = helmetItem.getItemMeta();
after that you can set the name also known as the Display Name to
Code:
helmetItemMeta.setDisplayName("Whatever");
then finally you have to set the Item Meta.
Code:
helmetItem.setItemMeta(helmetItemMeta);

then you give the player 'helmetItem' the same applies for every Item Stack unless you have an item stack builder.

Ok so i have this in my code
Code:
// Armor on join \\
    @EventHandler
    public void onJoin(PlayerJoinEvent e) {

        ItemStack helmetItem = new ItemStack(Material.STAINED_GLASS);
        ItemMeta helmetItemMeta = helmetItem.getItemMeta();
        helmetItem.addUnsafeEnchantment(Enchantment.DURABILITY, 10);
        helmetItem.setItemMeta(helmetItemMeta);

        e.getPlayer().setMaxHealth(2.0);
        e.getPlayer().setHealth(2.0);
        e.getPlayer().setFoodLevel(20);
    }

But all its not giving me the glass
 

Nerm

Deactivated
Feedback score
21
Posts
851
Reactions
714
Resources
0
Ok so i have this in my code
Code:
// Armor on join \\
    @EventHandler
    public void onJoin(PlayerJoinEvent e) {

        ItemStack helmetItem = new ItemStack(Material.STAINED_GLASS);
        ItemMeta helmetItemMeta = helmetItem.getItemMeta();
        helmetItem.addUnsafeEnchantment(Enchantment.DURABILITY, 10);
        helmetItem.setItemMeta(helmetItemMeta);

        e.getPlayer().setMaxHealth(2.0);
        e.getPlayer().setHealth(2.0);
        e.getPlayer().setFoodLevel(20);
    }

But all its not giving me the glass


Some enchantments can't go on some items if I'm correct.[DOUBLEPOST=1499792648][/DOUBLEPOST]My bad, Correction.

You have to add
Code:
e.getPlayer().getInventory().setHelmet(helmetItem);
 
Last edited:

Sniper

Software Developer
Supreme
Feedback score
65
Posts
1,312
Reactions
695
Resources
0
Ok so i have this in my code
Code:
// Armor on join \\
    @EventHandler
    public void onJoin(PlayerJoinEvent e) {

        ItemStack helmetItem = new ItemStack(Material.STAINED_GLASS);
        ItemMeta helmetItemMeta = helmetItem.getItemMeta();
        helmetItem.addUnsafeEnchantment(Enchantment.DURABILITY, 10);
        helmetItem.setItemMeta(helmetItemMeta);

        e.getPlayer().setMaxHealth(2.0);
        e.getPlayer().setHealth(2.0);
        e.getPlayer().setFoodLevel(20);
    }

But all its not giving me the glass
You never did e.getPlayer().getInventory().addItem(helmetItem);
 

chelsea1124

⚔️professional config and server creator⚔️
Premium
Feedback score
17
Posts
423
Reactions
53
Resources
3
Forgot he wanted it as a helmet :p
Thnak you your all a grate help[DOUBLEPOST=1499794290][/DOUBLEPOST]
Some enchantments can't go on some items if I'm correct.[DOUBLEPOST=1499792648][/DOUBLEPOST]My bad, Correction.

You have to add
Code:
e.getPlayer().getInventory().setHelmet(helmetItem);
Em its still not added the changed name or the enchant to it
 
Last edited:

Turtle

turtle#1989
Supreme
Feedback score
17
Posts
751
Reactions
419
Resources
0
Code:
      @EventHandler
      public void Spawn(PlayerMoveEvent e)
      {
        if (e.getTo().getY() < -10.0D) {
                                  World w = Bukkit.getServer().getWorld(getConfig().getString("spawn.world"));
                        double x = getConfig().getDouble("spawn.x");
                        double y = getConfig().getDouble("spawn.y");
                        double z = getConfig().getDouble("spawn.z");
                        p.teleport(new Location(w, x, y, z));
        }
      }
This is what I do
ewww http://www.oracle.com/technetwork/java/codeconvtoc-136057.html
 

Nalo

Feedback score
0
Posts
8
Reactions
1
Resources
0
Thnak you your all a grate help[DOUBLEPOST=1499794290][/DOUBLEPOST]
Em its still not added the changed name or the enchant to it
When you make a change and it still doesn't work, please attach the new code. It's impossible for us to know how you went wrong otherwise. :)
 

chelsea1124

⚔️professional config and server creator⚔️
Premium
Feedback score
17
Posts
423
Reactions
53
Resources
3
This is the code and its still not adding the name or the Enchant to it
Code:
 // Armor on join \\
    @EventHandler
    public void onJoin(PlayerJoinEvent e) {


        ItemStack helmetItem = new ItemStack(Material.STAINED_GLASS);
        ItemMeta helmetItemMeta = helmetItem.getItemMeta();
        helmetItem.addEnchantment(Enchantment.DURABILITY, 10);
        helmetItemMeta.setDisplayName("Whatever");
        helmetItem.setItemMeta(helmetItemMeta);
        e.getPlayer().getInventory().setHelmet(helmetItem);

        e.getPlayer().setMaxHealth(2.0);
        e.getPlayer().setHealth(2.0);
        e.getPlayer().setFoodLevel(20);
    }
[DOUBLEPOST=1499794997][/DOUBLEPOST]
When you make a change and it still doesn't work, please attach the new code. It's impossible for us to know how you went wrong otherwise. :)

There you go
 
Last edited:

Nalo

Feedback score
0
Posts
8
Reactions
1
Resources
0
This is the code and its still not adding the name or the Enchant to it
Code:
// Armor on join \\
    @EventHandler
    public void onJoin(PlayerJoinEvent e) {


        ItemStack helmetItem = new ItemStack(Material.STAINED_GLASS);
        ItemMeta helmetItemMeta = helmetItem.getItemMeta();
        helmetItem.addEnchantment(Enchantment.DURABILITY, 10);
        helmetItemMeta.setDisplayName("Whatever");
        helmetItem.setItemMeta(helmetItemMeta);
        e.getPlayer().getInventory().setHelmet(helmetItem);

        e.getPlayer().setMaxHealth(2.0);
        e.getPlayer().setHealth(2.0);
        e.getPlayer().setFoodLevel(20);
    }
[DOUBLEPOST=1499794997][/DOUBLEPOST]

There you go
#addUnsafeEnchantment() instead of #addEnchantment()?
 

Sniper

Software Developer
Supreme
Feedback score
65
Posts
1,312
Reactions
695
Resources
0
Still nothing and the name chaging is not working at all ether
do
helmetItem.addEnchantment(Enchantment.DURABILITY, 10, false);[DOUBLEPOST=1499796124][/DOUBLEPOST]The false at the end means that it is an unsafe enchantment, because unbreaking can normally not go to 10.

EDIT:
helmetItemMeta.addEnchantment(Enchantment.DURABILITY, 10, false);
 
Last edited:

chelsea1124

⚔️professional config and server creator⚔️
Premium
Feedback score
17
Posts
423
Reactions
53
Resources
3
do
helmetItem.addEnchantment(Enchantment.DURABILITY, 10, false);[DOUBLEPOST=1499796124][/DOUBLEPOST]The false at the end means that it is an unsafe enchantment, because unbreaking can normally not go to 10.

EDIT:
helmetItemMeta.addEnchantment(Enchantment.DURABILITY, 10, false);
Thnak you
 
Status
This thread has been locked.
Top