Search in sources :

Example 16 with Jukebox

use of org.bukkit.block.Jukebox in project MagicPlugin by elBukkit.

the class CompatibilityUtils method clearItems.

@Override
public void clearItems(Location location) {
    if (location == null)
        return;
    // Block-specific behaviors
    Block block = location.getBlock();
    BlockState blockState = block.getState();
    if (blockState instanceof Lootable) {
        Lootable lootable = (Lootable) blockState;
        lootable.setLootTable(null);
        blockState.update();
    }
    if (blockState instanceof Lectern) {
        Lectern lectern = (Lectern) blockState;
        lectern.getInventory().setItem(0, new ItemStack(Material.AIR));
        blockState.update();
    }
    if (blockState instanceof Jukebox) {
        ((Jukebox) blockState).setRecord(null);
        blockState.update();
    }
    // TODO: Just clear inventory instead?
    BlockEntity tileEntity = getTileEntity(location);
    if (tileEntity == null)
        return;
    CompoundTag tag = new CompoundTag();
    tileEntity.save(tag);
    // Is there really not an enum for these NBT types?
    ListTag itemList = tag.getList("Items", CompatibilityConstants.NBT_TYPE_COMPOUND);
    // Is it really necessary to clear the list before removing it?
    if (itemList != null) {
        itemList.clear();
        tag.remove("Items");
        tileEntity.load(tag);
        tileEntity.setChanged();
    }
}
Also used : Jukebox(org.bukkit.block.Jukebox) BlockState(org.bukkit.block.BlockState) Lootable(org.bukkit.loot.Lootable) FallingBlock(org.bukkit.entity.FallingBlock) CraftBlock(org.bukkit.craftbukkit.v1_17_R1.block.CraftBlock) Block(org.bukkit.block.Block) Lectern(org.bukkit.block.Lectern) ItemStack(org.bukkit.inventory.ItemStack) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) FallingBlockEntity(net.minecraft.world.entity.item.FallingBlockEntity) SignBlockEntity(net.minecraft.world.level.block.entity.SignBlockEntity)

Example 17 with Jukebox

use of org.bukkit.block.Jukebox in project MagicPlugin by elBukkit.

the class CompatibilityUtils method clearItems.

@Override
public void clearItems(Location location) {
    if (location == null)
        return;
    // Block-specific behaviors
    Block block = location.getBlock();
    BlockState blockState = block.getState();
    if (blockState instanceof Lootable) {
        Lootable lootable = (Lootable) blockState;
        lootable.setLootTable(null);
        blockState.update();
    }
    if (blockState instanceof Lectern) {
        Lectern lectern = (Lectern) blockState;
        lectern.getInventory().setItem(0, new ItemStack(Material.AIR));
        blockState.update();
    }
    if (blockState instanceof Jukebox) {
        ((Jukebox) blockState).setRecord(null);
        blockState.update();
    }
    // TODO: Just clear inventory instead?
    BlockEntity tileEntity = getTileEntity(location);
    if (tileEntity == null)
        return;
    CompoundTag tag = tileEntity.saveWithFullMetadata();
    // Is there really not an enum for these NBT types?
    ListTag itemList = tag.getList("Items", CompatibilityConstants.NBT_TYPE_COMPOUND);
    // Is it really necessary to clear the list before removing it?
    if (itemList != null) {
        itemList.clear();
        tag.remove("Items");
        tileEntity.load(tag);
        tileEntity.setChanged();
    }
}
Also used : Jukebox(org.bukkit.block.Jukebox) BlockState(org.bukkit.block.BlockState) Lootable(org.bukkit.loot.Lootable) FallingBlock(org.bukkit.entity.FallingBlock) CraftBlock(org.bukkit.craftbukkit.v1_18_R1.block.CraftBlock) Block(org.bukkit.block.Block) Lectern(org.bukkit.block.Lectern) ItemStack(org.bukkit.inventory.ItemStack) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) FallingBlockEntity(net.minecraft.world.entity.item.FallingBlockEntity) SignBlockEntity(net.minecraft.world.level.block.entity.SignBlockEntity)

Example 18 with Jukebox

use of org.bukkit.block.Jukebox in project modules-extra by CubeEngine.

the class ListenerPlayerBlock method onBlockBreak.

// Doors / Beds only logged bottom / feet
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockBreak(BlockBreakEvent event) {
    if (event.getBlock().getType() == AIR) {
        // breaking air !? -> no logging
        return;
    }
    if (!this.isActive(PlayerBlockBreak.class, event.getBlock().getWorld())) {
        return;
    }
    BlockState blockState = event.getBlock().getState();
    PlayerBlockBreak action;
    if (blockState instanceof NoteBlock) {
        action = this.newAction(PlayerNoteBlockBreak.class);
        ((PlayerNoteBlockBreak) action).setNote(((NoteBlock) blockState).getNote());
    } else if (blockState instanceof Sign) {
        action = this.newAction(PlayerSignBreak.class);
        ((PlayerSignBreak) action).setLines(((Sign) blockState).getLines());
    } else if (blockState instanceof Jukebox && ((Jukebox) blockState).getPlaying() != null) {
        action = this.newAction(PlayerJukeboxBreak.class);
        ((PlayerJukeboxBreak) action).setDisc(((Jukebox) blockState).getPlaying());
    } else if (blockState instanceof InventoryHolder) {
        action = this.newAction(PlayerContainerBreak.class, event.getBlock().getWorld());
        if (action == null) {
            action = this.newAction(PlayerBlockBreak.class);
        } else {
            ((PlayerContainerBreak) action).setContents(((InventoryHolder) blockState).getInventory().getContents());
        }
    // TODO item drops
    // itemDrop.logDropsFromChest((InventoryHolder)blockState,location,event.getPlayer());
    } else {
        action = this.newAction(PlayerBlockBreak.class);
        // WOOD_DOOR IRON_DOOR OR BED_BLOCK
        blockState = adjustBlockForDoubleBlocks(blockState);
    }
    action.setPlayer(event.getPlayer());
    action.setLocation(event.getBlock().getLocation());
    action.setOldBlock(blockState);
    action.setNewBlock(AIR);
    this.logAction(action);
    if (// portal?
    blockState.getType() == OBSIDIAN) {
        // TODO better & complete
        Block block = blockState.getBlock();
        for (BlockFace face : BLOCK_FACES) {
            if (block.getRelative(face).getType() == PORTAL) {
                Block portal = block.getRelative(face);
                PlayerBlockBreak pAction = this.newAction(PlayerBlockBreak.class);
                pAction.setPlayer(event.getPlayer());
                pAction.setLocation(portal.getLocation());
                pAction.setOldBlock(portal.getState());
                pAction.setNewBlock(AIR);
                pAction.reference = this.reference(action);
                this.logAction(pAction);
                break;
            }
        }
    }
    // TODO attached & falling
    ListenerBlock.logAttachedBlocks(this, module.getCore().getEventManager(), event.getBlock(), action);
    ListenerBlock.logFallingBlocks(this, module.getCore().getEventManager(), event.getBlock(), action);
}
Also used : PlayerBlockBreak(org.cubeengine.module.log.action.block.player.destroy.PlayerBlockBreak) Jukebox(org.bukkit.block.Jukebox) BlockState(org.bukkit.block.BlockState) PlayerJukeboxBreak(org.cubeengine.module.log.action.block.player.destroy.PlayerJukeboxBreak) NoteBlock(org.bukkit.block.NoteBlock) BlockFace(org.bukkit.block.BlockFace) ListenerBlock(org.cubeengine.module.log.action.block.ListenerBlock) Block(org.bukkit.block.Block) NoteBlock(org.bukkit.block.NoteBlock) Sign(org.bukkit.block.Sign) InventoryHolder(org.bukkit.inventory.InventoryHolder) PlayerNoteBlockBreak(org.cubeengine.module.log.action.block.player.destroy.PlayerNoteBlockBreak) EventHandler(org.bukkit.event.EventHandler)

Example 19 with Jukebox

use of org.bukkit.block.Jukebox in project modules-extra by CubeEngine.

the class ListenerPlayerBlockInteract method onPlayerInteract.

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerInteract(PlayerInteractEvent event) {
    // TODO put item into itemframe
    if (event.getAction() == RIGHT_CLICK_BLOCK) {
        ItemStack itemInHand = event.getPlayer().getItemInHand();
        Location location = event.getClickedBlock().getLocation();
        BlockState state = event.getClickedBlock().getState();
        ActionPlayerBlock action;
        BlockState newState = state.getBlock().getState();
        if (state instanceof InventoryHolder) {
            action = this.newAction(UseContainer.class, state.getWorld());
        } else if (state.getData() instanceof Openable) {
            action = this.newAction(UseDoor.class, state.getWorld());
            if (action != null) {
                state = adjustBlockForDoubleBlocks(state);
                Openable openable = (Openable) state.getBlock().getState().getData();
                openable.setOpen(!openable.isOpen());
                newState.setData((MaterialData) openable);
            }
        } else if (state.getData() instanceof Lever) {
            action = this.newAction(UseLever.class, state.getWorld());
            if (action != null) {
                Lever leverData = (Lever) state.getBlock().getState().getData();
                leverData.setPowered(!leverData.isPowered());
                newState.setData(leverData);
            }
        } else if (state.getType() == REDSTONE_COMPARATOR_ON || state.getType() == REDSTONE_COMPARATOR_OFF) {
            action = this.newAction(UseComparator.class, state.getWorld());
            if (action != null) {
                newState.setType(state.getType() == REDSTONE_COMPARATOR_ON ? REDSTONE_COMPARATOR_OFF : REDSTONE_COMPARATOR_ON);
            }
        } else if (state.getData() instanceof Button) {
            action = this.newAction(UseButton.class, state.getWorld());
            if (action != null) {
                Button button = (Button) state.getBlock().getState().getData();
                button.setPowered(true);
                newState.setData(button);
            }
        } else if (state.getData() instanceof Rails) {
            if (itemInHand.getType() == MINECART || itemInHand.getType() == STORAGE_MINECART || itemInHand.getType() == POWERED_MINECART || itemInHand.getType() == HOPPER_MINECART || // BOAT is done down below
            itemInHand.getType() == EXPLOSIVE_MINECART) {
                VehiclePrePlaceEvent vEvent = new VehiclePrePlaceEvent(event.getClickedBlock().getRelative(UP).getLocation(), event.getPlayer());
                this.module.getCore().getEventManager().fireEvent(vEvent);
            }
            action = null;
        } else if (itemInHand.getType().equals(BOAT)) {
            VehiclePrePlaceEvent vEvent = new VehiclePrePlaceEvent(event.getClickedBlock().getRelative(UP).getLocation(), event.getPlayer());
            this.module.getCore().getEventManager().fireEvent(vEvent);
            action = null;
        } else if (state.getData() instanceof Cake) {
            action = null;
            if (event.getPlayer().getFoodLevel() < 20 && !(event.getPlayer().getGameMode() == CREATIVE)) {
                action = this.newAction(UseCake.class, state.getWorld());
                if (action != null) {
                    Cake cake = (Cake) state.getBlock().getState().getData();
                    cake.setSlicesEaten(cake.getSlicesEaten() + 1);
                    if (cake.getSlicesRemaining() == 0) {
                        action.setNewBlock(AIR);
                        newState = null;
                    } else {
                        newState.setData(cake);
                    }
                }
            }
        } else if (state instanceof NoteBlock) {
            action = this.newAction(UseNoteblock.class, state.getWorld());
            if (action != null) {
                try {
                    ((NoteBlock) newState).setNote(((NoteBlock) newState).getNote().sharped());
                } catch (IllegalArgumentException e) {
                    ((NoteBlock) newState).setNote(new Note(1, Tone.F, true));
                }
            }
        } else if (state.getData() instanceof Diode) {
            action = this.newAction(UseRepeater.class, state.getWorld());
            if (action != null) {
                Diode diode = (Diode) state.getBlock().getState().getData();
                Integer delay = diode.getDelay() + 1;
                if (delay == 5) {
                    delay = 1;
                }
                diode.setDelay(delay);
                newState.setData(diode);
            }
        } else if (state.getType() == TNT) {
            action = null;
            if (itemInHand.getType() == FLINT_AND_STEEL) {
                action = this.newAction(UseTnt.class, state.getWorld());
                if (action != null) {
                    action.setNewBlock(AIR);
                    newState = null;
                }
            }
        } else if (state.getData() instanceof Crops || state.getType() == GRASS) {
            if (itemInHand.getData() instanceof Dye && ((Dye) itemInHand.getData()).getColor() == WHITE) {
                // TODO THIS IS BULLSHIT I need an event for bonemealUse...
                action = this.newAction(UseBonemeal.class, state.getWorld());
                if (action != null) {
                    // TODO adjust growth stage
                    // TODO do not log if fully grown
                    action.setNewBlock(state);
                    action.setLocation(state.getLocation());
                    action.setOldBlock(state);
                    action.setPlayer(event.getPlayer());
                    this.logAction(action);
                }
            }
            return;
        } else if (state instanceof Jukebox) {
            // TODO
            action = null;
        } else {
            action = null;
        }
        if (action != null) {
            action.setLocation(state.getLocation());
            action.setOldBlock(state);
            if (newState != null) {
                action.setNewBlock(newState);
            }
            action.setPlayer(event.getPlayer());
            this.logAction(action);
        }
    } else if (event.getAction() == PHYSICAL) {
        Block block = event.getClickedBlock();
        if (block.getType() == SOIL) {
            BlockTrample action = this.newAction(BlockTrample.class, block.getWorld());
            if (action != null) {
                BlockState stateUp = block.getRelative(UP).getState();
                if (stateUp.getData() instanceof Crops) {
                    BlockTrample actionUp = this.newAction(BlockTrample.class, block.getWorld());
                    actionUp.setLocation(stateUp.getLocation());
                    actionUp.setOldBlock(stateUp);
                    actionUp.setNewBlock(AIR);
                    actionUp.setPlayer(event.getPlayer());
                    this.logAction(actionUp);
                }
                action.setLocation(block.getLocation());
                action.setOldBlock(block.getState());
                action.setNewBlock(DIRT);
                action.setPlayer(event.getPlayer());
                this.logAction(action);
            }
        } else if (block.getState().getData() instanceof PressurePlate) {
            UsePlate action = this.newAction(UsePlate.class, block.getWorld());
            if (action != null) {
                // BlockState newState = block.getState();
                // PressurePlate plate = (PressurePlate)newState.getData();
                action.setLocation(block.getLocation());
                action.setOldBlock(block.getState());
                action.setNewBlock(block.getState());
                action.setPlayer(event.getPlayer());
                this.logAction(action);
            }
        }
    }
}
Also used : Diode(org.bukkit.material.Diode) Cake(org.bukkit.material.Cake) Openable(org.bukkit.material.Openable) Dye(org.bukkit.material.Dye) Button(org.bukkit.material.Button) PressurePlate(org.bukkit.material.PressurePlate) InventoryHolder(org.bukkit.inventory.InventoryHolder) Jukebox(org.bukkit.block.Jukebox) ActionPlayerBlock(org.cubeengine.module.log.action.block.player.ActionPlayerBlock) NoteBlock(org.bukkit.block.NoteBlock) Crops(org.bukkit.material.Crops) Rails(org.bukkit.material.Rails) BlockState(org.bukkit.block.BlockState) Lever(org.bukkit.material.Lever) Note(org.bukkit.Note) VehiclePrePlaceEvent(org.cubeengine.module.log.action.vehicle.VehiclePrePlaceEvent) Block(org.bukkit.block.Block) NoteBlock(org.bukkit.block.NoteBlock) ActionPlayerBlock(org.cubeengine.module.log.action.block.player.ActionPlayerBlock) MaterialData(org.bukkit.material.MaterialData) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Location(org.spongepowered.api.world.Location) EventHandler(org.bukkit.event.EventHandler)

Example 20 with Jukebox

use of org.bukkit.block.Jukebox in project FN-FAL-s-Amplifications by FN-FAL113.

the class JukeBox method previousDiscButton.

@Override
public void previousDiscButton(@Nullable Player player, BlockMenu menu, boolean goBackToDefaultSlot) {
    final Location location = menu.getLocation();
    final JukeboxCache cache = getCACHE_MAP().get(location);
    Jukebox jukebox = (Jukebox) menu.getBlock().getState();
    if (cache.isOn) {
        // is jukebox toggle on
        if (isSlotNotNull(menu, cache, -1)) {
            // if previous slot has a disc then play that
            playCurrentSlot(menu, cache, jukebox, location, -1);
        } else {
            // decrement the current slot by 1
            isOutOfBounds(menu, jukebox, cache, location, -1);
        }
    } else if (player != null) {
        player.sendMessage(Utils.colorTranslator("&eJukebox is turned off, please turn it on"));
    }
}
Also used : Jukebox(org.bukkit.block.Jukebox) Location(org.bukkit.Location)

Aggregations

Jukebox (org.bukkit.block.Jukebox)22 ItemStack (org.bukkit.inventory.ItemStack)13 Block (org.bukkit.block.Block)12 BlockState (org.bukkit.block.BlockState)10 InventoryHolder (org.bukkit.inventory.InventoryHolder)8 Location (org.bukkit.Location)6 CompoundTag (net.minecraft.nbt.CompoundTag)5 ListTag (net.minecraft.nbt.ListTag)5 FallingBlockEntity (net.minecraft.world.entity.item.FallingBlockEntity)5 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)5 SignBlockEntity (net.minecraft.world.level.block.entity.SignBlockEntity)5 Lectern (org.bukkit.block.Lectern)5 FallingBlock (org.bukkit.entity.FallingBlock)5 Lootable (org.bukkit.loot.Lootable)5 Sign (org.bukkit.block.Sign)4 Material (org.bukkit.Material)3 NoteBlock (org.bukkit.block.NoteBlock)3 EventHandler (org.bukkit.event.EventHandler)3 Banner (org.bukkit.block.Banner)2 BlockFace (org.bukkit.block.BlockFace)2