Skip to content
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

Tool Components #7200

Open
wants to merge 9 commits into
base: dev/feature
Choose a base branch
from
57 changes: 8 additions & 49 deletions src/main/java/ch/njol/skript/Skript.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,19 @@
import ch.njol.skript.aliases.Aliases;
import ch.njol.skript.bukkitutil.BurgerHelper;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.classes.data.BukkitClasses;
import ch.njol.skript.classes.data.BukkitEventValues;
import ch.njol.skript.classes.data.DefaultComparators;
import ch.njol.skript.classes.data.DefaultConverters;
import ch.njol.skript.classes.data.DefaultFunctions;
import ch.njol.skript.classes.data.DefaultOperations;
import ch.njol.skript.classes.data.JavaClasses;
import ch.njol.skript.classes.data.SkriptClasses;
import ch.njol.skript.classes.data.*;
import ch.njol.skript.command.Commands;
import ch.njol.skript.doc.Documentation;
import ch.njol.skript.events.EvtSkript;
import ch.njol.skript.hooks.Hook;
import ch.njol.skript.lang.Condition;
import ch.njol.skript.lang.Condition.ConditionType;
import ch.njol.skript.lang.*;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionInfo;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.Section;
import ch.njol.skript.lang.SkriptEvent;
import ch.njol.skript.lang.SkriptEventInfo;
import ch.njol.skript.lang.Statement;
import ch.njol.skript.lang.SyntaxElementInfo;
import ch.njol.skript.lang.Trigger;
import ch.njol.skript.lang.TriggerItem;
import ch.njol.skript.lang.Condition.ConditionType;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.skript.localization.ArgsMessage;
import ch.njol.skript.localization.Language;
import ch.njol.skript.localization.Message;
import ch.njol.skript.localization.PluralizingArgsMessage;
import ch.njol.skript.log.BukkitLoggerFilter;
import ch.njol.skript.log.CountingLogHandler;
import ch.njol.skript.log.ErrorDescLogHandler;
import ch.njol.skript.log.ErrorQuality;
import ch.njol.skript.log.LogEntry;
import ch.njol.skript.log.LogHandler;
import ch.njol.skript.log.SkriptLogger;
import ch.njol.skript.log.Verbosity;
import ch.njol.skript.log.*;
import ch.njol.skript.registrations.Classes;
import ch.njol.skript.registrations.EventValues;
import ch.njol.skript.registrations.Feature;
Expand All @@ -50,13 +25,7 @@
import ch.njol.skript.update.ReleaseStatus;
import ch.njol.skript.update.UpdateManifest;
import ch.njol.skript.util.Date;
import ch.njol.skript.util.EmptyStacktraceException;
import ch.njol.skript.util.ExceptionUtils;
import ch.njol.skript.util.FileUtils;
import ch.njol.skript.util.Getter;
import ch.njol.skript.util.Task;
import ch.njol.skript.util.Utils;
import ch.njol.skript.util.Version;
import ch.njol.skript.util.*;
import ch.njol.skript.util.chat.BungeeConverter;
import ch.njol.skript.util.chat.ChatMessages;
import ch.njol.skript.variables.Variables;
Expand All @@ -66,12 +35,9 @@
import ch.njol.util.StringUtils;
import ch.njol.util.coll.iterator.CheckedIterator;
import ch.njol.util.coll.iterator.EnumerationIterable;

import com.google.common.collect.Lists;
import com.google.gson.Gson;

import org.bstats.bukkit.Metrics;
import org.bstats.charts.SimplePie;
import org.bukkit.*;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
Expand All @@ -96,6 +62,7 @@
import org.skriptlang.skript.bukkit.SkriptMetrics;
import org.skriptlang.skript.bukkit.breeding.BreedingModule;
import org.skriptlang.skript.bukkit.displays.DisplayModule;
import org.skriptlang.skript.bukkit.toolcomponent.ToolComponentModule;
import org.skriptlang.skript.lang.comparator.Comparator;
import org.skriptlang.skript.lang.comparator.Comparators;
import org.skriptlang.skript.lang.converter.Converter;
Expand All @@ -119,17 +86,8 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.*;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicLong;
import java.util.jar.JarEntry;
Expand Down Expand Up @@ -557,6 +515,7 @@ public void onEnable() {
// todo: become proper module once registry api is merged
DisplayModule.load();
BreedingModule.load();
ToolComponentModule.load();
} catch (final Exception e) {
exception(e, "Could not load required .class files: " + e.getLocalizedMessage());
setEnabled(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.skriptlang.skript.bukkit.toolcomponent;

import ch.njol.skript.Skript;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.registrations.Classes;
import org.bukkit.inventory.meta.components.ToolComponent;
import org.bukkit.inventory.meta.components.ToolComponent.ToolRule;

import java.io.IOException;

public class ToolComponentModule {

public static void load() throws IOException {
if (!Skript.classExists("org.bukkit.inventory.meta.components.ToolComponent"))
return;

Skript.getAddonInstance().loadClasses("org.skriptlang.skript.bukkit.toolcomponent", "elements");

Classes.registerClass(new ClassInfo<>(ToolComponent.class, "toolcomponent")
.user("tool ?components?")
.name("Tool Component")
.description("The tool component of an item")
.requiredPlugins("Minecraft 1.20.6+")
.since("INSERT VERSION")
);

Classes.registerClass(new ClassInfo<>(ToolRule.class, "toolrule")
.user("tool ?rules?")
.name("Tool Rule")
.description("The tool rule of a tool component")
.requiredPlugins("Minecraft 1.20.6+")
.since("INSERT VERSION")
);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.skriptlang.skript.bukkit.toolcomponent.elements;

import ch.njol.skript.Skript;
import ch.njol.skript.conditions.base.PropertyCondition;
import ch.njol.skript.doc.*;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
import org.bukkit.inventory.meta.components.ToolComponent.ToolRule;
import org.jetbrains.annotations.Nullable;

@Name("Tool Rule - Drops Enabled")
@Description("If the drops of a tool rule are enabled.")
@Examples({
"set {_rules::*} to the tool rules of {_item}",
"loop {_rules::*}:",
"\tif the tool rule drops of loop-value is enabled:",
"\tremove loop-value from the tool rules of {_item}"
})
@RequiredPlugins("Minecraft 1.20.6")
@Since("INSERT VERSION")
public class CondToolRuleDrops extends PropertyCondition<ToolRule> {

static {
Skript.registerCondition(CondToolRuleDrops.class, ConditionType.PROPERTY,
"[the] tool rule drops of %toolrules% (is|are) enabled",
"[the] tool rule drops of %toolrules% (is|are) disabled"
);
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}

private boolean enable;
private Expression<ToolRule> toolRules;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
enable = matchedPattern == 0;
//noinspection unchecked
toolRules = (Expression<ToolRule>) exprs[0];
setExpr(toolRules);
return true;
}

@Override
public boolean check(ToolRule toolRule) {
return toolRule.isCorrectForDrops() == enable;
}

@Override
protected String getPropertyName() {
return "tool rule drops";
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "the tool rule drops of " + toolRules.toString(event, debug) + " are " + (enable ? "enabled" : "disabled");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.skriptlang.skript.bukkit.toolcomponent.elements;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.*;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
import org.bukkit.inventory.meta.components.ToolComponent.ToolRule;
import org.jetbrains.annotations.Nullable;

@Name("Tool Rule - Drops")
@Description("If the tool with this tool rule should drop items of a block defined from the blocks of this tool rule.")
@Examples({
"set {_rule} to a new tool rule with block types oak log, stone and obsidian",
"set the tool rule speed of {_rule} to 10",
"enable the tool rule drops of {_rule}",
"add {_rule} to the tool rules of {_item}"
})
@RequiredPlugins("Minecraft 1.20.6+")
@Since("INSERT VERSION")
public class EffToolRuleDrops extends Effect {

static {
Skript.registerEffect(EffToolRuleDrops.class,
"enable [the] tool rule drops (of|for) %toolrules%",
"disable [the] tool rule drops (of|for) %toolrules%");
}

private boolean enable;
private Expression<? extends ToolRule> exprToolRule;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
//noinspection unchecked
exprToolRule = (Expression<? extends ToolRule>) exprs[0];
enable = matchedPattern == 0;
return true;
}

@Override
protected void execute(Event event) {
for (ToolRule rule : exprToolRule.getArray(event)) {
rule.setCorrectForDrops(enable);
}
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return (enable ? "enable" : "disable") + " tool rule drops for " + exprToolRule.toString(event, debug);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.skriptlang.skript.bukkit.toolcomponent.elements;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.*;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Kleenean;
import org.bukkit.Material;
import org.bukkit.event.Event;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.components.ToolComponent;
import org.jetbrains.annotations.Nullable;

@Name("New Tool Component")
@Description("Gets a blank tool component.")
@Examples({
"set {_component} to a new blank tool component",
"set the tool component of {_item} to {_component}"
})
@RequiredPlugins("Minecraft 1.20.6+")
@Since("INSERT VERSION")
public class ExprNewToolComp extends SimpleExpression<ToolComponent> {

static {
Skript.registerExpression(ExprNewToolComp.class, ToolComponent.class, ExpressionType.SIMPLE,
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
"a new [blank|empty] tool component");
}

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

@Override
protected ToolComponent @Nullable [] get(Event event) {
return new ToolComponent[]{(new ItemStack(Material.APPLE)).getItemMeta().getTool()};
}

@Override
public boolean isSingle() {
return true;
}

@Override
public Class<ToolComponent> getReturnType() {
return ToolComponent.class;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "a new tool component";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.skriptlang.skript.bukkit.toolcomponent.elements;

import ch.njol.skript.Skript;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.doc.*;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Kleenean;
import org.bukkit.Material;
import org.bukkit.event.Event;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.components.ToolComponent.ToolRule;
import org.jetbrains.annotations.Nullable;

import java.util.List;

@Name("New Tool Rule")
@Description({
"Gets a new tool rule with provided block types.",
"NOTE: A tool rule must have at least one block type or will be considered invalid."
})
@Examples({
"set {_rule} to a new tool rule with block types oak log, stone and obsidian",
"set the tool rule speed of {_rule} to 10",
"enable the tool rule drops of {_rule}",
"add {_rule} to the tool rules of {_item}"
})
@RequiredPlugins("Minecraft 1.20.6+")
@Since("INSERT VERSION")
public class ExprNewToolRule extends SimpleExpression<ToolRule> {

static {
Skript.registerExpression(ExprNewToolRule.class, ToolRule.class, ExpressionType.SIMPLE,
"a new tool rule with block types %itemtypes%");
}

private Expression<ItemType> types;

@Override
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
//noinspection unchecked
types = (Expression<ItemType>) expressions[0];
return true;
}

@Override
protected ToolRule @Nullable [] get(Event event) {
List<Material> materials = types.stream(event).map(ItemType::getMaterial).toList();
return new ToolRule[]{(new ItemStack(Material.APPLE).getItemMeta().getTool().addRule(materials, null, null))};
}

@Override
public boolean isSingle() {
return true;
}

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

@Override
public String toString(@Nullable Event event, boolean debug) {
return null;
}

}
Loading