|
1 | 1 | package one.devos.nautical.losing_my_marbles.content.marble; |
2 | 2 |
|
| 3 | +import java.util.Objects; |
3 | 4 | import java.util.Optional; |
4 | 5 | import java.util.function.Consumer; |
5 | 6 |
|
|
9 | 10 | import net.minecraft.core.dispenser.DefaultDispenseItemBehavior; |
10 | 11 | import net.minecraft.core.dispenser.DispenseItemBehavior; |
11 | 12 | import net.minecraft.network.chat.Component; |
| 13 | +import net.minecraft.server.level.ServerLevel; |
12 | 14 | import net.minecraft.world.InteractionResult; |
13 | 15 | import net.minecraft.world.entity.EntitySpawnReason; |
14 | 16 | import net.minecraft.world.entity.EntityType; |
| 17 | +import net.minecraft.world.entity.player.Player; |
15 | 18 | import net.minecraft.world.item.Item; |
16 | 19 | import net.minecraft.world.item.ItemStack; |
17 | 20 | import net.minecraft.world.item.TooltipFlag; |
18 | 21 | import net.minecraft.world.item.component.TooltipDisplay; |
19 | 22 | import net.minecraft.world.item.context.UseOnContext; |
20 | 23 | import net.minecraft.world.level.Level; |
21 | 24 | import net.minecraft.world.level.block.DispenserBlock; |
| 25 | +import net.minecraft.world.level.block.state.BlockState; |
22 | 26 | import net.minecraft.world.level.gameevent.GameEvent; |
23 | | -import net.minecraft.world.phys.AABB; |
24 | | -import net.minecraft.world.phys.Vec3; |
25 | 27 | import one.devos.nautical.losing_my_marbles.content.LosingMyMarblesDataComponents; |
26 | 28 | import one.devos.nautical.losing_my_marbles.content.LosingMyMarblesEntities; |
27 | 29 | import one.devos.nautical.losing_my_marbles.content.LosingMyMarblesItems; |
28 | 30 | import one.devos.nautical.losing_my_marbles.content.marble.data.MarbleInstance; |
29 | 31 |
|
| 32 | +// placement and dispense logic based on spawn eggs |
30 | 33 | public final class MarbleItem extends Item { |
31 | | - // based on spawn eggs |
32 | 34 | public static final DispenseItemBehavior DISPENSE_BEHAVIOR = new DefaultDispenseItemBehavior() { |
33 | 35 | @Override |
34 | 36 | public ItemStack execute(BlockSource source, ItemStack stack) { |
@@ -68,26 +70,39 @@ public InteractionResult useOn(UseOnContext context) { |
68 | 70 | return InteractionResult.FAIL; |
69 | 71 | } |
70 | 72 |
|
71 | | - BlockPos pos = context.getClickedPos().relative(context.getClickedFace()); |
72 | 73 | Level level = context.getLevel(); |
73 | 74 |
|
74 | | - if (!level.noCollision(new AABB(pos))) |
75 | | - return InteractionResult.FAIL; |
76 | | - |
77 | 75 | Optional<MarbleInstance> instance = marble.get(level.registryAccess()); |
78 | 76 | if (instance.isEmpty()) { |
79 | 77 | return InteractionResult.FAIL; |
80 | 78 | } |
81 | 79 |
|
82 | | - if (level.isClientSide()) { |
| 80 | + if (!(level instanceof ServerLevel serverLevel)) { |
83 | 81 | return InteractionResult.SUCCESS; |
84 | 82 | } |
85 | 83 |
|
86 | | - MarbleEntity entity = new MarbleEntity(level, instance.get(), context.getPlayer()); |
87 | | - entity.setPos(Vec3.atCenterOf(pos)); |
88 | | - level.addFreshEntity(entity); |
| 84 | + Direction face = context.getClickedFace(); |
| 85 | + BlockPos pos = context.getClickedPos(); |
| 86 | + BlockState state = level.getBlockState(pos); |
| 87 | + Player player = context.getPlayer(); |
| 88 | + |
| 89 | + BlockPos actualPos = state.getCollisionShape(level, pos).isEmpty() ? pos : pos.relative(face); |
| 90 | + boolean offset = !Objects.equals(pos, actualPos) && face == Direction.UP; |
| 91 | + |
| 92 | + Consumer<MarbleEntity> config = EntityType.appendDefaultStackConfig(entity -> { |
| 93 | + entity.setMarble(instance.get()); |
| 94 | + entity.setOwner(player); |
| 95 | + }, level, stack, player); |
| 96 | + |
| 97 | + MarbleEntity spawned = LosingMyMarblesEntities.MARBLE.spawn( |
| 98 | + serverLevel, config, actualPos, EntitySpawnReason.SPAWN_ITEM_USE, true, offset |
| 99 | + ); |
| 100 | + |
| 101 | + if (spawned != null) { |
| 102 | + stack.shrink(1); |
| 103 | + level.gameEvent(player, GameEvent.ENTITY_PLACE, pos); |
| 104 | + } |
89 | 105 |
|
90 | | - stack.shrink(1); |
91 | 106 | return InteractionResult.SUCCESS_SERVER; |
92 | 107 | } |
93 | 108 |
|
|
0 commit comments