-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSongPlayer.java
317 lines (253 loc) · 11.3 KB
/
SongPlayer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package net.nimrod.noted.playing;
import net.minecraft.block.NoteBlock;
import net.minecraft.block.enums.Instrument;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket;
import net.minecraft.network.packet.c2s.play.PlayerInteractBlockC2SPacket;
import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import net.nimrod.noted.song.*;
import net.nimrod.noted.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map.Entry;
public class SongPlayer {
public boolean active = false; /* is song player playing */
public boolean paused = false; /* is the current song paused */
public Song currentSong = null; /* currently playing song structure */
private State state = State.WAITING; /* the current state of the bot */
private SongLoaderThread songLoaderThread = null; /* seperate execution thread for fetching songs from api */
private final List<BlockPos> noteBlockStage = new ArrayList<>(); /* holds the block positions of playable noteblocks */
private final List<BlockPos> playedNoteBlocks = new ArrayList<>(); /* holds the block positions of the noteblocks played during a single tick */
private final HashMap<BlockPos, Integer> pitchMap = new HashMap<>(); /* a mapping of noteblocks to note pitches */
private final int tuneNoteBlockDelay = 5; /* the time between tuning individual noteblocks (in ticks) */
private int tuneNoteBlockDelayCount = 0;
private static final MinecraftClient mc = MinecraftClient.getInstance();
public void toggleActive() {
active = !active;
if (!active)
reset();
LogUtils.chatLog("Toggled noted-client " + (active ? "§aon§f" : "§coff§f"));
}
public void togglePaused() {
if (currentSong == null) {
LogUtils.chatLog("No song playing");
return;
}
paused = !paused;
LogUtils.chatLog("Song " + (paused ? "§cpaused§f" : "§aunpaused§f"));
}
public void reset() {
paused = false;
currentSong = null;
state = State.WAITING;
songLoaderThread = null;
}
public void onHudRender(DrawContext context, float tickDelta) {
if (active && currentSong != null) {
String playingString = "Now Playing: " + currentSong.getName();
int playingStringX = mc.getWindow().getScaledWidth() - mc.textRenderer.getWidth(playingString) - 2;
String timeString = TimeUtils.formatTime(currentSong.getCurrentTime()) + "/" + TimeUtils.formatTime(currentSong.getLength());
int timeStringX = mc.getWindow().getScaledWidth() - mc.textRenderer.getWidth(timeString) - 2;
RenderUtils.drawString(context, playingString, playingStringX, 4, 0xffffff);
RenderUtils.drawString(context, timeString, timeStringX, 16, 0xffffff);
}
}
public void onWorldRender(MatrixStack matrixStack) {
switch (state) {
case STAGING:
case TUNING:
for (BlockPos noteBlock : noteBlockStage)
RenderUtils.drawBoxOutline(matrixStack, new Box(noteBlock), 0xffffff);
break;
case PLAYING:
for (BlockPos noteBlock : noteBlockStage) {
if (playedNoteBlocks.contains(noteBlock)) {
RenderUtils.drawBoxFilled(matrixStack, new Box(noteBlock), 0x14ec05);
} else {
Integer pitch = pitchMap.get(noteBlock);
if (pitch == null)
continue;
if (pitch != getNoteBlockNote(noteBlock))
RenderUtils.drawBoxFilled(matrixStack, new Box(noteBlock), 0xed0524);
}
}
break;
}
}
public void onTick() {
if (!active)
return;
if (songLoaderThread == null) {
songLoaderThread = new SongLoaderThread();
songLoaderThread.start();
}
if (!songLoaderThread.isAlive()) {
if (songLoaderThread.exception != null) {
LogUtils.chatLog("Failed to load song: " + songLoaderThread.exception.getMessage());
state = State.ERROR;
} else {
if (currentSong == null) {
currentSong = songLoaderThread.song;
LogUtils.chatLog("Loaded song: " + currentSong.getName());
state = State.STAGING;
}
}
}
switch (state) {
case STAGING:
/* center player */
mc.player.setPosition(MathHelper.floor(mc.player.getX()) + 0.5, mc.player.getY(), MathHelper.floor(mc.player.getZ()) + 0.5);
mc.getNetworkHandler().sendPacket(new PlayerMoveC2SPacket.PositionAndOnGround(mc.player.getX(), mc.player.getY(), mc.player.getZ(), mc.player.isOnGround()));
LogUtils.chatLog("Preparing noteblock stage...");
scanNoteBlockStage();
if (noteBlockStage.size() == 0) {
LogUtils.chatLog("Could not find any noteblocks within range");
state = State.ERROR;
return;
}
setupPitchMap();
if (pitchMap.isEmpty()) {
LogUtils.chatLog("Could not create pitch to noteblock mapping");
state = State.ERROR;
return;
}
LogUtils.chatLog("Tuning noteblocks...");
state = State.TUNING;
break;
case TUNING:
tuneNoteBlocks();
break;
case PLAYING:
playSongTick();
break;
case ERROR:
toggleActive();
break;
}
}
private void scanNoteBlockStage() {
noteBlockStage.clear();
/* locate all playable noteblocks within reach of the player */
int min = (int) (-mc.interactionManager.getReachDistance()) - 2;
int max = (int) mc.interactionManager.getReachDistance() + 2;
for (int y = min; y < max; y++) {
for (int x = min; x < max; x++) {
for (int z = min; z < max; z++) {
BlockPos blockPos = mc.player.getBlockPos().add(x, y + 1, z); // y + 1 accounts for eye height
if (!isValidNoteBlock(blockPos))
continue;
double distSquared = mc.player.getEyePos().squaredDistanceTo(Vec3d.ofCenter(blockPos));
if (distSquared > ServerPlayNetworkHandler.MAX_BREAK_SQUARED_DISTANCE)
continue;
noteBlockStage.add(blockPos);
}
}
}
}
private void setupPitchMap() {
pitchMap.clear();
/* define the requirements (distinct notes) of the song */
List<Note> songRequirements = new ArrayList<>();
currentSong.getNotes().stream().distinct().forEach(songRequirements::add);
for (Note note : songRequirements) {
for (BlockPos blockPos : noteBlockStage) {
if (pitchMap.containsKey(blockPos))
continue;
int pitch = note.getNoteId() % 25;
int instrument = note.getNoteId() / 25;
int noteBlockInstrument = getNoteBlockInstrument(blockPos).ordinal();
if (instrument == noteBlockInstrument && pitchMap.entrySet()
.stream()
.filter(e -> e.getValue() == pitch)
.noneMatch(e -> getNoteBlockInstrument(e.getKey())
.ordinal() == noteBlockInstrument))
{
pitchMap.put(blockPos, pitch);
break;
}
}
}
}
private void tuneNoteBlocks() {
for (Entry<BlockPos, Integer> e : pitchMap.entrySet()) {
int currentNote = getNoteBlockNote(e.getKey());
if (currentNote == -1)
continue;
if (currentNote != e.getValue()) {
if (++tuneNoteBlockDelayCount < tuneNoteBlockDelay)
return;
tuneNoteBlockDelayCount = 0;
mc.player.swingHand(Hand.MAIN_HAND);
int targetNote = e.getValue() < currentNote ? e.getValue() + 25 : e.getValue();
int requiredHits = Math.min(25, targetNote - currentNote);
for (int i = 0; i < requiredHits; i++)
tuneNoteBlock(e.getKey());
}
}
LogUtils.chatLog("Playing song...");
state = State.PLAYING;
}
private void playSongTick() {
if (paused) {
currentSong.pause();
return;
}
playedNoteBlocks.clear();
currentSong.play();
currentSong.advanceCurrentTime();
mc.player.swingHand(Hand.MAIN_HAND);
while (currentSong.reachedNextNote()) {
Note note = currentSong.getNextNote();
for (Entry<BlockPos, Integer> e : pitchMap.entrySet()) {
if (note.getNoteId() % 25 == getNoteBlockNote(e.getKey())) {
playedNoteBlocks.add(e.getKey());
playNoteBlock(e.getKey());
}
}
}
if (currentSong.finished())
reset();
}
private void playNoteBlock(BlockPos blockPos) {
if (!isValidNoteBlock(blockPos))
return;
mc.getNetworkHandler().sendPacket(new PlayerActionC2SPacket(PlayerActionC2SPacket.Action.START_DESTROY_BLOCK, blockPos, Direction.DOWN, 0));
}
private void tuneNoteBlock(BlockPos blockPos) {
if (!isValidNoteBlock(blockPos))
return;
mc.getNetworkHandler().sendPacket(new PlayerInteractBlockC2SPacket(Hand.MAIN_HAND, new BlockHitResult(Vec3d.ofCenter(blockPos), Direction.DOWN, blockPos, false), 0));
}
private Instrument getNoteBlockInstrument(BlockPos blockPos) {
if (!isValidNoteBlock(blockPos))
return Instrument.HARP;
return mc.world.getBlockState(blockPos).get(NoteBlock.INSTRUMENT);
}
private int getNoteBlockNote(BlockPos blockPos) {
if (!isValidNoteBlock(blockPos))
return -1;
return mc.world.getBlockState(blockPos).get(NoteBlock.NOTE);
}
private static boolean isValidNoteBlock(BlockPos blockPos) {
return mc.world.getBlockState(blockPos).getBlock() instanceof NoteBlock &&
mc.world.getBlockState(blockPos.up()).isAir();
}
public enum State {
WAITING,
STAGING,
TUNING,
PLAYING,
ERROR
}
}