Skip to content

Commit

Permalink
openFakePlayerEnderChest 添加设定值 ender_chest
Browse files Browse the repository at this point in the history
  • Loading branch information
Gu-ZT committed Nov 3, 2024
1 parent a4dd056 commit 4c1251a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 7 deletions.
6 changes: 4 additions & 2 deletions src/main/java/dev/dubhe/gugle/carpet/GcaSetting.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ public class GcaSetting {

// 允许玩家打开假人末影箱
@Rule(
categories = {GCA, EXPERIMENTAL, BOT}
categories = {GCA, EXPERIMENTAL, BOT},
options = {"ender_chest", "true", "false"},
validators = GcaValidators.EnderChest.class
)
public static boolean openFakePlayerEnderChest = false;
public static String openFakePlayerEnderChest = "false";

// 退出存档时保留假人
@Rule(
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/dev/dubhe/gugle/carpet/GcaValidators.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dev.dubhe.gugle.carpet;

import carpet.api.settings.CarpetRule;
import carpet.api.settings.Validator;
import net.minecraft.commands.CommandSourceStack;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class GcaValidators {
public static class EnderChest extends Validator<String> {
public static final List<String> OPTIONS = List.of("true", "false", "ender_chest");

@Override
public String validate(@Nullable CommandSourceStack commandSourceStack, CarpetRule<String> carpetRule, String newValue, String userString) {
return !OPTIONS.contains(newValue) ? null : newValue;
}

public String description() {
return "Can be limited to 'ender_chest' for use EnderChest open only, true/false for open directly/unable";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private static int list(CommandContext<CommandSourceStack> context) {
return 1;
}

private static MutableComponent botToComponent(BotInfo botInfo) {
private static @NotNull MutableComponent botToComponent(@NotNull BotInfo botInfo) {
MutableComponent desc = Component.literal(botInfo.desc).withStyle(
Style.EMPTY
.applyFormat(ChatFormatting.GRAY)
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/dev/dubhe/gugle/carpet/mixin/PlayerMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import dev.dubhe.gugle.carpet.GcaExtension;
import dev.dubhe.gugle.carpet.GcaSetting;
import dev.dubhe.gugle.carpet.tools.GcaUtils;
import dev.dubhe.gugle.carpet.api.tools.text.ComponentTranslate;
import dev.dubhe.gugle.carpet.tools.ClientUtils;
import dev.dubhe.gugle.carpet.tools.FakePlayerEnderChestContainer;
Expand All @@ -16,6 +17,7 @@
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.ChestMenu;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -40,14 +42,14 @@ private void tick(CallbackInfo ci) {
}

@WrapOperation(method = "interactOn", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/Entity;interact(Lnet/minecraft/world/entity/player/Player;Lnet/minecraft/world/InteractionHand;)Lnet/minecraft/world/InteractionResult;"))
private InteractionResult interactOn(Entity entity, Player player, InteractionHand hand, Operation<InteractionResult> original) {
private InteractionResult interactOn(Entity entity, @NotNull Player player, InteractionHand hand, Operation<InteractionResult> original) {
if (player.level().isClientSide()) {
// 客户端在交互前要先判断一下当前交互的实体是不是玩家,这用来防止意外的使用物品功能
if (entity instanceof Player && ClientUtils.isFakePlayer(player)) {
return InteractionResult.CONSUME;
}
} else {
if ((GcaSetting.openFakePlayerInventory || GcaSetting.openFakePlayerEnderChest) && entity instanceof EntityPlayerMPFake fakePlayer) {
if ((GcaSetting.openFakePlayerInventory || GcaUtils.openFakePlayerEnderChest(player)) && entity instanceof EntityPlayerMPFake fakePlayer) {
// 打开物品栏
InteractionResult result = this.openInventory(player, fakePlayer);
if (result != InteractionResult.PASS) {
Expand All @@ -60,11 +62,11 @@ private InteractionResult interactOn(Entity entity, Player player, InteractionHa
}

@Unique
private InteractionResult openInventory(Player player, EntityPlayerMPFake fakePlayer) {
private InteractionResult openInventory(@NotNull Player player, EntityPlayerMPFake fakePlayer) {
SimpleMenuProvider provider;
if (player.isShiftKeyDown()) {
// 打开末影箱
if (GcaSetting.openFakePlayerEnderChest) {
if (GcaUtils.openFakePlayerEnderChest(player)) {
provider = new SimpleMenuProvider(
(i, inventory, p) -> ChestMenu.sixRows(
i, inventory,
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/dev/dubhe/gugle/carpet/tools/GcaUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dev.dubhe.gugle.carpet.tools;

import dev.dubhe.gugle.carpet.GcaSetting;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Items;

public class GcaUtils {
public static boolean openFakePlayerEnderChest(Player player) {
if ("true".equals(GcaSetting.openFakePlayerEnderChest)) return true;
return "ender_chest".equals(GcaSetting.openFakePlayerEnderChest) &&
(
player.getMainHandItem().is(Items.ENDER_CHEST) ||
player.getOffhandItem().is(Items.ENDER_CHEST)
);
}
}

0 comments on commit 4c1251a

Please sign in to comment.