Skip to content

Commit

Permalink
Merge pull request #3487 from SkriptLang/dev-2.5
Browse files Browse the repository at this point in the history
Merging dev-2.5 -> Master (in prep for 2.5.1 release)
  • Loading branch information
ShaneBeee committed Oct 22, 2020
2 parents 7255ce1 + 3a57587 commit fbe0de2
Show file tree
Hide file tree
Showing 81 changed files with 2,866 additions and 310 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,11 @@ void createTestTask(String name, String environments, boolean devMode) {
}

// Register different Skript testing tasks
createTestTask('quickTest', 'src/test/skript/environments/main/paper-1.16.1.json', false)
createTestTask('quickTest', 'src/test/skript/environments/main/paper-1.16.3.json', false)
createTestTask('skriptTest', 'src/test/skript/environments/main', false)
createTestTask('skriptTestFull', 'src/test/skript/environments/', false)
createTestTask('skriptTestDev', 'src/test/skript/environments/main/' + (System.getProperty('skript.testEnv') == null
? 'paper-1.16.1.json' : System.getProperty('skript.testEnv') + '.json'), true)
? 'paper-1.16.3.json' : System.getProperty('skript.testEnv') + '.json'), true)

// Build flavor configurations
task githubResources(type: ProcessResources) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/ch/njol/skript/aliases/Aliases.java
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ private static ItemType getAlias(final String s) {
}
} else if ((b = lc.endsWith(" " + itemSingular)) || lc.endsWith(" " + itemPlural)) {
if ((i = getAlias_i("" + s.substring(0, s.length() - (b ? itemSingular.length() : itemPlural.length()) - 1))) != null) {
i = i.clone();
for (int j = 0; j < i.numTypes(); j++) {
final ItemData d = i.getTypes().get(j);
if (!d.isAnything && d.getType().isBlock()) {
Expand Down
32 changes: 27 additions & 5 deletions src/main/java/ch/njol/skript/aliases/ItemData.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.enchantments.Enchantment;
Expand All @@ -41,6 +42,7 @@
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.inventory.meta.SpawnEggMeta;
import org.bukkit.potion.PotionData;
import org.eclipse.jdt.annotation.Nullable;
Expand Down Expand Up @@ -79,6 +81,7 @@ public static class OldItemData {
static final MaterialRegistry materialRegistry;

private static final boolean SPAWN_EGG_META_EXISTS = Skript.classExists("org.bukkit.inventory.meta.SpawnEggMeta");
private static final boolean HAS_NEW_SKULL_META_METHODS = Skript.methodExists(SkullMeta.class, "getOwningPlayer");

// Load or create material registry
static {
Expand Down Expand Up @@ -404,28 +407,28 @@ private static MatchQuality compareItemMetas(ItemMeta first, ItemMeta second) {
String ourName = first.hasDisplayName() ? first.getDisplayName() : null;
String theirName = second.hasDisplayName() ? second.getDisplayName() : null;
if (!Objects.equals(ourName, theirName)) {
quality = ourName != null ? MatchQuality.SAME_MATERIAL : quality;
quality = ourName != null ? MatchQuality.SAME_MATERIAL : theirName != null ? MatchQuality.SAME_MATERIAL : quality;
}

// Lore
List<String> ourLore = first.hasLore() ? first.getLore() : null;
List<String> theirLore = second.hasLore() ? second.getLore() : null;
if (!Objects.equals(ourLore, theirLore)) {
quality = ourLore != null ? MatchQuality.SAME_MATERIAL : quality;
quality = ourLore != null ? MatchQuality.SAME_MATERIAL : theirLore != null ? MatchQuality.SAME_MATERIAL : quality;
}

// Enchantments
Map<Enchantment, Integer> ourEnchants = first.getEnchants();
Map<Enchantment, Integer> theirEnchants = second.getEnchants();
if (!Objects.equals(ourEnchants, theirEnchants)) {
quality = !ourEnchants.isEmpty() ? MatchQuality.SAME_MATERIAL : quality;
quality = !ourEnchants.isEmpty() ? MatchQuality.SAME_MATERIAL : !theirEnchants.isEmpty() ? MatchQuality.SAME_MATERIAL : quality;
}

// Item flags
Set<ItemFlag> ourFlags = first.getItemFlags();
Set<ItemFlag> theirFlags = second.getItemFlags();
if (!Objects.equals(ourFlags, theirFlags)) {
quality = !ourFlags.isEmpty() ? MatchQuality.SAME_MATERIAL : quality;
quality = !ourFlags.isEmpty() ? MatchQuality.SAME_MATERIAL : !theirFlags.isEmpty() ? MatchQuality.SAME_MATERIAL : quality;
}

// Potion data
Expand All @@ -450,6 +453,25 @@ private static MatchQuality compareItemMetas(ItemMeta first, ItemMeta second) {
return !Objects.equals(ourSpawnedType, theirSpawnedType) ? MatchQuality.SAME_MATERIAL : quality;
}

// Skull owner
if (second instanceof SkullMeta) {
if (!(first instanceof SkullMeta)) {
return MatchQuality.DIFFERENT; // Second is a skull, first is clearly not
}
// Compare skull owners
if (HAS_NEW_SKULL_META_METHODS) {
OfflinePlayer ourOwner = ((SkullMeta) first).getOwningPlayer();
OfflinePlayer theirOwner = ((SkullMeta) second).getOwningPlayer();
return !Objects.equals(ourOwner, theirOwner) ? MatchQuality.SAME_MATERIAL : quality;
} else { // Use old methods
@SuppressWarnings("deprecation")
String ourOwner = ((SkullMeta) first).getOwner();
@SuppressWarnings("deprecation")
String theirOwner = ((SkullMeta) second).getOwner();
return !Objects.equals(ourOwner, theirOwner) ? MatchQuality.SAME_MATERIAL : quality;
}
}

return quality;
}

Expand All @@ -460,7 +482,7 @@ private static MatchQuality compareItemMetas(ItemMeta first, ItemMeta second) {
* @return If this item can be considered the default item of its type.
*/
public boolean isDefault() {
return itemFlags == 0 && blockValues == null;
return isAlias || (itemFlags == 0 && blockValues == null);
}

/**
Expand Down
23 changes: 22 additions & 1 deletion src/main/java/ch/njol/skript/aliases/ItemType.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@
import java.util.RandomAccess;
import java.util.Set;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
Expand Down Expand Up @@ -360,6 +362,23 @@ public boolean setBlock(Block block, boolean applyPhysics) {
return false;
}

/**
* Send a block change to a player
* <p>This will send a fake block change to the player, and will not change the block on the server.</p>
*
* @param player Player to send change to
* @param location Location of block to change
*/
public void sendBlockChange(Player player, Location location) {
for (int i = random.nextInt(types.size()); i < types.size(); i++) {
ItemData d = types.get(i);
Material blockType = ItemUtils.asBlock(d.type);
if (blockType == null) // Ignore items which cannot be placed
continue;
BlockUtils.sendBlockChange(player, location, blockType, d.getBlockValues());
}
}

/**
* Intersects all ItemDatas with all ItemDatas of the given ItemType, returning an ItemType with at most n*m ItemDatas, where n = #ItemDatas of this ItemType, and m =
* #ItemDatas of the argument.
Expand Down Expand Up @@ -742,12 +761,14 @@ public final boolean removeFrom(final List<ItemStack>... lists) {
* it to return true for two "same items", even if their
* item meta is completely different.
*/
if (is != null && d.matchAlias(new ItemData(is)).isAtLeast(MatchQuality.EXACT)) {
ItemData other = is != null ? new ItemData(is) : null;
if (other != null && other.matchAlias(d).isAtLeast((d.isDefault() && !other.isDefault()) ? MatchQuality.SAME_MATERIAL : MatchQuality.EXACT)) {
if (all && amount == -1) {
list.set(i, null);
removed = 1;
continue;
}
assert is != null;
final int toRemove = Math.min(is.getAmount(), getAmount() - removed);
removed += toRemove;
if (toRemove == is.getAmount()) {
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/ch/njol/skript/bukkitutil/block/BlockSetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
*/
package ch.njol.skript.bukkitutil.block;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.eclipse.jdt.annotation.Nullable;

/**
Expand Down Expand Up @@ -65,4 +67,16 @@ public interface BlockSetter {
* @param flags Flags for block setter.
*/
void setBlock(Block block, Material type, @Nullable BlockValues values, int flags);

/**
* Send a block change to a player.
* <p>This will send a fake block change to the player, and will not change the block on the server.</p>
*
* @param player Player to send change to
* @param location Location of block to change
* @param type Material of change
* @param values Additional block data, such as block states.
*/
void sendBlockChange(Player player, Location location, Material type, @Nullable BlockValues values);

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
import java.lang.invoke.MethodType;
import java.util.Map;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.entity.FallingBlock;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.eclipse.jdt.annotation.Nullable;

Expand Down Expand Up @@ -149,7 +151,11 @@ public void setBlock(Block block, Material type, @Nullable BlockValues values, i
}
}


@Override
public void sendBlockChange(Player player, Location location, Material type, @Nullable BlockValues values) {
byte data = values != null ? (byte) ((MagicBlockValues) values).data : 0;
player.sendBlockChange(location, type, data);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Map;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Tag;
import org.bukkit.block.Block;
Expand All @@ -31,6 +32,7 @@
import org.bukkit.block.data.Directional;
import org.bukkit.block.data.type.Bed;
import org.bukkit.entity.FallingBlock;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.eclipse.jdt.annotation.Nullable;

Expand Down Expand Up @@ -284,6 +286,12 @@ private BlockFace findWallTorchSide(Block block) {
return null; // Can't place torch here legally
}

@Override
public void sendBlockChange(Player player, Location location, Material type, @Nullable BlockValues values) {
BlockData blockData = values != null ? ((NewBlockValues) values).data : type.createBlockData();
player.sendBlockChange(location, blockData);
}

}

private NewBlockSetter setter = new NewBlockSetter();
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/ch/njol/skript/classes/ClassInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,21 @@ public class ClassInfo<T> implements Debuggable {
*/
public ClassInfo(final Class<T> c, final String codeName) {
this.c = c;
if (!isVaildCodeName(codeName))
if (!isValidCodeName(codeName))
throw new IllegalArgumentException("Code names for classes must be lowercase and only consist of latin letters and arabic numbers");
this.codeName = codeName;
name = new Noun("types." + codeName);
}

/**
* Incorrect spelling in method name. This will be removed in the future.
*/
@Deprecated
public static boolean isVaildCodeName(final String name) {
return isValidCodeName(name);
}

public static boolean isValidCodeName(final String name) {
return name.matches("[a-z0-9]+");
}

Expand Down
48 changes: 48 additions & 0 deletions src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.inventory.InventoryPickupItemEvent;
import org.bukkit.event.inventory.PrepareItemCraftEvent;
import org.bukkit.event.player.PlayerBedEnterEvent;
import org.bukkit.event.player.PlayerBedLeaveEvent;
Expand All @@ -93,6 +94,7 @@
import org.bukkit.event.player.PlayerItemBreakEvent;
import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.event.player.PlayerItemDamageEvent;
import org.bukkit.event.player.PlayerItemMendEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.event.player.PlayerRiptideEvent;
Expand Down Expand Up @@ -726,6 +728,30 @@ public ItemType get(PlayerItemDamageEvent event) {
return new ItemType(event.getItem());
}
}, 0);
//PlayerItemMendEvent
if (Skript.classExists("org.bukkit.event.player.PlayerItemMendEvent")) {
EventValues.registerEventValue(PlayerItemMendEvent.class, Player.class, new Getter<Player, PlayerItemMendEvent>() {
@Override
@Nullable
public Player get(PlayerItemMendEvent e) {
return e.getPlayer();
}
}, 0);
EventValues.registerEventValue(PlayerItemMendEvent.class, ItemType.class, new Getter<ItemType, PlayerItemMendEvent>() {
@Override
@Nullable
public ItemType get(PlayerItemMendEvent e) {
return new ItemType(e.getItem());
}
}, 0);
EventValues.registerEventValue(PlayerItemMendEvent.class, Entity.class, new Getter<Entity, PlayerItemMendEvent>() {
@Override
@Nullable
public Entity get(PlayerItemMendEvent e) {
return e.getExperienceOrb();
}
}, 0);
}

// --- HangingEvents ---

Expand Down Expand Up @@ -982,6 +1008,28 @@ public Inventory get(final InventoryCloseEvent e) {
return e.getInventory();
}
}, 0);
//InventoryPickupItemEvent
EventValues.registerEventValue(InventoryPickupItemEvent.class, Inventory.class, new Getter<Inventory, InventoryPickupItemEvent>() {
@Nullable
@Override
public Inventory get(InventoryPickupItemEvent event) {
return event.getInventory();
}
}, 0);
EventValues.registerEventValue(InventoryPickupItemEvent.class, Item.class, new Getter<Item, InventoryPickupItemEvent>() {
@Nullable
@Override
public Item get(InventoryPickupItemEvent event) {
return event.getItem();
}
}, 0);
EventValues.registerEventValue(InventoryPickupItemEvent.class, ItemType.class, new Getter<ItemType, InventoryPickupItemEvent>() {
@Nullable
@Override
public ItemType get(InventoryPickupItemEvent event) {
return new ItemType(event.getItem().getItemStack());
}
}, 0);
//PortalCreateEvent
EventValues.registerEventValue(PortalCreateEvent.class, World.class, new Getter<World, PortalCreateEvent>() {
@Override
Expand Down
20 changes: 18 additions & 2 deletions src/main/java/ch/njol/skript/classes/data/DefaultComparators.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@

import java.util.Objects;

import ch.njol.skript.aliases.MatchQuality;
import ch.njol.skript.util.GameruleValue;
import ch.njol.skript.util.EnchantmentType;
import ch.njol.skript.util.Experience;
import ch.njol.skript.util.slot.EquipmentSlot;
import ch.njol.util.Kleenean;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
Expand Down Expand Up @@ -103,6 +105,8 @@ public boolean supportsOrdering() {

@Override
public Relation compare(Slot o1, Slot o2) {
if (o1 instanceof EquipmentSlot != o2 instanceof EquipmentSlot)
return Relation.NOT_EQUAL;
if (o1.isSameSlot(o2))
return Relation.EQUAL;
return Relation.NOT_EQUAL;
Expand Down Expand Up @@ -193,7 +197,19 @@ public boolean supportsOrdering() {
Comparators.registerComparator(ItemType.class, ItemType.class, new Comparator<ItemType, ItemType>() {
@Override
public Relation compare(final ItemType i1, final ItemType i2) {
return Relation.get(i2.isSupertypeOf(i1));
if (i1.isAll() != i2.isAll())
return Relation.NOT_EQUAL;
if (i1.getAmount() != i2.getAmount())
return Relation.NOT_EQUAL;
for (ItemData myType : i1.getTypes()) {
for (ItemData otherType : i2.getTypes()) {
// Don't require an EXACT match if the other ItemData is an alias. They only need to share a material.
if (myType.matchAlias(otherType).isAtLeast((otherType.isDefault() && !myType.isDefault()) ? MatchQuality.SAME_ITEM : MatchQuality.EXACT)) {
return Relation.EQUAL;
}
}
}
return Relation.NOT_EQUAL;
}

@Override
Expand Down Expand Up @@ -443,7 +459,7 @@ public boolean supportsOrdering() {
public Relation compare(final DamageCause dc, final ItemType t) {
switch (dc) {
case FIRE:
return Relation.get(t.isOfType(Material.LAVA));
return Relation.get(t.isOfType(Material.FIRE));
case LAVA:
return Relation.get(t.equals(lava));
case MAGIC:
Expand Down
Loading

0 comments on commit fbe0de2

Please sign in to comment.