Skip to content

Commit

Permalink
Implement fill action
Browse files Browse the repository at this point in the history
Signed-off-by: Pablete1234 <[email protected]>
  • Loading branch information
Pablete1234 committed Nov 26, 2022
1 parent 8b4f94b commit 9c8ad73
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
15 changes: 14 additions & 1 deletion core/src/main/java/tc/oc/pgm/action/ActionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.jetbrains.annotations.Nullable;
import tc.oc.pgm.action.actions.ActionNode;
import tc.oc.pgm.action.actions.ExposedAction;
import tc.oc.pgm.action.actions.FillAction;
import tc.oc.pgm.action.actions.KillEntitiesAction;
import tc.oc.pgm.action.actions.MessageAction;
import tc.oc.pgm.action.actions.ReplaceItemAction;
Expand All @@ -29,6 +30,8 @@
import tc.oc.pgm.filters.matcher.StaticFilter;
import tc.oc.pgm.filters.parse.FilterParser;
import tc.oc.pgm.kits.Kit;
import tc.oc.pgm.regions.BlockBoundedValidation;
import tc.oc.pgm.regions.RegionParser;
import tc.oc.pgm.util.MethodParser;
import tc.oc.pgm.util.MethodParsers;
import tc.oc.pgm.util.inventory.ItemMatcher;
Expand All @@ -44,13 +47,15 @@ public class ActionParser {
private final MapFactory factory;
private final FeatureDefinitionContext features;
private final FilterParser filters;
private final RegionParser regions;
private final VariablesModule variables;
private final Map<String, Method> methodParsers;

public ActionParser(MapFactory factory) {
this.factory = factory;
this.features = factory.getFeatures();
this.filters = factory.getFilters();
this.regions = factory.getRegions();
this.variables = factory.needModule(VariablesModule.class);
this.methodParsers = MethodParsers.getMethodParsersForClass(getClass());
}
Expand Down Expand Up @@ -261,7 +266,7 @@ public <T extends Filterable<?>> SetVariableAction<T> parseSetVariable(Element e
@MethodParser("kill-entities")
public KillEntitiesAction parseKillEntities(Element el, Class<?> scope)
throws InvalidXMLException {
return new KillEntitiesAction(factory.getFilters().parseFilterProperty(el, "filter"));
return new KillEntitiesAction(filters.parseProperty(el, "filter"));
}

@MethodParser("replace-item")
Expand All @@ -274,4 +279,12 @@ public ReplaceItemAction parseReplaceItem(Element el, Class<?> scope) throws Inv

return new ReplaceItemAction(matcher, item, keepAmount, keepEnchants);
}

@MethodParser("fill")
public FillAction parseFill(Element el, Class<?> scope) throws InvalidXMLException {
return new FillAction(
regions.parseProperty(Node.fromAttrOrSelf(el, "region"), BlockBoundedValidation.INSTANCE),
XMLUtils.parseMaterialData(Node.fromRequiredAttr(el, "material")),
XMLUtils.parseBoolean(el.getAttribute("events"), false));
}
}
38 changes: 38 additions & 0 deletions core/src/main/java/tc/oc/pgm/action/actions/FillAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package tc.oc.pgm.action.actions;

import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.event.block.BlockFormEvent;
import org.bukkit.material.MaterialData;
import tc.oc.pgm.api.match.Match;
import tc.oc.pgm.api.region.Region;

public class FillAction extends AbstractAction<Match> {

private final Region region;
private final MaterialData materialData;
private final boolean events;

public FillAction(Region region, MaterialData materialData, boolean events) {
super(Match.class);
this.region = region;
this.materialData = materialData;
this.events = events;
}

@Override
public void trigger(Match match) {
for (Block block : region.getBlocks(match.getWorld())) {
BlockState newState = block.getState();
newState.setMaterialData(materialData);

if (events) {
BlockFormEvent event = new BlockFormEvent(block, newState);
match.callEvent(event);
if (event.isCancelled()) continue;
}

newState.update(true, true);
}
}
}

0 comments on commit 9c8ad73

Please sign in to comment.