Fixed Sign Placement Bug!

Signs were able to be placed on the sides of storages. And some checks were missing!
This commit is contained in:
jameslfc19 2020-07-08 00:31:00 +01:00
parent a709d85917
commit 554c43b3fc
9 changed files with 60 additions and 20 deletions

View File

@ -6,7 +6,7 @@
<groupId>com.jamesdpeters.minecraft.chests</groupId>
<artifactId>ChestsPlusPlus-Master</artifactId>
<version>1.5-BETA-5</version>
<version>1.5-BETA-7</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>

View File

@ -6,6 +6,7 @@ import com.jamesdpeters.minecraft.chests.runnables.ChestLinkVerifier;
import com.jamesdpeters.minecraft.chests.storage.abstracts.AbstractStorage;
import com.jamesdpeters.minecraft.chests.serialize.Config;
import com.jamesdpeters.minecraft.chests.storage.abstracts.StorageType;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
@ -23,6 +24,8 @@ import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.persistence.PersistentDataType;
import java.util.stream.Collectors;
public class StorageListener implements Listener {
@EventHandler
@ -35,22 +38,22 @@ public class StorageListener implements Listener {
if (event.getBlockPlaced().getLocation().equals(signChangeEvent.getBlock().getLocation())) {
Sign sign = (Sign) signChangeEvent.getBlock().getState();
for (StorageType storageType : Config.getStorageTypes()) {
for (StorageType storageType : Config.getStorageTypes().stream().filter(storageType -> storageType.isValidBlockType(event.getBlockAgainst())).collect(Collectors.toList())) {
if(storageType.hasPermissionToAdd(event.getPlayer())) {
StorageInfo info = storageType.getStorageUtils().getStorageInfo(sign, signChangeEvent.getLines(), event.getPlayer().getUniqueId());
if (info != null) {
Location signLocation = event.getBlockPlaced().getLocation();
if (storageType.getStorageUtils().isValidSignPosition(signLocation)) {
if(!storageType.add(event.getPlayer(), info.getGroup(), event.getBlockAgainst().getLocation(), info.getPlayer())){
Location signLocation = event.getBlockPlaced().getLocation();
if (storageType.getStorageUtils().isValidSignPosition(signLocation)) {
StorageInfo info = storageType.getStorageUtils().getStorageInfo(sign, signChangeEvent.getLines(), event.getPlayer().getUniqueId());
if (info != null) {
if (!storageType.add(event.getPlayer(), info.getGroup(), event.getBlockAgainst().getLocation(), info.getPlayer())) {
sign.getBlock().breakNaturally();
done();
return;
}
storageType.getMessages().storageAdded(event.getPlayer(), signChangeEvent.getLine(1), info.getPlayer().getName());
signChange(sign,signChangeEvent,info.getPlayer(),event.getPlayer());
} else {
storageType.getMessages().invalidSignPlacement(event.getPlayer());
signChange(sign, signChangeEvent, info.getPlayer(), event.getPlayer());
}
} else {
storageType.getMessages().invalidSignPlacement(event.getPlayer());
}
} else {
Messages.NO_PERMISSION(event.getPlayer());

View File

@ -32,6 +32,7 @@ public class StorageUtils<T extends StorageInfo<S>, S extends AbstractStorage> {
String playerUUID = sign.getPersistentDataContainer().get(Values.playerUUID, PersistentDataType.STRING);
String group = ChatColor.stripColor(StringUtils.substringBetween(lines[1], "[", "]"));
if(playerUUID == null){
if(uuid == null) return null;
playerUUID = uuid.toString();
if(lines[2] != null){
OfflinePlayer owner = Config.getOfflinePlayer(lines[2]);
@ -93,14 +94,16 @@ public class StorageUtils<T extends StorageInfo<S>, S extends AbstractStorage> {
BlockFace facing = sign.getFacing().getOppositeFace();
Block toTest = block.getRelative(facing);
//Return if block isn't valid
return storageType.isValidBlockType(toTest);
//Check if block face is a valid place for a sign!
if(!storageType.getValidBlockFaces(toTest).contains(sign.getFacing())) return false;
// //Check if block placed against is already part of this group.
// if(block.getState() instanceof Sign) {
// StorageInfo info = getStorageInfo((Sign) block.getState());
// return (info == null);
// }
//Return if block isn't valid
if (!storageType.isValidBlockType(toTest)) return false;
//Check if block placed against is already part of this group.
StorageInfo info = getStorageInfo(toTest.getLocation());
System.out.println(info);
return (info == null);
}
return false;
}

View File

@ -144,6 +144,11 @@ public abstract class AbstractStorage implements ConfigurationSerializable {
public abstract String getIdentifier();
public abstract boolean shouldDisplayArmourStands();
/**
* @return whether to drop the inventory of this storage when it's removed.
*/
public abstract boolean dropInventory();
/**
* This is the distance from a full block to the size of the storage block. (e.g Chest is smaller than a regular block.)
* @return

View File

@ -81,6 +81,12 @@ public abstract class StorageType<T extends AbstractStorage> {
*/
public abstract BlockFace getStorageFacing(Block block);
/**
* @param block - the block being tested.
* @return A list of @{@link BlockFace} that are valid to place a sign on this type of storage.
*/
public abstract List<BlockFace> getValidBlockFaces(Block block);
public abstract StorageMessages getMessages();
/*
@ -251,7 +257,6 @@ public abstract class StorageType<T extends AbstractStorage> {
/* HELPER UTILS */
protected void placeSign(Block placedAgainst, Block toReplace, BlockFace facing, Player player, String identifier, String linkTag){
Bukkit.broadcastMessage("Placing Sign!");
if(toReplace.getType() == Material.AIR){
BlockState replacedBlockState = toReplace.getState();

View File

@ -88,6 +88,11 @@ public class AutoCraftingStorage extends AbstractStorage implements Configuratio
return Settings.isShouldDisplayAutoCraftStand();
}
@Override
public boolean dropInventory() {
return true;
}
@Override
public double getBlockOffset() {
return -0.07;

View File

@ -18,10 +18,14 @@ import org.bukkit.block.BlockFace;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
public class AutoCraftingStorageType extends StorageType<AutoCraftingStorage> {
private static final List<BlockFace> blockfaces = Arrays.asList(BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST);
public AutoCraftingStorageType(ConfigStorage store) {
super(store);
}
@ -74,8 +78,6 @@ public class AutoCraftingStorageType extends StorageType<AutoCraftingStorage> {
}
}
private static final BlockFace[] blockfaces = new BlockFace[]{BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST};
@Override
public BlockFace getStorageFacing(Block block) {
for(BlockFace face : blockfaces){
@ -88,6 +90,11 @@ public class AutoCraftingStorageType extends StorageType<AutoCraftingStorage> {
return null;
}
@Override
public List<BlockFace> getValidBlockFaces(Block block) {
return blockfaces;
}
@Override
public StorageMessages getMessages() {
return messages;

View File

@ -192,6 +192,11 @@ public class ChestLinkStorage extends AbstractStorage implements ConfigurationSe
return Settings.isShouldDisplayChestLinkStand();
}
@Override
public boolean dropInventory() {
return false;
}
@Override
public double getBlockOffset() {
return 0;

View File

@ -16,7 +16,9 @@ import org.bukkit.block.Chest;
import org.bukkit.block.data.Directional;
import org.bukkit.entity.Player;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
public class ChestLinkStorageType extends StorageType<ChestLinkStorage> {
@ -79,6 +81,11 @@ public class ChestLinkStorageType extends StorageType<ChestLinkStorage> {
return null;
}
@Override
public List<BlockFace> getValidBlockFaces(Block block) {
return Collections.singletonList(getStorageFacing(block));
}
@Override
public StorageMessages getMessages() {
return messages;