Skip to content

Commit

Permalink
添加特性:更好的展示框交互
Browse files Browse the repository at this point in the history
  • Loading branch information
Gu-ZT committed Oct 12, 2024
1 parent ee66195 commit 6ea2233
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 2 deletions.
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 @@ -67,6 +67,12 @@ public class GcaSetting {
)
public static boolean betterSignInteraction = false;

// 右键包含物品的展示框时与之附着的方块产生交互
@Rule(
categories = {GCA, EXPERIMENTAL}
)
public static boolean betterItemFrameInteraction = false;

// 快速合成时在物品栏保留一份物品
@Rule(
categories = {GCA, EXPERIMENTAL}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.dubhe.gugle.carpet.mixin;

import com.mojang.brigadier.CommandDispatcher;
import dev.dubhe.gugle.carpet.commands.BotCommand;
import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
Expand All @@ -13,7 +12,7 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(Commands.class)
public class CommandsMixin {
abstract class CommandsMixin {
@Shadow
@Final
private CommandDispatcher<CommandSourceStack> dispatcher;
Expand Down
83 changes: 83 additions & 0 deletions src/main/java/dev/dubhe/gugle/carpet/mixin/ItemFrameMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package dev.dubhe.gugle.carpet.mixin;

import dev.dubhe.gugle.carpet.GcaSetting;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.Holder;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.decoration.ItemFrame;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.asm.mixin.Mixin;
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.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin({ItemFrame.class})
abstract class ItemFrameMixin extends Entity {
public ItemFrameMixin(EntityType<?> entityType, Level level) {
super(entityType, level);
}

@Inject(
method = {"dropItem(Lnet/minecraft/world/entity/Entity;Z)V"},
at = {@At(
value = "INVOKE",
target = "Lnet/minecraft/world/entity/decoration/ItemFrame;setItem(Lnet/minecraft/world/item/ItemStack;)V"
)},
cancellable = true
)
private void dropItem(Entity entity, boolean bl, CallbackInfo ci) {
if (GcaSetting.betterItemFrameInteraction) {
if (entity instanceof Player player) {
if (!player.getMainHandItem().is(Items.CACTUS) && !player.getOffhandItem().is(Items.CACTUS)) {
ci.cancel();
}
}
}
}

@Inject(
method = {"interact"},
at = {@At(
value = "INVOKE",
target = "Lnet/minecraft/world/entity/decoration/ItemFrame;setRotation(I)V"
)},
cancellable = true
)
private void interact(Player player, InteractionHand interactionHand, CallbackInfoReturnable<InteractionResult> cir) {
if (GcaSetting.betterItemFrameInteraction) {
if ((!player.getMainHandItem().is(Items.CACTUS) || !player.getOffhandItem().is(this::gca$isGlass)) && (!player.getOffhandItem().is(Items.CACTUS) || !player.getMainHandItem().is(this::gca$isGlass))) {
if (!player.getMainHandItem().is(Items.CACTUS) && !player.getOffhandItem().is(Items.CACTUS)) {
Level level = this.level();
Direction direction = this.getDirection();
BlockPos blockPos = this.getOnPos().relative(direction, -1);
BlockState blockState = level.getBlockState(blockPos);
BlockHitResult hitResult = new BlockHitResult(Vec3.atCenterOf(blockPos), direction, blockPos, false);
blockState.useWithoutItem(level, player, hitResult);
cir.setReturnValue(InteractionResult.CONSUME);
}
} else {
this.setInvisible(!this.isInvisible());
cir.setReturnValue(InteractionResult.CONSUME);
}
}
}

@Unique
private boolean gca$isGlass(@NotNull Holder<Item> itemHolder) {
return itemHolder.is(BuiltInRegistries.ITEM.getKey(Items.GLASS)) || itemHolder.is(BuiltInRegistries.ITEM.getKey(Items.GLASS_PANE));
}
}
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 @@ -37,6 +37,9 @@
"carpet.rule.betterQuickCrafting.name": "Better Quick Crafting",
"carpet.rule.betterQuickCrafting.desc": "Keep an item in the inventory during quick crafting",

"carpet.rule.betterItemFrameInteraction.name": "Better ItemFrame Interaction",
"carpet.rule.betterItemFrameInteraction.desc": "Make the block attached to the ItemFrame interact when you right-click it",

"carpet.rule.botManagement.name": "Bot Management",
"carpet.rule.botManagement.desc": "A Bot Management Menu",

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 @@ -31,6 +31,9 @@
"carpet.rule.betterSignInteraction.name": "更好的告示牌交互",
"carpet.rule.betterSignInteraction.desc": "右键告示牌时与之附着的方块产生交互",

"carpet.rule.betterItemFrameInteraction.name": "更好的展示框交互",
"carpet.rule.betterItemFrameInteraction.desc": "右键包含物品的展示框时与之附着的方块产生交互",

"carpet.rule.betterSignEditing.name": "更好的告示牌编辑",
"carpet.rule.betterSignEditing.desc": "允许使用名称中包含“笔”的羽毛编辑告示牌",

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 @@ -28,6 +28,9 @@
"carpet.rule.betterSignInteraction.name": "更好的告示牌互動",
"carpet.rule.betterSignInteraction.desc": "右鍵點擊告示牌時與附著的方塊產生互動",

"carpet.rule.betterItemFrameInteraction.name": "更好的展示框互動",
"carpet.rule.betterItemFrameInteraction.desc": "右鍵點擊包含物品的展示框時與附著的方塊產生互動",

"carpet.rule.betterSignEditing.name": "更好的告示牌編輯",
"carpet.rule.betterSignEditing.desc": "可以使用名稱中包含「pen」的羽毛編輯告示牌",

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/gca.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"EntityPlayerMPFakeInvoker",
"FenceGateBlockMixin",
"FishingHookMixin",
"ItemFrameMixin",
"ItemStackMixin",
"PlayerAccessor",
"PlayerMixin",
Expand Down

0 comments on commit 6ea2233

Please sign in to comment.