diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/CommandProcessor.java b/src/main/java/com/github/hhhzzzsss/songplayer/CommandProcessor.java index 92a9eef..122cda6 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/CommandProcessor.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/CommandProcessor.java @@ -1,6 +1,7 @@ package com.github.hhhzzzsss.songplayer; import com.github.hhhzzzsss.songplayer.playing.SongHandler; +import com.github.hhhzzzsss.songplayer.song.Note; import com.github.hhhzzzsss.songplayer.song.Song; import java.io.File; @@ -25,6 +26,7 @@ public class CommandProcessor { commands.add(new useEssentialsCommandsCommand()); commands.add(new useVanillaCommandsCommand()); commands.add(new toggleFakePlayerCommand()); + commands.add(new testSongCommand()); } // returns true if it is a command and should be cancelled @@ -436,4 +438,30 @@ public class CommandProcessor { } } } + + private static class testSongCommand extends Command { + public String getName() { + return "testSong"; + } + public String getSyntax() { + return "$testSong"; + } + public String getDescription() { + return "Creates a song for testing"; + } + public boolean processCommand(String args) { + if (args.length() == 0) { + Song song = new Song("test_song"); + for (int i=0; i<400; i++) { + song.add(new Note(i, i*50)); + } + song.length = 400*50; + SongHandler.getInstance().setSong(song); + return true; + } + else { + return false; + } + } + } } diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/playing/SongHandler.java b/src/main/java/com/github/hhhzzzsss/songplayer/playing/SongHandler.java index b4a0555..8cdbea1 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/playing/SongHandler.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/playing/SongHandler.java @@ -104,7 +104,7 @@ public class SongHandler { } } - private void setSong(Song song) { + public void setSong(Song song) { currentSong = song; building = true; setCreativeIfNeeded(); @@ -158,6 +158,9 @@ public class SongHandler { } else if (!stage.missingNotes.isEmpty()) { int desiredNoteId = stage.missingNotes.pollFirst(); BlockPos bp = stage.noteblockPositions.get(desiredNoteId); + if (bp == null) { + return; + } int blockId = Block.getRawIdFromState(world.getBlockState(bp)); int currentNoteId = (blockId-SongPlayer.NOTEBLOCK_BASE_ID)/2; if (currentNoteId != desiredNoteId) { @@ -219,7 +222,10 @@ public class SongHandler { currentSong.advanceTime(); while (currentSong.reachedNextNote()) { Note note = currentSong.getNextNote(); - attackBlock(stage.noteblockPositions.get(note.noteId)); + BlockPos bp = stage.noteblockPositions.get(note.noteId); + if (bp != null) { + attackBlock(bp); + } } if (currentSong.finished()) { diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/playing/Stage.java b/src/main/java/com/github/hhhzzzsss/songplayer/playing/Stage.java index 558d47a..2603aba 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/playing/Stage.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/playing/Stage.java @@ -9,6 +9,7 @@ import com.github.hhhzzzsss.songplayer.song.Song; import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.client.network.ClientPlayerEntity; +import net.minecraft.entity.EntityPose; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; @@ -52,7 +53,9 @@ public class Stage { for (int dz = -4; dz <= 4; dz++) { if (Math.abs(dx) == 4 && Math.abs(dz) == 4) { noteblockLocations.add(new BlockPos(position.getX() + dx, position.getY() + 0, position.getZ() + dz)); + noteblockLocations.add(new BlockPos(position.getX() + dx, position.getY() + 2, position.getZ() + dz)); breakLocations.add(new BlockPos(position.getX() + dx, position.getY() + 1, position.getZ() + dz)); + breakLocations.add(new BlockPos(position.getX() + dx, position.getY() + 3, position.getZ() + dz)); } else { noteblockLocations.add(new BlockPos(position.getX() + dx, position.getY() - 1, position.getZ() + dz)); noteblockLocations.add(new BlockPos(position.getX() + dx, position.getY() + 2, position.getZ() + dz)); @@ -62,13 +65,27 @@ public class Stage { } } } + for (int dx = -4; dx <= 4; dx++) { + for (int dz = -4; dz <= 4; dz++) { + if (withinBreakingDist(dx, -3, dz)) { + noteblockLocations.add(new BlockPos(position.getX() + dx, position.getY() - 3, position.getZ() + dz)); + } + if (withinBreakingDist(dx, 4, dz)) { + noteblockLocations.add(new BlockPos(position.getX() + dx, position.getY() + 4, position.getZ() + dz)); + } + } + } // Sorting noteblock and break locations noteblockLocations.sort((a, b) -> { // First sort by y - if (a.getY() < b.getY()) { + int a_dy = a.getY() - position.getY(); + int b_dy = b.getY() - position.getY(); + if (a_dy == -1) a_dy = 0; // same layer + if (b_dy == -1) b_dy = 0; // same layer + if (Math.abs(a_dy) < Math.abs(b_dy)) { return -1; - } else if (a.getY() > b.getY()) { + } else if (Math.abs(a_dy) > Math.abs(b_dy)) { return 1; } // Then sort by horizontal distance @@ -94,6 +111,46 @@ public class Stage { return 0; } }); + + // Remove already-existing notes from missingNotes, adding their positions to noteblockPositions, and create a list of unused noteblock locations + ArrayList unusedNoteblockLocations = new ArrayList<>(); + for (BlockPos nbPos : noteblockLocations) { + BlockState bs = SongPlayer.MC.world.getBlockState(nbPos); + int blockId = Block.getRawIdFromState(bs); + if (blockId >= SongPlayer.NOTEBLOCK_BASE_ID && blockId < SongPlayer.NOTEBLOCK_BASE_ID+800) { + int noteId = (blockId-SongPlayer.NOTEBLOCK_BASE_ID)/2; + if (missingNotes.contains(noteId)) { + missingNotes.remove(noteId); + noteblockPositions.put(noteId, nbPos); + } + else { + unusedNoteblockLocations.add(nbPos); + } + } + else { + unusedNoteblockLocations.add(nbPos); + } + } + + // Cull noteblocks that won't fit in stage + if (missingNotes.size() > unusedNoteblockLocations.size()) { + while (missingNotes.size() > unusedNoteblockLocations.size()) { + missingNotes.pollLast(); + } + } + + // Populate missing noteblocks into the unused noteblock locations + int idx = 0; + for (int noteId : missingNotes) { + BlockPos bp = unusedNoteblockLocations.get(idx++); + noteblockPositions.put(noteId, bp); + int dy = bp.getY() - position.getY(); + // Optional break locations + if (dy < -1 || dy > 2) { + breakLocations.add(bp.up()); + } + } + requiredBreaks = breakLocations .stream() .filter((bp) -> Block.getRawIdFromState(SongPlayer.MC.world.getBlockState(bp)) != 0) @@ -129,39 +186,16 @@ public class Stage { }) .collect(Collectors.toCollection(LinkedList::new)); - // Remove already-existing notes from missingNotes, adding their positions to noteblockPositions, and create a list of unused noteblock locations - ArrayList unusedNoteblockLocations = new ArrayList<>(); - for (BlockPos nbPos : noteblockLocations) { - BlockState bs = SongPlayer.MC.world.getBlockState(nbPos); - int blockId = Block.getRawIdFromState(bs); - if (blockId >= SongPlayer.NOTEBLOCK_BASE_ID && blockId < SongPlayer.NOTEBLOCK_BASE_ID+800) { - int noteId = (blockId-SongPlayer.NOTEBLOCK_BASE_ID)/2; - if (missingNotes.contains(noteId)) { -// stage.tunedNoteblocks[noteId] = pos; - missingNotes.remove(noteId); - noteblockPositions.put(noteId, nbPos); - } - else { - unusedNoteblockLocations.add(nbPos); - } - } - else { - unusedNoteblockLocations.add(nbPos); - } - } - - // Populate missing noteblocks into the unused noteblock locations - int idx = 0; - for (int noteId : missingNotes) { - if (idx >= unusedNoteblockLocations.size()) { - System.out.println("Too many noteblocks!"); - break; - } - noteblockPositions.put(noteId, unusedNoteblockLocations.get(idx++)); - } - // Set total missing notes totalMissingNotes = missingNotes.size(); + + System.out.println(player.getEyeY()); + } + + boolean withinBreakingDist(int dx, int dy, int dz) { + double dy1 = dy + 0.5 - 1.62; // Standing eye height + double dy2 = dy + 0.5 - 1.27; // Crouching eye height + return dx*dx + dy1*dy1 + dz*dz < 5.99*5.99 && dx*dx + dy2*dy2 + dz*dz < 5.99*5.99; } public boolean nothingToBuild() {