• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CyclopsMC / CyclopsCore / #479033794

03 May 2025 01:07PM UTC coverage: 22.057% (-0.04%) from 22.101%
#479033794

push

github

rubensworks
Merge remote-tracking branch 'origin/master-1.19-lts' into master-1.20-lts

0 of 21 new or added lines in 3 files covered. (0.0%)

1 existing line in 1 file now uncovered.

2297 of 10414 relevant lines covered (22.06%)

0.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/org/cyclops/cyclopscore/item/ItemGui.java
1
package org.cyclops.cyclopscore.item;
2

3
import net.minecraft.core.BlockPos;
4
import net.minecraft.network.FriendlyByteBuf;
5
import net.minecraft.resources.ResourceLocation;
6
import net.minecraft.server.level.ServerPlayer;
7
import net.minecraft.stats.Stat;
8
import net.minecraft.tags.ItemTags;
9
import net.minecraft.world.InteractionHand;
10
import net.minecraft.world.InteractionResult;
11
import net.minecraft.world.InteractionResultHolder;
12
import net.minecraft.world.MenuProvider;
13
import net.minecraft.world.entity.player.Player;
14
import net.minecraft.world.inventory.AbstractContainerMenu;
15
import net.minecraft.world.item.Item;
16
import net.minecraft.world.item.ItemStack;
17
import net.minecraft.world.item.context.UseOnContext;
18
import net.minecraft.world.level.Level;
19
import net.minecraft.world.level.block.Blocks;
20
import net.minecraft.world.level.block.LecternBlock;
21
import net.minecraft.world.level.block.state.BlockState;
22
import net.minecraftforge.common.util.FakePlayer;
23
import net.minecraftforge.network.NetworkHooks;
24
import org.cyclops.cyclopscore.inventory.InventoryLocationPlayer;
25
import org.cyclops.cyclopscore.inventory.ItemLocation;
26

27
import javax.annotation.Nullable;
28

29
/**
30
 * Configurable item that can show a GUI on right clicking.
31
 *
32
 * Implement {@link #getContainer(Level, Player, ItemLocation)}
33
 * and {@link #getContainerClass(Level, Player, ItemStack)} to specify the gui.
34
 *
35
 * Optionally implement {@link #getOpenStat()} to specify a stat on gui opening.
36
 *
37
 * @author rubensworks
38
 */
39
public abstract class ItemGui extends Item {
40

41
    protected ItemGui(Item.Properties properties) {
42
        super(properties);
×
43
    }
×
44

45
    @Nullable
46
    public abstract MenuProvider getContainer(Level world, Player player, ItemLocation itemLocation);
47

48
    public abstract Class<? extends AbstractContainerMenu> getContainerClass(Level world, Player player, ItemStack itemStack);
49

50
    @Override
51
    public boolean onDroppedByPlayer(ItemStack itemstack, Player player) {
52
        if(!itemstack.isEmpty()
×
53
                && player instanceof ServerPlayer
54
                && player.containerMenu != null
55
                && player.containerMenu.getClass() == getContainerClass(player.level(), player, itemstack)) {
×
56
            player.closeContainer();
×
57
        }
58
        return super.onDroppedByPlayer(itemstack, player);
×
59
    }
60

61
    /**
62
     * Open the gui for a certain item index in the player inventory.
63
     * @param world The world.
64
     * @param player The player.
65
     * @param itemLocation The item with its location.
66
     */
67
    public void openGuiForItemIndex(Level world, ServerPlayer player, ItemLocation itemLocation) {
68
        if (!world.isClientSide()) {
×
69
            MenuProvider containerProvider = this.getContainer(world, player, itemLocation);
×
70
            if (containerProvider != null) {
×
71
                NetworkHooks.openScreen(player, containerProvider, packetBuffer -> this.writeExtraGuiData(packetBuffer, world, player, itemLocation));
×
72
                Stat<ResourceLocation> openStat = this.getOpenStat();
×
73
                if (openStat != null) {
×
74
                    player.awardStat(openStat);
×
75
                }
76
            }
77
        }
78
    }
×
79

80
    /**
81
     * Write additional data to a packet buffer that will be sent to the client when opening the gui.
82
     * @param packetBuffer A packet buffer to write to.
83
     * @param world The world.
84
     * @param player The player.
85
     * @param itemLocation The item with its location.
86
     */
87
    public void writeExtraGuiData(FriendlyByteBuf packetBuffer, Level world, ServerPlayer player,
88
                                  ItemLocation itemLocation) {
89
        ItemLocation.writeToPacketBuffer(packetBuffer, itemLocation);
×
90
    }
×
91

92
    /**
93
     * @return An optional gui opening statistic.
94
     */
95
    @Nullable
96
    protected Stat<ResourceLocation> getOpenStat() {
97
        return null;
×
98
    }
99

100
    @Override
101
    public InteractionResultHolder<ItemStack> use(Level world, Player player, InteractionHand hand) {
102
        ItemStack itemStack = player.getItemInHand(hand);
×
103
        if (player instanceof FakePlayer) {
×
104
            return new InteractionResultHolder<>(InteractionResult.FAIL, itemStack);
×
105
        }
106
        if (player instanceof ServerPlayer) {
×
107
            openGuiForItemIndex(world, (ServerPlayer) player, InventoryLocationPlayer.getInstance().handToLocation(player, hand, player.getInventory().selected));
×
108
        }
109
        return new InteractionResultHolder<>(InteractionResult.SUCCESS, itemStack);
×
110
    }
111

112
    @Override
113
    public InteractionResult useOn(UseOnContext context) {
NEW
114
        Level level = context.getLevel();
×
NEW
115
        BlockPos pos = context.getClickedPos();
×
NEW
116
        BlockState blockState = level.getBlockState(pos);
×
NEW
117
        if (context.getItemInHand().is(ItemTags.LECTERN_BOOKS) && blockState.is(Blocks.LECTERN)) {
×
NEW
118
            return LecternBlock.tryPlaceBook(context.getPlayer(), level, pos, blockState, context.getItemInHand()) ? InteractionResult.sidedSuccess(level.isClientSide) : InteractionResult.PASS;
×
119
        }
NEW
120
        return super.useOn(context);
×
121
    }
122
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc