Skip to content

Commit 6116809

Browse files
committed
rework placement logic to match spawn eggs
1 parent 8564dbd commit 6116809

2 files changed

Lines changed: 31 additions & 18 deletions

File tree

common/src/main/java/one/devos/nautical/losing_my_marbles/content/marble/MarbleEntity.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import net.minecraft.world.level.storage.ValueOutput;
4242
import net.minecraft.world.phys.Vec3;
4343
import one.devos.nautical.losing_my_marbles.content.LosingMyMarblesDataComponents;
44-
import one.devos.nautical.losing_my_marbles.content.LosingMyMarblesEntities;
4544
import one.devos.nautical.losing_my_marbles.content.LosingMyMarblesItemTags;
4645
import one.devos.nautical.losing_my_marbles.content.marble.data.MarbleInstance;
4746
import one.devos.nautical.losing_my_marbles.content.marble.data.shape.MarbleShape;
@@ -83,11 +82,6 @@ public MarbleEntity(EntityType<?> type, Level level, MarbleInstance marble) {
8382
this.refreshDimensions();
8483
}
8584

86-
public MarbleEntity(Level level, MarbleInstance marble, @Nullable LivingEntity owner) {
87-
this(LosingMyMarblesEntities.MARBLE, level, marble);
88-
this.owner = owner == null ? null : new EntityReference<>(owner);
89-
}
90-
9185
public MarbleInstance marble() {
9286
return Objects.requireNonNull(this.marble, "marble() called too early");
9387
}
@@ -104,6 +98,10 @@ public void setMarble(MarbleInstance marble) {
10498
}
10599
}
106100

101+
public void setOwner(@Nullable LivingEntity entity) {
102+
this.owner = entity == null ? null : new EntityReference<>(entity);
103+
}
104+
107105
public double distanceTraveled() {
108106
return this.distanceTraveled;
109107
}

common/src/main/java/one/devos/nautical/losing_my_marbles/content/marble/MarbleItem.java

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package one.devos.nautical.losing_my_marbles.content.marble;
22

3+
import java.util.Objects;
34
import java.util.Optional;
45
import java.util.function.Consumer;
56

@@ -9,26 +10,27 @@
910
import net.minecraft.core.dispenser.DefaultDispenseItemBehavior;
1011
import net.minecraft.core.dispenser.DispenseItemBehavior;
1112
import net.minecraft.network.chat.Component;
13+
import net.minecraft.server.level.ServerLevel;
1214
import net.minecraft.world.InteractionResult;
1315
import net.minecraft.world.entity.EntitySpawnReason;
1416
import net.minecraft.world.entity.EntityType;
17+
import net.minecraft.world.entity.player.Player;
1518
import net.minecraft.world.item.Item;
1619
import net.minecraft.world.item.ItemStack;
1720
import net.minecraft.world.item.TooltipFlag;
1821
import net.minecraft.world.item.component.TooltipDisplay;
1922
import net.minecraft.world.item.context.UseOnContext;
2023
import net.minecraft.world.level.Level;
2124
import net.minecraft.world.level.block.DispenserBlock;
25+
import net.minecraft.world.level.block.state.BlockState;
2226
import net.minecraft.world.level.gameevent.GameEvent;
23-
import net.minecraft.world.phys.AABB;
24-
import net.minecraft.world.phys.Vec3;
2527
import one.devos.nautical.losing_my_marbles.content.LosingMyMarblesDataComponents;
2628
import one.devos.nautical.losing_my_marbles.content.LosingMyMarblesEntities;
2729
import one.devos.nautical.losing_my_marbles.content.LosingMyMarblesItems;
2830
import one.devos.nautical.losing_my_marbles.content.marble.data.MarbleInstance;
2931

32+
// placement and dispense logic based on spawn eggs
3033
public final class MarbleItem extends Item {
31-
// based on spawn eggs
3234
public static final DispenseItemBehavior DISPENSE_BEHAVIOR = new DefaultDispenseItemBehavior() {
3335
@Override
3436
public ItemStack execute(BlockSource source, ItemStack stack) {
@@ -68,26 +70,39 @@ public InteractionResult useOn(UseOnContext context) {
6870
return InteractionResult.FAIL;
6971
}
7072

71-
BlockPos pos = context.getClickedPos().relative(context.getClickedFace());
7273
Level level = context.getLevel();
7374

74-
if (!level.noCollision(new AABB(pos)))
75-
return InteractionResult.FAIL;
76-
7775
Optional<MarbleInstance> instance = marble.get(level.registryAccess());
7876
if (instance.isEmpty()) {
7977
return InteractionResult.FAIL;
8078
}
8179

82-
if (level.isClientSide()) {
80+
if (!(level instanceof ServerLevel serverLevel)) {
8381
return InteractionResult.SUCCESS;
8482
}
8583

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+
}
89105

90-
stack.shrink(1);
91106
return InteractionResult.SUCCESS_SERVER;
92107
}
93108

0 commit comments

Comments
 (0)