mirror of
https://github.com/songoda/FabledSkyBlock.git
synced 2024-09-29 23:17:26 +02:00
Use getBlockFast method from SongodaCore
This commit is contained in:
parent
93ae3e3e05
commit
7552fc6c91
@ -4,6 +4,7 @@ import com.google.common.io.Files;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.songoda.core.compatibility.CompatibleMaterial;
|
||||
import com.songoda.skyblock.SkyBlock;
|
||||
import com.songoda.skyblock.config.FileManager;
|
||||
import com.songoda.skyblock.utils.Compression;
|
||||
@ -18,16 +19,11 @@ import com.songoda.skyblock.utils.world.entity.EntityUtil;
|
||||
import java.io.FileInputStream;
|
||||
import java.util.Base64;
|
||||
|
||||
import net.minecraft.server.v1_15_R1.ChunkSection;
|
||||
import net.minecraft.server.v1_15_R1.IBlockData;
|
||||
import net.minecraft.server.v1_15_R1.World;
|
||||
import net.minecraft.server.v1_15_R1.WorldServer;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.craftbukkit.v1_15_R1.CraftWorld;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
@ -58,22 +54,23 @@ public final class StructureUtil {
|
||||
|
||||
String originBlockLocation = "";
|
||||
|
||||
for (Block blockList : blocks.keySet()) {
|
||||
Location location = blocks.get(blockList);
|
||||
for (Block block : blocks.keySet()) {
|
||||
Location location = blocks.get(block);
|
||||
CompatibleMaterial material = CompatibleMaterial.getMaterial(block);
|
||||
|
||||
if (location.isOriginLocation()) {
|
||||
originBlockLocation = location.getX() + ":" + location.getY() + ":" + location.getZ() + ":" + positions[0].getWorld().getName();
|
||||
|
||||
if (blockList.getType() == Material.AIR) {
|
||||
blockData.add(BlockUtil.convertBlockToBlockData(blockList, location.getX(), location.getY(), location.getZ()));
|
||||
if (material == CompatibleMaterial.AIR) {
|
||||
blockData.add(BlockUtil.convertBlockToBlockData(block, location.getX(), location.getY(), location.getZ()));
|
||||
}
|
||||
}
|
||||
|
||||
if (blockList.getType() == Material.AIR) {
|
||||
if (material == CompatibleMaterial.AIR) {
|
||||
continue;
|
||||
}
|
||||
|
||||
blockData.add(BlockUtil.convertBlockToBlockData(blockList, location.getX(), location.getY(), location.getZ()));
|
||||
blockData.add(BlockUtil.convertBlockToBlockData(block, location.getX(), location.getY(), location.getZ()));
|
||||
}
|
||||
|
||||
for (Entity entityList : entities.keySet()) {
|
||||
@ -174,13 +171,13 @@ public final class StructureUtil {
|
||||
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(SkyBlock.getInstance(), () -> {
|
||||
try {
|
||||
org.bukkit.Location blockRotationLocation = LocationUtil.rotateLocation(new org.bukkit.Location(location.getWorld(), blockDataList.getX(), blockDataList.getY(), blockDataList.getZ()), type);
|
||||
org.bukkit.Location blockLocation = new org.bukkit.Location(location.getWorld(), location.getX() - Math.abs(Integer.valueOf(storage.getOriginLocation().split(":")[0])),
|
||||
location.getY() - Integer.valueOf(storage.getOriginLocation().split(":")[1]), location.getZ() + Math.abs(Integer.valueOf(storage.getOriginLocation().split(":")[2])));
|
||||
org.bukkit.Location blockLocation = new org.bukkit.Location(location.getWorld(), location.getX() - Math.abs(Integer.parseInt(storage.getOriginLocation().split(":")[0])),
|
||||
location.getY() - Integer.parseInt(storage.getOriginLocation().split(":")[1]), location.getZ() + Math.abs(Integer.parseInt(storage.getOriginLocation().split(":")[2])));
|
||||
blockLocation.add(blockRotationLocation);
|
||||
BlockUtil.convertBlockDataToBlock(blockLocation.getBlock(), blockDataList);
|
||||
} catch (Exception e) {
|
||||
SkyBlock.getInstance().getLogger()
|
||||
.warning("Unable to convert BlockData to Block for type {" + blockDataList.getMaterial() + ":" + blockDataList.getData() + "} in structure {" + structure.getStructureFile() + "}");
|
||||
.warning("Unable to convert BlockData to Block for type {" + blockDataList.getCompatibleMaterial() + ":" + blockDataList.getData() + "} in structure {" + structure.getStructureFile() + "}");
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -191,8 +188,8 @@ public final class StructureUtil {
|
||||
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(SkyBlock.getInstance(), () -> {
|
||||
try {
|
||||
org.bukkit.Location blockRotationLocation = LocationUtil.rotateLocation(new org.bukkit.Location(location.getWorld(), entityDataList.getX(), entityDataList.getY(), entityDataList.getZ()), type);
|
||||
org.bukkit.Location blockLocation = new org.bukkit.Location(location.getWorld(), location.getX() - Math.abs(Integer.valueOf(storage.getOriginLocation().split(":")[0])),
|
||||
location.getY() - Integer.valueOf(storage.getOriginLocation().split(":")[1]), location.getZ() + Math.abs(Integer.valueOf(storage.getOriginLocation().split(":")[2])));
|
||||
org.bukkit.Location blockLocation = new org.bukkit.Location(location.getWorld(), location.getX() - Math.abs(Integer.parseInt(storage.getOriginLocation().split(":")[0])),
|
||||
location.getY() - Integer.parseInt(storage.getOriginLocation().split(":")[1]), location.getZ() + Math.abs(Integer.parseInt(storage.getOriginLocation().split(":")[2])));
|
||||
blockLocation.add(blockRotationLocation);
|
||||
EntityUtil.convertEntityDataToEntity(entityDataList, blockLocation, type);
|
||||
} catch (Exception e) {
|
||||
|
@ -1,10 +1,10 @@
|
||||
package com.songoda.skyblock.utils.world.block;
|
||||
|
||||
import com.songoda.core.compatibility.CompatibleMaterial;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Color;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.SkullType;
|
||||
import org.bukkit.block.Biome;
|
||||
import org.bukkit.block.BlockFace;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.potion.PotionEffectType;
|
||||
@ -17,9 +17,9 @@ import java.util.Map;
|
||||
@SuppressWarnings("deprecation")
|
||||
public class BlockData {
|
||||
|
||||
private String material = Material.AIR.toString();
|
||||
private String blockData = "";
|
||||
private String biome = Biome.PLAINS.toString();
|
||||
private CompatibleMaterial compatibleMaterial;
|
||||
private String material;
|
||||
private String biome;
|
||||
private String stateType = BlockStateType.NORMAL.toString();
|
||||
private String dataType = BlockDataType.NORMAL.toString();
|
||||
private String baseColor = Color.WHITE.toString();
|
||||
@ -46,7 +46,7 @@ public class BlockData {
|
||||
private int fuelLevel = 0;
|
||||
private int delay = 0;
|
||||
|
||||
private byte data = (byte) 0;
|
||||
private byte data;
|
||||
|
||||
private short burnTime = (short) 0;
|
||||
private short cookTime = (short) 0;
|
||||
@ -55,10 +55,8 @@ public class BlockData {
|
||||
|
||||
private boolean exactTeleport = true;
|
||||
|
||||
public BlockData(String material, byte data, int x, int y, int z, String biome) {
|
||||
this.material = material;
|
||||
this.data = data;
|
||||
|
||||
public BlockData(CompatibleMaterial material, int x, int y, int z, String biome) {
|
||||
this.compatibleMaterial = material;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
@ -66,20 +64,14 @@ public class BlockData {
|
||||
this.biome = biome;
|
||||
}
|
||||
|
||||
public String getMaterial() {
|
||||
return this.material;
|
||||
public CompatibleMaterial getCompatibleMaterial() {
|
||||
if (compatibleMaterial == null)
|
||||
return CompatibleMaterial.getMaterial(material);
|
||||
return compatibleMaterial;
|
||||
}
|
||||
|
||||
public void setMaterial(String material) {
|
||||
this.material = material;
|
||||
}
|
||||
|
||||
public String getBlockData() {
|
||||
return blockData;
|
||||
}
|
||||
|
||||
public void setBlockData(String blockData) {
|
||||
this.blockData = blockData;
|
||||
public void setMaterial(CompatibleMaterial material) {
|
||||
this.compatibleMaterial = material;
|
||||
}
|
||||
|
||||
public String getBiome() {
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.songoda.skyblock.utils.world.block;
|
||||
|
||||
import com.songoda.core.compatibility.CompatibleMaterial;
|
||||
import com.songoda.core.utils.BlockUtils;
|
||||
import com.songoda.skyblock.utils.item.ItemStackUtil;
|
||||
import com.songoda.skyblock.utils.version.NMSUtil;
|
||||
import org.bukkit.*;
|
||||
@ -18,18 +19,14 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public final class BlockUtil {
|
||||
public final class BlockUtil extends BlockUtils {
|
||||
|
||||
public static BlockData convertBlockToBlockData(Block block, int x, int y, int z) {
|
||||
BlockData blockData = new BlockData(block.getType().toString(), block.getData(), x, y, z, block.getBiome().toString());
|
||||
BlockData blockData = new BlockData(CompatibleMaterial.getMaterial(block), x, y, z, block.getBiome().toString());
|
||||
|
||||
int NMSVersion = NMSUtil.getVersionNumber();
|
||||
blockData.setVersion(NMSVersion);
|
||||
|
||||
if (NMSVersion > 12) {
|
||||
blockData.setBlockData(block.getBlockData().getAsString());
|
||||
}
|
||||
|
||||
BlockState blockState = block.getState();
|
||||
MaterialData materialData = blockState.getData();
|
||||
|
||||
@ -249,14 +246,9 @@ public final class BlockUtil {
|
||||
public static void convertBlockDataToBlock(Block block, BlockData blockData) {
|
||||
int NMSVersion = NMSUtil.getVersionNumber();
|
||||
|
||||
Material material = null;
|
||||
CompatibleMaterial material = blockData.getCompatibleMaterial();
|
||||
|
||||
if (NMSVersion > 12 && blockData.getVersion() > 12 && blockData.getBlockData() != null) {
|
||||
block.setBlockData(Bukkit.getServer().createBlockData(blockData.getBlockData()));
|
||||
} else {
|
||||
material = CompatibleMaterial.getMaterial(blockData.getMaterial()).getMaterial();
|
||||
setBlockFast(block.getWorld(), block.getX(), block.getY(), block.getZ(), material, blockData.getData());
|
||||
}
|
||||
setBlockFast(block.getWorld(), block.getX(), block.getY(), block.getZ(), material, blockData.getData());
|
||||
|
||||
// TODO Create a class to support biome changes
|
||||
// block.setBiome(Biome.valueOf(blockData.getBiome().toUpperCase()));
|
||||
@ -420,9 +412,10 @@ public final class BlockUtil {
|
||||
stairs.setFacingDirection(BlockFace.valueOf(blockData.getFacing()));
|
||||
state.setData(stairs);
|
||||
} else if (blockDataType == BlockDataType.FLOWERPOT) {
|
||||
if (NMSVersion >= 8 && NMSVersion <= 12) {
|
||||
setBlockFast(block.getWorld(), block.getX(), block.getY() - 1, block.getZ(), CompatibleMaterial.STONE, (byte) 0);
|
||||
if (NMSVersion >= 8 && NMSVersion <= 12) {
|
||||
if (block.getLocation().clone().subtract(0.0D, 1.0D, 0.0D).getBlock().getType() == Material.AIR) {
|
||||
setBlockFast(block.getWorld(), block.getX(), block.getY() - 1, block.getZ(), Material.STONE, (byte) 0);
|
||||
setBlockFast(block.getWorld(), block.getX(), block.getY() - 1, block.getZ(), CompatibleMaterial.STONE, (byte) 0);
|
||||
}
|
||||
|
||||
if (blockData.getFlower() != null && !blockData.getFlower().isEmpty()) {
|
||||
@ -430,10 +423,10 @@ public final class BlockUtil {
|
||||
String[] flower = blockData.getFlower().split(":");
|
||||
int materialData = Integer.parseInt(flower[1]);
|
||||
|
||||
material = CompatibleMaterial.getMaterial(flower[0].toUpperCase()).getMaterial();
|
||||
material = CompatibleMaterial.getMaterial(flower[0].toUpperCase());
|
||||
|
||||
if (material != null) {
|
||||
ItemStack is = new ItemStack(material, 1, (byte) materialData);
|
||||
ItemStack is = new ItemStack(material.getMaterial(), 1, (byte) materialData);
|
||||
|
||||
World world = block.getWorld();
|
||||
|
||||
@ -468,16 +461,16 @@ public final class BlockUtil {
|
||||
|
||||
if (blockData.getVersion() > 12) {
|
||||
if (NMSVersion > 12) {
|
||||
material = Material.valueOf(flower[0].toUpperCase());
|
||||
material = CompatibleMaterial.valueOf(flower[0].toUpperCase());
|
||||
}
|
||||
} else {
|
||||
if (NMSVersion < 13) {
|
||||
material = Material.valueOf(flower[0].toUpperCase());
|
||||
material = CompatibleMaterial.valueOf(flower[0].toUpperCase());
|
||||
}
|
||||
}
|
||||
|
||||
if (material != null) {
|
||||
flowerPot.setContents(new MaterialData(material, (byte) Integer.parseInt(flower[1])));
|
||||
flowerPot.setContents(new MaterialData(material.getMaterial(), (byte) Integer.parseInt(flower[1])));
|
||||
}
|
||||
|
||||
state.setData(flowerPot);
|
||||
@ -485,7 +478,7 @@ public final class BlockUtil {
|
||||
}
|
||||
}
|
||||
|
||||
if (blockData.getMaterial().equals("DOUBLE_PLANT")) {
|
||||
if (blockData.getCompatibleMaterial().equals("DOUBLE_PLANT")) {
|
||||
Block topBlock = block.getLocation().add(0.0D, 1.0D, 0.0D).getBlock();
|
||||
Block bottomBlock = block.getLocation().subtract(0.0D, 1.0D, 0.0D).getBlock();
|
||||
|
||||
@ -516,36 +509,4 @@ public final class BlockUtil {
|
||||
|
||||
return nearbyBlocks;
|
||||
}
|
||||
|
||||
private static Class<?> IBlockDataClass = NMSUtil.getNMSClass("IBlockData");
|
||||
private static Class<?> blocksClass = NMSUtil.getNMSClass("Blocks");
|
||||
|
||||
|
||||
private static void setBlockFast(World world, int x, int y, int z, Material material, byte data) {
|
||||
try {
|
||||
Object worldHandle = world.getClass().getMethod("getHandle").invoke(world);
|
||||
Object chunk = worldHandle.getClass().getMethod("getChunkAt", int.class, int.class).invoke(worldHandle, x >> 4, z >> 4);
|
||||
Object blockPosition = NMSUtil.getNMSClass("BlockPosition").getConstructor(int.class, int.class, int.class).newInstance(x & 0xF, y, z & 0xF);
|
||||
|
||||
if (NMSUtil.getVersionNumber() > 12) {
|
||||
Object block = blocksClass.getField(material.name()).get(null);
|
||||
Object IBlockData = block.getClass().getMethod("getBlockData").invoke(block);
|
||||
worldHandle.getClass().getMethod("setTypeAndData", blockPosition.getClass(), IBlockDataClass, int.class).invoke(worldHandle, blockPosition, IBlockData, 2);
|
||||
|
||||
try {
|
||||
chunk.getClass().getMethod("a", blockPosition.getClass(), IBlockDataClass, boolean.class).invoke(chunk, blockPosition, IBlockData, true);
|
||||
} catch (Exception ignored) {
|
||||
chunk.getClass().getMethod("setType", blockPosition.getClass(), IBlockDataClass, boolean.class).invoke(chunk, blockPosition, IBlockData, true);
|
||||
}
|
||||
} else {
|
||||
Object IBlockData = NMSUtil.getNMSClass("Block").getMethod("getByCombinedId", int.class).invoke(null, material.getId() + (data << 12));
|
||||
worldHandle.getClass().getMethod("setTypeAndData", blockPosition.getClass(), IBlockDataClass, int.class).invoke(worldHandle, blockPosition, IBlockData, 3);
|
||||
chunk.getClass().getMethod("a", blockPosition.getClass(), IBlockDataClass).invoke(chunk, blockPosition, IBlockData);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Block block = world.getBlockAt(x, y, z);
|
||||
block.getState().setRawData(data);
|
||||
block.setType(material);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user