Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.lisenochek.mcrust.objects.player;
- import org.bukkit.GameMode;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.entity.ArmorStand;
- import org.bukkit.entity.Entity;
- import org.bukkit.entity.Player;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.metadata.FixedMetadataValue;
- import ru.lisenochek.mcrust.Main;
- import ru.lisenochek.mcrust.objects.misc.custom.CustomWorld;
- import ru.lisenochek.mcrust.objects.misc.entity.CustomEntity;
- import ru.lisenochek.mcrust.sql.SQLManager;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.List;
- public class Backpack {
- private static HashMap<String, Backpack> backpacksMap = new HashMap<>();
- private String ownerName;
- private Location location;
- private Entity entity;
- private List<ItemStack> dropList;
- private Backpack(String ownerName, Location location) {
- this.ownerName = ownerName;
- this.location = location.getBlock().getType() == Material.STATIONARY_WATER ? location.getWorld().getHighestBlockAt(location).getLocation() : location.getBlock().getType() != Material.IRON_DOOR_BLOCK && location.getBlock().getType().isSolid() ? location.clone().add(0, 1, 0) : location;
- }
- public static Backpack create(Player player) {
- if (player.getGameMode() != GameMode.SURVIVAL) return null;
- if (player.getWorld() != CustomWorld.getCustomWorld().getWorld()) return null;
- return new Backpack(player.getName(), player.getLocation().getBlock().getLocation()).setDropList(Arrays.asList(player.getInventory().getContents())).spawn();
- }
- public static Backpack create(String ownerName, Location location, List<ItemStack> dropList) {
- return new Backpack(ownerName, location).setDropList(dropList).spawn();
- }
- public static Backpack fromOwnerName(String ownerName) {
- return backpacksMap.get(ownerName);
- }
- public static int getBackpacksAmount() {
- return backpacksMap.size();
- }
- public static void save() {
- for (Backpack backpack : backpacksMap.values()) {
- SQLManager.getManager().addBackpackData(backpack);
- backpack.getEntity().remove();
- }
- }
- public String getOwnerName() {
- return ownerName;
- }
- public Location getLocation() {
- return location;
- }
- public Entity getEntity() {
- return entity;
- }
- public List<ItemStack> getDropList() {
- return dropList;
- }
- public Backpack setDropList(List<ItemStack> list) {
- List<ItemStack> dropList = new ArrayList<>();
- for (ItemStack stack : list) if (stack != null) dropList.add(stack);
- this.dropList = dropList;
- return this;
- }
- public Backpack spawn() {
- if (dropList.size() == 0) return null;
- ArmorStand backpack = CustomEntity.spawn(location.clone().add(0.5D, -1.4D, 0.5D), ArmorStand.class);
- backpack.setHelmet(new ItemStack(Material.CARPET, 1, (short) 4));
- backpack.setGravity(false);
- backpack.setVisible(false);
- backpack.setMetadata("backpackOwner", new FixedMetadataValue(Main.plugin, ownerName));
- entity = backpack;
- backpacksMap.put(ownerName, this);
- return this;
- }
- public void remove() {
- backpacksMap.remove(ownerName);
- entity.remove();
- SQLManager.getManager().deleteBackpackData(this);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement