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

Merge dev/patch into master #6517

Merged
merged 11 commits into from
Apr 1, 2024
Merged
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ org.gradle.parallel=true

groupid=ch.njol
name=skript
version=2.8.3
version=2.8.4
jarName=Skript.jar
testEnv=java17/paper-1.20.4
testEnvJavaVersion=17
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

plugins {
id 'org.gradle.toolchains.foojay-resolver-convention' version '0.7.0'
id 'org.gradle.toolchains.foojay-resolver-convention' version '0.8.0'
}

rootProject.name = 'Skript'
19 changes: 14 additions & 5 deletions src/main/java/ch/njol/skript/aliases/ItemData.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.data.BlockData;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.ItemFlag;
Expand Down Expand Up @@ -188,15 +189,23 @@ public ItemData(ItemStack stack) {
this(stack, BlockCompat.INSTANCE.getBlockValues(stack));
this.itemForm = true;
}

public ItemData(BlockState block) {
this.type = ItemUtils.asItem(block.getType());

/**
* @deprecated Use {@link ItemData#ItemData(BlockData)} instead
*/
@Deprecated
public ItemData(BlockState blockState) {
this(blockState.getBlockData());
}

public ItemData(BlockData blockData) {
this.type = blockData.getMaterial();
this.stack = new ItemStack(type);
this.blockValues = BlockCompat.INSTANCE.getBlockValues(block);
this.blockValues = BlockCompat.INSTANCE.getBlockValues(blockData);
}

public ItemData(Block block) {
this(block.getState());
this(block.getBlockData());
}

/**
Expand Down
33 changes: 24 additions & 9 deletions src/main/java/ch/njol/skript/aliases/ItemType.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Skull;
import org.bukkit.block.data.BlockData;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
Expand Down Expand Up @@ -184,10 +185,16 @@ public ItemType(ItemStack i) {
add_(new ItemData(i));
}

public ItemType(BlockState b) {
// amount = 1;
add_(new ItemData(b));
// TODO metadata - spawners, skulls, etc.
/**
* @deprecated Use {@link #ItemType(BlockData)} instead
*/
@Deprecated
public ItemType(BlockState blockState) {
this(blockState.getBlockData());
}

public ItemType(BlockData blockData) {
add_(new ItemData(blockData));
}

/**
Expand All @@ -211,7 +218,7 @@ public void setTo(ItemType i) {
}

public ItemType(Block block) {
this(block.getState());
this(block.getBlockData());
}

/**
Expand Down Expand Up @@ -272,17 +279,25 @@ public boolean isOfType(@Nullable ItemStack item) {
return isOfType(new ItemData(item));
}

public boolean isOfType(@Nullable BlockState block) {
if (block == null)
/**
* @deprecated Use {@link #isOfType(BlockData)} instead
*/
@Deprecated
public boolean isOfType(@Nullable BlockState blockState) {
return blockState != null && isOfType(blockState.getBlockData());
}

public boolean isOfType(@Nullable BlockData blockData) {
if (blockData == null)
return isOfType(Material.AIR, null);

return isOfType(new ItemData(block));
return isOfType(new ItemData(blockData));
}

public boolean isOfType(@Nullable Block block) {
if (block == null)
return isOfType(Material.AIR, null);
return isOfType(block.getState());
return isOfType(block.getBlockData());
}

public boolean isOfType(ItemData type) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,15 @@ public static Material asBlock(Material type) {
return null;
}
}

/**
* Gets an item material corresponding to given block material, which might
* be the given material.
* @param type Material.
* @return Item version of material or null.
* @deprecated This just returns itself and has no use
*/
@Deprecated
public static Material asItem(Material type) {
// Assume (naively) that all types are valid items
return type;
Expand Down
19 changes: 13 additions & 6 deletions src/main/java/ch/njol/skript/bukkitutil/block/BlockCompat.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.FallingBlock;
import org.bukkit.inventory.ItemStack;
import org.eclipse.jdt.annotation.Nullable;

import ch.njol.skript.Skript;
import ch.njol.skript.aliases.ItemFlags;

/**
Expand All @@ -42,13 +42,15 @@ public interface BlockCompat {
BlockCompat INSTANCE = new NewBlockCompat();

static final BlockSetter SETTER = INSTANCE.getSetter();

/**
* Gets block values from a block state. They can be compared to other
* values if needed, but cannot be used to retrieve any other data.
* @param block Block state to retrieve value from.
* @return Block values.
* @deprecated Use {@link #getBlockValues(BlockData)} instead
*/
@Deprecated
@Nullable
BlockValues getBlockValues(BlockState block);

Expand All @@ -60,8 +62,11 @@ public interface BlockCompat {
*/
@Nullable
default BlockValues getBlockValues(Block block) {
return getBlockValues(block.getState());
return getBlockValues(block.getBlockData());
}

@Nullable
BlockValues getBlockValues(BlockData blockData);

/**
* Gets block values from a item stack. They can be compared to other values
Expand All @@ -71,17 +76,19 @@ default BlockValues getBlockValues(Block block) {
*/
@Nullable
BlockValues getBlockValues(ItemStack stack);

/**
* Creates a block state from a falling block.
* @param entity Falling block entity
* @return Block state.
* @deprecated This shouldn't be used
*/
@Deprecated
BlockState fallingBlockToState(FallingBlock entity);

@Nullable
default BlockValues getBlockValues(FallingBlock entity) {
return getBlockValues(fallingBlockToState(entity));
return getBlockValues(entity.getBlockData());
}

/**
Expand Down
26 changes: 17 additions & 9 deletions src/main/java/ch/njol/skript/bukkitutil/block/NewBlockCompat.java
Original file line number Diff line number Diff line change
Expand Up @@ -325,16 +325,23 @@ public void sendBlockChange(Player player, Location location, Material type, @Nu
}

private NewBlockSetter setter = new NewBlockSetter();


/**
* @deprecated Use {@link #getBlockValues(BlockData)} instead
*/
@Deprecated
@Nullable
@Override
public BlockValues getBlockValues(BlockState block) {
// If block doesn't have useful data, data field of type is MaterialData
if (block.getType().isBlock())
return new NewBlockValues(block.getType(), block.getBlockData(), false);
return null;
public BlockValues getBlockValues(BlockState blockState) {
return getBlockValues(blockState.getBlockData());
}


@Nullable
@Override
public BlockValues getBlockValues(BlockData blockData) {
return new NewBlockValues(blockData.getMaterial(), blockData, false);
}

@Override
@Nullable
public BlockValues getBlockValues(ItemStack stack) {
Expand All @@ -345,15 +352,16 @@ public BlockValues getBlockValues(ItemStack stack) {
}
return null;
}

@Override
public BlockSetter getSetter() {
return setter;
}

@Deprecated
@Override
public BlockState fallingBlockToState(FallingBlock entity) {
BlockState state = entity.getWorld().getBlockAt(0, 0, 0).getState();
BlockState state = entity.getLocation().getBlock().getState();
state.setBlockData(entity.getBlockData());
return state;
}
Expand Down
26 changes: 22 additions & 4 deletions src/main/java/ch/njol/skript/classes/data/DefaultFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -376,10 +376,28 @@ public Location[] execute(FunctionEvent<?> e, Object[][] params) {
}
}.description("Creates a location from a world and 3 coordinates, with an optional yaw and pitch.",
"If for whatever reason the world is not found, it will fallback to the server's main world.")
.examples("location(0, 128, 0)",
"location(player's x-coordinate, player's y-coordinate + 5, player's z-coordinate, player's world, 0, 90)",
"location(0, 64, 0, world \"world_nether\")",
"location(100, 110, -145, world(\"my_custom_world\"))")
.examples("# TELEPORTING",
"teleport player to location(1,1,1, world \"world\")",
"teleport player to location(1,1,1, world \"world\", 100, 0)",
"teleport player to location(1,1,1, world \"world\", yaw of player, pitch of player)",
"teleport player to location(1,1,1, world of player)",
"teleport player to location(1,1,1, world(\"world\"))",
"teleport player to location({_x}, {_y}, {_z}, {_w}, {_yaw}, {_pitch})",
"# SETTING BLOCKS",
"set block at location(1,1,1, world \"world\") to stone",
"set block at location(1,1,1, world \"world\", 100, 0) to stone",
"set block at location(1,1,1, world of player) to stone",
"set block at location(1,1,1, world(\"world\")) to stone",
"set block at location({_x}, {_y}, {_z}, {_w}) to stone",
"# USING VARIABLES",
"set {_l1} to location(1,1,1)",
"set {_l2} to location(10,10,10)",
"set blocks within {_l1} and {_l2} to stone",
"if player is within {_l1} and {_l2}:",
"# OTHER",
"kill all entities in radius 50 around location(1,65,1, world \"world\")",
"delete all entities in radius 25 around location(50,50,50, world \"world_nether\")",
"ignite all entities in radius 25 around location(1,1,1, world of player)")
.since("2.2"));

Functions.registerFunction(new SimpleJavaFunction<Date>("date", new Parameter[] {
Expand Down
43 changes: 35 additions & 8 deletions src/main/java/ch/njol/skript/entity/EntityData.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.bukkit.RegionAccessor;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.eclipse.jdt.annotation.Nullable;

Expand Down Expand Up @@ -94,6 +95,7 @@ public abstract class EntityData<E extends Entity> implements SyntaxElement, Ygg
} catch (NoSuchMethodException | SecurityException ignored) { /* We already checked if the method exists */ }
}

private static final boolean HAS_ENABLED_BY_FEATURE = Skript.methodExists(EntityType.class, "isEnabledByFeature", World.class);
public final static String LANGUAGE_NODE = "entities";

public final static Message m_age_pattern = new Message(LANGUAGE_NODE + ".age pattern");
Expand Down Expand Up @@ -463,6 +465,25 @@ private E apply(E entity) {
return entity;
}

/**
* Check if this entity type can spawn.
* <p>Some entity types may be restricted by experimental datapacks.</p>
*
* @param world World to check if entity can spawn in
* @return True if entity can spawn else false
*/
public boolean canSpawn(@Nullable World world) {
if (world == null)
return false;
if (HAS_ENABLED_BY_FEATURE) {
// Check if the entity can actually be spawned
// Some entity types may be restricted by experimental datapacks
EntityType bukkitEntityType = EntityUtils.toBukkitEntityType(this);
return bukkitEntityType.isEnabledByFeature(world);
}
return true;
}

/**
* Spawn this entity data at a location.
*
Expand All @@ -476,7 +497,7 @@ public final E spawn(Location location) {

/**
* Spawn this entity data at a location.
* The consumer allows for modiciation to the entity before it actually gets spawned.
* The consumer allows for modification to the entity before it actually gets spawned.
* <p>
* Bukkit's own {@link org.bukkit.util.Consumer} is deprecated.
* Use {@link #spawn(Location, Consumer)}
Expand All @@ -494,7 +515,7 @@ public E spawn(Location location, org.bukkit.util.@Nullable Consumer<E> consumer

/**
* Spawn this entity data at a location.
* The consumer allows for modiciation to the entity before it actually gets spawned.
* The consumer allows for modification to the entity before it actually gets spawned.
*
* @param location The {@link Location} to spawn the entity at.
* @param consumer A {@link Consumer} to apply the entity changes to.
Expand All @@ -503,10 +524,13 @@ public E spawn(Location location, org.bukkit.util.@Nullable Consumer<E> consumer
@Nullable
public E spawn(Location location, @Nullable Consumer<E> consumer) {
assert location != null;
World world = location.getWorld();
if (!canSpawn(world))
return null;
if (consumer != null) {
return EntityData.spawn(location, getType(), e -> consumer.accept(this.apply(e)));
} else {
return apply(location.getWorld().spawn(location, getType()));
return apply(world.spawn(location, getType()));
}
}

Expand Down Expand Up @@ -646,23 +670,26 @@ public void deserialize(final Fields fields) throws StreamCorruptedException, No
protected boolean deserialize(final String s) {
return false;
}

@SuppressWarnings({"unchecked", "deprecation"})
protected static <E extends Entity> @Nullable E spawn(Location location, Class<E> type, Consumer<E> consumer) {
World world = location.getWorld();
if (world == null)
return null;
try {
if (WORLD_1_17_CONSUMER) {
return (@Nullable E) WORLD_1_17_CONSUMER_METHOD.invoke(location.getWorld(), location, type,
return (@Nullable E) WORLD_1_17_CONSUMER_METHOD.invoke(world, location, type,
(org.bukkit.util.Consumer<E>) consumer::accept);
} else if (WORLD_1_13_CONSUMER) {
return (@Nullable E) WORLD_1_13_CONSUMER_METHOD.invoke(location.getWorld(), location, type,
return (@Nullable E) WORLD_1_13_CONSUMER_METHOD.invoke(world, location, type,
(org.bukkit.util.Consumer<E>) consumer::accept);
}
} catch (InvocationTargetException | IllegalAccessException e) {
if (Skript.testing())
Skript.exception(e, "Can't spawn " + type.getName());
return null;
}
return location.getWorld().spawn(location, type, consumer);
return world.spawn(location, type, consumer);
}

}
Loading
Loading