Skip to content

Commit

Permalink
Adds an ItemAdder hook to delete any blocks when island is deleted. (#…
Browse files Browse the repository at this point in the history
…2250)

* Adds an ItemAdder hook to delete any blocks when island is deleted.

Also includes a flag for explosions.

* Make the error reporting method non-abstract.

This is not a mandatory method for many hooks.

* Delete this class as it is not used any more and just duplicate.

* Added test class.

* Minor issues resolved.
  • Loading branch information
tastybento authored Dec 28, 2023
1 parent 86d8d14 commit cc5c8aa
Show file tree
Hide file tree
Showing 7 changed files with 364 additions and 158 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,13 @@
<version>RC-36</version>
<scope>provided</scope>
</dependency>
<!-- ItemsAdder -->
<dependency>
<groupId>com.github.LoneDev6</groupId>
<artifactId>api-itemsadder</artifactId>
<version>3.6.1</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/world/bentobox/bentobox/BentoBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.commands.BentoBoxCommand;
import world.bentobox.bentobox.database.DatabaseSetup;
import world.bentobox.bentobox.hooks.ItemsAdderHook;
import world.bentobox.bentobox.hooks.MultiverseCoreHook;
import world.bentobox.bentobox.hooks.MyWorldsHook;
import world.bentobox.bentobox.hooks.SlimefunHook;
Expand Down Expand Up @@ -235,6 +236,9 @@ private void completeSetup(long loadTime) {
// Register Slimefun
hooksManager.registerHook(new SlimefunHook());

// Register ItemsAdder
hooksManager.registerHook(new ItemsAdderHook(this));

// TODO: re-enable after implementation
//hooksManager.registerHook(new DynmapHook());
// TODO: re-enable after rework
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/world/bentobox/bentobox/api/hooks/Hook.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,7 @@ public boolean isPluginAvailable() {
* Returns an explanation that will be sent to the user to tell them why the hook process did not succeed.
* @return the probable causes why the hook process did not succeed.
*/
public abstract String getFailureCause();
public String getFailureCause() {
return "";
}
}
124 changes: 124 additions & 0 deletions src/main/java/world/bentobox/bentobox/hooks/ItemsAdderHook.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package world.bentobox.bentobox.hooks;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.EntityExplodeEvent;

import dev.lone.itemsadder.api.CustomBlock;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.api.flags.FlagListener;
import world.bentobox.bentobox.api.flags.clicklisteners.CycleClick;
import world.bentobox.bentobox.api.hooks.Hook;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.managers.RanksManager;

/**
* Hook to enable itemsadder blocks to be deleted when islands are deleted.
* It also includes a flag to track explosion access
*/
public class ItemsAdderHook extends Hook {

/**
* This flag allows to switch which island member group can use explosive items from Items Adder.
*/
public static final Flag ITEMS_ADDER_EXPLOSIONS =
new Flag.Builder("ITEMS_ADDER_EXPLOSIONS", Material.TNT).
type(Flag.Type.PROTECTION).
defaultRank(RanksManager.MEMBER_RANK).
clickHandler(new CycleClick("ITEMS_ADDER_EXPLOSIONS",
RanksManager.VISITOR_RANK, RanksManager.OWNER_RANK))
.
build();

private BentoBox plugin;

private BlockInteractListener listener;

/**
* Register the hook
* @param plugin BentoBox
*/
public ItemsAdderHook(BentoBox plugin) {
super("ItemsAdder", Material.NETHER_STAR);
this.plugin = plugin;
}

@Override
public boolean hook() {
// See if ItemsAdder is around
if (Bukkit.getPluginManager().getPlugin("ItemsAdder") == null) {
return false;
}
// Register listener
listener = new BlockInteractListener();
Bukkit.getPluginManager().registerEvents(listener, plugin);
plugin.getFlagsManager().registerFlag(ITEMS_ADDER_EXPLOSIONS);
return true;
}

/**
* @return the listener
*/
protected BlockInteractListener getListener() {
return listener;
}

/**
* Remove the CustomBlock at location
* @param location
*/
public void clearBlockInfo(Location location) {
CustomBlock.remove(location);
}

class BlockInteractListener extends FlagListener {

/**
* Handles explosions of ItemAdder items
* @param event explosion event
*/
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onExplosion(EntityExplodeEvent event)
{
if (!EntityType.PLAYER.equals(event.getEntityType())) {
// Ignore non-player explosions.
return;
}

Player player = (Player) event.getEntity();

if (!player.hasPermission("XXXXXX")) {
// Ignore players that does not have magic XXXXXX permission.
return;
}

// Use BentoBox flag processing system to validate usage.
// Technically not necessary as internally it should be cancelled by BentoBox.

if (!this.checkIsland(event, player, event.getLocation(), ITEMS_ADDER_EXPLOSIONS)) {
// Remove any blocks from the explosion list if required
event.blockList().removeIf(block -> this.protect(player, block.getLocation()));
event.setCancelled(this.protect(player, event.getLocation()));
}
}


/**
* This method returns if the protection in given location is enabled or not.
* @param player Player who triggers explosion.
* @param location Location where explosion happens.
* @return {@code true} if location is protected, {@code false} otherwise.
*/
private boolean protect(Player player, Location location)
{
return plugin.getIslands().getProtectedIslandAt(location)
.map(island -> !island.isAllowed(User.getInstance(player), ITEMS_ADDER_EXPLOSIONS)).orElse(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.database.objects.IslandDeletion;
import world.bentobox.bentobox.hooks.ItemsAdderHook;
import world.bentobox.bentobox.hooks.SlimefunHook;
import world.bentobox.bentobox.util.MyBiomeGrid;

Expand Down Expand Up @@ -194,10 +195,12 @@ private void copyChunkDataToChunk(Chunk toChunk, Chunk fromChunk, BoundingBox li
if (x % 4 == 0 && y % 4 == 0 && z % 4 == 0) {
toChunk.getBlock(x, y, z).setBiome(fromChunk.getBlock(x, y, z).getBiome());
}
// Delete any slimefun blocks
// Delete any 3rd party blocks
Location loc = new Location(toChunk.getWorld(), baseX + x, y, baseZ + z);
plugin.getHooks().getHook("Slimefun")
.ifPresent(sf -> ((SlimefunHook) sf).clearBlockInfo(loc, true));
.ifPresent(hook -> ((SlimefunHook) hook).clearBlockInfo(loc, true));
plugin.getHooks().getHook("ItemsAdder")
.ifPresent(hook -> ((ItemsAdderHook) hook).clearBlockInfo(loc));
}
}
}
Expand Down Expand Up @@ -377,10 +380,12 @@ private void copyChunkDataToChunk(Chunk chunk, ChunkGenerator.ChunkData chunkDat
if (x % 4 == 0 && y % 4 == 0 && z % 4 == 0) {
chunk.getBlock(x, y, z).setBiome(biomeGrid.getBiome(x, y, z));
}
// Delete any slimefun blocks
// Delete any 3rd party blocks
Location loc = new Location(chunk.getWorld(), baseX + x, y, baseZ + z);
plugin.getHooks().getHook("Slimefun")
.ifPresent(sf -> ((SlimefunHook) sf).clearBlockInfo(loc, true));
plugin.getHooks().getHook("ItemsAdder")
.ifPresent(hook -> ((ItemsAdderHook) hook).clearBlockInfo(loc));
}
}
}
Expand Down
154 changes: 0 additions & 154 deletions src/main/java/world/bentobox/bentobox/nms/SimpleWorldRegenerator.java

This file was deleted.

Loading

0 comments on commit cc5c8aa

Please sign in to comment.