package mc.craftstuebchen.de.backpacks;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin {
HashMap<UUID, Inventory> backpacks = new HashMap<UUID, Inventory>();
File file = new File(getDataFolder().getPath(), "backpacks.yml");
YamlConfiguration cfg = YamlConfiguration.loadConfiguration(file);
@Override
public void onEnable() {
this.getCommand("backpack").setExecutor(new Backpack_Command(this));
new PlayerIntractListener(this);
for(String uuid : cfg.getConfigurationSection("").getKeys(false)){
Inventory inv = Bukkit.createInventory(null, 27, "§8Rucksack");
for(Object o : cfg.getList(uuid)){
inv.addItem((ItemStack) o == null ? new ItemStack(Material.AIR) : (ItemStack)o);
}
backpacks.put(UUID.fromString(uuid), inv);
}
}
@Override
public void onDisable() {
for(UUID uuid : backpacks.keySet()){
List<ItemStack> stacks = new LinkedList<ItemStack>();
for(ItemStack stack : backpacks.get(uuid)){
stacks.add(stack == null ? new ItemStack(Material.AIR) : stack);
}
cfg.set(uuid.toString(), stacks);
save();
}
}
public HashMap<UUID, Inventory> getBackpacks() {
return backpacks;
}
public void save(){
try {
cfg.save(file);
} catch (IOException e) {
e.printStackTrace();
}
}
public void createBackpack(UUID uuid){
backpacks.put(uuid, Bukkit.createInventory(null, 27, "§8Rucksack"));
}
}
package mc.craftstuebchen.de.backpacks;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
public class PlayerIntractListener implements Listener {
private Main instance;
public PlayerIntractListener(Main instance){
this.instance = instance;
this.instance.getServer().getPluginManager().registerEvents(this, instance);
}
@EventHandler
public void oninteract(PlayerInteractEvent e){
if(e.getPlayer().getItemInHand() == null) return;
if(e.getPlayer().getItemInHand().getType() == Material.MAGMA_CREAM){
if(!(this.instance.getBackpacks().containsKey(e.getPlayer().getUniqueId()))) this.instance.createBackpack(e.getPlayer().getUniqueId());
e.getPlayer().closeInventory();
e.getPlayer().openInventory(this.instance.backpacks.get(e.getPlayer().getUniqueId()));
}
}
}
package mc.craftstuebchen.de.backpacks;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Backpack_Command implements CommandExecutor {
private Main instance;
public Backpack_Command(Main instance){
this.instance = instance;
}
@SuppressWarnings("deprecation")
@Override
public boolean onCommand(CommandSender cs, Command cmd, String label, String[] args) {
if(!(cs instanceof Player)){
cs.sendMessage("Du bist kein Spieler");
return true;
}
Player p = (Player) cs;
if(cmd.getName().equalsIgnoreCase("backpack")){
if(args.length == 0){
p.openInventory(this.instance.getBackpacks().get(p.getUniqueId()));
return true;
}
if(args.length == 1){
if(p.hasPermission("backpacks.seeothers")){
p.openInventory(this.instance.getBackpacks().get(Bukkit.getOfflinePlayer(args[0]).getUniqueId()));
} else {
p.sendMessage("§7[§6Backpacks§7]§c Du hast keine Rechte , auf den Rucksack von anderen Spieler zuzugreifen.");
}
return true;
}
p.sendMessage("§7[§6Backpacks§7]§c Nutze: /backpack oder /backpack <spielername>");
}
return true;
}
}