Added container transaction support for item frames (...)

Fixed ClassCastException when targeting an armor stand with #container
This commit is contained in:
Intelli 2022-02-17 20:56:20 -07:00
parent e462e68806
commit fb79071ee7
23 changed files with 423 additions and 359 deletions

View file

@ -38,6 +38,7 @@ import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.ItemFrame;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.BlockInventoryHolder;
@ -400,7 +401,7 @@ public class Util extends Queue {
}
public static void mergeItems(Material material, ItemStack[] items) {
if (material != null && material.equals(Material.ARMOR_STAND)) {
if (material != null && (material.equals(Material.ARMOR_STAND) || BukkitAdapter.ADAPTER.isItemFrame(material))) {
return;
}
try {
@ -681,13 +682,17 @@ public class Util extends Queue {
container = location.getBlock();
}
if (type.equals(Material.ARMOR_STAND)) {
if (type == Material.ARMOR_STAND) {
LivingEntity entity = (LivingEntity) container;
EntityEquipment equipment = Util.getEntityEquipment(entity);
if (equipment != null) {
contents = getArmorStandContents(equipment);
}
}
else if (type == Material.ITEM_FRAME) {
ItemFrame entity = (ItemFrame) container;
contents = Util.getItemFrameItem(entity);
}
else {
Block block = (Block) container;
Inventory inventory = Util.getContainerInventory(block.getState(), true);
@ -695,7 +700,8 @@ public class Util extends Queue {
contents = inventory.getContents();
}
}
if (type.equals(Material.ARMOR_STAND)) {
if (type == Material.ARMOR_STAND || type == Material.ITEM_FRAME) {
boolean hasItem = false;
for (ItemStack item : contents) {
if (item != null && !item.getType().equals(Material.AIR)) {
@ -753,6 +759,17 @@ public class Util extends Queue {
return equipment;
}
public static ItemStack[] getItemFrameItem(ItemFrame entity) {
ItemStack[] contents = null;
try {
contents = new ItemStack[] { entity.getItem() };
}
catch (Exception e) {
e.printStackTrace();
}
return contents;
}
public static int getEntityId(EntityType type) {
return getEntityId(type.name(), true);
}
@ -780,10 +797,12 @@ public class Util extends Queue {
switch (type) {
case ARMOR_STAND:
return Material.ARMOR_STAND;
case ITEM_FRAME:
return Material.ITEM_FRAME;
case ENDER_CRYSTAL:
return Material.END_CRYSTAL;
default:
return null;
return BukkitAdapter.ADAPTER.getFrameType(type);
}
}
@ -824,16 +843,6 @@ public class Util extends Queue {
return type;
}
public static int getHangingDelay(Map<String, Integer> hangingDelay, int wid, int x, int y, int z) {
String token = wid + "." + x + "." + y + "." + z;
int delay = 0;
if (hangingDelay.get(token) != null) {
delay = hangingDelay.get(token) + 1;
}
hangingDelay.put(token, delay);
return delay;
}
public static int getItemStackHashCode(ItemStack item) {
try {
return item.hashCode();

View file

@ -3,7 +3,6 @@ package net.coreprotect.utility.entity;
import java.util.Locale;
import org.bukkit.Art;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
@ -15,7 +14,6 @@ import org.bukkit.entity.ItemFrame;
import org.bukkit.entity.Painting;
import org.bukkit.inventory.ItemStack;
import net.coreprotect.CoreProtect;
import net.coreprotect.bukkit.BukkitAdapter;
import net.coreprotect.model.BlockGroup;
import net.coreprotect.utility.Util;
@ -27,178 +25,174 @@ public class HangingUtil {
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void spawnHanging(final BlockState blockstate, final Material rowType, final String hangingData, final int rowData, int delay) {
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(CoreProtect.getInstance(), () -> {
try {
Block block = blockstate.getBlock();
int x = block.getX();
int y = block.getY();
int z = block.getZ();
public static void spawnHanging(final BlockState blockstate, final Material rowType, final String hangingData, final int rowData) {
try {
Block block = blockstate.getBlock();
int x = block.getX();
int y = block.getY();
int z = block.getZ();
BlockFace hangingFace = null;
if (hangingData != null && !hangingData.contains(":") && hangingData.contains("=")) {
BlockFace hangingFace = null;
if (hangingData != null && !hangingData.contains(":") && hangingData.contains("=")) {
try {
hangingFace = BlockFace.valueOf(hangingData.split("=")[1].toUpperCase(Locale.ROOT));
}
catch (Exception e) {
e.printStackTrace();
}
}
for (Entity e : block.getChunk().getEntities()) {
if ((BukkitAdapter.ADAPTER.isItemFrame(rowType) && e instanceof ItemFrame) || (rowType.equals(Material.PAINTING) && e instanceof Painting)) {
Location el = e.getLocation();
if (el.getBlockX() == x && el.getBlockY() == y && el.getBlockZ() == z) {
if (hangingFace == null || ((Hanging) e).getFacing() == hangingFace) {
e.remove();
break;
}
}
}
}
BlockFace faceSet = null;
BlockFace face = null;
if (hangingFace == null) {
Block c1 = block.getWorld().getBlockAt((x + 1), y, z);
Block c2 = block.getWorld().getBlockAt((x - 1), y, z);
Block c3 = block.getWorld().getBlockAt(x, y, (z + 1));
Block c4 = block.getWorld().getBlockAt(x, y, (z - 1));
if (!BlockGroup.NON_ATTACHABLE.contains(c1.getType())) {
faceSet = BlockFace.WEST;
block = c1;
}
else if (!BlockGroup.NON_ATTACHABLE.contains(c2.getType())) {
faceSet = BlockFace.EAST;
block = c2;
}
else if (!BlockGroup.NON_ATTACHABLE.contains(c3.getType())) {
faceSet = BlockFace.NORTH;
block = c3;
}
else if (!BlockGroup.NON_ATTACHABLE.contains(c4.getType())) {
faceSet = BlockFace.SOUTH;
block = c4;
}
if (!Util.solidBlock(Util.getType(block.getRelative(BlockFace.EAST)))) {
face = BlockFace.EAST;
}
else if (!Util.solidBlock(Util.getType(block.getRelative(BlockFace.NORTH)))) {
face = BlockFace.NORTH;
}
else if (!Util.solidBlock(Util.getType(block.getRelative(BlockFace.WEST)))) {
face = BlockFace.WEST;
}
else if (!Util.solidBlock(Util.getType(block.getRelative(BlockFace.SOUTH)))) {
face = BlockFace.SOUTH;
}
}
else {
faceSet = hangingFace;
face = hangingFace;
}
if (faceSet != null && face != null) {
if (rowType.equals(Material.PAINTING)) {
String name = Util.getArtName(rowData);
Art painting = Art.getByName(name.toUpperCase(Locale.ROOT));
int height = painting.getBlockHeight();
int width = painting.getBlockWidth();
int paintingX = x;
int paintingY = y;
int paintingZ = z;
if (height != 1 || width != 1) {
if (height > 1) {
if (height != 3) {
paintingY = paintingY - 1;
}
}
if (width > 1) {
if (faceSet.equals(BlockFace.WEST)) {
paintingZ--;
}
else if (faceSet.equals(BlockFace.SOUTH)) {
paintingX--;
}
}
}
Block spawnBlock = hangingFace != null ? block : block.getRelative(face);
if (hangingFace == null) {
Util.setTypeAndData(spawnBlock, Material.AIR, null, true);
}
Painting hanging = null;
try {
hangingFace = BlockFace.valueOf(hangingData.split("=")[1].toUpperCase(Locale.ROOT));
hanging = block.getWorld().spawn(spawnBlock.getLocation(), Painting.class);
}
catch (Exception e) {
e.printStackTrace();
}
if (hanging != null) {
hanging.teleport(block.getWorld().getBlockAt(paintingX, paintingY, paintingZ).getLocation());
hanging.setFacingDirection(faceSet, true);
hanging.setArt(painting, true);
}
}
for (Entity e : block.getChunk().getEntities()) {
if ((BukkitAdapter.ADAPTER.isItemFrame(rowType) && e instanceof ItemFrame) || (rowType.equals(Material.PAINTING) && e instanceof Painting)) {
Location el = e.getLocation();
if (el.getBlockX() == x && el.getBlockY() == y && el.getBlockZ() == z) {
if (hangingFace == null || ((Hanging) e).getFacing() == hangingFace) {
e.remove();
break;
}
}
}
}
BlockFace faceSet = null;
BlockFace face = null;
if (hangingFace == null) {
Block c1 = block.getWorld().getBlockAt((x + 1), y, z);
Block c2 = block.getWorld().getBlockAt((x - 1), y, z);
Block c3 = block.getWorld().getBlockAt(x, y, (z + 1));
Block c4 = block.getWorld().getBlockAt(x, y, (z - 1));
if (!BlockGroup.NON_ATTACHABLE.contains(c1.getType())) {
faceSet = BlockFace.WEST;
block = c1;
}
else if (!BlockGroup.NON_ATTACHABLE.contains(c2.getType())) {
faceSet = BlockFace.EAST;
block = c2;
}
else if (!BlockGroup.NON_ATTACHABLE.contains(c3.getType())) {
faceSet = BlockFace.NORTH;
block = c3;
}
else if (!BlockGroup.NON_ATTACHABLE.contains(c4.getType())) {
faceSet = BlockFace.SOUTH;
block = c4;
}
if (!Util.solidBlock(Util.getType(block.getRelative(BlockFace.EAST)))) {
face = BlockFace.EAST;
}
else if (!Util.solidBlock(Util.getType(block.getRelative(BlockFace.NORTH)))) {
face = BlockFace.NORTH;
}
else if (!Util.solidBlock(Util.getType(block.getRelative(BlockFace.WEST)))) {
face = BlockFace.WEST;
}
else if (!Util.solidBlock(Util.getType(block.getRelative(BlockFace.SOUTH)))) {
face = BlockFace.SOUTH;
}
}
else {
faceSet = hangingFace;
face = hangingFace;
}
if (faceSet != null && face != null) {
if (rowType.equals(Material.PAINTING)) {
String name = Util.getArtName(rowData);
Art painting = Art.getByName(name.toUpperCase(Locale.ROOT));
int height = painting.getBlockHeight();
int width = painting.getBlockWidth();
int paintingX = x;
int paintingY = y;
int paintingZ = z;
if (height != 1 || width != 1) {
if (height > 1) {
if (height != 3) {
paintingY = paintingY - 1;
}
}
if (width > 1) {
if (faceSet.equals(BlockFace.WEST)) {
paintingZ--;
}
else if (faceSet.equals(BlockFace.SOUTH)) {
paintingX--;
}
}
}
else if (BukkitAdapter.ADAPTER.isItemFrame(rowType)) {
try {
Block spawnBlock = hangingFace != null ? block : block.getRelative(face);
if (hangingFace == null) {
Util.setTypeAndData(spawnBlock, Material.AIR, null, true);
}
Painting hanging = null;
try {
hanging = block.getWorld().spawn(spawnBlock.getLocation(), Painting.class);
}
catch (Exception e) {
}
if (hanging != null) {
hanging.teleport(block.getWorld().getBlockAt(paintingX, paintingY, paintingZ).getLocation());
Class itemFrame = BukkitAdapter.ADAPTER.getFrameClass(rowType);
Entity entity = block.getWorld().spawn(spawnBlock.getLocation(), itemFrame);
if (entity instanceof ItemFrame) {
ItemFrame hanging = (ItemFrame) entity;
hanging.teleport(block.getWorld().getBlockAt(x, y, z).getLocation());
hanging.setFacingDirection(faceSet, true);
hanging.setArt(painting, true);
}
}
else if (BukkitAdapter.ADAPTER.isItemFrame(rowType)) {
try {
Block spawnBlock = hangingFace != null ? block : block.getRelative(face);
if (hangingFace == null) {
Util.setTypeAndData(spawnBlock, Material.AIR, null, true);
}
Class itemFrame = BukkitAdapter.ADAPTER.getFrameClass(rowType);
Entity entity = block.getWorld().spawn(spawnBlock.getLocation(), itemFrame);
if (entity instanceof ItemFrame) {
ItemFrame hanging = (ItemFrame) entity;
hanging.teleport(block.getWorld().getBlockAt(x, y, z).getLocation());
hanging.setFacingDirection(faceSet, true);
Material type = Util.getType(rowData);
if (type != null) {
ItemStack istack = new ItemStack(type, 1);
hanging.setItem(istack);
}
Material type = Util.getType(rowData);
if (type != null) {
ItemStack istack = new ItemStack(type, 1);
hanging.setItem(istack);
}
}
catch (Exception e) {
}
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}, delay);
}
public static void removeHanging(final BlockState block, final String hangingData, int delay) {
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(CoreProtect.getInstance(), () -> {
try {
BlockFace hangingFace = null;
if (hangingData != null && !hangingData.contains(":") && hangingData.contains("=")) {
try {
hangingFace = BlockFace.valueOf(hangingData.split("=")[1].toUpperCase(Locale.ROOT));
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
for (Entity e : block.getChunk().getEntities()) {
if (e instanceof ItemFrame || e instanceof Painting) {
Location el = e.getLocation();
if (el.getBlockX() == block.getX() && el.getBlockY() == block.getY() && el.getBlockZ() == block.getZ()) {
if (hangingFace == null || ((Hanging) e).getFacing() == hangingFace) {
e.remove();
}
public static void removeHanging(final BlockState block, final String hangingData) {
try {
BlockFace hangingFace = null;
if (hangingData != null && !hangingData.contains(":") && hangingData.contains("=")) {
try {
hangingFace = BlockFace.valueOf(hangingData.split("=")[1].toUpperCase(Locale.ROOT));
}
catch (Exception e) {
e.printStackTrace();
}
}
for (Entity e : block.getChunk().getEntities()) {
if (e instanceof ItemFrame || e instanceof Painting) {
Location el = e.getLocation();
if (el.getBlockX() == block.getX() && el.getBlockY() == block.getY() && el.getBlockZ() == block.getZ()) {
if (hangingFace == null || ((Hanging) e).getFacing() == hangingFace) {
e.remove();
}
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}, delay);
}
catch (Exception e) {
e.printStackTrace();
}
}
}