Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package FFA;
- import com.google.gson.Gson;
- import com.google.gson.JsonElement;
- import com.google.gson.JsonObject;
- import com.google.gson.JsonParser;
- import com.google.gson.stream.JsonWriter;
- import org.bukkit.*;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandExecutor;
- import org.bukkit.command.CommandSender;
- import org.bukkit.configuration.file.FileConfiguration;
- import org.bukkit.enchantments.Enchantment;
- import org.bukkit.entity.Arrow;
- import org.bukkit.entity.Player;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.EventPriority;
- import org.bukkit.event.Listener;
- import org.bukkit.event.block.Action;
- import org.bukkit.event.block.BlockPlaceEvent;
- import org.bukkit.event.entity.EntityDamageByEntityEvent;
- import org.bukkit.event.player.*;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.inventory.PlayerInventory;
- import org.bukkit.inventory.meta.ItemMeta;
- import org.bukkit.plugin.PluginManager;
- import org.bukkit.plugin.java.JavaPlugin;
- import org.bukkit.scheduler.BukkitRunnable;
- import org.bukkit.util.io.BukkitObjectInputStream;
- import org.bukkit.util.io.BukkitObjectOutputStream;
- import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
- import java.io.*;
- import java.text.DecimalFormat;
- import java.util.*;
- import java.util.concurrent.locks.ReadWriteLock;
- import java.util.concurrent.locks.ReentrantReadWriteLock;
- public class Main
- extends JavaPlugin
- implements Listener, CommandExecutor
- {
- FileConfiguration config;
- File cfile;
- File invSaves;
- ReadWriteLock lock;
- Gson gson;
- public Map<UUID, Map.Entry<ItemStack[], ItemStack[]>> inventories = new HashMap<>();
- JsonParser parser;
- public void onEnable()
- {
- getServer().getPluginManager().registerEvents(this, this);
- getServer().getPluginManager().registerEvents(new Events(), this);
- this.config = getConfig();
- this.config.options().copyDefaults(true);
- saveDefaultConfig();
- this.cfile = new File(getDataFolder(), "config.yml");
- invSaves = new File(getDataFolder(), "inventories.json");
- lock = new ReentrantReadWriteLock();
- gson = new Gson();
- parser = new JsonParser();
- lock.readLock().lock();
- try(FileReader fr = new FileReader(invSaves)) {
- JsonObject tree = parser.parse(fr).getAsJsonObject();
- for(Map.Entry<String, JsonElement> entry : tree.entrySet()) {
- UUID uuid = UUID.fromString(entry.getKey());
- JsonObject data = entry.getValue().getAsJsonObject();
- ItemStack[] contents = arrayFromBase64(data.get("inventory").getAsString());
- ItemStack[] armour = arrayFromBase64(data.get("armour").getAsString());
- inventories.put(uuid, new AbstractMap.SimpleEntry<>(contents, armour));
- }
- }catch (IOException e) {
- e.printStackTrace();
- }finally {
- lock.readLock().unlock();
- }
- }
- @EventHandler
- public void onPlayerJoin(PlayerJoinEvent event)
- {
- Player player = event.getPlayer();
- player.getInventory().clear();
- player.setGameMode(GameMode.SURVIVAL);
- teleportToSpawn(player);
- if(!inventories.containsKey(player.getUniqueId())) {
- ItemStack diamondsword = new ItemStack(Material.IRON_SWORD);
- diamondsword.addEnchantment(Enchantment.DAMAGE_ALL, 1);
- diamondsword.setAmount(1);
- ItemStack bow = new ItemStack(Material.BOW);
- bow.addEnchantment(Enchantment.ARROW_DAMAGE, 2);
- bow.setAmount(1);
- ItemStack frod = new ItemStack(Material.FISHING_ROD);
- frod.addEnchantment(Enchantment.DURABILITY, 3);
- frod.setAmount(1);
- ItemStack gapple1 = new ItemStack(Material.GOLDEN_APPLE);
- gapple1.setAmount(6);
- ItemStack arrow = new ItemStack(Material.ARROW);
- arrow.setAmount(64);
- ItemStack waterbucket = new ItemStack(Material.WATER_BUCKET);
- waterbucket.setAmount(1);
- ItemStack fns = new ItemStack(Material.FLINT_AND_STEEL);
- fns.setAmount(1);
- ItemStack madera = new ItemStack(Material.WOOD);
- madera.setAmount(64);
- ItemStack piedra = new ItemStack(Material.COBBLESTONE);
- piedra.setAmount(64);
- ItemStack helmet = new ItemStack(Material.IRON_HELMET);
- helmet.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 2);
- helmet.setAmount(1);
- ItemStack chestplate = new ItemStack(Material.IRON_CHESTPLATE);
- chestplate.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 2);
- chestplate.setAmount(1);
- ItemStack leggings = new ItemStack(Material.IRON_LEGGINGS);
- leggings.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 2);
- leggings.setAmount(1);
- ItemStack boots = new ItemStack(Material.IRON_BOOTS);
- boots.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 2);
- boots.setAmount(1);
- player.getInventory().setItem(0, diamondsword);
- player.getInventory().setItem(1, frod);
- player.getInventory().setItem(2, bow);
- player.getInventory().setItem(4, gapple1);
- player.getInventory().setItem(11, arrow);
- player.getInventory().setItem(8, piedra);
- player.getInventory().setItem(5, waterbucket);
- player.getInventory().setItem(6, fns);
- player.getInventory().setHelmet(helmet);
- player.getInventory().setChestplate(chestplate);
- player.getInventory().setLeggings(leggings);
- player.getInventory().setBoots(boots);
- }else {
- Map.Entry<ItemStack[], ItemStack[]> inventory = inventories.get(player.getUniqueId());
- player.getInventory().setContents(inventory.getKey());
- player.getInventory().setArmorContents(inventory.getValue());
- }
- }
- @EventHandler
- public void onRespawn(PlayerRespawnEvent e)
- {
- Player player = e.getPlayer();
- Player p = e.getPlayer();
- if (this.inventories.containsKey(e.getPlayer().getUniqueId())) {
- Map.Entry<ItemStack[], ItemStack[]> inventory = inventories.get(p.getUniqueId());
- p.getInventory().setContents(inventory.getKey());
- p.getInventory().setArmorContents(inventory.getValue());
- }else {
- ItemStack diamondsword = new ItemStack(Material.IRON_SWORD);
- diamondsword.addEnchantment(Enchantment.DAMAGE_ALL, 1);
- diamondsword.setAmount(1);
- ItemStack bow = new ItemStack(Material.BOW);
- bow.addEnchantment(Enchantment.ARROW_DAMAGE, 2);
- bow.setAmount(1);
- ItemStack frod = new ItemStack(Material.FISHING_ROD);
- frod.addEnchantment(Enchantment.DURABILITY, 3);
- frod.setAmount(1);
- ItemStack gapple1 = new ItemStack(Material.GOLDEN_APPLE);
- gapple1.setAmount(6);
- ItemStack arrow = new ItemStack(Material.ARROW);
- arrow.setAmount(64);
- ItemStack waterbucket = new ItemStack(Material.WATER_BUCKET);
- waterbucket.setAmount(1);
- ItemStack fns = new ItemStack(Material.FLINT_AND_STEEL);
- fns.setAmount(1);
- ItemStack madera = new ItemStack(Material.WOOD);
- madera.setAmount(64);
- ItemStack piedra = new ItemStack(Material.COBBLESTONE);
- piedra.setAmount(64);
- ItemStack helmet = new ItemStack(Material.IRON_HELMET);
- helmet.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 2);
- helmet.setAmount(1);
- ItemStack chestplate = new ItemStack(Material.IRON_CHESTPLATE);
- chestplate.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 2);
- chestplate.setAmount(1);
- ItemStack leggings = new ItemStack(Material.IRON_LEGGINGS);
- leggings.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 2);
- leggings.setAmount(1);
- ItemStack boots = new ItemStack(Material.IRON_BOOTS);
- boots.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 2);
- boots.setAmount(1);
- player.getInventory().setItem(0, diamondsword);
- player.getInventory().setItem(1, frod);
- player.getInventory().setItem(2, bow);
- player.getInventory().setItem(4, gapple1);
- player.getInventory().setItem(11, arrow);
- player.getInventory().setItem(8, piedra);
- player.getInventory().setItem(5, waterbucket);
- player.getInventory().setItem(6, fns);
- player.getInventory().setHelmet(helmet);
- player.getInventory().setChestplate(chestplate);
- player.getInventory().setLeggings(leggings);
- player.getInventory().setBoots(boots);
- }
- teleportToSpawn(player);
- }
- public boolean isInt(String s)
- {
- try
- {
- Integer.parseInt(s);
- return true;
- }
- catch (Exception ex) {}
- return false;
- }
- public String color(String s)
- {
- return ChatColor.translateAlternateColorCodes('&', s);
- }
- @EventHandler(priority=EventPriority.HIGHEST, ignoreCancelled=false)
- public void onJoin(PlayerJoinEvent e)
- {
- Player p = e.getPlayer();
- p.setHealth(20.0D);
- p.teleport(p.getWorld().getSpawnLocation());
- for (Player allp : Bukkit.getOnlinePlayers()) {
- allp.showPlayer(p);
- }
- }
- @EventHandler
- public void onPlace(final BlockPlaceEvent e)
- {
- if (e.getItemInHand().getType().equals(Material.WOOD)) {
- new BukkitRunnable()
- {
- public void run()
- {
- if (e.getBlock().getType().equals(Material.WOOD)){
- e.getBlock().setType(Material.AIR);
- }
- }
- }
- .runTaskLater(this, getConfig().getInt("times.wood"));
- }
- World w = Bukkit.getWorld("ffa");
- if ((e.getPlayer().getGameMode().equals(GameMode.SURVIVAL)) && (e.getPlayer().getLocation().getBlockY() > e.getPlayer().getWorld().getHighestBlockYAt(e.getPlayer().getLocation().getBlockX() + 1, e.getPlayer().getLocation().getBlockZ()) + 10))
- {
- e.setCancelled(true);
- e.getPlayer().sendMessage(ChatColor.RED + "You have reached the Max Build Height!");
- }
- }
- @EventHandler
- public void onPlace2(final BlockPlaceEvent e)
- {
- if (e.getItemInHand().getType().equals(Material.COBBLESTONE)) {
- new BukkitRunnable()
- {
- public void run()
- {
- if (e.getBlock().getType().equals(Material.COBBLESTONE)) {
- e.getBlock().setType(Material.AIR);
- Player p = e.getPlayer();
- Player player = e.getPlayer();
- ItemStack cobblestone = new ItemStack(Material.COBBLESTONE);
- player.getInventory().addItem(cobblestone);
- }
- }
- }
- .runTaskLater(this, getConfig().getInt("times.cobblestone"));
- }
- }
- @EventHandler
- public void onPlace3(final BlockPlaceEvent e)
- {
- if (e.getItemInHand().getType().equals(Material.FLINT_AND_STEEL)) {
- new BukkitRunnable()
- {
- public void run()
- {
- if (e.getBlock().getType().equals(Material.FIRE)) {
- e.getBlock().setType(Material.AIR);
- }
- }
- }
- .runTaskLater(this, getConfig().getInt("times.fire"));
- }
- }
- @EventHandler
- public void onBucketEmpty(PlayerBucketEmptyEvent e) {
- e.getPlayer().getInventory().getItemInHand().setType(Material.WATER_BUCKET);
- e.setCancelled(true);
- if (e.getPlayer().getFireTicks() > 1) {
- e.getPlayer().setFireTicks(0);
- }
- }
- public void onInteractEvent(PlayerInteractEvent e)
- {
- if ((e.getItem() != null) && (e.getPlayer().getItemInHand().getType().equals(Material.FLINT_AND_STEEL)) &&
- (e.getAction().equals(Action.RIGHT_CLICK_BLOCK)))
- {
- Player player = e.getPlayer();
- ItemStack item = player.getItemInHand();
- if (item.getDurability() == 0) {
- item.setDurability((short)62);
- }
- }
- }
- private void registerEvents()
- {
- PluginManager plm = Bukkit.getPluginManager();
- }
- public void teleportToSpawn(Player p)
- {
- double X = getConfig().getDouble("Storage.DoNotEdit.RespawnLocation.X");
- double Y = getConfig().getDouble("Storage.DoNotEdit.RespawnLocation.Y");
- double Z = getConfig().getDouble("Storage.DoNotEdit.RespawnLocation.Z");
- Float Pitch = Float.valueOf((float)getConfig().getDouble("Storage.DoNotEdit.RespawnLocation.Pitch"));
- Float Yaw = Float.valueOf((float)getConfig().getDouble("Storage.DoNotEdit.RespawnLocation.Yaw"));
- String WorldName = getConfig().getString("Storage.DoNotEdit.RespawnLocation.World");
- World World = Bukkit.getWorld(WorldName);
- Location loc = new Location(World, X, Y, Z, Pitch.floatValue(), Yaw.floatValue());
- p.teleport(loc);
- }
- public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args)
- {
- Player p = (Player)sender;
- if (cmd.getName().equalsIgnoreCase("ffasetspawn"))
- {
- if (p.hasPermission("ac.admin")) {
- Player player = (Player) sender;
- Location loc = player.getLocation();
- double X = loc.getX();
- double Y = loc.getY();
- double Z = loc.getZ();
- double Pitch = loc.getPitch();
- double Yaw = loc.getYaw();
- String World = loc.getWorld().getName();
- getConfig().set("Storage.DoNotEdit.RespawnLocation.X", Double.valueOf(X));
- getConfig().set("Storage.DoNotEdit.RespawnLocation.Y", Double.valueOf(Y));
- getConfig().set("Storage.DoNotEdit.RespawnLocation.Z", Double.valueOf(Z));
- getConfig().set("Storage.DoNotEdit.RespawnLocation.Pitch", Double.valueOf(Pitch));
- getConfig().set("Storage.DoNotEdit.RespawnLocation.Yaw", Double.valueOf(Yaw));
- getConfig().set("Storage.DoNotEdit.RespawnLocation.World", World);
- saveConfig();
- return true;
- }
- }
- else if (cmd.getName().equalsIgnoreCase("ffaspawn"))
- {
- if (p.hasPermission("ac.admin"))
- {
- p.sendMessage(org.bukkit.ChatColor.translateAlternateColorCodes('&', getConfig().getString("tpspawn")));
- p.teleport(p.getWorld().getSpawnLocation());
- }
- else
- {
- p.sendMessage(org.bukkit.ChatColor.translateAlternateColorCodes('&', getConfig().getString("cant")));
- }
- }
- else if (cmd.getName().equalsIgnoreCase("cc"))
- {
- if (p.hasPermission("ac.mod"))
- {
- int i = 0;
- while (i < 120)
- {
- Bukkit.broadcastMessage("");
- i++;
- }
- Bukkit.broadcastMessage(ChatColor.RED +"Chat was cleared by " + ChatColor.GREEN + p.getName() + "");
- }
- else
- {
- p.sendMessage(org.bukkit.ChatColor.translateAlternateColorCodes('&', getConfig().getString("cant")));
- }
- }
- else if (cmd.getName().equalsIgnoreCase("save"))
- {
- Player pl = (Player)sender;
- //this.inventories.put(p.getUniqueId(), Arrays.stream(p.getInventory().getContents()).map(ItemStack::clone).toArray(ItemStack[]::new));
- //this.armor.put(p.getUniqueId(), Arrays.stream(p.getInventory().getArmorContents()).map(ItemStack::clone).toArray(ItemStack[]::new));
- Map.Entry<ItemStack[], ItemStack[]> inventory = null;
- try {
- inventory = playerInventoryFromBase64(playerInventoryToBase64(pl.getInventory()));
- }catch (IOException ignored) {}
- if(inventory != null) {
- inventories.put(p.getUniqueId(), inventory);
- new BukkitRunnable() {
- @Override
- public void run() {
- lock.writeLock().lock();
- JsonWriter writer = null;
- try {
- writer = new JsonWriter(new FileWriter(invSaves));
- writer.setIndent(" ");
- writer.beginObject();
- for(Map.Entry<UUID, Map.Entry<ItemStack[], ItemStack[]>> entry : inventories.entrySet()) {
- writer.name(entry.getKey().toString()).beginObject().name("inventory").value(arrayToBase64(entry.getValue().getKey())).name("armour").value(arrayToBase64(entry.getValue().getValue())).endObject();
- }
- writer.endObject();
- }catch (IOException ignored) {
- }finally {
- lock.writeLock().unlock();
- if(writer != null) try {
- writer.close();
- }catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }.runTaskAsynchronously(this);
- }
- p.getPlayer().sendMessage(ChatColor.RED + "Your inventory has been saved.");
- }
- else if (cmd.getName().equalsIgnoreCase("?"))
- {
- if (p.hasPermission("ac.admin"))
- {
- }
- }
- return false;
- }
- public void onEntityDamageByEntity(EntityDamageByEntityEvent e) {
- if (((e.getDamager() instanceof Arrow)) && ((e.getEntity() instanceof Player)) && ((((Arrow) e.getDamager()).getShooter() instanceof Player)) &&
- ((e.getDamager() instanceof Arrow))) {
- Player victim = (Player) e.getEntity();
- Player killer = (Player) ((Arrow) e.getDamager()).getShooter();
- if (killer.getItemInHand().getType() == Material.BOW){
- double h = victim.getHealth() / 2.0D;
- DecimalFormat df = new DecimalFormat("#.#");
- killer.sendMessage(ChatColor.GREEN + victim.getName() + ChatColor.GRAY + " is now at " + ChatColor.GREEN + df.format(h) + ChatColor.DARK_RED + " ❤");
- }
- }
- }
- public static boolean isPlayerInSpawnR(Player player, Location loc1, Location loc2)
- {
- double[] dim = new double[2];
- dim[0] = loc1.getX();
- dim[1] = loc2.getX();
- Arrays.sort(dim);
- if ((player.getLocation().getX() > dim[1]) || (player.getLocation().getX() < dim[0])) {
- return false;
- }
- dim[0] = loc1.getZ();
- dim[1] = loc2.getZ();
- Arrays.sort(dim);
- if ((player.getLocation().getZ() > dim[1]) || (player.getLocation().getZ() < dim[0])) {
- return false;
- }
- return true;
- }
- public static String playerInventoryToBase64(PlayerInventory inventory) {
- try {
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);
- // Write the size of the inventory
- dataOutput.writeInt(inventory.getSize());
- // Save every element in the list
- for (int i = 0; i < inventory.getSize(); i++) {
- dataOutput.writeObject(inventory.getItem(i));
- }
- for(int i = 0; i < 4; i++) {
- dataOutput.writeObject(inventory.getArmorContents()[i]);
- }
- // Serialize that array
- dataOutput.close();
- return Base64Coder.encodeLines(outputStream.toByteArray());
- } catch (Exception e) {
- throw new IllegalStateException("Unable to save item stacks.", e);
- }
- }
- public static Map.Entry<ItemStack[], ItemStack[]> playerInventoryFromBase64(String data) throws IOException {
- try {
- ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data));
- BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
- List<ItemStack> inventory = new ArrayList<>();
- List<ItemStack> armour = new ArrayList<>();
- int size = dataInput.readInt();
- for(int i = 0; i < size; i++) {
- inventory.add((ItemStack) dataInput.readObject());
- }
- for(int i = 0; i < 4; i++) {
- armour.add((ItemStack) dataInput.readObject());
- }
- dataInput.close();
- return new AbstractMap.SimpleEntry<>(inventory.toArray(new ItemStack[size]), armour.toArray(new ItemStack[4]));
- } catch (Exception e) {
- throw new IOException("Unable to decode class type.", e);
- }
- }
- public static String arrayToBase64(ItemStack[] array) {
- try {
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);
- // Write the size of the inventory
- dataOutput.writeInt(array.length);
- // Save every element in the list
- for (int i = 0; i < array.length; i++) {
- dataOutput.writeObject(array[i]);
- }
- // Serialize that array
- dataOutput.close();
- return Base64Coder.encodeLines(outputStream.toByteArray());
- } catch (Exception e) {
- throw new IllegalStateException("Unable to save item stacks.", e);
- }
- }
- public static ItemStack[] arrayFromBase64(String data) throws IOException {
- try {
- ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data));
- BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
- ItemStack[] array = new ItemStack[dataInput.readInt()];
- // Read the serialized inventory
- for (int i = 0; i < array.length; i++) {
- array[i] = (ItemStack) dataInput.readObject();
- }
- dataInput.close();
- return array;
- } catch (ClassNotFoundException e) {
- throw new IOException("Unable to decode class type.", e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment