Skip to content

Commit

Permalink
Merge pull request #85 from Th3Shadowbroker/fix/itemsadder
Browse files Browse the repository at this point in the history
Implemented fix to ensure custom block deletion when a block is mined
  • Loading branch information
Th3Shadowbroker authored Nov 27, 2022
2 parents f4f668b + 7132adb commit fb69c82
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>dev.th3shadowbroker.spigot</groupId>
<artifactId>OuroborosMines</artifactId>
<version>1.11.2-SNAPSHOT</version>
<version>1.11.3-SNAPSHOT</version>

<properties>
<plugin.name>${project.artifactId}</plugin.name>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2022 Jens Fischer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package dev.th3shadowbroker.ouroboros.mines.events.thirdparty.itemsadder;

import dev.th3shadowbroker.ouroboros.mines.util.MaterialIdentifier;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.bukkit.Location;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;

@AllArgsConstructor
public class PlaceCustomBlockEvent extends Event {

@Getter
private final Location location;

@Getter
private final MaterialIdentifier materialIdentifier;

@Getter
private static final HandlerList handlerList = new HandlerList();

@Override
public HandlerList getHandlers() {
return handlerList;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2022 Jens Fischer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package dev.th3shadowbroker.ouroboros.mines.events.thirdparty.itemsadder;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.bukkit.block.Block;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;

@AllArgsConstructor
public class RemoveCustomBlockEvent extends Event {

@Getter
private final Block block;

@Getter
private static final HandlerList handlerList = new HandlerList();

@Override
public HandlerList getHandlers() {
return handlerList;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import dev.th3shadowbroker.ouroboros.mines.events.DepositDiscoveredEvent;
import dev.th3shadowbroker.ouroboros.mines.events.MaterialMinedEvent;
import dev.th3shadowbroker.ouroboros.mines.events.RegionCheckEvent;
import dev.th3shadowbroker.ouroboros.mines.events.thirdparty.itemsadder.RemoveCustomBlockEvent;
import dev.th3shadowbroker.ouroboros.mines.regions.MiningRegion;
import dev.th3shadowbroker.ouroboros.mines.util.*;
import org.bukkit.*;
Expand Down Expand Up @@ -106,7 +107,8 @@ public void onBlockBreak(BlockBreakEvent event) {
//If richness was never set or hit 0
if (!plugin.getTaskManager().hasPendingReplacementTask(event.getBlock())) {
long cooldown = minedMaterial.get().getCooldown();
new ReplacementTask(event.getBlock().getLocation(), event.getBlock().getType(), cooldown);
new ReplacementTask(event.getBlock().getLocation(), event.getBlock().getType(), cooldown)
.withMaterialIdentifier(minedMaterial.get().getMaterialIdentifier());

// Bamboo or Sugar cane?
if (BlockUtils.isStackable(event.getBlock())) {
Expand Down Expand Up @@ -181,7 +183,10 @@ private void breakNaturally(BlockBreakEvent event, MineableMaterial mineableMate
event.setDropItems(false);

Location blockLocation = event.getBlock().getLocation();

removeCustomBlockClaim(event.getBlock());
blockLocation.getBlock().setType(Material.AIR);

customDefaultDrops.get().forEach(
itemStack -> {
blockLocation.getWorld().dropItemNaturally(blockLocation, itemStack);
Expand Down Expand Up @@ -225,4 +230,8 @@ private boolean performRegionCheck(Player player, MiningRegion miningRegion, Blo
return !regionCheckEvent.isCancelled();
}

private void removeCustomBlockClaim(Block block) {
Bukkit.getServer().getPluginManager().callEvent(new RemoveCustomBlockEvent(block));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import dev.th3shadowbroker.ouroboros.mines.OuroborosMines;
import dev.th3shadowbroker.ouroboros.mines.events.DefaultDropsCheckEvent;
import dev.th3shadowbroker.ouroboros.mines.events.MaterialCheckEvent;
import dev.th3shadowbroker.ouroboros.mines.events.thirdparty.itemsadder.PlaceCustomBlockEvent;
import dev.th3shadowbroker.ouroboros.mines.events.thirdparty.itemsadder.RemoveCustomBlockEvent;
import dev.th3shadowbroker.ouroboros.mines.util.MaterialIdentifier;
import dev.th3shadowbroker.ouroboros.mines.util.MineableMaterial;
import org.bukkit.Bukkit;
Expand Down Expand Up @@ -55,6 +57,19 @@ public void onDefaultDropsCheck(DefaultDropsCheckEvent event) {
}
}

@EventHandler
public void onRemoveCustomBlock(RemoveCustomBlockEvent event) {
Optional.ofNullable(CustomBlock.byAlreadyPlaced(event.getBlock())).ifPresent(CustomBlock::remove);
}

@EventHandler
public void onPlaceCustomBlock(PlaceCustomBlockEvent event) {
Optional.ofNullable(CustomBlock.getInstance(event.getMaterialIdentifier().toString()))
.ifPresent(cb -> {
cb.place(event.getLocation());
});
}

private boolean isCustomBlock(MineableMaterial mineableMaterial, Block block) {
MaterialIdentifier namespacedId = mineableMaterial.getMaterialIdentifier();
if (!namespacedId.isInDefaultNamespace()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,20 @@
package dev.th3shadowbroker.ouroboros.mines.util;

import dev.th3shadowbroker.ouroboros.mines.OuroborosMines;
import dev.th3shadowbroker.ouroboros.mines.events.thirdparty.itemsadder.PlaceCustomBlockEvent;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.BlockData;
import org.bukkit.event.EventException;
import org.bukkit.plugin.RegisteredListener;
import org.bukkit.scheduler.BukkitTask;

import java.util.Arrays;
import java.util.Optional;

public class ReplacementTask implements Runnable {

private final BukkitTask task;
Expand All @@ -39,6 +46,8 @@ public class ReplacementTask implements Runnable {

private final boolean isAttachableBlock;

private MaterialIdentifier materialIdentifier;

private final OuroborosMines plugin = OuroborosMines.INSTANCE;

public ReplacementTask(Location blockLocation, Material material, long cooldownSeconds) {
Expand Down Expand Up @@ -77,8 +86,21 @@ public void run() {
retryLater();
}
} else {
location.getBlock().setType(minedMaterial);
location.getBlock().setBlockData(blockData);
var identifier = Optional.ofNullable(materialIdentifier);
if (identifier.isEmpty() || identifier.get().isInDefaultNamespace()) {
location.getBlock().setType(minedMaterial);
location.getBlock().setBlockData(blockData);
} else {
// Call has to be forced to guarantee execution after /stop command
PlaceCustomBlockEvent pcbe = new PlaceCustomBlockEvent(location, identifier.get());
Arrays.stream(PlaceCustomBlockEvent.getHandlerList().getRegisteredListeners()).forEach(rl -> {
try {
rl.callEvent(pcbe);
} catch (EventException e) {
throw new RuntimeException(e);
}
});
}
}
} else {
retryLater();
Expand Down Expand Up @@ -109,4 +131,10 @@ public boolean isAttachableBlock() {
public Block getBlock() {
return location.getBlock();
}

public ReplacementTask withMaterialIdentifier(MaterialIdentifier materialIdentifier) {
this.materialIdentifier = materialIdentifier;
return this;
}

}

0 comments on commit fb69c82

Please sign in to comment.