-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
📝 Add Example1 after drastic rework of concepts
- Loading branch information
1 parent
875c85e
commit 272787d
Showing
1 changed file
with
75 additions
and
0 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
src/main/java/io/graversen/minecraft/rcon/examples/Example1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package io.graversen.minecraft.rcon.examples; | ||
|
||
import io.graversen.minecraft.rcon.MinecraftRcon; | ||
import io.graversen.minecraft.rcon.commands.GiveCommand; | ||
import io.graversen.minecraft.rcon.commands.tellraw.TellRawCommand; | ||
import io.graversen.minecraft.rcon.commands.tellraw.TellRawCommandBuilder; | ||
import io.graversen.minecraft.rcon.commands.tellraw.TellRawCompositeCommand; | ||
import io.graversen.minecraft.rcon.commands.title.TitleCommand; | ||
import io.graversen.minecraft.rcon.commands.title.TitleCommandBuilder; | ||
import io.graversen.minecraft.rcon.service.ConnectOptions; | ||
import io.graversen.minecraft.rcon.service.MinecraftRconService; | ||
import io.graversen.minecraft.rcon.service.RconDetails; | ||
import io.graversen.minecraft.rcon.util.Colors; | ||
import io.graversen.minecraft.rcon.util.MinecraftItem; | ||
import io.graversen.minecraft.rcon.util.Selectors; | ||
import io.graversen.minecraft.rcon.util.TitlePositions; | ||
|
||
import java.time.Duration; | ||
import java.util.List; | ||
|
||
public class Example1 { | ||
public static void main(String[] args) { | ||
|
||
// Define a simple MinecraftRconService | ||
// Assuming Minecraft server is running on localhost and password set to "test" | ||
// If no port is specified, the default Minecraft RCON port will be used | ||
final MinecraftRconService minecraftRconService = new MinecraftRconService( | ||
RconDetails.localhost("test"), | ||
ConnectOptions.defaults() | ||
); | ||
|
||
// Let's go! | ||
minecraftRconService.connectBlocking(Duration.ofSeconds(3)); | ||
|
||
// After connecting, we can (crudely) fetch the underlying Minecraft RCON provider | ||
final MinecraftRcon minecraftRcon = minecraftRconService.minecraftRcon().orElseThrow(IllegalStateException::new); | ||
|
||
// Build a TellRaw command - first half of the desired message | ||
final TellRawCommand tellRawCommand1 = new TellRawCommandBuilder() | ||
.targeting(Selectors.ALL_PLAYERS) | ||
.withText("It's dangerous to go alone - ") | ||
.withColor(Colors.GRAY) | ||
.italic() | ||
.build(); | ||
|
||
|
||
// Build another TellRaw command - other half of the message | ||
final TellRawCommand tellRawCommand2 = new TellRawCommandBuilder() | ||
.targeting(Selectors.ALL_PLAYERS) | ||
.withText("Take this!") | ||
.withColor(Colors.DARK_AQUA) | ||
.italic() | ||
.build(); | ||
|
||
// We are able to transparently stitch together multiple 'tellraw' commands, | ||
// combining their styles and texts into a composite viewing | ||
final TellRawCompositeCommand tellRawCompositeCommand = new TellRawCompositeCommand(List.of(tellRawCommand1, tellRawCommand2)); | ||
|
||
// Let's also add a nice title to the players' screens | ||
final TitleCommand titleCommand = new TitleCommandBuilder() | ||
.targeting(Selectors.ALL_PLAYERS) | ||
.atPosition(TitlePositions.TITLE) | ||
.withColor(Colors.GREEN) | ||
.withText("Welcome!") | ||
.build(); | ||
|
||
// We'll give everyone a diamond sword - it's dangerous without | ||
final GiveCommand giveCommand = new GiveCommand( | ||
Selectors.ALL_PLAYERS.getSelectorString(), new MinecraftItem("diamond_sword"), 1 | ||
); | ||
|
||
// Fire away! | ||
minecraftRcon.sendAsync(tellRawCompositeCommand, titleCommand, giveCommand); | ||
} | ||
} |