Skip to content

Commit

Permalink
Merge pull request #88 from RTOServer/dev/wandering_trader
Browse files Browse the repository at this point in the history
新增特性,游商生成失败提醒
  • Loading branch information
Gu-ZT authored Jan 4, 2025
2 parents 8436d6e + c39609b commit 911380b
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/main/java/dev/dubhe/gugle/carpet/GcaSetting.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ public class GcaSetting {
)
public static boolean welcomePlayer = false;

// 流浪商人生成失败提醒
@Rule(
categories = {GCA, EXPERIMENTAL}
)
public static boolean wanderingTraderSpawnFailedWarning = false;

//#if MC>=12100
// 服务器玩家转移命令
@Rule(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package dev.dubhe.gugle.carpet.mixin;

import dev.dubhe.gugle.carpet.GcaSetting;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.RandomSource;
import net.minecraft.world.entity.npc.WanderingTraderSpawner;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Debug;
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.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Debug(export = true, print = true)
@Mixin(WanderingTraderSpawner.class)
abstract class WanderingTraderMixin {
@Unique
private MinecraftServer gca$server = null;
@Unique
private ServerPlayer gca$player = null;
@Shadow
private int spawnChance;

@Inject(method = "tick", at = @At("HEAD"))
private void spawn(@NotNull ServerLevel serverLevel, boolean bl, boolean bl2, CallbackInfoReturnable<Integer> cir) {
this.gca$server = serverLevel.getServer();
}

@Redirect(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/RandomSource;nextInt(I)I"))
private int spawn0(@NotNull RandomSource instance, int i) {
int result = instance.nextInt(i);
if (result > this.spawnChance) {
this.gca$sendMsg("Probability not met, expected i > %s, but obtained %s".formatted(this.spawnChance, result));
}
return result;
}

@Redirect(method = "spawn", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/RandomSource;nextInt(I)I"))
private int spawn1(@NotNull RandomSource instance, int i) {
int result = instance.nextInt(i);
if (result != 0) {
this.gca$sendMsg("Probability not met, expected i != 0, but obtained %s".formatted(result));
}
return result;
}

@Contract(pure = true)
@Redirect(method = "spawn", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/level/ServerLevel;getRandomPlayer()Lnet/minecraft/server/level/ServerPlayer;"))
private @Nullable ServerPlayer getRandomPlayer(@NotNull ServerLevel instance) {
this.gca$player = instance.getRandomPlayer();
return this.gca$player;
}

@Inject(method = "spawn", at = @At(value = "RETURN", ordinal = 2))
private void spawn2(ServerLevel serverLevel, CallbackInfoReturnable<Boolean> cir) {
this.gca$sendMsg("The biome does not meet the requirements, Current player: %s".formatted(this.gca$player.getScoreboardName()));
}

@Inject(method = "spawn", at = @At(value = "RETURN", ordinal = 4))
private void spawn3(ServerLevel serverLevel, CallbackInfoReturnable<Boolean> cir) {
this.gca$sendMsg("Not has enough space, Current player: %s".formatted(this.gca$player.getScoreboardName()));
}

@Unique
private void gca$sendMsg(String msg) {
if (!GcaSetting.wanderingTraderSpawnFailedWarning) return;
if (this.gca$server == null) return;
this.gca$server.getPlayerList().broadcastSystemMessage(
Component.literal("Wandering Trader Spawn Failed, Reason: ").withStyle(ChatFormatting.YELLOW),
false
);
this.gca$server.getPlayerList().broadcastSystemMessage(
Component.literal(msg).withStyle(ChatFormatting.YELLOW),
false
);
}
}
3 changes: 3 additions & 0 deletions src/main/resources/assets/gca/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@
"carpet.rule.welcomePlayer.name": "Welcome Player",
"carpet.rule.welcomePlayer.desc": "Welcome Player",

"carpet.rule.wanderingTraderSpawnFailedWarning.name": "Wandering Trader Spawn Failed Warning",
"carpet.rule.wanderingTraderSpawnFailedWarning.desc": "Wandering Trader Spawn Failed Warning",

"carpet.rule.commandTransfer.name": "Command Transfer",
"carpet.rule.commandTransfer.desc": "/transfer command",

Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/assets/gca/lang/zh_cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@
"carpet.rule.commandTransfer.name": "服务器玩家转移命令",
"carpet.rule.commandTransfer.desc": "/transfer 命令",

"carpet.rule.wanderingTraderSpawnFailedWarning.name": "流浪商人生成失败提醒",
"carpet.rule.wanderingTraderSpawnFailedWarning.desc": "在流浪商人生成失败进行提醒,并告诉玩家生成失败的原因",

"gca.action.attack.interval.12": "间隔 12 gt攻击:%s",
"gca.action.attack.continuous": "一直左键: %s",
"gca.action.use.continuous": "一直右键:%s",
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/assets/gca/lang/zh_tw.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@
"carpet.rule.commandTransfer.name": "Command Transfer",
"carpet.rule.commandTransfer.desc": "/transfer command",

"carpet.rule.wanderingTraderSpawnFailedWarning.name": "Wandering Trader Spawn Failed Warning",
"carpet.rule.wanderingTraderSpawnFailedWarning.desc": "Wandering Trader Spawn Failed Warning",

"gca.action.attack.interval.12": "間隔 12 gt攻擊:%s",
"gca.action.attack.continuous": "連續攻擊:%s",
"gca.action.use.continuous": "連續使用:%s",
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/gca.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"ServerPlaceRecipeMixin",
"SignBlockMixin",
"SlotMixin",
"TransferCommandMixin"
"TransferCommandMixin",
"WanderingTraderMixin"
],
"server": [
],
Expand Down
1 change: 1 addition & 0 deletions versions/1.20.1/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ publishMods {
changelog = "Changelog"
type = BETA
modLoaders.add("fabric")
modLoaders.add("quilt")

curseforge {
accessToken = providers.environmentVariable("CURSEFORGE_TOKEN")
Expand Down

0 comments on commit 911380b

Please sign in to comment.