Skip to content

Commit 70afa77

Browse files
authored
Merge branch 'develop' into 447_magic_block_flag
2 parents 90e805c + 5d86e45 commit 70afa77

File tree

2 files changed

+45
-21
lines changed

2 files changed

+45
-21
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
<!-- Do not change unless you want different name for local builds. -->
6565
<build.number>-LOCAL</build.number>
6666
<!-- This allows to change between versions. -->
67-
<build.version>1.20.0</build.version>
67+
<build.version>1.20.1</build.version>
6868
<!-- SonarCloud -->
6969
<sonar.projectKey>BentoBoxWorld_AOneBlock</sonar.projectKey>
7070
<sonar.organization>bentobox-world</sonar.organization>
@@ -281,7 +281,7 @@
281281
<plugin>
282282
<groupId>org.apache.maven.plugins</groupId>
283283
<artifactId>maven-compiler-plugin</artifactId>
284-
<version>3.8.1</version>
284+
<version>3.13.0</version>
285285
<configuration>
286286
<release>${java.version}</release>
287287
</configuration>

src/main/java/world/bentobox/aoneblock/listeners/BlockListener.java

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.bukkit.event.block.BlockFromToEvent;
3737
import org.bukkit.event.entity.EntityInteractEvent;
3838
import org.bukkit.event.entity.EntitySpawnEvent;
39+
import org.bukkit.event.entity.ItemSpawnEvent;
3940
import org.bukkit.event.player.PlayerBucketFillEvent;
4041
import org.bukkit.event.player.PlayerInteractEvent;
4142
import org.bukkit.inventory.EquipmentSlot;
@@ -260,38 +261,61 @@ public void onBlockBreak(final PlayerBucketFillEvent e) {
260261
}
261262
}
262263

264+
263265
/**
264-
* Drop items at the top of the block.
265-
*
266-
* @param event EntitySpawnEvent object.
266+
* This handler listens for items spawning.
267+
* If an item spawns exactly at an island's center block,
268+
* it cancels the spawn and re-drops the item 1 block higher
269+
* (at the center of that block) to stack it neatly.
267270
*/
268271
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
269-
public void onItemStackSpawn(EntitySpawnEvent event) {
272+
public void onItemSpawn(ItemSpawnEvent event) {
273+
// --- Guard Clauses: Exit early if conditions aren't met ---
274+
275+
// 1. Check if the "drop on top" feature is enabled.
270276
if (!this.addon.getSettings().isDropOnTop()) {
271-
// Do nothing as item spawning is not interested in this case.
277+
// Feature is disabled, so we don't need to do anything.
272278
return;
273279
}
274280

275-
if (!EntityType.ITEM.equals(event.getEntityType())) {
276-
// We are interested only in dropped item entities.
277-
return;
278-
}
281+
// Get the spawn location once.
282+
Location spawnLocation = event.getLocation();
279283

280-
if (!this.addon.inWorld(event.getLocation().getWorld())) {
281-
// Not correct world
284+
// 2. Check if the spawn is happening in a world managed by the addon.
285+
if (!this.addon.inWorld(spawnLocation.getWorld())) {
286+
// Not a relevant world, ignore this event.
282287
return;
283288
}
284289

285-
Entity entity = event.getEntity();
286-
Location location = event.getLocation();
287-
288-
Optional<Island> optionalIsland = this.addon.getIslands().getIslandAt(location)
289-
.filter(island -> location.getBlock().getLocation().equals(island.getCenter()));
290+
// Find an island at the spawn location.
291+
Optional<Island> optionalIsland = this.addon.getIslands().getIslandAt(spawnLocation)
292+
// Chained to the Optional: Filter the island.
293+
// Only keep it if the block the item spawned in
294+
// is *exactly* the island's center.
295+
.filter(island -> {
296+
// .getBlock().getLocation() converts a precise location
297+
// (e.g., 10.2, 64.5, 12.8) to its block's location (10.0, 64.0, 12.0).
298+
Location blockLocation = spawnLocation.getBlock().getLocation();
299+
return blockLocation.equals(island.getCenter());
300+
});
290301

302+
// If we found an island AND it passed the filter (spawned at center)...
291303
if (optionalIsland.isPresent()) {
292-
// Teleport entity to the top of magic block.
293-
entity.teleport(optionalIsland.get().getCenter().add(0.5, 1, 0.5));
294-
entity.setVelocity(new Vector(0, 0, 0));
304+
// 1. Cancel the original item spawn.
305+
event.setCancelled(true);
306+
307+
// 2. Get the island and the item stack that was supposed to spawn.
308+
Island island = optionalIsland.get();
309+
// We use event.getEntity() which is guaranteed to be an Item.
310+
ItemStack itemStack = event.getEntity().getItemStack();
311+
312+
// 3. Calculate the new, clean drop location.
313+
// .add(0.5, 1, 0.5) moves it to the center of the block (0.5)
314+
// and one block up (1.0) so it sits on top.
315+
Location newDropLocation = island.getCenter().add(0.5, 1, 0.5);
316+
317+
// 4. Drop the item stack at the new location.
318+
spawnLocation.getWorld().dropItem(newDropLocation, itemStack);
295319
}
296320
}
297321

0 commit comments

Comments
 (0)