Skip to content

Instantly share code, notes, and snippets.

@thinkverse
Last active February 4, 2020 14:54
Show Gist options
  • Save thinkverse/0bd0f870c0c6bf87a559c7722997394d to your computer and use it in GitHub Desktop.
Save thinkverse/0bd0f870c0c6bf87a559c7722997394d to your computer and use it in GitHub Desktop.
Spawn SplashPotion on Player
public final class PotionCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (sender instanceof Player) {
final Player PLAYER = ((Player) sender).getPlayer();
final World WORLD = PLAYER.getWorld();
// Give player the Splash Potion.
// PLAYER.getInventory().addItem(makeSplashPotionItemStack(PotionType.INSTANT_HEAL, false, true));
final ThrownPotion THROWABLE = (ThrownPotion) WORLD.spawnEntity(PLAYER.getLocation(), EntityType.SPLASH_POTION);
THROWABLE.setItem(makeSplashPotionItemStack(PotionType.INSTANT_HEAL, false, true));
return true;
}
return false;
}
/**
* Make a {@link ItemStack} of {@link Material#SPLASH_POTION}
* @param potionType {@link PotionType}
* @param extended {@link Boolean} Should the potion be extended?
* @param upgraded {@link Boolean} Should the potion be upgraded?
* @return {@link ItemStack}
*/
private ItemStack makeSplashPotionItemStack(final PotionType potionType, final boolean extended, final boolean upgraded) {
final ItemStack POTION = new ItemStack(Material.SPLASH_POTION, 1);
final PotionMeta META = (PotionMeta) POTION.getItemMeta();
META.setBasePotionData(new PotionData(potionType, extended, upgraded));
POTION.setItemMeta(META);
return POTION;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment