11package lu .kolja .expandedae .item .misc ;
22
3- import appeng .api . parts . IPartHost ;
3+ import appeng .blockentity . networking . CableBusBlockEntity ;
44import appeng .helpers .IPriorityHost ;
55import appeng .items .AEBaseItem ;
66import appeng .util .InteractionUtil ;
77import lu .kolja .expandedae .definition .ExpLang ;
88import net .minecraft .MethodsReturnNonnullByDefault ;
9+ import net .minecraft .core .BlockPos ;
910import net .minecraft .network .chat .Component ;
1011import net .minecraft .world .InteractionHand ;
1112import net .minecraft .world .InteractionResult ;
1516import net .minecraft .world .item .TooltipFlag ;
1617import net .minecraft .world .item .context .UseOnContext ;
1718import net .minecraft .world .level .Level ;
19+ import net .minecraft .world .level .block .entity .BlockEntity ;
20+ import net .minecraft .world .phys .Vec3 ;
1821import org .jetbrains .annotations .Nullable ;
1922
2023import javax .annotation .ParametersAreNonnullByDefault ;
2528public class PriorityCardItem extends AEBaseItem {
2629 public static final String NBT_PRIO = "priority" ;
2730 public static final String NBT_MODE = "mode" ;
31+
2832 public PriorityCardItem (Properties properties ) {
2933 super (properties );
3034 }
3135
3236 @ Override
3337 public InteractionResult useOn (UseOnContext ctx ) {
3438 if (ctx .getLevel ().isClientSide ) return InteractionResult .FAIL ;
35- var block = ctx .getLevel ().getBlockEntity (ctx .getClickedPos ());
39+
40+ BlockPos hitPos = ctx .getClickedPos ();
41+ var block = ctx .getLevel ().getBlockEntity (hitPos );
3642 var tag = ctx .getItemInHand ().getOrCreateTag ();
3743 var prio = tag .getInt (NBT_PRIO );
3844 var mode = tag .getBoolean (NBT_MODE );
@@ -41,10 +47,15 @@ public InteractionResult useOn(UseOnContext ctx) {
4147 if (block instanceof IPriorityHost prioHost ) {
4248 prioHost .setPriority (prio );
4349 be = block .getBlockState ().getBlock ().getName ();
44- } else if (block instanceof IPartHost partHost && partHost .getPart (ctx .getHorizontalDirection ()) instanceof IPriorityHost prioPart ) {
45- prioPart .setPriority (prio );
46- var partItem = partHost .getPart (ctx .getClickedFace ()).getPartItem ().asItem ();
47- be = partItem .getName (new ItemStack (partItem ));
50+ } else if (block instanceof CableBusBlockEntity cable ) {
51+ Vec3 hitVec = ctx .getClickLocation ();
52+ Vec3 hitInBlock = new Vec3 (hitVec .x - hitPos .getX (), hitVec .y - hitPos .getY (), hitVec .z - hitPos .getZ ());
53+ var selected = cable .getCableBus ().selectPartLocal (hitInBlock );
54+ if (selected .part instanceof IPriorityHost prioPart ) {
55+ prioPart .setPriority (prio );
56+ var partItem = selected .part .getPartItem ().asItem ();
57+ be = partItem .getName (new ItemStack (partItem ));
58+ } else return InteractionResult .FAIL ;
4859 } else return InteractionResult .FAIL ;
4960 ctx .getPlayer ().sendSystemMessage (ExpLang .PRIO_CHANGED .text (be , "§b" + prio ));
5061 prio += mode ? 1 : -1 ;
0 commit comments