Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.lumengaming.farplacer;
- import java.util.HashSet;
- import java.util.List;
- import java.util.UUID;
- import org.bukkit.Bukkit;
- import org.bukkit.GameMode;
- import org.bukkit.Material;
- import org.bukkit.block.*;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandExecutor;
- import org.bukkit.command.CommandSender;
- import org.bukkit.entity.Player;
- import org.bukkit.event.Event;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.HandlerList;
- import org.bukkit.event.Listener;
- import org.bukkit.event.block.Action;
- import org.bukkit.event.block.BlockBreakEvent;
- import org.bukkit.event.block.BlockPlaceEvent;
- import org.bukkit.event.inventory.InventoryOpenEvent;
- import org.bukkit.event.player.PlayerCommandPreprocessEvent;
- import org.bukkit.event.player.PlayerInteractEvent;
- import org.bukkit.inventory.Inventory;
- import org.bukkit.inventory.InventoryHolder;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.material.*;
- import org.bukkit.plugin.java.JavaPlugin;
- /**
- *
- * @author prota
- */
- public class Main extends JavaPlugin implements Listener, CommandExecutor {
- private HashSet<UUID> farplacers = new HashSet<>();
- @Override
- public void onEnable() {
- this.getServer().getPluginManager().registerEvents(this, this);
- getCommand("farplace").setExecutor(this);
- }
- @Override
- public void onDisable() {
- HandlerList.unregisterAll((JavaPlugin) this);
- this.farplacers.clear();
- Bukkit.getScheduler().cancelTasks(this);
- }
- @Override
- public boolean onCommand(CommandSender cs, Command cmnd, String string, String[] args) {
- return doCommand(cs, args);
- }
- @EventHandler
- public void onFarpon(PlayerCommandPreprocessEvent e) {
- if (e.getMessage().equalsIgnoreCase("/farpon")) {
- doCommand(e.getPlayer(), new String[]{"on"});
- e.setCancelled(true);
- } else if (e.getMessage().equalsIgnoreCase("/farpoff")) {
- doCommand(e.getPlayer(), new String[]{"off"});
- e.setCancelled(true);
- }
- }
- private boolean doCommand(CommandSender cs, String[] args) {
- if (args.length == 1) {
- if (cs instanceof Player) {
- if (cs.hasPermission("farplacer.use") || cs.isOp()) {
- if (args[0].equalsIgnoreCase("on")) {
- this.farplacers.add(((Player) cs).getUniqueId());
- cs.sendMessage("§aFarplacing is now enabled. §7For some reason, interact events in air are only fired if you are holding an item.");
- } else if (args[0].equalsIgnoreCase("off")) {
- this.farplacers.remove(((Player) cs).getUniqueId());
- cs.sendMessage("§aFarplacing is now disabled.");
- } else {
- cs.sendMessage("§c/farp on/off");
- }
- } else {
- cs.sendMessage("§cOh no! D: You lack the 'farplacer.use' permission node. You can't use this command.");
- }
- } else {
- cs.sendMessage("§cYou have to be a player.");
- }
- } else {
- cs.sendMessage("§c/farp on/off");
- }
- return true;
- }
- @EventHandler(ignoreCancelled = false)
- public void onFarPlace(PlayerInteractEvent e) {
- Player p = e.getPlayer();
- if (!this.isFarPlacer(p.getUniqueId())) {
- return;
- }
- HashSet<Material> dummySet = null;
- if (e.getAction() == Action.RIGHT_CLICK_AIR) {
- //<editor-fold defaultstate="collapsed" desc="place block">
- List lastBlocks = null;
- try {
- lastBlocks = p.getLastTwoTargetBlocks(dummySet, 100);
- } catch (IllegalStateException ex) {
- lastBlocks = null;
- }
- if ((lastBlocks == null) || (lastBlocks.size() < 2) || (((Block) lastBlocks.get(1)).getType() == Material.AIR)) {
- p.sendMessage("§ctarget is out of range.");
- return; // no block found.
- }
- ItemStack item = p.getItemInHand();
- Block block = (Block) lastBlocks.get(0);
- Block against = (Block) lastBlocks.get(1);
- BlockState blockState = block.getState();
- PlayerInteractEvent evt = new PlayerInteractEvent(p, Action.RIGHT_CLICK_BLOCK, item, against, against.getFace(block));
- Bukkit.getServer().getPluginManager().callEvent(evt);
- if (!evt.isCancelled()) {
- //place block.
- //if interact(block, player)....
- boolean interacted = false;
- if (!p.isSneaking()) {
- interacted = Interact(p, against);
- }
- if (!interacted) {
- block.setTypeIdAndData(item.getTypeId(), (byte) item.getDurability(), true);
- BlockPlaceEvent event = new BlockPlaceEvent(block, blockState, against, p.getItemInHand(), p, true);
- Bukkit.getServer().getPluginManager().callEvent(event);
- if (event.isCancelled()) {
- blockState.update(true);
- return;
- }
- }
- }
- //</editor-fold>
- } else if (e.getAction() == Action.LEFT_CLICK_AIR) {
- //<editor-fold defaultstate="collapsed" desc="remove block">
- List lastBlocks = null;
- try {
- lastBlocks = p.getLastTwoTargetBlocks(dummySet, 80);
- } catch (IllegalStateException ex) {
- lastBlocks = null;
- }
- if ((lastBlocks == null) || (lastBlocks.size() < 2) || (((Block) lastBlocks.get(1)).getType() == Material.AIR)) {
- p.sendMessage("§ctarget is out of range.");
- return; // no block found.
- }
- Block block = (Block) lastBlocks.get(1);
- if (block != null) {
- PlayerInteractEvent evt = new PlayerInteractEvent(p, Action.LEFT_CLICK_BLOCK, p.getItemInHand(), block, BlockFace.SELF);
- Bukkit.getServer().getPluginManager().callEvent(evt);
- if (!evt.isCancelled()) {
- BlockBreakEvent event = new BlockBreakEvent(block, p);
- Bukkit.getServer().getPluginManager().callEvent(event);
- if (!event.isCancelled()) {
- if (p.getGameMode() == GameMode.CREATIVE){
- block.setType(Material.AIR);
- }else{
- block.breakNaturally();
- }
- }
- }
- }
- //</editor-fold>
- }
- }
- //<editor-fold defaultstate="collapsed" desc="Interact?">
- /**
- * Check if the material is a block and is interactive
- *
- * @param p
- * @param b
- * @return True if this material is a block and it was interacted with.
- *
- */
- public boolean Interact(Player p, Block b) {
- if (b == null) {
- return false;
- }
- Material m = b.getType();
- Inventory inv;
- switch (m) {
- case LEVER:
- Lever lever = (Lever) b.getState();
- lever.setPowered(lever.isPowered());
- return true;
- //case REDSTONE_ORE:
- case WOOD_BUTTON:
- case STONE_BUTTON:
- Button stoneButton = (Button) b.getState();
- stoneButton.setPowered(true);
- return true;
- case FENCE_GATE:
- case SPRUCE_FENCE_GATE:
- case BIRCH_FENCE_GATE:
- case JUNGLE_FENCE_GATE:
- case DARK_OAK_FENCE_GATE:
- case ACACIA_FENCE_GATE:
- case TRAP_DOOR:
- case WOOD_DOOR:
- case WOODEN_DOOR:
- case SPRUCE_DOOR:
- case BIRCH_DOOR:
- case JUNGLE_DOOR:
- case ACACIA_DOOR:
- case DARK_OAK_DOOR:
- if (b.getState() instanceof Openable){
- Openable td = (Openable) b.getState();
- td.setOpen(!td.isOpen());
- return true;
- }else{
- return false;
- }
- case DRAGON_EGG:
- //fuck dragon eggs.
- b.setType(Material.AIR);
- return true;
- // case HOPPER:
- // Hopper hopper = (Hopper) b.getState();
- // inv = hopper.getInventory();
- // p.openInventory(inv);
- // return false;
- // case CAULDRON:
- case WORKBENCH:
- p.openWorkbench(b.getLocation(), false);
- return true;
- case ENCHANTMENT_TABLE:
- p.openEnchanting(b.getLocation(), false);
- return true;
- case ENDER_CHEST:
- inv = p.getEnderChest();
- p.openInventory(inv);
- return true;
- case BEACON:
- case BREWING_STAND:
- case BURNING_FURNACE:
- case FURNACE:
- case TRAPPED_CHEST:
- case DISPENSER:
- case CHEST:
- case WHITE_SHULKER_BOX:
- case ORANGE_SHULKER_BOX:
- case MAGENTA_SHULKER_BOX:
- case LIGHT_BLUE_SHULKER_BOX:
- case YELLOW_SHULKER_BOX:
- case LIME_SHULKER_BOX:
- case PINK_SHULKER_BOX:
- case GRAY_SHULKER_BOX:
- case SILVER_SHULKER_BOX:
- case CYAN_SHULKER_BOX:
- case PURPLE_SHULKER_BOX:
- case BLUE_SHULKER_BOX:
- case BROWN_SHULKER_BOX:
- case GREEN_SHULKER_BOX:
- case RED_SHULKER_BOX:
- case BLACK_SHULKER_BOX:
- case DROPPER:
- case HOPPER:
- if (b.getState() instanceof InventoryHolder){
- InventoryHolder invHolder = (InventoryHolder) b.getState();
- inv = invHolder.getInventory();
- p.openInventory(inv);
- return true;
- }else{
- return false;
- }
- case JUKEBOX:
- case CAULDRON:
- case COMMAND:
- case DIODE:
- case DIODE_BLOCK_OFF:
- case DIODE_BLOCK_ON:
- case REDSTONE_COMPARATOR_OFF:
- case REDSTONE_COMPARATOR_ON:
- case DAYLIGHT_DETECTOR:
- case OBSERVER:
- case COMMAND_REPEATING:
- case COMMAND_CHAIN:
- case STRUCTURE_VOID:
- case REDSTONE_COMPARATOR:
- case EXPLOSIVE_MINECART:
- case HOPPER_MINECART:
- case COMMAND_MINECART:
- case END_CRYSTAL:
- case KNOWLEDGE_BOOK:
- case STORAGE_MINECART:
- case POWERED_MINECART:
- //<editor-fold defaultstate="collapsed" desc="No effect">
- case AIR:
- case STONE:
- case GRASS:
- case DIRT:
- case COBBLESTONE:
- case WOOD:
- case SAPLING:
- case BEDROCK:
- case WATER:
- case STATIONARY_WATER:
- case LAVA:
- case STATIONARY_LAVA:
- case SAND:
- case GRAVEL:
- case GOLD_ORE:
- case IRON_ORE:
- case COAL_ORE:
- case LOG:
- case LEAVES:
- case SPONGE:
- case GLASS:
- case LAPIS_ORE:
- case LAPIS_BLOCK:
- case SANDSTONE:
- case NOTE_BLOCK:
- case BED_BLOCK:
- case POWERED_RAIL:
- case DETECTOR_RAIL:
- case PISTON_STICKY_BASE:
- case WEB:
- case LONG_GRASS:
- case DEAD_BUSH:
- case PISTON_BASE:
- case PISTON_EXTENSION:
- case WOOL:
- case PISTON_MOVING_PIECE:
- case YELLOW_FLOWER:
- case RED_ROSE:
- case BROWN_MUSHROOM:
- case RED_MUSHROOM:
- case GOLD_BLOCK:
- case IRON_BLOCK:
- case DOUBLE_STEP:
- case STEP:
- case BRICK:
- case TNT:
- case BOOKSHELF:
- case MOSSY_COBBLESTONE:
- case OBSIDIAN:
- case TORCH:
- case FIRE:
- case MOB_SPAWNER:
- case WOOD_STAIRS:
- case REDSTONE_WIRE:
- case DIAMOND_ORE:
- case DIAMOND_BLOCK:
- case CROPS:
- case SOIL:
- case SIGN_POST:
- case LADDER:
- case RAILS:
- case COBBLESTONE_STAIRS:
- case WALL_SIGN:
- case STONE_PLATE:
- case IRON_DOOR_BLOCK:
- case WOOD_PLATE:
- case REDSTONE_ORE:
- case GLOWING_REDSTONE_ORE:
- case REDSTONE_TORCH_OFF:
- case REDSTONE_TORCH_ON:
- case SNOW:
- case ICE:
- case SNOW_BLOCK:
- case CACTUS:
- case CLAY:
- case SUGAR_CANE_BLOCK:
- case FENCE:
- case PUMPKIN:
- case NETHERRACK:
- case SOUL_SAND:
- case GLOWSTONE:
- case PORTAL:
- case JACK_O_LANTERN:
- case CAKE_BLOCK:
- case STAINED_GLASS:
- case MONSTER_EGGS:
- case SMOOTH_BRICK:
- case HUGE_MUSHROOM_1:
- case HUGE_MUSHROOM_2:
- case IRON_FENCE:
- case THIN_GLASS:
- case MELON_BLOCK:
- case PUMPKIN_STEM:
- case MELON_STEM:
- case VINE:
- case BRICK_STAIRS:
- case SMOOTH_STAIRS:
- case MYCEL:
- case WATER_LILY:
- case NETHER_BRICK:
- case NETHER_FENCE:
- case NETHER_BRICK_STAIRS:
- case NETHER_WARTS:
- case ENDER_PORTAL:
- case ENDER_PORTAL_FRAME:
- case ENDER_STONE:
- case REDSTONE_LAMP_OFF:
- case REDSTONE_LAMP_ON:
- case WOOD_DOUBLE_STEP:
- case WOOD_STEP:
- case COCOA:
- case SANDSTONE_STAIRS:
- case EMERALD_ORE:
- case TRIPWIRE_HOOK:
- case TRIPWIRE:
- case EMERALD_BLOCK:
- case SPRUCE_WOOD_STAIRS:
- case BIRCH_WOOD_STAIRS:
- case JUNGLE_WOOD_STAIRS:
- case COBBLE_WALL:
- case FLOWER_POT:
- case CARROT:
- case POTATO:
- case SKULL:
- case ANVIL:
- case GOLD_PLATE:
- case IRON_PLATE:
- case REDSTONE_BLOCK:
- case QUARTZ_ORE:
- case QUARTZ_BLOCK:
- case QUARTZ_STAIRS:
- case ACTIVATOR_RAIL:
- case STAINED_CLAY:
- case STAINED_GLASS_PANE:
- case LEAVES_2:
- case LOG_2:
- case ACACIA_STAIRS:
- case DARK_OAK_STAIRS:
- case SLIME_BLOCK:
- case BARRIER:
- case PRISMARINE:
- case SEA_LANTERN:
- case HAY_BLOCK:
- case CARPET:
- case HARD_CLAY:
- case COAL_BLOCK:
- case PACKED_ICE:
- case DOUBLE_PLANT:
- case STANDING_BANNER:
- case WALL_BANNER:
- case DAYLIGHT_DETECTOR_INVERTED:
- case RED_SANDSTONE:
- case RED_SANDSTONE_STAIRS:
- case DOUBLE_STONE_SLAB2:
- case STONE_SLAB2:
- case SPRUCE_FENCE:
- case BIRCH_FENCE:
- case JUNGLE_FENCE:
- case DARK_OAK_FENCE:
- case ACACIA_FENCE:
- case END_ROD:
- case CHORUS_PLANT:
- case CHORUS_FLOWER:
- case PURPUR_BLOCK:
- case PURPUR_PILLAR:
- case PURPUR_STAIRS:
- case PURPUR_DOUBLE_SLAB:
- case PURPUR_SLAB:
- case END_BRICKS:
- case BEETROOT_BLOCK:
- case GRASS_PATH:
- case END_GATEWAY:
- case FROSTED_ICE:
- case MAGMA:
- case NETHER_WART_BLOCK:
- case RED_NETHER_BRICK:
- case BONE_BLOCK:
- case WHITE_GLAZED_TERRACOTTA:
- case ORANGE_GLAZED_TERRACOTTA:
- case MAGENTA_GLAZED_TERRACOTTA:
- case LIGHT_BLUE_GLAZED_TERRACOTTA:
- case YELLOW_GLAZED_TERRACOTTA:
- case LIME_GLAZED_TERRACOTTA:
- case PINK_GLAZED_TERRACOTTA:
- case GRAY_GLAZED_TERRACOTTA:
- case SILVER_GLAZED_TERRACOTTA:
- case CYAN_GLAZED_TERRACOTTA:
- case PURPLE_GLAZED_TERRACOTTA:
- case BLUE_GLAZED_TERRACOTTA:
- case BROWN_GLAZED_TERRACOTTA:
- case GREEN_GLAZED_TERRACOTTA:
- case RED_GLAZED_TERRACOTTA:
- case BLACK_GLAZED_TERRACOTTA:
- case CONCRETE:
- case CONCRETE_POWDER:
- case STRUCTURE_BLOCK:
- case IRON_SPADE:
- case IRON_PICKAXE:
- case IRON_AXE:
- case FLINT_AND_STEEL:
- case APPLE:
- case BOW:
- case ARROW:
- case COAL:
- case DIAMOND:
- case IRON_INGOT:
- case GOLD_INGOT:
- case IRON_SWORD:
- case WOOD_SWORD:
- case WOOD_SPADE:
- case WOOD_PICKAXE:
- case WOOD_AXE:
- case STONE_SWORD:
- case IRON_TRAPDOOR:
- case IRON_DOOR:
- case STONE_SPADE:
- case STONE_PICKAXE:
- case STONE_AXE:
- case DIAMOND_SWORD:
- case DIAMOND_SPADE:
- case DIAMOND_PICKAXE:
- case DIAMOND_AXE:
- case STICK:
- case BOWL:
- case MUSHROOM_SOUP:
- case GOLD_SWORD:
- case GOLD_SPADE:
- case GOLD_PICKAXE:
- case GOLD_AXE:
- case STRING:
- case FEATHER:
- case SULPHUR:
- case WOOD_HOE:
- case STONE_HOE:
- case IRON_HOE:
- case DIAMOND_HOE:
- case GOLD_HOE:
- case SEEDS:
- case WHEAT:
- case BREAD:
- case LEATHER_HELMET:
- case LEATHER_CHESTPLATE:
- case LEATHER_LEGGINGS:
- case LEATHER_BOOTS:
- case CHAINMAIL_HELMET:
- case CHAINMAIL_CHESTPLATE:
- case CHAINMAIL_LEGGINGS:
- case CHAINMAIL_BOOTS:
- case IRON_HELMET:
- case IRON_CHESTPLATE:
- case IRON_LEGGINGS:
- case IRON_BOOTS:
- case DIAMOND_HELMET:
- case DIAMOND_CHESTPLATE:
- case DIAMOND_LEGGINGS:
- case DIAMOND_BOOTS:
- case GOLD_HELMET:
- case GOLD_CHESTPLATE:
- case GOLD_LEGGINGS:
- case GOLD_BOOTS:
- case FLINT:
- case PORK:
- case GRILLED_PORK:
- case PAINTING:
- case GOLDEN_APPLE:
- case SIGN:
- case BUCKET:
- case WATER_BUCKET:
- case LAVA_BUCKET:
- case MINECART:
- case SADDLE:
- case REDSTONE:
- case SNOW_BALL:
- case BOAT:
- case LEATHER:
- case MILK_BUCKET:
- case CLAY_BRICK:
- case CLAY_BALL:
- case SUGAR_CANE:
- case PAPER:
- case BOOK:
- case SLIME_BALL:
- case EGG:
- case COMPASS:
- case FISHING_ROD:
- case WATCH:
- case GLOWSTONE_DUST:
- case RAW_FISH:
- case COOKED_FISH:
- case INK_SACK:
- case BONE:
- case SUGAR:
- case CAKE:
- case BED:
- case COOKIE:
- case MAP:
- case SHEARS:
- case MELON:
- case PUMPKIN_SEEDS:
- case MELON_SEEDS:
- case RAW_BEEF:
- case COOKED_BEEF:
- case RAW_CHICKEN:
- case COOKED_CHICKEN:
- case ROTTEN_FLESH:
- case ENDER_PEARL:
- case BLAZE_ROD:
- case GHAST_TEAR:
- case GOLD_NUGGET:
- case NETHER_STALK:
- case POTION:
- case GLASS_BOTTLE:
- case SPIDER_EYE:
- case FERMENTED_SPIDER_EYE:
- case BLAZE_POWDER:
- case MAGMA_CREAM:
- case BREWING_STAND_ITEM:
- case CAULDRON_ITEM:
- case EYE_OF_ENDER:
- case SPECKLED_MELON:
- case MONSTER_EGG:
- case EXP_BOTTLE:
- case FIREBALL:
- case BOOK_AND_QUILL:
- case WRITTEN_BOOK:
- case EMERALD:
- case ITEM_FRAME:
- case FLOWER_POT_ITEM:
- case CARROT_ITEM:
- case POTATO_ITEM:
- case BAKED_POTATO:
- case POISONOUS_POTATO:
- case EMPTY_MAP:
- case GOLDEN_CARROT:
- case SKULL_ITEM:
- case CARROT_STICK:
- case NETHER_STAR:
- case PUMPKIN_PIE:
- case FIREWORK:
- case FIREWORK_CHARGE:
- case ENCHANTED_BOOK:
- case NETHER_BRICK_ITEM:
- case QUARTZ:
- case PRISMARINE_SHARD:
- case PRISMARINE_CRYSTALS:
- case RABBIT:
- case COOKED_RABBIT:
- case RABBIT_STEW:
- case RABBIT_FOOT:
- case RABBIT_HIDE:
- case ARMOR_STAND:
- case IRON_BARDING:
- case GOLD_BARDING:
- case DIAMOND_BARDING:
- case LEASH:
- case NAME_TAG:
- case MUTTON:
- case COOKED_MUTTON:
- case BANNER:
- case SPRUCE_DOOR_ITEM:
- case BIRCH_DOOR_ITEM:
- case JUNGLE_DOOR_ITEM:
- case ACACIA_DOOR_ITEM:
- case DARK_OAK_DOOR_ITEM:
- case CHORUS_FRUIT:
- case CHORUS_FRUIT_POPPED:
- case BEETROOT:
- case BEETROOT_SEEDS:
- case BEETROOT_SOUP:
- case DRAGONS_BREATH:
- case SPLASH_POTION:
- case SPECTRAL_ARROW:
- case TIPPED_ARROW:
- case LINGERING_POTION:
- case SHIELD:
- case ELYTRA:
- case BOAT_SPRUCE:
- case BOAT_BIRCH:
- case BOAT_JUNGLE:
- case BOAT_ACACIA:
- case BOAT_DARK_OAK:
- case TOTEM:
- case SHULKER_SHELL:
- case IRON_NUGGET:
- case GOLD_RECORD:
- case GREEN_RECORD:
- case RECORD_3:
- case RECORD_4:
- case RECORD_5:
- case RECORD_6:
- case RECORD_7:
- case RECORD_8:
- case RECORD_9:
- case RECORD_10:
- case RECORD_11:
- case RECORD_12:
- default:
- return false;
- //</editor-fold>
- }
- }
- //</editor-fold>
- private boolean isFarPlacer(UUID uniqueId) {
- return this.farplacers.contains(uniqueId);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment