Skip to content

Instantly share code, notes, and snippets.

@Minikloon
Created December 12, 2016 20:01
Show Gist options
  • Save Minikloon/cb8821f4e3ead55a731990713a550b7c to your computer and use it in GitHub Desktop.
Save Minikloon/cb8821f4e3ead55a731990713a550b7c to your computer and use it in GitHub Desktop.
package me.rwd.funevents.items;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.*;
/**
* Created by George on 06-Dec-16.
* Distribution of thise code is forbidden unless
* permission is granted. Any code on Github is
* primarily for learning purposes. Please msg
*
* @Rapidwolf_ on twitter for information
*/
public class AnvilDrop extends CustomItem {
@Override
protected ItemDefinition getItemDefinition() {
return new ItemDefinition(Material.STICK, ChatColor.RED + "" + ChatColor.BOLD + "Anvildrop");
}
@Override
public Recipe getRecipe() {
ShapedRecipe recipe = new ShapedRecipe(getItemStack());
recipe.shape(
"III",
"IAI",
"III"
);
recipe.setIngredient('I', Material.IRON_BLOCK);
recipe.setIngredient('A', Material.ANVIL);
return recipe;
}
@Override
public void onUse(Player player) {
Location spawnLoc = player.getLocation().add(0, 10, 0);
spawnLoc.getBlock().setType(Material.ANVIL);
}
}
package me.rwd.funevents.items;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.material.MaterialData;
public abstract class CustomItem {
public ItemStack getItemStack() {
ItemDefinition itemDef = getItemDefinition();
ItemStack item = itemDef.mat.toItemStack();
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(itemDef.name);
item.setItemMeta(meta);
return item;
}
protected abstract ItemDefinition getItemDefinition();
public abstract Recipe getRecipe();
public abstract void onUse(Player player);
protected class ItemDefinition {
public final MaterialData mat;
public final String name;
public ItemDefinition(MaterialData mat, String name) {
this.mat = mat;
this.name = name;
}
public ItemDefinition(Material mat, String name) {
this.mat = new MaterialData(mat);
this.name = name;
}
}
}
package me.rwd.funevents.items;
import com.google.common.collect.Maps;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import java.util.Collection;
import java.util.Map;
import static me.rwd.funevents.utils.ItemUtils.*;
public class CustomItemListener implements Listener {
private final Map<Integer, CustomItem> customItems;
public CustomItemListener(Collection<CustomItem> items) {
this.customItems = Maps.uniqueIndex(items, item -> hashItemStack(item.getItemStack()));
}
@EventHandler
public void onUseItem(PlayerInteractEvent e) {
if(! e.getHand().equals(EquipmentSlot.HAND))
return;
ItemStack inHand = e.getItem();
CustomItem customItem = customItems.get(hashItemStack(inHand));
if(customItem == null)
return;
customItem.onUse(e.getPlayer());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment