Skip to content

Commit

Permalink
Added pauseplay command demo
Browse files Browse the repository at this point in the history
  • Loading branch information
egold555 committed Apr 24, 2024
1 parent 21612d2 commit d96009b
Show file tree
Hide file tree
Showing 11 changed files with 437 additions and 322 deletions.
Binary file added run/saves/Upright Player Piano/data/raids.dat
Binary file not shown.
Binary file modified run/saves/Upright Player Piano/entities/r.0.-1.mca
Binary file not shown.
Binary file modified run/saves/Upright Player Piano/level.dat
Binary file not shown.
Binary file modified run/saves/Upright Player Piano/region/r.-1.-1.mca
Binary file not shown.
Binary file modified run/saves/Upright Player Piano/region/r.0.-1.mca
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import plu.capstone.playerpiano.addon.minecraftmod.command.ModCommands;
import plu.capstone.playerpiano.addon.minecraftmod.piano.PianoWSClient;

public class PianoMod implements ModInitializer {
// This logger is used to write text to the console and the log file.
Expand All @@ -22,6 +24,8 @@ public void onInitialize() {

LOGGER.info("Hello Fabric world!");

ModCommands.registerModCommands();

try {
wsClient = new PianoWSClient("ws://localhost:8898/ws");
wsClient.connect();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package plu.capstone.playerpiano.addon.minecraftmod.command;

import com.mojang.brigadier.context.CommandContext;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;

public class ModCommands {

public static void registerModCommands() {
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
TestCommand.register(dispatcher);
PauseUnpauseCommand.register(dispatcher);
});
}

public static void sendFeedback(CommandContext<ServerCommandSource> context, String text) {
sendFeedback(context, Text.of(text));
}
public static void sendFeedback(CommandContext<ServerCommandSource> context, Text msg) {
context.getSource().sendFeedback(() -> msg, false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package plu.capstone.playerpiano.addon.minecraftmod.command;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import plu.capstone.playerpiano.addon.minecraftmod.piano.PianoController;

public class PauseUnpauseCommand {

public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
dispatcher.register(CommandManager.literal("piano-pause").executes(PauseUnpauseCommand::execute));
}

private static int execute(CommandContext<ServerCommandSource> context) {

PianoController.getInstance().pauseUnpauseSong();

return 1;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package plu.capstone.playerpiano.addon.minecraftmod.command;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;

public class TestCommand {

public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
dispatcher.register(CommandManager.literal("hello").executes(TestCommand::execute));
}

private static int execute(CommandContext<ServerCommandSource> context) {
String text = "Hello, " + context.getSource().getPlayer().getDisplayName().getString() + "!";
ModCommands.sendFeedback(context, text);
return 1;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package plu.capstone.playerpiano.addon.minecraftmod.piano;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse.BodyHandlers;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.function.Consumer;


public class PianoController {

HttpClient client = HttpClient.newHttpClient();

private static PianoController instance;

private String uuid;

public static PianoController getInstance() {
if(instance == null) instance = new PianoController();
return instance;
}

public void onConnect(String uuid) {
this.uuid = uuid;
}

public void pauseUnpauseSong() {
sendPostRequest("control/pause", "", System.out::println);
}

private void sendPostRequest(String url, String body, Consumer<String> callback) {

HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:8898/api/" + url))
.timeout(Duration.ofSeconds(5))
.header("Content-Type", "application/json")
.POST(BodyPublishers.ofString(body))
.build();
client.sendAsync(request, BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(callback);

}

}
Loading

0 comments on commit d96009b

Please sign in to comment.