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

Minecraft Tags #7127

Merged
merged 30 commits into from
Jan 1, 2025
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
74212a1
minecraft tags part 1
sovdeeth Oct 3, 2024
cc91087
tags part 2
sovdeeth Oct 3, 2024
85d3a43
forgot to remove _settag for entity tags too
sovdeeth Oct 3, 2024
6d2f935
all tags of type
sovdeeth Oct 3, 2024
4ccf691
major refactor
sovdeeth Oct 3, 2024
c7cc80a
Add origin filtering
sovdeeth Oct 3, 2024
9e84318
populate omitted namespaces
sovdeeth Oct 3, 2024
a509e46
implement simplify for future use cases
sovdeeth Oct 3, 2024
6ebdf64
Merge branch 'dev/feature' into feature/block-tags
sovdeeth Oct 3, 2024
ed88f9a
all the documentation you could ever want!
sovdeeth Oct 4, 2024
012fe09
Merge branch 'feature/block-tags' of https://github.com/sovdeeth/Skri…
sovdeeth Oct 4, 2024
9b87198
gatekeep gaslight girlboss the paper tags
sovdeeth Oct 4, 2024
fcf4c88
basic tests and fix comparisons
sovdeeth Oct 4, 2024
c19703e
custom tags!
sovdeeth Oct 4, 2024
593e5f4
add ways to turn tag into a string
sovdeeth Oct 4, 2024
fbb5e0d
a few more examples
sovdeeth Oct 5, 2024
a106964
requested changes
sovdeeth Oct 12, 2024
d8a9328
Merge branch 'dev/feature' into feature/block-tags
sovdeeth Oct 13, 2024
f1203b2
plural exprtags and parity between tag toString and toVariableString
sovdeeth Nov 10, 2024
10e0645
Merge branch 'dev/feature' into feature/block-tags
sovdeeth Nov 10, 2024
6cfe138
Merge branch 'dev/feature' into feature/block-tags
Moderocky Nov 12, 2024
cd6e699
Fix over-zealous refactor
sovdeeth Dec 15, 2024
80cf6f6
Merge branch 'dev/feature' into feature/block-tags
sovdeeth Dec 15, 2024
3fc9856
Requested changes
sovdeeth Dec 15, 2024
3daec96
Requested changes
sovdeeth Dec 20, 2024
ae7149b
Merge branch 'dev/feature' into feature/block-tags
Efnilite Dec 27, 2024
a85d420
Update src/main/java/ch/njol/util/Pair.java
sovdeeth Dec 27, 2024
f60090a
Merge branch 'dev/feature' into feature/block-tags
sovdeeth Dec 29, 2024
89f40e3
Update src/main/java/org/skriptlang/skript/bukkit/tags/TagType.java
sovdeeth Dec 29, 2024
ac13b99
Merge branch 'dev/feature' into feature/block-tags
sovdeeth Jan 1, 2025
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
2 changes: 2 additions & 0 deletions src/main/java/ch/njol/skript/Skript.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import org.skriptlang.skript.bukkit.SkriptMetrics;
import org.skriptlang.skript.bukkit.tags.TagModule;
import org.skriptlang.skript.bukkit.breeding.BreedingModule;
import org.skriptlang.skript.bukkit.displays.DisplayModule;
import org.skriptlang.skript.lang.comparator.Comparator;
Expand Down Expand Up @@ -556,6 +557,7 @@ public void onEnable() {
getAddonInstance().loadClasses("org.skriptlang.skript.bukkit", "misc");
// todo: become proper module once registry api is merged
DisplayModule.load();
TagModule.load();
BreedingModule.load();
} catch (final Exception e) {
exception(e, "Could not load required .class files: " + e.getLocalizedMessage());
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/ch/njol/skript/aliases/ItemType.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -1415,6 +1416,17 @@ public Material getMaterial() {
}

/**
* @return All Materials this ItemType represents.
*/
public Material[] getMaterials() {
Set<Material> materials = new HashSet<>();
for (ItemData data : types) {
materials.add(data.getType());
}
return materials.toArray(new Material[0]);
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @return A random block material this ItemType represents.
* @throws IllegalStateException If {@link #hasBlock()} is false.
*/
Expand Down
86 changes: 31 additions & 55 deletions src/main/java/ch/njol/util/Pair.java
Original file line number Diff line number Diff line change
@@ -1,70 +1,49 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.util;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnknownNullability;

import java.io.Serial;
import java.io.Serializable;
import java.util.Map.Entry;
import java.util.Objects;

import org.jetbrains.annotations.Nullable;

/**
* @author Peter Güttinger
*/
public class Pair<T1, T2> implements Entry<T1, T2>, Cloneable, Serializable {
@Serial
private static final long serialVersionUID = 8296563685697678334L;

@Nullable
protected T1 first;
@Nullable
protected T2 second;

protected @UnknownNullability T1 first;
protected @UnknownNullability T2 second;

public Pair() {
first = null;
second = null;
}

public Pair(final @Nullable T1 first, final @Nullable T2 second) {
public Pair(@Nullable T1 first, @Nullable T2 second) {
this.first = first;
this.second = second;
}

public Pair(final Entry<T1, T2> e) {
this.first = e.getKey();
this.second = e.getValue();
public Pair(@NotNull Entry<T1, T2> entry) {
this.first = entry.getKey();
this.second = entry.getValue();
}

@Nullable
public T1 getFirst() {

public @UnknownNullability T1 getFirst() {
return first;
}

public void setFirst(final @Nullable T1 first) {
public void setFirst(@Nullable T1 first) {
this.first = first;
}

@Nullable
public T2 getSecond() {

public @UnknownNullability T2 getSecond() {
return second;
}

public void setSecond(final @Nullable T2 second) {
public void setSecond(@Nullable T2 second) {
this.second = second;
}

Expand All @@ -73,23 +52,22 @@ public void setSecond(final @Nullable T2 second) {
*/
@Override
public String toString() {
return "" + first + "," + second;
return first + "," + second;
}

/**
* Checks for equality with Entries to match {@link #hashCode()}
*/
@Override
public final boolean equals(final @Nullable Object obj) {
public final boolean equals(@Nullable Object obj) {
if (obj == this)
return true;
if (!(obj instanceof Entry))
if (!(obj instanceof Entry<?, ?> entry))
return false;
final Entry<?, ?> other = (Entry<?, ?>) obj;
final T1 first = this.first;
final T2 second = this.second;
return (first == null ? other.getKey() == null : first.equals(other.getKey())) &&
(second == null ? other.getValue() == null : second.equals(other.getValue()));
T1 first = this.first;
T2 second = this.second;
return (first == null ? entry.getKey() == null : first.equals(entry.getKey())) &&
(second == null ? entry.getValue() == null : second.equals(entry.getValue()));
}

/**
Expand All @@ -101,21 +79,19 @@ public final int hashCode() {
}

@Override
@Nullable
public T1 getKey() {
public @UnknownNullability T1 getKey() {
return first;
}

@Override
@Nullable
public T2 getValue() {
public @UnknownNullability T2 getValue() {
return second;
}

@Override
@Nullable
public T2 setValue(final @Nullable T2 value) {
final T2 old = second;

sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
public @UnknownNullability T2 setValue(@Nullable T2 value) {
T2 old = second;
second = value;
return old;
}
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/org/skriptlang/skript/bukkit/tags/SkriptTag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.skriptlang.skript.bukkit.tags;

import org.bukkit.Keyed;
import org.bukkit.NamespacedKey;
import org.bukkit.Tag;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.UnmodifiableView;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class SkriptTag<T extends Keyed> implements Tag<T> {
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved

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

public SkriptTag(NamespacedKey key, Collection<T> contents) {
this.contents = new HashSet<>(contents);
this.key = key;
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public boolean isTagged(@NotNull T item) {
return contents.contains(item);
}

@Override
public @NotNull @UnmodifiableView Set<T> getValues() {
return Collections.unmodifiableSet(contents);
}

@Override
public @NotNull NamespacedKey getKey() {
return key;
}
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved

}
114 changes: 114 additions & 0 deletions src/main/java/org/skriptlang/skript/bukkit/tags/TagModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package org.skriptlang.skript.bukkit.tags;

import ch.njol.skript.Skript;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.bukkitutil.EntityUtils;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.classes.Parser;
import ch.njol.skript.entity.EntityData;
import ch.njol.skript.lang.ParseContext;
import ch.njol.skript.registrations.Classes;
import ch.njol.skript.util.slot.Slot;
import org.bukkit.Keyed;
import org.bukkit.Tag;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Entity;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Contract;
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;

public class TagModule {

// paper tags
public static final boolean PAPER_TAGS_EXIST = Skript.classExists("com.destroystokyo.paper.MaterialTags");

// tag object
public static Tags TAGS;

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

sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
// 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")
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
.user("minecraft ?tags?")
.name("Minecraft Tag")
.description("A tag that classifies a material, or entity.")
.since("INSERT VERSION")
.parser(new Parser<Tag<?>>() {
@Override
public boolean canParse(ParseContext context) {
return false;
}

@Override
public String toString(Tag<?> tag, int flags) {
return "tag " + tag.getKey();
}

@Override
public String toVariableNameString(Tag<?> tag) {
return toString(tag, 0);
}
}));

// 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);
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved

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

/**
* Retrieves a Keyed array based on the type of the provided input object.
*
* @param input the input object to determine the keyed value, can be of type Entity,
* EntityData, ItemType, ItemStack, Slot, Block, or BlockData.
* @return a Keyed array corresponding to the input's type, or null if the input is null
* or if no corresponding Keyed value can be determined. ItemTypes may return multiple values,
* though everything else will return a single element array.
*/
@Contract(value = "null -> null", pure = true)
public static @Nullable Keyed[] getKeyed(Object input) {
Keyed value = null;
Keyed[] values = null;
if (input == null)
return null;
if (input instanceof Entity entity) {
value = entity.getType();
} if (input instanceof EntityData<?> data) {
value = EntityUtils.toBukkitEntityType(data);
} else if (input instanceof ItemType itemType) {
values = itemType.getMaterials();
} else if (input instanceof ItemStack itemStack) {
value = itemStack.getType();
} else if (input instanceof Slot slot) {
ItemStack stack = slot.getItem();
if (stack == null)
return null;
value = stack.getType();
} else if (input instanceof Block block) {
value = block.getType();
} else if (input instanceof BlockData data) {
value = data.getMaterial();
}
if (value == null)
return values;
return new Keyed[]{value};
}

}
Loading