Skip to content

Commit

Permalink
Merge branch 'dev/patch' into patch/1.21-support
Browse files Browse the repository at this point in the history
  • Loading branch information
sovdeeth authored Jul 1, 2024
2 parents eff0972 + 6ee4628 commit d01fa48
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 41 deletions.
9 changes: 6 additions & 3 deletions src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
import org.bukkit.damage.DamageSource;
import org.bukkit.damage.DamageType;
import org.bukkit.entity.Damageable;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class HealthUtils {

Expand Down Expand Up @@ -112,8 +112,11 @@ public static double getFinalDamage(EntityDamageEvent e) {
return e.getFinalDamage() / 2;
}

public static void setDamage(EntityDamageEvent e, double damage) {
e.setDamage(damage * 2);
public static void setDamage(EntityDamageEvent event, double damage) {
event.setDamage(damage * 2);
// Set last damage manually as Bukkit doesn't appear to do that
if (event.getEntity() instanceof LivingEntity)
((LivingEntity) event.getEntity()).setLastDamage(damage * 2);
}

@Nullable
Expand Down
35 changes: 25 additions & 10 deletions src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,17 @@ public class ItemUtils {
* @return Damage.
*/
public static int getDamage(ItemStack itemStack) {
ItemMeta meta = itemStack.getItemMeta();
if (meta instanceof Damageable)
return ((Damageable) meta).getDamage();
return getDamage(itemStack.getItemMeta());
}

/**
* Gets damage/durability of an itemmeta, or 0 if it does not have damage.
* @param itemMeta ItemMeta.
* @return Damage.
*/
public static int getDamage(ItemMeta itemMeta) {
if (itemMeta instanceof Damageable)
return ((Damageable) itemMeta).getDamage();
return 0; // Non damageable item
}

Expand Down Expand Up @@ -143,15 +151,22 @@ public static Material asItem(Material type) {
/**
* Tests whether two item stacks are of the same type, i.e. it ignores the amounts.
*
* @param is1
* @param is2
* @param itemStack1
* @param itemStack2
* @return Whether the item stacks are of the same type
*/
public static boolean itemStacksEqual(final @Nullable ItemStack is1, final @Nullable ItemStack is2) {
if (is1 == null || is2 == null)
return is1 == is2;
return is1.getType() == is2.getType() && ItemUtils.getDamage(is1) == ItemUtils.getDamage(is2)
&& is1.getItemMeta().equals(is2.getItemMeta());
public static boolean itemStacksEqual(@Nullable ItemStack itemStack1, @Nullable ItemStack itemStack2) {
if (itemStack1 == null || itemStack2 == null)
return itemStack1 == itemStack2;
if (itemStack1.getType() != itemStack2.getType())
return false;

ItemMeta itemMeta1 = itemStack1.getItemMeta();
ItemMeta itemMeta2 = itemStack2.getItemMeta();
if (itemMeta1 == null || itemMeta2 == null)
return itemMeta1 == itemMeta2;

return itemStack1.getItemMeta().equals(itemStack2.getItemMeta());
}

// Only 1.15 and versions after have Material#isAir method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ public DefaultConverters() {}
return ((Block) holder).getLocation();
if (holder instanceof BlockState)
return BlockUtils.getLocation(((BlockState) holder).getBlock());
if (holder instanceof DoubleChest) {
DoubleChest doubleChest = (DoubleChest) holder;
if (doubleChest.getLeftSide() != null) {
return BlockUtils.getLocation(((BlockState) doubleChest.getLeftSide()).getBlock());
} else if (((DoubleChest) holder).getRightSide() != null) {
return BlockUtils.getLocation(((BlockState) doubleChest.getRightSide()).getBlock());
}
}
return null;
});

Expand Down
46 changes: 18 additions & 28 deletions src/main/java/ch/njol/skript/expressions/ExprLastDamage.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,47 +19,34 @@
*/
package ch.njol.skript.expressions;

import org.bukkit.entity.LivingEntity;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

import ch.njol.skript.classes.Changer.ChangeMode;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.expressions.base.SimplePropertyExpression;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.util.Kleenean;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

@Name("Last Damage")
@Description("The last damage that was done to an entity. Note that changing it doesn't deal more/less damage.")
@Examples({"set last damage of event-entity to 2"})
@Since("2.5.1")
public class ExprLastDamage extends SimplePropertyExpression<LivingEntity, Number> {

static {
register(ExprLastDamage.class, Number.class, "last damage", "livingentities");
}

@Nullable
private ExprDamage damageExpr;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
damageExpr = new ExprDamage();
return true;
}


@Nullable
@Override
@SuppressWarnings("null")
public Number convert(LivingEntity livingEntity) {
return damageExpr.get(livingEntity.getLastDamageCause())[0];
return livingEntity.getLastDamage() / 2;
}

@Nullable
@Override
public Class<?>[] acceptChange(ChangeMode mode) {
Expand All @@ -72,35 +59,38 @@ public Class<?>[] acceptChange(ChangeMode mode) {
return null;
}
}


@SuppressWarnings("ConstantValue")
@Override
public void change(Event e, @Nullable Object[] delta, ChangeMode mode) {
if (delta != null) {
if (delta != null && delta[0] instanceof Number) {
double damage = ((Number) delta[0]).doubleValue() * 2;

switch (mode) {
case SET:
for (LivingEntity entity : getExpr().getArray(e))
entity.setLastDamage((Long) delta[0]);
entity.setLastDamage(damage);
break;
case REMOVE:
delta[0] = (Long) delta[0] * -1;
damage = damage * -1;
case ADD:
for (LivingEntity entity : getExpr().getArray(e))
entity.setLastDamage((Long) delta[0] + entity.getLastDamage());
entity.setLastDamage(damage + entity.getLastDamage());
break;
default:
assert false;
}
}
}

@Override
public Class<? extends Number> getReturnType() {
return Number.class;
}

@Override
protected String getPropertyName() {
return "last damage";
}

}
9 changes: 9 additions & 0 deletions src/main/resources/lang/default.lang
Original file line number Diff line number Diff line change
Expand Up @@ -2168,6 +2168,8 @@ attribute types:
generic_attack_damage: generic attack damage, attack damage
generic_attack_knockback: generic attack knockback, attack knockback
generic_attack_speed: generic attack speed, attack speed
generic_burning_time: generic burning time, burning time
generic_explosion_knockback_resistance: generic explosion knockback resistance, explosion knockback resistance
generic_flying_speed: generic flying speed, flying speed
generic_follow_range: generic follow range, follow range
generic_gravity: generic gravity, gravity
Expand All @@ -2176,15 +2178,22 @@ attribute types:
generic_luck: generic luck, luck
generic_max_absorption: generic max absorption, max absorption
generic_max_health: generic max health, max health
generic_movement_efficiency: generic movement efficiency, movement efficiency
generic_movement_speed: generic movement speed, movement speed
generic_oxygen_bonus: generic oxygen bonus, oxygen bonus
generic_safe_fall_distance: generic safe fall distance, safe fall distance
generic_fall_damage_multiplier: generic fall damage multiplier, fall damage multiplier
generic_scale: generic scale, scale
generic_step_height: generic step height, step height
generic_water_movement_efficiency: generic water movement efficiency, water movement efficiency
horse_jump_strength: horse jump strength
player_block_break_speed: player block break speed, block break speed
player_block_interaction_range: player block interaction range, block interaction range
player_entity_interaction_range: player entity interaction range, entity interaction range
player_mining_efficiency: player mining efficiency, mining efficiency
player_sneaking_speed: player sneaking speed, sneaking speed
player_submerged_mining_speed: player submerged mining speed, submerged mining speed
player_sweeping_damage_ratio: player sweeping damage ratio, sweeping damage ratio
zombie_spawn_reinforcements: zombie spawn reinforcements

# -- Environments --
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
test "inventory holder location double chest":
set {_b::1} to the block at spawn of world "world"
set {_b::2} to the block north of {_b::1}
set {_prev::1} to type of block at {_b::1}
set {_prev::2} to type of block at {_b::2}

set block at {_b::1} to chest[facing=east;type=right]
set block at {_b::2} to chest[facing=east;type=left]

set {_inv} to inventory of {_b::1}
set {_holder} to holder of {_inv}

set {_a-loc} to location of {_holder}
set {_b-loc::*} to location of {_b::1} and location of {_b::2}

# clean up first in case assert fails
set block at {_b::1} to {_prev::1}
set block at {_b::2} to {_prev::2}

assert {_b-loc::*} contains {_a-loc} with "holder location of double chest differs from block location"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
test "remove air from air slot":
set {_chest} to chest inventory with 3 rows
# throws exception if not fixed
remove 1 of (slot 0 of {_chest}) from (slot 0 of {_chest})
15 changes: 15 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprLastDamage.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
test "last damage":
set {_l} to location 0.5 above highest block at location(1,1,1)
spawn a sheep at {_l}
set {_e} to last spawned entity

assert last damage of {_e} = 0 with "last damage of newly spawned entity should be 0"

damage {_e} by 1
assert last damage of {_e} = 1 with "last damage of entity should be 1 after damaging it by 1"

set last damage of {_e} to 3
assert last damage of {_e} = 3 with "last damage of entity should be 3 after setting to 3"

# thank you for your service
delete entity in {_e}

0 comments on commit d01fa48

Please sign in to comment.