Skip to content

Commit

Permalink
Fixed generate event not checking for new blocks set in the event
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed May 5, 2022
1 parent 8ab5bd4 commit 80481d4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
Expand Up @@ -59,6 +59,7 @@
import com.bgsoftware.superiorskyblock.utils.ServerVersion;
import com.bgsoftware.superiorskyblock.utils.debug.PluginDebugger;
import com.bgsoftware.superiorskyblock.utils.events.EventResult;
import com.bgsoftware.superiorskyblock.utils.events.EventsBus;
import com.bgsoftware.superiorskyblock.utils.islands.IslandUtils;
import com.bgsoftware.superiorskyblock.utils.islands.SortingComparators;
import com.bgsoftware.superiorskyblock.utils.islands.SortingTypes;
Expand Down Expand Up @@ -3100,7 +3101,13 @@ public Key generateBlock(Location location, World.Environment environment, boole
}
}

Key generatedBlock = KeyImpl.of(newState);
EventResult<EventsBus.GenerateBlockResult> eventResult = plugin.getEventsBus().callIslandGenerateBlockEvent(
this, location, KeyImpl.of(newState));

if (eventResult.isCancelled())
return null;

Key generatedBlock = eventResult.getResult().getBlock();

PluginDebugger.debug("Action: Generate Block, Island: " + getOwner().getName() + ", Block: " + generatedBlock);

Expand All @@ -3111,13 +3118,8 @@ public Key generateBlock(Location location, World.Environment environment, boole
// If the block is a custom block, and the event was cancelled - we need to call the handleBlockPlace manually.
handleBlockPlace(generatedBlock, 1);

EventResult<Boolean> eventResult = plugin.getEventsBus().callIslandGenerateBlockEvent(this, location, generatedBlock);

if (eventResult.isCancelled())
return null;

// Checking whether the plugin should set the block in the world.
if (eventResult.getResult()) {
if (eventResult.getResult().isPlaceBlock()) {
int combinedId;

try {
Expand Down
Expand Up @@ -430,9 +430,9 @@ public boolean callIslandEnterProtectedEvent(SuperiorPlayer superiorPlayer, Isla
return !islandEnterProtectedEvent.isCancelled();
}

public EventResult<Boolean> callIslandGenerateBlockEvent(Island island, Location location, Key block) {
public EventResult<GenerateBlockResult> callIslandGenerateBlockEvent(Island island, Location location, Key block) {
return callEvent(() -> new IslandGenerateBlockEvent(island, location, block), "islandgenerateblockevent",
true, IslandGenerateBlockEvent::isPlaceBlock);
new GenerateBlockResult(block, true), GenerateBlockResult::new);
}

public boolean callIslandInviteEvent(SuperiorPlayer superiorPlayer, SuperiorPlayer targetPlayer, Island island) {
Expand Down Expand Up @@ -777,4 +777,28 @@ public UpgradeCost getUpgradeCost() {

}

public static final class GenerateBlockResult {

private final Key block;
private final boolean placeBlock;

public GenerateBlockResult(IslandGenerateBlockEvent event) {
this(event.getBlock(), event.isPlaceBlock());
}

public GenerateBlockResult(Key block, boolean placeBlock) {
this.block = block;
this.placeBlock = placeBlock;
}

public Key getBlock() {
return block;
}

public boolean isPlaceBlock() {
return placeBlock;
}

}

}

0 comments on commit 80481d4

Please sign in to comment.