Skip to content

Commit c1381ab

Browse files
authored
Merge pull request #454 from BentoBoxWorld/develop
Release 1.20.1
2 parents ba51942 + 5d86e45 commit c1381ab

File tree

3 files changed

+47
-31
lines changed

3 files changed

+47
-31
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ jobs:
1414
- uses: actions/checkout@v3
1515
with:
1616
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
17-
- name: Set up JDK 17
17+
- name: Set up JDK 21
1818
uses: actions/setup-java@v3
1919
with:
2020
distribution: 'adopt'
21-
java-version: 17
21+
java-version: 21
2222
- name: Cache SonarCloud packages
2323
uses: actions/cache@v3
2424
with:

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 & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
import org.bukkit.block.Biome;
2121
import org.bukkit.block.Block;
2222
import org.bukkit.block.BlockFace;
23-
import org.bukkit.block.BrushableBlock;
2423
import org.bukkit.block.Chest;
25-
import org.bukkit.block.SuspiciousSand;
2624
import org.bukkit.block.data.Brushable;
2725
import org.bukkit.block.data.type.Leaves;
2826
import org.bukkit.entity.Entity;
@@ -37,17 +35,15 @@
3735
import org.bukkit.event.block.BlockFromToEvent;
3836
import org.bukkit.event.entity.EntityInteractEvent;
3937
import org.bukkit.event.entity.EntitySpawnEvent;
38+
import org.bukkit.event.entity.ItemSpawnEvent;
4039
import org.bukkit.event.player.PlayerBucketFillEvent;
4140
import org.bukkit.event.player.PlayerInteractEvent;
4241
import org.bukkit.inventory.EquipmentSlot;
4342
import org.bukkit.inventory.ItemStack;
44-
import org.bukkit.loot.LootTables;
4543
import org.bukkit.util.Vector;
4644
import org.eclipse.jdt.annotation.NonNull;
4745
import org.eclipse.jdt.annotation.Nullable;
4846

49-
import com.bgsoftware.wildstacker.api.loot.LootTable;
50-
5147
import world.bentobox.aoneblock.AOneBlock;
5248
import world.bentobox.aoneblock.dataobjects.OneBlockIslands;
5349
import world.bentobox.aoneblock.events.MagicBlockEntityEvent;
@@ -109,8 +105,6 @@ public class BlockListener implements Listener {
109105
*/
110106
public static final int SAVE_EVERY = 50;
111107

112-
private final Random random = new Random();
113-
114108
// Loot for suspicious blocks
115109
private static final Map<Material, Double> LOOT;
116110
static {
@@ -247,38 +241,61 @@ public void onBlockBreak(final PlayerBucketFillEvent e) {
247241
}
248242
}
249243

244+
250245
/**
251-
* Drop items at the top of the block.
252-
*
253-
* @param event EntitySpawnEvent object.
246+
* This handler listens for items spawning.
247+
* If an item spawns exactly at an island's center block,
248+
* it cancels the spawn and re-drops the item 1 block higher
249+
* (at the center of that block) to stack it neatly.
254250
*/
255251
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
256-
public void onItemStackSpawn(EntitySpawnEvent event) {
252+
public void onItemSpawn(ItemSpawnEvent event) {
253+
// --- Guard Clauses: Exit early if conditions aren't met ---
254+
255+
// 1. Check if the "drop on top" feature is enabled.
257256
if (!this.addon.getSettings().isDropOnTop()) {
258-
// Do nothing as item spawning is not interested in this case.
257+
// Feature is disabled, so we don't need to do anything.
259258
return;
260259
}
261260

262-
if (!EntityType.ITEM.equals(event.getEntityType())) {
263-
// We are interested only in dropped item entities.
264-
return;
265-
}
261+
// Get the spawn location once.
262+
Location spawnLocation = event.getLocation();
266263

267-
if (!this.addon.inWorld(event.getLocation().getWorld())) {
268-
// Not correct world
264+
// 2. Check if the spawn is happening in a world managed by the addon.
265+
if (!this.addon.inWorld(spawnLocation.getWorld())) {
266+
// Not a relevant world, ignore this event.
269267
return;
270268
}
271269

272-
Entity entity = event.getEntity();
273-
Location location = event.getLocation();
270+
// Find an island at the spawn location.
271+
Optional<Island> optionalIsland = this.addon.getIslands().getIslandAt(spawnLocation)
272+
// Chained to the Optional: Filter the island.
273+
// Only keep it if the block the item spawned in
274+
// is *exactly* the island's center.
275+
.filter(island -> {
276+
// .getBlock().getLocation() converts a precise location
277+
// (e.g., 10.2, 64.5, 12.8) to its block's location (10.0, 64.0, 12.0).
278+
Location blockLocation = spawnLocation.getBlock().getLocation();
279+
return blockLocation.equals(island.getCenter());
280+
});
281+
282+
// If we found an island AND it passed the filter (spawned at center)...
283+
if (optionalIsland.isPresent()) {
284+
// 1. Cancel the original item spawn.
285+
event.setCancelled(true);
274286

275-
Optional<Island> optionalIsland = this.addon.getIslands().getIslandAt(location)
276-
.filter(island -> location.getBlock().getLocation().equals(island.getCenter()));
287+
// 2. Get the island and the item stack that was supposed to spawn.
288+
Island island = optionalIsland.get();
289+
// We use event.getEntity() which is guaranteed to be an Item.
290+
ItemStack itemStack = event.getEntity().getItemStack();
277291

278-
if (optionalIsland.isPresent()) {
279-
// Teleport entity to the top of magic block.
280-
entity.teleport(optionalIsland.get().getCenter().add(0.5, 1, 0.5));
281-
entity.setVelocity(new Vector(0, 0, 0));
292+
// 3. Calculate the new, clean drop location.
293+
// .add(0.5, 1, 0.5) moves it to the center of the block (0.5)
294+
// and one block up (1.0) so it sits on top.
295+
Location newDropLocation = island.getCenter().add(0.5, 1, 0.5);
296+
297+
// 4. Drop the item stack at the new location.
298+
spawnLocation.getWorld().dropItem(newDropLocation, itemStack);
282299
}
283300
}
284301

@@ -607,7 +624,6 @@ private void spawnBlock(@NonNull OneBlockObject nextBlock, @NonNull Block block)
607624

608625
}
609626

610-
@SuppressWarnings("deprecation")
611627
@EventHandler
612628
public void onPlayerInteract(PlayerInteractEvent e) {
613629
if (e.getAction() != Action.RIGHT_CLICK_BLOCK) return;

0 commit comments

Comments
 (0)