Skip to content

Commit

Permalink
requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sovdeeth committed Dec 15, 2024
1 parent 6cfe138 commit 0607cd7
Show file tree
Hide file tree
Showing 19 changed files with 70 additions and 66 deletions.
2 changes: 1 addition & 1 deletion src/main/java/ch/njol/skript/aliases/AliasesParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ protected Map<String, Variation> parseKeyVariations(String name) {
int insertPoint = -1;

/**
* Tags by their names. All variations can add and overwrite them.
* TagRegistry by their names. All variations can add and overwrite them.
*/
Map<String, Object> tags = new HashMap<>();

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/ch/njol/skript/aliases/AliasesProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class AliasesProvider {
private final Set<Material> materials;

/**
* Tags are in JSON format. We may need GSON when merging tags
* TagRegistry are in JSON format. We may need GSON when merging tags
* (which might be done if variations are used).
*/
private final Gson gson;
Expand Down Expand Up @@ -89,7 +89,7 @@ public Map<String, Object> parseMojangson(String raw) {
/**
* Applies given tags to an item stack.
* @param stack Item stack.
* @param tags Tags.
* @param tags TagRegistry.
* @return Additional flags for the item.
*/
public int applyTags(ItemStack stack, Map<String, Object> tags) {
Expand Down Expand Up @@ -127,7 +127,7 @@ public int applyTags(ItemStack stack, Map<String, Object> tags) {
* Adds an alias to this provider.
* @param name Name of alias without any patterns or variation blocks.
* @param id Id of material.
* @param tags Tags for material.
* @param tags TagRegistry for material.
* @param blockStates Block states.
*/
public void addAlias(AliasName name, String id, @Nullable Map<String, Object> tags, Map<String, String> blockStates) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/ch/njol/skript/aliases/ItemData.java
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ public void deserialize(Fields fields) throws StreamCorruptedException, NotSeria

/**
* Creates a plain copy of this ItemData. It will have same material,
* amount of 1 and same block values. Tags will also be copied, with
* amount of 1 and same block values. TagRegistry will also be copied, with
* following exceptions:
* <ul>
* <li>Damage: 1.13 tag-damage is only used for actual durability.
Expand Down Expand Up @@ -635,7 +635,7 @@ public ItemData aliasCopy() {

/**
* Applies tags to this item.
* @param tags Tags in Mojang's JSON format.
* @param tags TagRegistry in Mojang's JSON format.
*/
public void applyTags(String tags) {
if (stack == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import ch.njol.util.Kleenean;
import ch.njol.util.coll.CollectionUtils;

@Name("Scoreboard Tags")
@Name("Scoreboard TagRegistry")
@Description({"Scoreboard tags are simple list of texts stored directly in the data of an <a href='classes.html#entity'>entity</a>.",
"So this is a Minecraft related thing, not Bukkit, so the tags will not get removed when the server stops. " +
"You can visit <a href='https://minecraft.wiki/w/Scoreboard#Tags'>visit Minecraft Wiki</a> for more info.",
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ch/njol/skript/util/chat/ChatMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ else if (c2 == '>')
} else {
name = tag;
}
name = name.toLowerCase(Locale.ENGLISH); // Tags are case-insensitive
name = name.toLowerCase(Locale.ENGLISH); // TagRegistry are case-insensitive

boolean tryHex = Utils.HEX_SUPPORTED && name.startsWith("#");
ChatColor chatColor = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@
import java.util.HashSet;
import java.util.Set;

/**
* Represents a custom tag created by the Skript user.
* Implementation of {@link Tag} with a custom set of contents.
* @param <T> The type of the contents.
*/
public class SkriptTag<T extends Keyed> implements Tag<T> {

private final Set<T> contents;
private final NamespacedKey key;

public SkriptTag(NamespacedKey key, Collection<T> contents) {
this.contents = new HashSet<>(contents);
this.key = key;
this.contents = new HashSet<>(contents);
}

@Override
Expand Down
14 changes: 5 additions & 9 deletions src/main/java/org/skriptlang/skript/bukkit/tags/TagModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.lang.comparator.Comparators;
import org.skriptlang.skript.lang.comparator.Relation;
import org.skriptlang.skript.lang.converter.Converter;
import org.skriptlang.skript.lang.converter.Converters;

import java.io.IOException;

Expand All @@ -30,15 +28,13 @@ public class TagModule {
public static final boolean PAPER_TAGS_EXIST = Skript.classExists("com.destroystokyo.paper.MaterialTags");

// tag object
public static Tags TAGS;
public static TagRegistry TagRegistry;

public static void load() throws IOException {
// abort if no class exists
if (!Skript.classExists("org.bukkit.Tag"))
return;

// load classes (todo: replace with registering methods after regitration api
Skript.getAddonInstance().loadClasses("org.skriptlang.skript.bukkit", "tags");

// Classes
Classes.registerClass(new ClassInfo<>(Tag.class, "minecrafttag")
Expand All @@ -63,14 +59,14 @@ public String toVariableNameString(Tag<?> tag) {
}
}));

// load classes (todo: replace with registering methods after registration api
Skript.getAddonInstance().loadClasses("org.skriptlang.skript.bukkit", "tags");

// compare tags by keys, not by object instance.
Comparators.registerComparator(Tag.class, Tag.class, (a, b) -> Relation.get(a.getKey().equals(b.getKey())));

// converter to String
Converters.registerConverter(Tag.class, String.class, tag -> tag.getKey().toString(), Converter.NO_LEFT_CHAINING);

// init tags
TAGS = new Tags();
TagRegistry = new TagRegistry();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
/**
* A class in charge of storing and handling all the tags Skript can access.
*/
public class Tags {
public class TagRegistry {

private final TagSourceMap tagSourceMap = new TagSourceMap();

/**
* Each new instance will create a new set of tag sources, in an effort to be reload safe.
*/
public Tags() {
TagRegistry() {
tagSourceMap.put(TagType.ITEMS, new BukkitTagSource<>("items", TagType.ITEMS));
tagSourceMap.put(TagType.BLOCKS, new BukkitTagSource<>("blocks", TagType.BLOCKS));
tagSourceMap.put(TagType.ENTITIES, new BukkitTagSource<>("entity_types", TagType.ENTITIES));
Expand Down Expand Up @@ -72,15 +72,15 @@ public Tags() {
* @param origin The origin to filter by.
* @param typeClass The class the tags should be applicable to.
* @param types Tag types to check with. Leaving this empty will check all tag types.
* @return Tags from the given origin and types that apply to the given class.
* @return TagRegistry from the given origin and types that apply to the given class.
* @param <T> see typeClass.
*/
public <T extends Keyed> Iterable<Tag<T>> getTags(TagOrigin origin, Class<T> typeClass, TagType<?>... types) {
List<Iterator<Tag<T>>> tagIterators = new ArrayList<>();
if (types == null)
types = tagSourceMap.map.keys().toArray(new TagType[0]);
for (TagType<?> type : types) {
if (type.type() == typeClass) {
if (typeClass.isAssignableFrom(type.type())) {
//noinspection unchecked
Iterator<Tag<T>> iterator = getTags(origin, (TagType<T>) type).iterator();
if (iterator.hasNext())
Expand All @@ -99,7 +99,7 @@ public <T extends Keyed> Iterable<Tag<T>> getTags(TagOrigin origin, Class<T> typ
* Gets all the tags of a specific origin that are of a specific type.
* @param origin The origin to filter by.
* @param type The type of tags to get.
* @return Tags from the given origin that are of the given type.
* @return TagRegistry from the given origin that are of the given type.
* @param <T> The class these tags apply to.
*/
public <T extends Keyed> Iterable<Tag<T>> getTags(TagOrigin origin, TagType<T> type) {
Expand All @@ -114,21 +114,16 @@ public <T extends Keyed> Iterable<Tag<T>> getTags(TagOrigin origin, TagType<T> t
return new Iterator<>() {
//<editor-fold desc="iterator over tagSources, returning each individual tag">
private Iterator<Tag<T>> currentTagIter = tagSources.next().getAllTags().iterator();
private Iterator<Tag<T>> nextTagIter;

@Override
public boolean hasNext() {
// does the current source have more tags
if (currentTagIter.hasNext())
return true;
// is there another source in the pipeline? if so, check it.
if (nextTagIter != null)
return nextTagIter.hasNext();
// if there's no known next source, check if have one.
// if we do, mark it as the next source.
// if we have another tag source, mark it as the next source.
if (tagSources.hasNext()) {
nextTagIter = tagSources.next().getAllTags().iterator();
return nextTagIter.hasNext();
currentTagIter = tagSources.next().getAllTags().iterator();
return currentTagIter.hasNext();
}
return false;
}
Expand All @@ -138,12 +133,6 @@ public Tag<T> next() {
// if current source has more, get more.
if (currentTagIter.hasNext())
return currentTagIter.next();
// if current source is dry, switch to using the next source
if (nextTagIter != null && nextTagIter.hasNext()) {
currentTagIter = nextTagIter;
nextTagIter = null;
return currentTagIter.next();
}
throw new IllegalStateException("Called next without calling hasNext to set the next tag iterator.");
}
//</editor-fold>
Expand All @@ -158,7 +147,7 @@ public Tag<T> next() {
* @param origin The origin to filter by.
* @param type The type of tags to get.
* @param predicate A predicate to filter the tags with.
* @return Tags from the given origin that are of the given type and that pass the filter.
* @return TagRegistry from the given origin that are of the given type and that pass the filter.
* @param <T> The class these tags apply to.
*/
public <T extends Keyed> Iterable<Tag<T>> getMatchingTags(TagOrigin origin, TagType<T> type, Predicate<Tag<T>> predicate) {
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/org/skriptlang/skript/bukkit/tags/TagType.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ public class TagType<T extends Keyed> {
* @param type The class this tag type applies to.
*/
public TagType(String pattern, Class<T> type) {
this.pattern = pattern;
this.type = type;
this.toString = pattern;
this(pattern, pattern, type);
}

/**
Expand Down Expand Up @@ -77,7 +75,7 @@ public String toString() {
* Adds types to the registered tag types.
* @param type The types to add.
*/
private static void addType(TagType<?>... type) {
public static void addType(TagType<?>... type) {
REGISTERED_TAG_TYPES.addAll(List.of(type));
}

Expand Down Expand Up @@ -120,6 +118,7 @@ private static void addType(TagType<?>... type) {
public static @NotNull String getFullPattern() {
return getFullPattern(false);
}

/**
* @param required whether the choice should be optional or required.
* @return Returns a choice pattern for use in Skript patterns. Contains parse marks.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public boolean check(Event event) {
return elements.check(event, element -> {
boolean isAny = (element instanceof ItemType itemType && !itemType.isAll());
Keyed[] values = TagModule.getKeyed(element);
if (values == null)
return false;
if (values == null)
return false;

for (Tag<Keyed> tag : tags) {
if (isTagged(tag, values, !isAny)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"include the characters A to Z, 0 to 9, and '/', '.', '_', and '-'. Otherwise, the tag will not register.",
"",
"Please note that two tags can share a name if they are of different types. Registering a new tag of the same " +
"name and type will overwrite the existing tag. Tags will reset on server shutdown."
"name and type will overwrite the existing tag. TagRegistry will reset on server shutdown."
})
@Examples({
"register a new custom entity tag named \"fish\" using cod, salmon, tropical fish, and pufferfish",
Expand All @@ -61,7 +61,7 @@ public class EffRegisterTag extends Effect {

static {
Skript.registerEffect(EffRegisterTag.class,
"register [a[n]] [new] [custom] " + TagType.getFullPattern(true) +
"register [a[n]] [custom] " + TagType.getFullPattern(true) +
" tag named %string% (containing|using) %entitydatas/itemtypes%");
}

Expand All @@ -74,9 +74,7 @@ public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean is
//noinspection unchecked
name = (Expression<String>) expressions[0];
if (name instanceof Literal<String> literal) {
String key = literal.getSingle();
if (key.startsWith("skript:"))
key = key.substring(7);
String key = removeSkriptNamespace(literal.getSingle());
if (!KEY_PATTERN.matcher(key).matches()) {
Skript.error("Tag names can only contain the following characters: 'a-z', '0-9', '/', '.', '_', and '-'.");
return false;
Expand All @@ -94,8 +92,7 @@ protected void execute(Event event) {
if (name == null)
return;

if (name.startsWith("skript:"))
name = name.substring(7);
name = removeSkriptNamespace(name);

if (!KEY_PATTERN.matcher(name).matches())
return;
Expand All @@ -121,6 +118,12 @@ protected void execute(Event event) {
}
}

private static @NotNull String removeSkriptNamespace(@NotNull String key) {
if (key.startsWith("skript:"))
key = key.substring(7);
return key;
}

@Contract("_, _ -> new")
private @NotNull Tag<Material> getMaterialTag(NamespacedKey key, Object @NotNull [] contents) {
ThreadLocalRandom random = ThreadLocalRandom.current();
Expand All @@ -132,7 +135,8 @@ protected void execute(Event event) {
tagContents.add((Material) values[random.nextInt(0, values.length)]);
} else {
for (Keyed value : values) {
tagContents.add((Material) value);
if (value instanceof Material material)
tagContents.add(material);
}
}
}
Expand All @@ -144,7 +148,8 @@ protected void execute(Event event) {
List<EntityType> tagContents = new ArrayList<>();
for (Object object : contents) {
for (Keyed value : TagModule.getKeyed(object)) {
tagContents.add((EntityType) value);
if (value instanceof EntityType entityType)
tagContents.add(entityType);
}
}
return new SkriptTag<>(key, tagContents);
Expand All @@ -155,4 +160,5 @@ public String toString(@Nullable Event event, boolean debug) {
return "register a new " + type.toString() + " tag named " + name.toString(event, debug) + " containing " +
contents.toString(event, debug);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
@Name("Tag")
@Description({
"Represents a tag which can be used to classify items, blocks, or entities.",
"Tags are composed of a value and an optional namespace: \"minecraft:oak_logs\".",
"TagRegistry are composed of a value and an optional namespace: \"minecraft:oak_logs\".",
"If you omit the namespace, one will be provided for you, depending on what kind of tag you're using. " +
"For example, `tag \"doors\"` will be the tag \"minecraft:doors\", " +
"while `paper tag \"doors\"` will be \"paper:doors\".",
Expand Down Expand Up @@ -97,7 +97,7 @@ public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean is

Tag<?> tag;
for (TagType<?> type : types) {
tag = TagModule.TAGS.getTag(origin, type, key);
tag = TagModule.TagRegistry.getTag(origin, type, key);
if (tag != null
// ensures that only datapack/minecraft tags are sent when specifically requested
&& (origin != TagOrigin.BUKKIT || (datapackOnly ^ tag.getKey().getNamespace().equals("minecraft")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import java.util.Objects;

@Name("Tags Contents")
@Name("TagRegistry Contents")
@Description({
"Returns all the values that a tag contains.",
"For item and block tags, this will return items. For entity tags, " +
Expand Down
Loading

0 comments on commit 0607cd7

Please sign in to comment.