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

ItemData/BlockCompat - strip out BlockState middleman #6473

Merged
merged 8 commits into from
Mar 30, 2024
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
34 changes: 25 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,26 @@ 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) {
if (blockState == null) return false;
return isOfType(blockState.getBlockData());
ShaneBeee marked this conversation as resolved.
Show resolved Hide resolved
}

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
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
Loading