Skip to content

Commit

Permalink
Any X (#6728)
Browse files Browse the repository at this point in the history
* Add any-type & named.

* Basic test for ExprName.

* Make existing types any-types.

* Register special any-info (for checking purposes).

* Register any named type.

* Add converters for everything to any-named.

* Basic changes to ExprName.

* Add more tests.

* Add any-named lang entry.

* Add any-sized type.

* Support current types as amount.

* New amount support + test.

* Change tests so they pass :)

* Fix world converter for 1.13.

* Support containers.

* Remove converter method I didn't use.

* Clean up code for Java 17.

* Apply suggestions from code review

Co-authored-by: sovdee <10354869+sovdeeth@users.noreply.github.com>

* Do change requests.

* Patterns.

* Add docs.

* Fix.

* Support numbered in empty.

* Switch numbers for walrus.

* Fix spacing.

* Add user pattern.

* Fix imports.

* Apply suggestions from code review

Co-authored-by: Patrick Miller <apickledwalrus@gmail.com>

* Fix switch.

* Change method in slot.

* Remove method for walrus.

* Remove throw.

* ExprName Fixes

* Apply suggestions from code review

Co-authored-by: Patrick Miller <apickledwalrus@gmail.com>

* Fix incompatibility in dependency.

* Patch entity comparison thingy.

* Do requested changes.

* Some changes.

---------

Co-authored-by: sovdee <10354869+sovdeeth@users.noreply.github.com>
Co-authored-by: Patrick Miller <apickledwalrus@gmail.com>
Co-authored-by: Efnilite <35348263+Efnilite@users.noreply.github.com>
  • Loading branch information
4 people authored Dec 29, 2024
1 parent a0f3a5b commit a18c5ae
Show file tree
Hide file tree
Showing 22 changed files with 826 additions and 305 deletions.
39 changes: 38 additions & 1 deletion src/main/java/ch/njol/skript/aliases/ItemType.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import ch.njol.skript.bukkitutil.BukkitUnsafe;
import ch.njol.skript.bukkitutil.ItemUtils;
import ch.njol.skript.lang.Unit;
import ch.njol.skript.lang.util.common.AnyAmount;
import ch.njol.skript.lang.util.common.AnyNamed;
import ch.njol.skript.localization.Adjective;
import ch.njol.skript.localization.GeneralWords;
import ch.njol.skript.localization.Language;
Expand Down Expand Up @@ -35,6 +37,7 @@
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.NotSerializableException;
Expand All @@ -55,7 +58,8 @@
import java.util.stream.Collectors;

@ContainerType(ItemStack.class)
public class ItemType implements Unit, Iterable<ItemData>, Container<ItemStack>, YggdrasilExtendedSerializable {
public class ItemType implements Unit, Iterable<ItemData>, Container<ItemStack>, YggdrasilExtendedSerializable,
AnyNamed, AnyAmount {

static {
// This handles updating ItemType and ItemData variable records
Expand Down Expand Up @@ -1444,4 +1448,37 @@ public ItemType getBaseType() {
return copy;
}

@Override
public @Nullable String name() {
ItemMeta meta = this.getItemMeta();
return meta.hasDisplayName() ? meta.getDisplayName() : null;
}

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

@Override
public void setName(String name) {
ItemMeta meta = this.getItemMeta();
meta.setDisplayName(name);
this.setItemMeta(meta);
}

@Override
public @NotNull Number amount() {
return this.getAmount();
}

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

@Override
public void setAmount(@Nullable Number amount) throws UnsupportedOperationException {
this.setAmount(amount != null ? amount.intValue() : 0);
}

}
40 changes: 40 additions & 0 deletions src/main/java/ch/njol/skript/classes/AnyInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package ch.njol.skript.classes;

import ch.njol.skript.lang.util.common.AnyProvider;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
* A special kind of {@link ClassInfo} for dealing with 'any'-accepting types.
* These auto-generate their user patterns (e.g. {@code named} -> {@code any named thing}).
*
* @see AnyProvider
*/
public class AnyInfo<Type extends AnyProvider> extends ClassInfo<Type> {

/**
* @param c The class
* @param codeName The name used in patterns
*/
public AnyInfo(Class<Type> c, String codeName) {
super(c, codeName);
this.user("(any )?" + codeName + " (thing|object)s?");
}

@Override
public ClassInfo<Type> user(String... userInputPatterns) throws PatternSyntaxException {
if (this.userInputPatterns == null)
return super.user(userInputPatterns);
// Allow appending the patterns.
List<Pattern> list = new ArrayList<>(List.of(this.userInputPatterns));
for (String pattern : userInputPatterns) {
list.add(Pattern.compile(pattern));
}
this.userInputPatterns = list.toArray(new Pattern[0]);
return this;
}

}
Loading

0 comments on commit a18c5ae

Please sign in to comment.