8000 Implement fill action by Pablete1234 · Pull Request #1103 · PGMDev/PGM · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Implement fill action #1103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 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,13 @@ 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")),
filters.parseProperty(Node.fromAttr(el, "filter")),
XMLUtils.parseBoolean(el.getAttribute("events"), false));
}
}
46 changes: 46 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,46 @@
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 org.jetbrains.annotations.Nullable;
import tc.oc.pgm.api.filter.Filter;
import tc.oc.pgm.api.match.Match;
import tc.oc.pgm.api.region.Region;
import tc.oc.pgm.filters.query.BlockQuery;

public class FillAction extends AbstractAction<Match> {

private final Region region;
private final MaterialData materialData;
private final @Nullable Filter filter;
private final boolean events;

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

@Override
public void trigger(Match match) {
for (Block block : region.getBlocks(match.getWorld())) {
if (filter != null && filter.query(new BlockQuery(block)).isDenied()) continue;

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