Skip to content

Commit

Permalink
Suggested changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Asleeepp committed Dec 28, 2024
1 parent 1dd5af1 commit 6dd9152
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 78 deletions.
63 changes: 19 additions & 44 deletions src/main/java/ch/njol/skript/expressions/ExprBrushableItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
import ch.njol.skript.classes.Changer;
import ch.njol.skript.classes.Changer.ChangeMode;
import ch.njol.skript.doc.*;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Kleenean;
import ch.njol.skript.expressions.base.SimplePropertyExpression;
import org.bukkit.block.Block;
import org.bukkit.block.BrushableBlock;
import org.bukkit.block.BlockState;
Expand All @@ -27,50 +23,24 @@
})
@Since("INSERT VERSION")
@RequiredPlugins("Minecraft 1.20+")
public class ExprBrushableItem extends SimpleExpression<ItemStack> {
public class ExprBrushableItem extends SimplePropertyExpression<Block, ItemStack> {

private static final boolean SUPPORTS_DUSTING = Skript.classExists("org.bukkit.block.BrushableBlock");

static {
if (SUPPORTS_DUSTING)
Skript.registerExpression(ExprBrushableItem.class, ItemStack.class, ExpressionType.SIMPLE,
"[the] (brushable|buried) item of %blocks%",
"%blocks%'[s] (brushable|buried) item");
register(ExprBrushableItem.class, ItemStack.class,
"(brushable|buried) item",
"blocks");
}

private Expression<Block> blocks;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
blocks = (Expression<Block>) exprs[0];
return true;
}

@Nullable
@Override
protected ItemStack[] get(Event event) {
Block[] blockArray = blocks.getArray(event);
ItemStack[] items = new ItemStack[blockArray.length];
for (int i = 0; i < blockArray.length; i++) {
Block block = blockArray[i];
BlockState state = block.getState();
if (state instanceof BrushableBlock brushableBlock) {
items[i] = brushableBlock.getItem();
} else {
items[i] = null;
}
public @Nullable ItemStack convert(Block block) {
BlockState state = block.getState();
if (state instanceof BrushableBlock brushableBlock) {
return brushableBlock.getItem();
}
return items;
}

@Override
public boolean isSingle() {
return blocks.isSingle();
}

@Override
public Class<? extends ItemStack> getReturnType() {
return ItemStack.class;
return null;
}

@Override
Expand All @@ -83,9 +53,9 @@ public Class<?>[] acceptChange(ChangeMode mode) {

@Override
public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
if (mode == Changer.ChangeMode.SET && delta.length > 0) {
if (mode == Changer.ChangeMode.SET && delta != null && delta.length > 0) {
ItemStack newItem = (ItemStack) delta[0];
for (Block block : blocks.getArray(event)) {
for (Block block : getExpr().getArray(event)) {
BlockState state = block.getState();
if (state instanceof BrushableBlock brushableBlock) {
brushableBlock.setItem(newItem);
Expand All @@ -96,8 +66,13 @@ public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return blocks.toString(event, debug) + "'s brushable item";
public Class<? extends ItemStack> getReturnType() {
return ItemStack.class;
}

@Override
protected String getPropertyName() {
return "brushable item";
}

}
64 changes: 30 additions & 34 deletions src/main/java/ch/njol/skript/expressions/ExprDustedStage.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ch.njol.skript.classes.Changer;
import ch.njol.skript.classes.Changer.ChangeMode;
import ch.njol.skript.doc.*;
import ch.njol.skript.expressions.base.PropertyExpression;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
Expand All @@ -25,41 +26,56 @@
})
@Since("INSERT VERSION")
@RequiredPlugins("Minecraft 1.20+")
public class ExprDustedStage extends SimpleExpression<Integer> {
public class ExprDustedStage extends PropertyExpression<Block, Integer> {

private static final boolean SUPPORTS_DUSTING = Skript.classExists("org.bukkit.block.data.Brushable");

static {
if (SUPPORTS_DUSTING)
Skript.registerExpression(ExprDustedStage.class, Integer.class, ExpressionType.SIMPLE,
"[the] [:max[imum]] dust[ed|ing] (value|stage|progress[ion]) of %blocks%",
"%blocks%'[s] [:max[imum]] dust[ed|ing] (value|stage|progress[ion])");
register(ExprDustedStage.class, Integer.class,
"[:max[imum]] dust[ed|ing] (value|stage|progress[ion])",
"blocks");
}

private Expression<Block> blocks;
private boolean isMax;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
blocks = (Expression<Block>) exprs[0];
setExpr((Expression<Block>) exprs[0]);
isMax = parseResult.hasTag("max");
return true;
}

@Override
protected Integer @Nullable [] get(Event event) {
for (Block block : blocks.getArray(event)) {
protected Integer[] get(Event event, Block[] source) {
return get(source, block -> {
if (block != null && block.getBlockData() instanceof Brushable brushable) {
Brushable brushableBlock = (Brushable) block.getBlockData();
return new Integer[]{isMax ? brushableBlock.getMaximumDusted() : brushableBlock.getDusted()};
return isMax ? brushable.getMaximumDusted() : brushable.getDusted();
}
return null;
});
}

@Override
public @Nullable Class<?>[] acceptChange(ChangeMode mode) {
if (!isMax && mode == Changer.ChangeMode.SET) {
return new Class[]{Integer.class};
}
return new Integer[0];
return null;
}

@Override
public boolean isSingle() {
return blocks.isSingle();
public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
if (isMax || mode != Changer.ChangeMode.SET || delta == null || delta.length == 0)
return;

int value = (Integer) delta[0];
for (Block block : getExpr().getArray(event)) {
if (block != null && block.getBlockData() instanceof Brushable brushable) {
brushable.setDusted(value);
block.setBlockData(brushable);
}
}
}

@Override
Expand All @@ -69,27 +85,7 @@ public Class<? extends Integer> getReturnType() {

@Override
public String toString(@Nullable Event event, boolean debug) {
return (isMax ? "maximum " : "") + blocks.toString(event, debug) + " dusting progression";
return (isMax ? "maximum " : "") + getExpr().toString(event, debug) + "'s dusted stage";
}

@Override
public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
if (mode == Changer.ChangeMode.SET && delta.length > 0) {
for (Block block : blocks.getArray(event)) {
if (block != null && block.getBlockData() instanceof Brushable brushable) {
Brushable brushableBlock = (Brushable) block.getBlockData();
brushableBlock.setDusted(((Integer) delta[0]).intValue());
block.setBlockData(brushableBlock);
}
}
}
}

@Override
public @Nullable Class<?>[] acceptChange(ChangeMode mode) {
if (mode == Changer.ChangeMode.SET) {
return new Class[]{Integer.class};
}
return null;
}
}

0 comments on commit 6dd9152

Please sign in to comment.