-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from Apricityx/1.21.1
new rule: run command on sign.
- Loading branch information
Showing
13 changed files
with
324 additions
and
11 deletions.
There are no files selected for viewing
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,32 @@ | ||
name: build | ||
on: [pull_request, push] | ||
|
||
jobs: | ||
build: | ||
strategy: | ||
matrix: | ||
# Use these Java versions | ||
java: [ | ||
21, # Current Java LTS | ||
] | ||
runs-on: ubuntu-22.04 | ||
steps: | ||
- name: checkout repository | ||
uses: actions/checkout@v4 | ||
- name: validate gradle wrapper | ||
uses: gradle/wrapper-validation-action@v2 | ||
- name: setup jdk ${{ matrix.java }} | ||
uses: actions/setup-java@v4 | ||
with: | ||
java-version: ${{ matrix.java }} | ||
distribution: 'microsoft' | ||
- name: make gradle wrapper executable | ||
run: chmod +x ./gradlew | ||
- name: build | ||
run: ./gradlew build | ||
- name: capture build artifacts | ||
if: ${{ matrix.java == '21' }} # Only upload artifacts built from latest java | ||
uses: actions/upload-artifact@v4 | ||
with: | ||
name: Artifacts | ||
path: build/libs/ |
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,28 @@ | ||
on: | ||
release: | ||
types: [published] | ||
|
||
name: Create Release | ||
|
||
jobs: | ||
release: | ||
name: Upload Release Asset | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
- name: Setup java | ||
uses: actions/setup-java@v1 | ||
with: | ||
java-version: 21 | ||
- name: Build project | ||
run: | | ||
chmod +x ./gradlew | ||
./gradlew build | ||
- name: Publish mod | ||
uses: Kir-Antipov/mc-publish@v2.1 | ||
with: | ||
modrinth-token: ${{ secrets.MODRINTH_TOKEN }} | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
loader: fabric |
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
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,38 @@ | ||
package tech.mctown.cma; | ||
|
||
import net.minecraft.entity.player.PlayerEntity; | ||
import net.minecraft.item.ItemUsageContext; | ||
import net.minecraft.server.MinecraftServer; | ||
import net.minecraft.server.network.ServerPlayerEntity; | ||
import net.minecraft.text.Text; | ||
|
||
|
||
public class CMALogger { | ||
public static void toConsole(String message) { | ||
System.out.println(message); | ||
} | ||
|
||
public static void debug(String message) { | ||
System.out.println("\033[34m[CMA DEBUG]\033[0m" + "\033[33m" + message + "\033[0m"); | ||
} | ||
|
||
public static void toGame(String message, ServerPlayerEntity player) { | ||
player.sendMessage(Text.of(message), false); | ||
} | ||
|
||
public static void toGame(String message, MinecraftServer server) { | ||
Text textMessage = Text.of(message); | ||
for (ServerPlayerEntity player : server.getPlayerManager().getPlayerList()) { | ||
player.sendMessage(textMessage, false); | ||
} | ||
} | ||
|
||
public static void toGame(String message, PlayerEntity player) { | ||
player.sendMessage(Text.of(message)); | ||
} | ||
|
||
public static void error(String s, Exception e) { | ||
System.out.println("\033[31m[CMA ERROR]\033[0m" + "\033[33m" + s + "\033[0m"); | ||
e.printStackTrace(); | ||
} | ||
} |
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,50 @@ | ||
package tech.mctown.cma; | ||
|
||
import carpet.CarpetSettings; | ||
import com.mojang.brigadier.CommandDispatcher; | ||
import net.minecraft.server.command.ServerCommandSource; | ||
import net.minecraft.server.network.ServerPlayerEntity; | ||
import net.minecraft.text.Style; | ||
import net.minecraft.text.Text; | ||
import net.minecraft.text.TextColor; | ||
import net.minecraft.util.Formatting; | ||
|
||
import java.util.Map; | ||
|
||
public class CMAUtils { | ||
public static void executeCommand(ServerPlayerEntity player, String command) { | ||
if (player == null || command == null || command.trim().isEmpty()) { | ||
throw new IllegalArgumentException("Player or command cannot be null or empty"); | ||
} | ||
// 若带有 / 则去掉 | ||
if (command.startsWith("/")) { | ||
command = command.substring(1); | ||
} | ||
var server = player.getServer(); | ||
if (server == null) { | ||
throw new IllegalStateException("Player is not on a server"); | ||
} | ||
|
||
ServerCommandSource source = player.getCommandSource(); | ||
CommandDispatcher<ServerCommandSource> dispatcher = server.getCommandManager().getDispatcher(); | ||
|
||
try { | ||
// 解析和执行命令 | ||
var parseResults = dispatcher.parse(command, source); | ||
dispatcher.execute(parseResults); | ||
} catch (Exception e) { | ||
Text text = Text.literal(getTranslation("carpet.commandWentWrong")) | ||
.setStyle(Style.EMPTY.withColor(TextColor.fromFormatting(Formatting.RED))).append(Text.literal(" /" + command + ": " + e.getMessage()) | ||
.setStyle(Style.EMPTY.withColor(TextColor.fromFormatting(Formatting.RED)).withUnderline(true))); | ||
player.sendMessage(text, false); | ||
CMALogger.debug("Failed to execute command: " + command + e.getMessage()); | ||
} | ||
} | ||
|
||
public static String getTranslation(String key) { | ||
Map<String, String> lang = CMATranslations.getTranslationFromResourcePath(CarpetSettings.language); | ||
String translationText = lang.get(key); | ||
CMALogger.debug("Translation with key" + key + ": " + translationText); | ||
return lang.get(key); | ||
} | ||
} |
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,12 @@ | ||
package tech.mctown.cma.mixins.Test; | ||
|
||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
|
||
@Mixin(TestMixin.class) | ||
public abstract class TestMixin { | ||
// @Inject(method = "testMixin", at = @At("HEAD"), cancellable = true) | ||
// private void testMixin(){ | ||
|
||
} |
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
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
72 changes: 72 additions & 0 deletions
72
src/main/java/tech/mctown/cma/mixins/runCommandOnSign/AbstractSignBlockMixin.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,72 @@ | ||
package tech.mctown.cma.mixins.runCommandOnSign; | ||
|
||
import net.minecraft.block.BlockState; | ||
import net.minecraft.block.entity.SignBlockEntity; | ||
import net.minecraft.block.entity.SignText; | ||
import net.minecraft.entity.player.PlayerEntity; | ||
import net.minecraft.item.Items; | ||
import net.minecraft.server.command.CommandOutput; | ||
import net.minecraft.server.command.ServerCommandSource; | ||
import net.minecraft.server.network.ServerPlayerEntity; | ||
import net.minecraft.server.world.ServerWorld; | ||
import net.minecraft.text.Text; | ||
import net.minecraft.util.ActionResult; | ||
import net.minecraft.util.hit.BlockHitResult; | ||
import net.minecraft.util.math.BlockPos; | ||
import net.minecraft.util.math.Vec2f; | ||
import net.minecraft.util.math.Vec3d; | ||
import net.minecraft.world.World; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.Unique; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
import tech.mctown.cma.CMALogger; | ||
import tech.mctown.cma.CMASettings; | ||
import tech.mctown.cma.CMAUtils; | ||
|
||
import java.util.Arrays; | ||
import java.util.Objects; | ||
|
||
@Mixin(net.minecraft.block.AbstractSignBlock.class) // 向告示牌注入代码 | ||
public class AbstractSignBlockMixin { | ||
@Inject(method = "onUse", at = @At("HEAD"), cancellable = true) | ||
public void cancelOpenGUI(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit, CallbackInfoReturnable<ActionResult> cir) { | ||
if (CMASettings.runCommandOnSign) { | ||
if (player instanceof ServerPlayerEntity) { | ||
if (world.getBlockEntity(pos) instanceof SignBlockEntity signBlockEntity) { | ||
if (player.getMainHandStack().isOf(Items.AIR) && player.isSneaking()) { | ||
CMALogger.debug("AbstractSignBlockMixin cancelOpenGUI"); | ||
boolean isFront = signBlockEntity.isPlayerFacingFront(player); | ||
SignText texts = signBlockEntity.getText(isFront); | ||
Text[] text = texts.getMessages(false); | ||
CMALogger.debug(Arrays.toString(text)); | ||
// 若告示牌上的文本以 / 开头,则执行命令 | ||
StringBuilder command = new StringBuilder(text[0].getString()); | ||
if (command.toString().startsWith("/")) { | ||
for (int i = 1; i < 4; i++) { | ||
if (Objects.equals(text[i].getString(), "")) { | ||
continue; | ||
} | ||
command.append(text[i].getString()); | ||
} | ||
CMALogger.debug("Command: " + command); | ||
CMAUtils.executeCommand((ServerPlayerEntity) player, command.toString()); | ||
// Objects.requireNonNull(player.getServer()).getCommandManager().executeWithPrefix(createCommandSource(player, world, pos), command.toString()); | ||
} | ||
cir.setReturnValue(ActionResult.SUCCESS); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Unique | ||
private ServerCommandSource createCommandSource(PlayerEntity player, World world, BlockPos pos) { | ||
String string = player == null ? "Sign" : player.getName().getString(); | ||
Text text = (Text) (player == null ? Text.literal("Sign") : player.getDisplayName()); | ||
return new ServerCommandSource(CommandOutput.DUMMY, Vec3d.ofCenter(pos), Vec2f.ZERO, (ServerWorld) world, 2, string, text, world.getServer(), player); | ||
} | ||
} |
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
Oops, something went wrong.