Skip to content
Permalink
Browse files
Add block_explode event
  • Loading branch information
PseudoKnight committed Aug 22, 2020
1 parent b780a89 commit 2c666b7d976e6bf79928f305a1537b403ab86d69
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 0 deletions.
@@ -26,6 +26,7 @@
import com.laytonsmith.abstraction.events.MCBlockBurnEvent;
import com.laytonsmith.abstraction.events.MCBlockDispenseEvent;
import com.laytonsmith.abstraction.events.MCBlockEvent;
import com.laytonsmith.abstraction.events.MCBlockExplodeEvent;
import com.laytonsmith.abstraction.events.MCBlockFadeEvent;
import com.laytonsmith.abstraction.events.MCBlockFromToEvent;
import com.laytonsmith.abstraction.events.MCBlockGrowEvent;
@@ -46,6 +47,7 @@
import org.bukkit.event.block.BlockBurnEvent;
import org.bukkit.event.block.BlockDispenseEvent;
import org.bukkit.event.block.BlockEvent;
import org.bukkit.event.block.BlockExplodeEvent;
import org.bukkit.event.block.BlockFadeEvent;
import org.bukkit.event.block.BlockFromToEvent;
import org.bukkit.event.block.BlockGrowEvent;
@@ -561,4 +563,51 @@ public Object _GetObject() {
return bfe;
}
}

@abstraction(type = Implementation.Type.BUKKIT)
public static class BukkitMCBlockExplodeEvent implements MCBlockExplodeEvent {

BlockExplodeEvent event;

public BukkitMCBlockExplodeEvent(BlockExplodeEvent event) {
this.event = event;
}

@Override
public List<MCBlock> getBlocks() {
List<MCBlock> ret = new ArrayList<>();
for(Block b : event.blockList()) {
ret.add(new BukkitMCBlock(b));
}
return ret;
}

@Override
public void setBlocks(List<MCBlock> blocks) {
event.blockList().clear();
for(MCBlock b : blocks) {
event.blockList().add((Block) b.getHandle());
}
}

@Override
public float getYield() {
return event.getYield();
}

@Override
public void setYield(float power) {
event.setYield(power);
}

@Override
public MCBlock getBlock() {
return new BukkitMCBlock(event.getBlock());
}

@Override
public Object _GetObject() {
return event;
}
}
}
@@ -17,6 +17,7 @@
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockBurnEvent;
import org.bukkit.event.block.BlockDispenseEvent;
import org.bukkit.event.block.BlockExplodeEvent;
import org.bukkit.event.block.BlockFadeEvent;
import org.bukkit.event.block.BlockFromToEvent;
import org.bukkit.event.block.BlockGrowEvent;
@@ -140,4 +141,10 @@ public void onBlockFade(BlockFadeEvent e) {
BukkitBlockEvents.BukkitMCBlockFadeEvent bfe = new BukkitBlockEvents.BukkitMCBlockFadeEvent(e);
EventUtils.TriggerListener(Driver.BLOCK_FADE, "block_fade", bfe);
}

@EventHandler(priority = EventPriority.LOWEST)
public void onBlockExplode(BlockExplodeEvent e) {
BukkitBlockEvents.BukkitMCBlockExplodeEvent bee = new BukkitBlockEvents.BukkitMCBlockExplodeEvent(e);
EventUtils.TriggerListener(Driver.BLOCK_EXPLODE, "block_explode", bee);
}
}
@@ -0,0 +1,17 @@
package com.laytonsmith.abstraction.events;

import com.laytonsmith.abstraction.blocks.MCBlock;

import java.util.List;

public interface MCBlockExplodeEvent extends MCBlockEvent {

List<MCBlock> getBlocks();

void setBlocks(List<MCBlock> blocks);

float getYield();

void setYield(float power);

}
@@ -18,6 +18,7 @@ public enum Driver {
BLOCK_BREAK,
BLOCK_BURN,
BLOCK_DISPENSE,
BLOCK_EXPLODE,
BLOCK_GROW,
BLOCK_IGNITE,
BLOCK_PLACE,
@@ -3,6 +3,7 @@
import com.laytonsmith.PureUtilities.Common.StringUtils;
import com.laytonsmith.PureUtilities.Version;
import com.laytonsmith.abstraction.MCItemStack;
import com.laytonsmith.abstraction.MCLocation;
import com.laytonsmith.abstraction.StaticLayer;
import com.laytonsmith.abstraction.blocks.MCBlock;
import com.laytonsmith.abstraction.blocks.MCMaterial;
@@ -11,6 +12,7 @@
import com.laytonsmith.abstraction.events.MCBlockBreakEvent;
import com.laytonsmith.abstraction.events.MCBlockBurnEvent;
import com.laytonsmith.abstraction.events.MCBlockDispenseEvent;
import com.laytonsmith.abstraction.events.MCBlockExplodeEvent;
import com.laytonsmith.abstraction.events.MCBlockFadeEvent;
import com.laytonsmith.abstraction.events.MCBlockFromToEvent;
import com.laytonsmith.abstraction.events.MCBlockGrowEvent;
@@ -1433,4 +1435,88 @@ public boolean modifyEvent(String key, Mixed value, BindableEvent event) {
return false;
}
}

@api
public static class block_explode extends AbstractEvent {

@Override
public String getName() {
return "block_explode";
}

@Override
public Driver driver() {
return Driver.BLOCK_EXPLODE;
}

@Override
public String docs() {
return "{}"
+ "Called when a block explodes (like beds in the nether) or a plugin creates an explosion."
+ "{location: The location of the explosion."
+ " | blocks: An array of block locations that will be destroyed by this explosion."
+ " | yield: Decimal percentage of blocks destroyed that will drop items.}"
+ "{blocks | yield}"
+ "{}";
}

@Override
public Version since() {
return MSVersion.V3_3_4;
}

@Override
public boolean matches(Map<String, Mixed> prefilter, BindableEvent e) throws PrefilterNonMatchException {
return e instanceof MCBlockExplodeEvent;
}

@Override
public BindableEvent convert(CArray manualObject, Target t) {
return null;
}

@Override
public Map<String, Mixed> evaluate(BindableEvent e) throws EventException {
if(!(e instanceof MCBlockExplodeEvent)) {
throw new EventException("Cannot convert event to BlockExplodeEvent");
}
MCBlockExplodeEvent event = (MCBlockExplodeEvent) e;
Target t = Target.UNKNOWN;
Map<String, Mixed> ret = evaluate_helper(event);

MCBlock blk = event.getBlock();
ret.put("location", ObjectGenerator.GetGenerator().location(blk.getLocation(), false));
CArray blocks = new CArray(t);
for(MCBlock b : event.getBlocks()) {
blocks.push(ObjectGenerator.GetGenerator().location(b.getLocation(), false), t);
}
ret.put("blocks", blocks);
ret.put("yield", new CDouble(event.getYield(), t));
return ret;
}

@Override
public boolean modifyEvent(String key, Mixed value, BindableEvent event) {
if(event instanceof MCBlockExplodeEvent) {
MCBlockExplodeEvent e = (MCBlockExplodeEvent) event;
if(key.equals("yield")) {
e.setYield((float) ArgumentValidation.getDouble(value, value.getTarget()));
return true;
}
if(key.equals("blocks")) {
if(value.isInstanceOf(CArray.TYPE)) {
CArray ba = (CArray) value;
List<MCBlock> blocks = new ArrayList<>();
for(Mixed m : ba.asList()) {
MCLocation loc = ObjectGenerator.GetGenerator().location(m, null, value.getTarget());
blocks.add(loc.getBlock());
}
e.setBlocks(blocks);
return true;
}
}
}
return false;
}
}
}

0 comments on commit 2c666b7

Please sign in to comment.