Skip to content

Instantly share code, notes, and snippets.

@phase
Created June 2, 2016 07:57
Show Gist options
  • Select an option

  • Save phase/83299316d1344cdfc87addef8dbf759e to your computer and use it in GitHub Desktop.

Select an option

Save phase/83299316d1344cdfc87addef8dbf759e to your computer and use it in GitHub Desktop.
package com.valygard.aohruthless.metadatatutorial;
import org.bukkit.Bukkit;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.entity.Zombie;
import org.bukkit.metadata.FixedMetadataValue;
/**
* To put some context into it, I use metadata in relation to zombies in my King
* of the Hill plugin. This has saved me lots of time and code from using NMS /
* Reflection where it was unnecessary.
*
* @author Anand
*
*/
public class MetadataTutorial {
/**
* This static method will spawn a zombie on the player and add metadata.
* Note: Adding metadata does not make the zombie stop attacking the player,
* you will need to listen for that in an EntityTargetEvent.
*
* @param plugin
* this is your main class. Replace "MainClass" with the class
* that extends JavaPlugin.
* @param p
* this is the player we want to make the owner of the zombie.
*/
public static void spawnZombie(MainClass plugin, Player p) {
Zombie z = (Zombie) p.getWorld().spawnEntity(p.getLocation(),
EntityType.ZOMBIE);
/*
* Setting the metadata.
*
* LivingEntity#setMetadata(String s, MetadataValue mv); The string is
* the metadata tag that we will use to get the zombie anywhere else in
* the plugin.
*
* There are two types of metadata: LazyMetadataValues, and
* FixedMetadataValues which extend lazy ones. We will be working with
* FixedMetadataValues.
*
* A FixedMetadataValue is, well, fixed. It is unchanging and therefore
* very reliable.
*
* You will need to call a new FixedMetadataValue, which requires a
* plugin argument and an arbitrary string. This string does not matter
* and you can name it whatever you want. I recommend that your plugin
* directly points your main class but Plugin, and JavaPlugin variable
* types also work.
*/
z.setMetadata(p.getName(), new FixedMetadataValue(plugin, "yes!"));
}
/**
* Metadata is useless unless we can check if the zombie has metadata to
* begin with.
*
* @param z
* the zombie to check
* @return the player, or null if none was found online.
*/
public static Player getPlayerWithZombie(Zombie z) {
for (Player p : Bukkit.getOnlinePlayers()) {
/*
* Remember how we set metadata. We had a string argument, and a
* metadata value. If the zombie has a metadata cache with this
* string value, we have a match.
*
* So right away with a few lines of code we are able to retrieve a
* player who the zombie belongs to.
*/
if (z.hasMetadata(p.getName())) {
return p;
}
}
return null;
}
/**
* Maybe we want to remove the player as a zombie owner. We can do that
* because the Metadata API allows us to remove metadata with just one line
* of code.
*
* @param z
* the zombie who has the metadata to be removed.
* @param owner
* the player whose name is the metadata.
* @param plugin
* we need a plugin argument to remove metadata.
*/
public static void setOwnerless(Zombie z, Player owner, MainClass plugin) {
if (z.hasMetadata(owner.getName())) {
/*
* Remember that string argument? We remove a zombie's metadata
* based on this string argument, so we don't accidentally avoid all
* metadata tags from other plugins or even elsewhere in this
* plugin. (Yes! The plot thickens: You can have multiple metadata
* tags)
*
* We also need the plugin argument from which we set metadata.
*/
z.removeMetadata(owner.getName(), plugin);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment