/** * checks if this block would give a reaction if you click on it without * shifting, e.g. opening a chest or switching a lever */ public static boolean isInteractiveBlock(Block b) { if (b == null || b.getState() == null) { return false; } if (b.getType() == Material.WORKBENCH || b.getType() == Material.ENCHANTMENT_TABLE || b.getType() == Material.ANVIL || b.getType() == Material.BREWING_STAND || b.getState() instanceof InventoryHolder || b.getState() instanceof NoteBlock) { return true; } if (b.getState().getData() instanceof Button || b.getState().getData() instanceof Lever || b.getState().getData() instanceof Door || b.getState().getData() instanceof TrapDoor || b.getState().getData() instanceof Gate || b.getState().getData() instanceof Comparator) { if (b.getType() != Material.IRON_DOOR && b.getType() != Material.IRON_DOOR_BLOCK && b.getType() != Material.IRON_TRAPDOOR) { return true; } } return false; }
@Override public void setInputs(CommandSender initiator, String[] args) { // final Player player = (Player) initiator; final int ticksMax = (args.length > 1) ? Integer.parseInt(args[1]) : 20; final Location base = player.getLocation().clone(); final int channels = this.trainingValues.length; for (int i = 0; i < channels; i++) { base.clone().add(i, 0, 0).getBlock().setType(Material.NOTE_BLOCK); } new BukkitRunnable() { int tick = 0; @SuppressWarnings("deprecation") @Override public void run() { tick++; int row = 0; for (Neuron n : ai.getOutputNeurons()) { if (n.isTriggered()) { ((NoteBlock) base.clone().add(row, 0, 0).getBlock() .getState()).setRawNote((byte) n.getID()); ((NoteBlock) base.clone().add(row, 0, 0).getBlock() .getState()).play(); row++; } } if (tick > ticksMax) cancel(); } }.runTaskTimer(Main.getMainClass(), 30, 9); }
@Override protected void execute(Event event) { Block block = blockExpression.getSingle(event); NoteBlock noteBlock = (NoteBlock) block.getState(); if (instrumentExpression == null) { noteBlock.play(); } else { Logging.debug(this, "Instrument: " + instrumentExpression.getSingle(event)); Logging.debug(this, "Note: " + (noteExpression == null ? noteBlock.getNote() : noteExpression.getSingle(event))); noteBlock.play(instrumentExpression.getSingle(event), (noteExpression == null ? noteBlock.getNote() : noteExpression.getSingle(event))); } }
@SuppressWarnings("deprecation") public static Optional<String> serializeState(BlockState state) { YamlConfiguration yaml = new YamlConfiguration(); // http://minecraft.gamepedia.com/Block_entity was used as a reference for this method if (state instanceof InventoryHolder) { yaml.set(INVENTORY_KEY, InventoryHelper.serializeInventory(((InventoryHolder) state).getInventory())); } if (state instanceof Sign) { yaml.set(SIGN_LINES_KEY, Arrays.asList(((Sign) state).getLines())); } else if (Support.BANNER && state instanceof Banner) { yaml.set(BANNER_BASE_COLOR_KEY, ((Banner) state).getBaseColor().name()); ConfigurationSection patternSection = yaml.createSection(BANNER_PATTERNS_KEY); List<Pattern> patterns = ((Banner) state).getPatterns(); for (int i = 0; i < patterns.size(); i++) { ConfigurationSection subSection = patternSection.createSection("" + i); subSection.set(BANNER_PATTERN_COLOR_KEY, patterns.get(i).getColor().name()); subSection.set(BANNER_PATTERN_TYPE_KEY, patterns.get(i).getPattern().name()); } } else if (state instanceof CreatureSpawner) { yaml.set(SPAWNER_TYPE_KEY, ((CreatureSpawner) state).getSpawnedType().name()); yaml.set(SPAWNER_DELAY_KEY, ((CreatureSpawner) state).getDelay()); } else if (state instanceof NoteBlock) { yaml.set(NOTE_OCTAVE_KEY, ((NoteBlock) state).getNote().getOctave()); yaml.set(NOTE_TONE_KEY, ((NoteBlock) state).getNote().getTone().name()); } else if (state instanceof Jukebox) { if (((Jukebox) state).isPlaying()) { yaml.set(JUKEBOX_DISC_KEY, ((Jukebox) state).getPlaying()); } } else if (state instanceof Skull) { yaml.set(SKULL_OWNER_KEY, ((Skull) state).getOwner()); yaml.set(SKULL_ROTATION_KEY, ((Skull) state).getRotation().name()); } else if (state instanceof CommandBlock) { yaml.set(COMMAND_NAME_KEY, ((CommandBlock) state).getName()); yaml.set(COMMAND_CMD_KEY, ((CommandBlock) state).getCommand()); } else if (state instanceof FlowerPot) { yaml.set(FLOWER_TYPE_KEY, ((FlowerPot) state).getContents().getItemType().name()); yaml.set(FLOWER_DATA_KEY, ((FlowerPot) state).getContents().getData()); } if (yaml.getKeys(false).size() > 0) { return Optional.of(yaml.saveToString()); } return Optional.absent(); }
@Override @SuppressWarnings("deprecation") public void setOldBlock(BlockState state) { super.setOldBlock(state); this.note = ((NoteBlock)state).getNote().getId(); }
@Override public void change(Block block, Note note, Changer.ChangeMode changeMode) { MundoUtil.cast(block.getState(), NoteBlock.class).ifPresent(noteBlock -> noteBlock.setNote(note)); }
@Override public Note convert(Block block) { return MundoUtil.cast(block.getState(), NoteBlock.class).map(NoteBlock::getNote).orElse(null); }
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onBlockBreak(BlockBreakEvent event) { if (event.getBlock().getType() == AIR) { return; // breaking air !? -> no logging } 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); blockState = adjustBlockForDoubleBlocks(blockState); // WOOD_DOOR IRON_DOOR OR BED_BLOCK } action.setPlayer(event.getPlayer()); action.setLocation(event.getBlock().getLocation()); action.setOldBlock(blockState); action.setNewBlock(AIR); this.logAction(action); if (blockState.getType() == OBSIDIAN) // portal? { // 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); }
/** * Constructor. * * @param blockState The {@link org.bukkit.block.BlockState} that needs to be serialized. */ public SerializableBlockEntity(BlockState blockState) { PreCon.notNull(blockState); _location = blockState.getLocation(); _material = blockState.getType(); _data = blockState.getRawData(); if (blockState instanceof InventoryHolder) { _contents = ((InventoryHolder) blockState).getInventory().getContents(); } if (blockState instanceof CommandBlock) { CommandBlock commandBlock = (CommandBlock)blockState; _commandName = commandBlock.getName(); _command = commandBlock.getCommand(); } if (blockState instanceof CreatureSpawner) { CreatureSpawner spawner = (CreatureSpawner)blockState; _creatureTypeName = spawner.getCreatureTypeName(); _creatureDelay = spawner.getDelay(); } if (blockState instanceof NoteBlock) { NoteBlock noteBlock = (NoteBlock)blockState; _noteTone = noteBlock.getNote().getTone(); _noteOctave = noteBlock.getNote().getOctave(); _noteSharped = noteBlock.getNote().isSharped(); } if (blockState instanceof Sign) { Sign sign = (Sign)blockState; _signLines = sign.getLines().clone(); } if (blockState instanceof Skull) { Skull skull = (Skull)blockState; _skullType = skull.getSkullType(); _skullRotation = skull.getRotation(); _skullOwner = skull.getOwner(); } }
private void applyTile(BlockState blockState) { boolean requiresUpdate = false; // InventoryHolder if (blockState instanceof InventoryHolder && _contents != null) { InventoryHolder holder = (InventoryHolder)blockState; Inventory inventory = holder.getInventory(); inventory.setContents(_contents); requiresUpdate = true; } // CommandBlock if (blockState instanceof CommandBlock) { CommandBlock commandBlock = (CommandBlock)blockState; if (_commandName != null) commandBlock.setName(_commandName); if (_command != null) commandBlock.setCommand(_command); requiresUpdate = true; } // CreatureSpawner if (blockState instanceof CreatureSpawner) { CreatureSpawner spawner = (CreatureSpawner)blockState; if (_creatureTypeName != null) { spawner.setCreatureTypeByName(_creatureTypeName); spawner.setDelay(_creatureDelay); } requiresUpdate = true; } if (blockState instanceof NoteBlock && _noteTone != null) { NoteBlock noteBlock = (NoteBlock)blockState; Note note = new Note(_noteOctave, _noteTone, _noteSharped); noteBlock.setNote(note); requiresUpdate = true; } if (blockState instanceof Sign && _signLines != null) { Sign sign = (Sign)blockState; for (int i=0; i < 4; i++) sign.setLine(i, _signLines[i]); requiresUpdate = true; } if (blockState instanceof Skull && _skullType != null) { Skull skull = (Skull)blockState; skull.setSkullType(_skullType); skull.setRotation(_skullRotation); skull.setOwner(_skullOwner); requiresUpdate = true; } if (requiresUpdate) { blockState.update(true); } }
public CHMIDINotePlayer(NoteBlock noteBlock) { this.noteBlock = noteBlock; }
public Construct exec(Target t, Environment environment, Construct... args) throws ConfigRuntimeException { MCLocation location = ObjectGenerator.GetGenerator().location(args[1], null, t); BlockState blockState = ((Location) location.getHandle()).getBlock().getState(); if (!(blockState instanceof NoteBlock)) { throw new ConfigRuntimeException("The block at the specified location is not a note block", ExceptionType.RangeException, t); } NoteBlock noteBlock = (NoteBlock) blockState; File midiFile = new File(t.file().getParentFile(), args[0].val()); if (!Security.CheckSecurity(midiFile.getAbsolutePath())) { throw new ConfigRuntimeException("You do not have permission to access the file '" + midiFile.getAbsolutePath() + "'", ExceptionType.SecurityException, t); } //id String id; if ((args.length == 2) || (args[2] instanceof CNull)) { id = String.valueOf(System.nanoTime()); while (CHMIDISequencerManager.sequencerExists(id)) { id = String.valueOf(System.nanoTime()); } } else { if (CHMIDISequencerManager.sequencerExists(args[2].val())) { throw new ConfigRuntimeException("A sequence with the given id ('" + args[2].val() + "') already exists.", ExceptionType.PluginInternalException, t); } else { id = args[2].val(); } } //create final CHMIDISequencer sequencer = new CHMIDISequencer(id, midiFile, new CHMIDINotePlayer(noteBlock), t); //playNow final boolean startNow; if (args.length == 4) { startNow = Static.getBoolean(args[3]); } else { startNow = true; } CHMIDISequencerManager.addSequencer(sequencer, t); queue = new RunnableQueue("MethodScript-CHMIDI-" + id); queue.invokeLater(environment.getEnv(GlobalEnv.class).GetDaemonManager(), new Runnable() { public void run() { try { sequencer.open(); if (startNow) { sequencer.start(); } while (sequencer.getMIDISequencer().isRunning() || sequencer.isPaused()) { Thread.sleep(500); } } catch (Exception exception) { } finally { CHMIDISequencerManager.deleteSequencer(sequencer, Target.UNKNOWN); } } }); return new CString(id, t); }