Fix for CaveAir issue.

It wasn't possible to create Storages where the block to replace was CaveAir!
This commit is contained in:
jameslfc19 2020-07-24 17:29:12 +01:00
parent 86322f02d9
commit 80aef139ac
3 changed files with 49 additions and 8 deletions

View File

@ -1,6 +1,5 @@
package com.jamesdpeters.minecraft.chests.listeners;
import com.jamesdpeters.minecraft.chests.ChestsPlusPlus;
import com.jamesdpeters.minecraft.chests.misc.Messages;
import com.jamesdpeters.minecraft.chests.misc.Utils;
import com.jamesdpeters.minecraft.chests.misc.Values;
@ -18,7 +17,6 @@ import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Chest;
import org.bukkit.block.Sign;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Directional;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
@ -39,8 +37,6 @@ import java.util.stream.Collectors;
public class StorageListener implements Listener {
private BlockData air = Material.AIR.createBlockData();
@EventHandler
public void playerInteract(BlockPlaceEvent event){
if(event.getBlockPlaced().getState() instanceof Sign){
@ -127,7 +123,7 @@ public class StorageListener implements Listener {
BlockFace blockFace = storageType.onStoragePlacedBlockFace(event.getPlayer(), event.getBlockPlaced());
Block signSpace = event.getBlockPlaced().getRelative(blockFace);
if (signSpace.getType() != Material.AIR) {
if (!Utils.isAir(signSpace)) {
event.setCancelled(true);
return;
}

View File

@ -20,6 +20,14 @@ import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.RegisteredListener;
import org.bukkit.util.Vector;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
@ -209,4 +217,40 @@ public class Utils {
public static <T> void addIfNotNull(List<T> list, T value){
if(value != null) list.add(value);
}
public static void saveLoadedChunksToCSV(){
PrintWriter outputFile = getOutputFile("LoadedChunks@"+System.currentTimeMillis()+".csv"); // this sends the output to file1
// Write the file as a comma seperated file (.csv) so it can be read it into EXCEL
outputFile.println("Chunk X, Chunk Z, World");
// now make a loop to write the contents of each step to disk, one number at a time
Bukkit.getWorlds().forEach(world -> {
for (Chunk loadedChunk : world.getLoadedChunks()) {
outputFile.println(loadedChunk.getX()+", "+loadedChunk.getZ()+", "+world.getName());
}
});
outputFile.close(); // close the output file
System.out.println("Saved CSV Data");
}
private static PrintWriter getOutputFile(String filenamePath){
try {
Path path = Paths.get("outputs/"+filenamePath);
Files.createDirectories(path.getParent());
FileWriter file = new FileWriter(String.valueOf(path)); // this creates the file with the given name
return new PrintWriter(file); // this sends the output to file
} catch (IOException e) {
System.err.println("File couldn't be accessed it may be being used by another process!");
System.err.println("Close the file and press Enter to try again!");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try { reader.readLine(); } catch (IOException ex) { ex.printStackTrace(); }
return getOutputFile(filenamePath);
}
}
public static boolean isAir(Block block){
return (block.getType() == Material.AIR) || (block.getType() == Material.CAVE_AIR);
}
}

View File

@ -4,6 +4,7 @@ package com.jamesdpeters.minecraft.chests.storage.abstracts;
import com.jamesdpeters.minecraft.chests.ChestsPlusPlus;
import com.jamesdpeters.minecraft.chests.misc.Messages;
import com.jamesdpeters.minecraft.chests.misc.Settings;
import com.jamesdpeters.minecraft.chests.misc.Utils;
import com.jamesdpeters.minecraft.chests.misc.Values;
import com.jamesdpeters.minecraft.chests.serialize.Config;
import com.jamesdpeters.minecraft.chests.serialize.ConfigStorage;
@ -281,7 +282,7 @@ public abstract class StorageType<T extends AbstractStorage> {
/* HELPER UTILS */
protected void placeSign(Block placedAgainst, Block toReplace, BlockFace facing, Player player, OfflinePlayer ownerPlayer, String identifier, String linkTag, boolean requireSign){
if(toReplace.getType() == Material.AIR){
if(Utils.isAir(toReplace)){
BlockState replacedBlockState = toReplace.getState();
Material signMaterial = Material.OAK_WALL_SIGN;
@ -322,7 +323,7 @@ public abstract class StorageType<T extends AbstractStorage> {
if(owner != null) {
lines[2] = owner;
}
Material airType = toReplace.getType();
Material wallSign = Material.getMaterial(signMaterial.name().replace("SIGN", "WALL_SIGN"));
toReplace.setType(wallSign != null ? wallSign : Material.OAK_WALL_SIGN);
Sign sign = (Sign) toReplace.getState();
@ -335,7 +336,7 @@ public abstract class StorageType<T extends AbstractStorage> {
BlockPlaceEvent event = new BlockPlaceEvent(sign.getBlock(),replacedBlockState,placedAgainst,new ItemStack(Material.AIR),player,true, EquipmentSlot.HAND);
ChestsPlusPlus.PLUGIN.getServer().getPluginManager().callEvent(event);
if(event.isCancelled()){
sign.setType(Material.AIR);
sign.setType(airType);
return;
}