Skip to content
Permalink
Browse files

Merge pull request #544 from Pieter12345/master

Add affectedBlocks to piston_retract event
  • Loading branch information
PseudoKnight committed Dec 25, 2019
2 parents c88780c + 3e6eccc commit f1f9cd9344cbd49f43ac8d57c17fb2ae462e6230
@@ -92,6 +92,24 @@ public boolean isSticky() {
return event.isSticky();
}

@Override
public List<MCBlock> getAffectedBlocks() {
List<Block> bukkitBlocks;
if(event instanceof BlockPistonExtendEvent) {
bukkitBlocks = ((BlockPistonExtendEvent) event).getBlocks();
} else if(event instanceof BlockPistonRetractEvent) {
bukkitBlocks = ((BlockPistonRetractEvent) event).getBlocks();
} else {
throw new Error("Unsupported piston event: " + event.getClass().getName());
}

List<MCBlock> blocks = new ArrayList<>(bukkitBlocks.size());
for(Block b : bukkitBlocks) {
blocks.add(new BukkitMCBlock(b));
}
return blocks;
}

@Override
public boolean isCancelled() {
return event.isCancelled();
@@ -113,17 +131,6 @@ public BukkitMCBlockPistonExtendEvent(BlockPistonExtendEvent e) {

event = e;
}

@Override
public List<MCBlock> getPushedBlocks() {
List<MCBlock> blocks = new ArrayList<>();

for(Block b : event.getBlocks()) {
blocks.add(new BukkitMCBlock(b));
}

return blocks;
}
}

@abstraction(type = Implementation.Type.BUKKIT)
@@ -1,11 +1,24 @@
package com.laytonsmith.abstraction.events;

import java.util.List;

import com.laytonsmith.abstraction.blocks.MCBlock;
import com.laytonsmith.abstraction.blocks.MCBlockFace;

public interface MCBlockPistonEvent extends MCBlockEvent {

/**
* Get the direction in which the pushed or pulled blocks are moved.
* @return The direction.
*/
MCBlockFace getDirection();

/**
* Get all blocks that will be moved by the extending or retracting piston.
* @return A {@link List} containing all pushed or pulled blocks.
*/
List<MCBlock> getAffectedBlocks();

boolean isSticky();

boolean isCancelled();
@@ -1,9 +1,4 @@
package com.laytonsmith.abstraction.events;

import com.laytonsmith.abstraction.blocks.MCBlock;
import java.util.List;

public interface MCBlockPistonExtendEvent extends MCBlockPistonEvent {

List<MCBlock> getPushedBlocks();
}
@@ -4,5 +4,11 @@

public interface MCBlockPistonRetractEvent extends MCBlockPistonEvent {

/**
* Get the location of the block that is being pulled by the retracting piston.
* @return The location.
* @deprecated Use {@link #getPulledBlocks()} to obtain all pulled blocks instead.
*/
@Deprecated
MCLocation getRetractedLocation();
}
@@ -16,7 +16,6 @@
import com.laytonsmith.abstraction.events.MCBlockGrowEvent;
import com.laytonsmith.abstraction.events.MCBlockIgniteEvent;
import com.laytonsmith.abstraction.events.MCBlockPistonEvent;
import com.laytonsmith.abstraction.events.MCBlockPistonExtendEvent;
import com.laytonsmith.abstraction.events.MCBlockPistonRetractEvent;
import com.laytonsmith.abstraction.events.MCBlockPlaceEvent;
import com.laytonsmith.abstraction.events.MCNotePlayEvent;
@@ -76,17 +75,29 @@ public boolean modifyEvent(String key, Mixed value, BindableEvent e) {
return false;
}

Map<String, Mixed> evaluate_stub(BindableEvent e) throws EventException {
@Override
public Map<String, Mixed> evaluate(BindableEvent e) throws EventException {
MCBlockPistonEvent event = (MCBlockPistonEvent) e;
Target t = Target.UNKNOWN;
Map<String, Mixed> map = evaluate_helper(event);

MCBlock block = event.getBlock();

map.put("location", ObjectGenerator.GetGenerator().location(block.getLocation(), false));
map.put("location", ObjectGenerator.GetGenerator().location(event.getBlock().getLocation(), false));
map.put("isSticky", CBoolean.get(event.isSticky()));
map.put("direction", new CString(event.getDirection().name(), t));

CArray affected = new CArray(t);
for(MCBlock block : event.getAffectedBlocks()) {
MCMaterial mat = block.getType();
CArray blk = CArray.GetAssociativeArray(t);
blk.set("name", mat.getName(), t);
blk.set("x", new CInt(block.getX(), t), t);
blk.set("y", new CInt(block.getY(), t), t);
blk.set("z", new CInt(block.getZ(), t), t);
blk.set("world", new CString(block.getWorld().getName(), t), t);
affected.push(blk, t);
}
map.put("affectedBlocks", affected);

return map;
}
}
@@ -104,7 +115,7 @@ public String docs() {
return "{} "
+ "This event is called when a piston is extended. Cancelling the event cancels the move."
+ "{location: the locationArray of this piston | direction: direction of travel"
+ " | sticky: true if the piston is sticky | affectedBlocks: blocks pushed}"
+ " | sticky: true if the piston is sticky, false otherwise | affectedBlocks: blocks pushed/pulled}"
+ "{} "
+ "{} "
+ "{}";
@@ -115,27 +126,6 @@ public Driver driver() {
return Driver.PISTON_EXTEND;
}

@Override
public Map<String, Mixed> evaluate(BindableEvent e) throws EventException {
MCBlockPistonExtendEvent event = (MCBlockPistonExtendEvent) e;
Target t = Target.UNKNOWN;
Map<String, Mixed> map = evaluate_stub(e);

CArray affected = new CArray(t);
for(MCBlock block : event.getPushedBlocks()) {
MCMaterial mat = block.getType();
CArray blk = CArray.GetAssociativeArray(t);
blk.set("name", mat.getName(), t);
blk.set("x", new CInt(block.getX(), t), t);
blk.set("y", new CInt(block.getY(), t), t);
blk.set("z", new CInt(block.getZ(), t), t);
blk.set("world", new CString(block.getWorld().getName(), t), t);
affected.push(blk, t);
}
map.put("affectedBlocks", affected);
return map;
}

@Override
public Version since() {
return MSVersion.V3_3_1;
@@ -155,8 +145,9 @@ public String docs() {
return "{} "
+ "This event is called when a piston is retracted. Cancelling the event cancels the move."
+ "{location: the locationArray of this piston | direction: direction of travel"
+ " | sticky: true if the piston is sticky | retractedLocation: if the piston"
+ " is sticky and attached to a block, where the attached block would end up }"
+ " | sticky: true if the piston is sticky, false otherwise | affectedBlocks: blocks pushed/pulled"
+ " | retractedLocation: if the piston is sticky and attached to a block, where the attached"
+ " block would end up }"
+ "{} "
+ "{} "
+ "{}";
@@ -170,7 +161,7 @@ public Driver driver() {
@Override
public Map<String, Mixed> evaluate(BindableEvent e) throws EventException {
MCBlockPistonRetractEvent event = (MCBlockPistonRetractEvent) e;
Map<String, Mixed> map = evaluate_stub(e);
Map<String, Mixed> map = super.evaluate(e);
map.put("retractedLocation", ObjectGenerator.GetGenerator().location(event.getRetractedLocation(), false));
return map;
}

0 comments on commit f1f9cd9

Please sign in to comment.
You can’t perform that action at this time.