Skip to content

Commit

Permalink
Merge branch 'dev/patch' into fix/entity-damage-event-1.20.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Moderocky authored Apr 6, 2024
2 parents ae0b208 + 972dd38 commit aef0572
Show file tree
Hide file tree
Showing 21 changed files with 164 additions and 63 deletions.
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
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
13 changes: 8 additions & 5 deletions src/main/java/ch/njol/skript/entity/EntityData.java
Original file line number Diff line number Diff line change
Expand Up @@ -670,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);
}

}
4 changes: 2 additions & 2 deletions src/main/java/ch/njol/skript/entity/FallingBlockData.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,15 @@ public ItemType convert(ItemType t) {
@Override
protected boolean init(final @Nullable Class<? extends FallingBlock> c, final @Nullable FallingBlock e) {
if (e != null) // TODO material data support
types = new ItemType[] {new ItemType(BlockCompat.INSTANCE.fallingBlockToState(e))};
types = new ItemType[] {new ItemType(e.getBlockData())};
return true;
}

@Override
protected boolean match(final FallingBlock entity) {
if (types != null) {
for (final ItemType t : types) {
if (t.isOfType(BlockCompat.INSTANCE.fallingBlockToState(entity)))
if (t.isOfType(entity.getBlockData()))
return true;
}
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ch/njol/skript/events/EvtBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public boolean check(final Event event) {
if (event instanceof BlockFormEvent) {
BlockFormEvent blockFormEvent = (BlockFormEvent) event;
BlockState newState = blockFormEvent.getNewState();
item = new ItemType(newState);
item = new ItemType(newState.getBlockData());
blockData = newState.getBlockData();
} else if (event instanceof BlockEvent) {
BlockEvent blockEvent = (BlockEvent) event;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/ch/njol/skript/events/EvtGrow.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ private static boolean checkFrom(Event event, Literal<Object> types) {
BlockState oldState = ((BlockGrowEvent) event).getBlock().getState();
return types.check(event, type -> {
if (type instanceof ItemType) {
return ((ItemType) type).isOfType(oldState);
return ((ItemType) type).isOfType(oldState.getBlockData());
} else if (type instanceof BlockData) {
return ((BlockData) type).matches(oldState.getBlockData());
}
Expand All @@ -201,7 +201,7 @@ private static boolean checkTo(Event event, Literal<Object> types) {
BlockState newState = ((BlockGrowEvent) event).getNewState();
return types.check(event, type -> {
if (type instanceof ItemType) {
return ((ItemType) type).isOfType(newState);
return ((ItemType) type).isOfType(newState.getBlockData());
} else if (type instanceof BlockData) {
return ((BlockData) type).matches(newState.getBlockData());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
@Examples({
"on inventory click:",
"\ttype of event-inventory is anvil inventory",
"\tif the anvil input text of the event-inventory is \"FREE OP\":",
"\tif the anvil text input of the event-inventory is \"FREE OP\":",
"\t\tban player"
})
@Since("2.7")
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/ch/njol/skript/expressions/ExprElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@
"The first, last, range or a random element of a set, e.g. a list variable.",
"See also: <a href='#ExprRandom'>random expression</a>"
})
@Examples("broadcast the first 3 elements of {top players::*}")
@Examples({
"broadcast the first 3 elements of {top players::*}",
"set {_last} to last element of {top players::*}",
"set {_random player} to random element out of all players",
"send 2nd last element of {top players::*} to player",
"set {page2::*} to elements from 11 to 20 of {top players::*}"
})
@Since("2.0, 2.7 (relative to last element), 2.8.0 (range of elements)")
public class ExprElement<T> extends SimpleExpression<T> {

Expand Down
Loading

0 comments on commit aef0572

Please sign in to comment.