Fix compatibility with 1.13 Material class

- Fixes sign handling to properly detect and store signs, fixes #440
- Fixes safe teleporting (also improves block detection, making it even safer)
This commit is contained in:
Thijs Wiefferink 2018-10-28 00:12:24 +02:00
parent c8e1ebb068
commit b0341e2a3e
11 changed files with 153 additions and 35 deletions

View File

@ -164,7 +164,7 @@ public final class AreaShop extends JavaPlugin implements AreaShopInterface {
try {
rawVersion = worldGuard.getDescription().getVersion();
if(rawVersion.contains("-SNAPSHOT;")) {
String buildNumber = rawVersion.substring(rawVersion.indexOf("-SNAPSHOT;") + 10, rawVersion.length());
String buildNumber = rawVersion.substring(rawVersion.indexOf("-SNAPSHOT;") + 10);
if(buildNumber.contains("-")) {
buildNumber = buildNumber.substring(0, buildNumber.indexOf("-"));
try {

View File

@ -3,6 +3,7 @@ package me.wiefferink.areashop.commands;
import me.wiefferink.areashop.features.signs.RegionSign;
import me.wiefferink.areashop.features.signs.SignsFeature;
import me.wiefferink.areashop.regions.GeneralRegion;
import me.wiefferink.areashop.tools.Materials;
import me.wiefferink.areashop.tools.Utils;
import org.bukkit.Material;
import org.bukkit.block.Block;
@ -51,7 +52,7 @@ public class AddsignCommand extends CommandAreaShop {
block = next;
}
}
if(block == null || !(block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST)) {
if(block == null || !Materials.isSign(block.getType())) {
plugin.message(sender, "addsign-noSign");
return;
}

View File

@ -2,6 +2,7 @@ package me.wiefferink.areashop.commands;
import me.wiefferink.areashop.features.signs.RegionSign;
import me.wiefferink.areashop.features.signs.SignsFeature;
import me.wiefferink.areashop.tools.Materials;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.command.CommandSender;
@ -47,7 +48,7 @@ public class DelsignCommand extends CommandAreaShop {
block = next;
}
}
if(block == null || !(block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST)) {
if(block == null || !Materials.isSign(block.getType())) {
plugin.message(sender, "delsign-noSign");
return;
}

View File

@ -13,22 +13,15 @@ import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class TeleportFeature extends RegionFeature {
private static final ArrayList<Material> canSpawnIn = new ArrayList<>(Arrays.asList(Material.WOOD_DOOR, Material.WOODEN_DOOR, Material.SIGN_POST, Material.WALL_SIGN, Material.STONE_PLATE, Material.IRON_DOOR_BLOCK, Material.WOOD_PLATE, Material.TRAP_DOOR, Material.REDSTONE_LAMP_OFF, Material.REDSTONE_LAMP_ON, Material.DRAGON_EGG, Material.GOLD_PLATE, Material.IRON_PLATE));
private static final ArrayList<Material> cannotSpawnOn = new ArrayList<>(Arrays.asList(Material.PISTON_EXTENSION, Material.PISTON_MOVING_PIECE, Material.SIGN_POST, Material.WALL_SIGN, Material.STONE_PLATE, Material.IRON_DOOR_BLOCK, Material.WOOD_PLATE, Material.TRAP_DOOR, Material.REDSTONE_LAMP_OFF, Material.REDSTONE_LAMP_ON, Material.CACTUS, Material.IRON_FENCE, Material.FENCE_GATE, Material.THIN_GLASS, Material.NETHER_FENCE, Material.DRAGON_EGG, Material.GOLD_PLATE, Material.IRON_PLATE, Material.STAINED_GLASS_PANE));
private static final ArrayList<Material> cannotSpawnBeside = new ArrayList<>(Arrays.asList(Material.LAVA, Material.STATIONARY_LAVA, Material.CACTUS));
public TeleportFeature() {
}
public TeleportFeature(GeneralRegion region) {
setRegion(region);
}
@ -410,13 +403,13 @@ public class TeleportFeature extends RegionFeature {
Block above = head.getRelative(BlockFace.UP);
// Check the block at the feet and head of the player
if((feet.getType().isSolid() && !canSpawnIn.contains(feet.getType())) || feet.isLiquid()) {
if((feet.getType().isSolid() && !canSpawnIn(feet.getType())) || feet.isLiquid()) {
return false;
} else if((head.getType().isSolid() && !canSpawnIn.contains(head.getType())) || head.isLiquid()) {
} else if((head.getType().isSolid() && !canSpawnIn(head.getType())) || head.isLiquid()) {
return false;
} else if(!below.getType().isSolid() || cannotSpawnOn.contains(below.getType()) || below.isLiquid()) {
} else if(!below.getType().isSolid() || cannotSpawnOn(below.getType()) || below.isLiquid()) {
return false;
} else if(above.isLiquid() || cannotSpawnBeside.contains(above.getType())) {
} else if(above.isLiquid() || cannotSpawnBeside(above.getType())) {
return false;
}
@ -437,13 +430,58 @@ public class TeleportFeature extends RegionFeature {
// Check the blocks around the player
for(Material material : around) {
if(cannotSpawnBeside.contains(material)) {
if(cannotSpawnBeside(material)) {
return false;
}
}
return true;
}
/**
* Check if a player can spawn in here.
* @param material Material to check (assumed that this is at the feet or head level)
* @return true when it is safe to spawn inside, otherwise false
*/
private static boolean canSpawnIn(Material material) {
String name = material.name();
return name.contains("DOOR")
|| name.contains("SIGN")
|| name.contains("PLATE") // Redstone plates
|| name.equals("DRAGON_EGG");
}
/**
* Check if a player can spawn on here.
* @param material Material to check (assumed that this is below the feet)
* @return true when it is safe to spawn on top of, otherwise false
*/
private static boolean cannotSpawnOn(Material material) {
String name = material.name();
return name.equals("CACTUS")
|| name.contains("PISTON")
|| name.contains("SIGN")
|| name.contains("DOOR")
|| name.contains("PLATE")
|| name.contains("REDSTONE_LAMP")
|| name.contains("FENCE")
|| name.contains("GLASS_PANE") || name.contains("THIN_GLASS")
|| name.equals("DRAGON_EGG")
|| name.contains("MAGMA");
}
/**
* Check if a player can spawn next to it.
* @param material Material to check (assumed that this is somewhere around the player)
* @return true when it is safe to spawn next to, otherwise false
*/
private static boolean cannotSpawnBeside(Material material) {
String name = material.name();
return name.contains("LAVA")
|| name.contains("CACTUS")
|| name.equals("FIRE")
|| name.contains("MAGMA");
}
/**
* Get the start location of a safe teleport search.
* @param player The player to get it for

View File

@ -3,6 +3,7 @@ package me.wiefferink.areashop.features.signs;
import com.google.common.base.Objects;
import me.wiefferink.areashop.AreaShop;
import me.wiefferink.areashop.regions.GeneralRegion;
import me.wiefferink.areashop.tools.Materials;
import me.wiefferink.areashop.tools.Utils;
import me.wiefferink.interactivemessenger.processing.Message;
import org.bukkit.Bukkit;
@ -99,11 +100,13 @@ public class RegionSign {
* @return Material of the sign, normally {@link Material#WALL_SIGN} or {@link Material#SIGN_POST}, but could be something else or null.
*/
public Material getMaterial() {
try {
return Material.valueOf(getRegion().getConfig().getString("general.signs." + key + ".signType"));
} catch(NullPointerException | IllegalArgumentException e) {
return null;
String name = getRegion().getConfig().getString("general.signs." + key + ".signType");
if ("WALL_SIGN".equals(name)) {
return Materials.wallSign;
} else if ("SIGN_POST".equals(name) || "SIGN".equals(name)) {
return Materials.floorSign;
}
return null;
}
/**
@ -145,16 +148,16 @@ public class RegionSign {
return true;
}
Sign signState = null;
// Place the sign back (with proper rotation and type) after it has been hidden or (indirectly) destroyed
if(block.getType() != Material.WALL_SIGN && block.getType() != Material.SIGN_POST) {
Sign signState = null;
if(!Materials.isSign(block.getType())) {
Material signType = getMaterial();
if(signType != Material.WALL_SIGN && signType != Material.SIGN_POST) {
if(!Materials.isSign(signType)) {
AreaShop.debug("Setting sign", key, "of region", getRegion().getName(), "failed, could not set sign block back");
return false;
}
block.setType(signType);
signState = (Sign)block.getState();
signState = (Sign) block.getState();
org.bukkit.material.Sign signData = (org.bukkit.material.Sign)signState.getData();
BlockFace signFace = getFacing();
if(signFace != null) {
@ -163,13 +166,17 @@ public class RegionSign {
}
}
if(signState == null) {
signState = (Sign)block.getState();
signState = (Sign) block.getState();
}
// Save current rotation and type
org.bukkit.material.Sign signData = (org.bukkit.material.Sign)signState.getData();
if(!regionConfig.isString("general.signs." + key + ".signType")) {
getRegion().setSetting("general.signs." + key + ".signType", signState.getType().toString());
String signType = signState.getType().name();
if (signType.equals("SIGN")) {
signType = "SIGN_POST"; // Save with a backwards-compatible name
}
getRegion().setSetting("general.signs." + key + ".signType", signType);
}
if(!regionConfig.isString("general.signs." + key + ".facing")) {
getRegion().setSetting("general.signs." + key + ".facing", signData.getFacing().toString());

View File

@ -9,6 +9,7 @@ import me.wiefferink.areashop.managers.FileManager;
import me.wiefferink.areashop.regions.BuyRegion;
import me.wiefferink.areashop.regions.GeneralRegion;
import me.wiefferink.areashop.regions.RentRegion;
import me.wiefferink.areashop.tools.Materials;
import me.wiefferink.areashop.tools.Utils;
import me.wiefferink.bukkitdo.Do;
import org.bukkit.Chunk;
@ -88,7 +89,7 @@ public class SignsFeature extends RegionFeature {
}
Block block = event.getBlock();
// Check if it is a sign
if(block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST) {
if(Materials.isSign(block.getType())) {
// Check if the rent sign is really the same as a saved rent
RegionSign regionSign = SignsFeature.getSignByLocation(block.getLocation());
if(regionSign == null) {
@ -107,7 +108,8 @@ public class SignsFeature extends RegionFeature {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onIndirectSignBreak(BlockPhysicsEvent event) {
if(event.getBlock().getType() == Material.SIGN_POST || event.getBlock().getType() == Material.WALL_SIGN) {
// 1.13+ does not support this anymore, only way would be checking surrounding blocks, which is still not 100% sufficient
if(Materials.isSign(event.getBlock().getType())) {
// Check if the rent sign is really the same as a saved rent
if(SignsFeature.getSignByLocation(event.getBlock().getLocation()) != null) {
// Cancel the sign breaking, will create a floating sign but at least it is not disconnected/gone
@ -116,15 +118,12 @@ public class SignsFeature extends RegionFeature {
}
}
@EventHandler(priority = EventPriority.HIGH)
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onSignClick(PlayerInteractEvent event) {
if(event.isCancelled()) {
return;
}
Block block = event.getClickedBlock();
// Check for clicking a sign and rightclicking
if((event.getAction() == Action.RIGHT_CLICK_BLOCK || event.getAction() == Action.LEFT_CLICK_BLOCK)
&& (block.getType() == Material.SIGN_POST || block.getType() == Material.WALL_SIGN)) {
&& Materials.isSign(block.getType())) {
// Check if the rent sign is really the same as a saved rent
RegionSign regionSign = SignsFeature.getSignByLocation(block.getLocation());
if(regionSign == null) {

View File

@ -3,6 +3,7 @@ package me.wiefferink.areashop.managers;
import me.wiefferink.areashop.features.signs.RegionSign;
import me.wiefferink.areashop.features.signs.SignsFeature;
import me.wiefferink.areashop.regions.GeneralRegion;
import me.wiefferink.areashop.tools.Materials;
import me.wiefferink.areashop.tools.Utils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
@ -120,7 +121,7 @@ public class SignLinkerManager extends Manager implements Listener {
block = next;
}
}
if(block == null || !(block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST)) {
if(block == null || !Materials.isSign(block.getType())) {
plugin.message(player, "linksigns-noSign");
return;
}

View File

@ -0,0 +1,70 @@
package me.wiefferink.areashop.tools;
import me.wiefferink.areashop.AreaShop;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import java.util.Arrays;
import java.util.List;
public class Materials {
private static boolean legacyMaterials = false;
static {
List<String> legacyMaterialVersions = Arrays.asList("1.7", "1.8", "1.9", "1.10", "1.11", "1.12");
for(String legacyMaterialVersion : legacyMaterialVersions) {
String version = Bukkit.getBukkitVersion();
// Detects '1.8', '1.8.3', '1.8-pre1' style versions
if(version.equals(legacyMaterialVersion)
|| version.startsWith(legacyMaterialVersion + ".")
|| version.startsWith(legacyMaterialVersion + "-")) {
legacyMaterials = true;
break;
}
}
}
public static final Material wallSign = get("WALL_SIGN");
public static final Material floorSign = get("SIGN", "SIGN_POST");
/**
* Get Material version independent.
* @param name Name in 1.13 and later (uppercase, with underscores)
* @return Material matching the name
*/
public static Material get(String name) {
return get(name, null);
}
/**
* Get Material version independent.
* @param name Name in 1.13 and later (uppercase, with underscores)
* @param legacyName Name in 1.12 and earlier (uppercase, with underscores)
* @return Material matching the name
*/
public static Material get(String name, String legacyName) {
Material result;
if (legacyMaterials && legacyName != null) {
result = Material.getMaterial(legacyName);
} else {
result = Material.getMaterial(name);
}
if (result == null) {
AreaShop.debug("Materials.get() null result:", name, legacyName);
}
return result;
}
/**
* Check if a Material is a sign (of either the wall or floor type).
* @param material Material to check
* @return true if the given material is a sign
*/
public static boolean isSign(Material material) {
return material != null && material.name().contains("SIGN");
}
}

View File

@ -13,6 +13,7 @@ commands:
aliases: [as]
awareness:
- !@UTF8
api-version: 1.13
permissions:
areashop.*:
description: Give access to all commands of AreaShop

View File

@ -15,7 +15,7 @@
<dependencies>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<artifactId>craftbukkit</artifactId>
</dependency>
<dependency>
<groupId>me.wiefferink</groupId>

View File

@ -15,7 +15,7 @@
<dependencies>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<artifactId>craftbukkit</artifactId>
</dependency>
<dependency>
<groupId>me.wiefferink</groupId>