-
-
Save FerusGrim/712f44e7f7310494502f to your computer and use it in GitHub Desktop.
CustomInventory util
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.bukkit.Bukkit; | |
import org.bukkit.Material; | |
import org.bukkit.entity.Player; | |
import org.bukkit.inventory.Inventory; | |
import org.bukkit.inventory.ItemStack; | |
import org.bukkit.inventory.meta.ItemMeta; | |
import java.util.Arrays; | |
import java.util.HashMap; | |
import java.util.Map; | |
/** | |
* @author FerusGrim | |
*/ | |
public class CustomInventory { | |
private String name; | |
private int invSize; | |
private final Map<Integer, ItemStack> itemMap; | |
public CustomInventory(String name, int invSize, Map<Integer, ItemStack> itemMap) { | |
this.name = name; | |
this.invSize = invSize; | |
this.itemMap = itemMap; | |
} | |
public CustomInventory(String name, int invSize) { | |
this(name, invSize, new HashMap<>()); | |
} | |
public CustomInventory(String name) { | |
this(name, 1); | |
} | |
public String getName() { | |
return this.name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public int getInvSize() { | |
return this.invSize; | |
} | |
public void setInvSize(int invSize) { | |
this.invSize = invSize; | |
} | |
public Map<Integer, ItemStack> getItemMap() { | |
return this.itemMap; | |
} | |
public ItemStack getItem(int slot) { | |
return this.itemMap.get(slot); | |
} | |
public void addItem(int slot, ItemStack item) { | |
this.itemMap.put(slot, item); | |
} | |
public void removeItem(int slot) { | |
this.itemMap.remove(slot); | |
} | |
public Inventory generateInventory() { | |
final Inventory inventory = Bukkit.createInventory(null, this.invSize, this.name); | |
for (int slot : this.itemMap.keySet()) { | |
inventory.setItem(slot, this.itemMap.get(slot)); | |
} | |
return inventory; | |
} | |
public void openInventory(Player player) { | |
player.openInventory(this.generateInventory()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void setupInventory(Player player) { | |
CustomInventory cInv = new CustomInventory("Custom Inventory", 9); | |
ItemStack itemStack = new ItemStack(Material.BLAZE_ROD); | |
ItemMeta meta = itemStack.getItemMeta(); | |
meta.setDisplayName("Rod of Amazingness"); | |
meta.setLore(Arrays.asList("Destroy your foes with justice!")); | |
itemStack.setItemMeta(meta); | |
cInv.addItem(5, itemStack); | |
cInv.openInventory(player); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment