Add block adapters and replace a lot of legacy code

This commit is contained in:
Daniel Saukel 2019-06-13 02:01:56 +02:00
parent 5ca6409485
commit 33efd8f51d
17 changed files with 336 additions and 72 deletions

20
api/pom.xml Normal file
View File

@ -0,0 +1,20 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.erethon.dungeonsxl</groupId>
<artifactId>dungeonsxl-api</artifactId>
<version>0.18-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>de.erethon.dungeonsxl</groupId>
<artifactId>dungeonsxl-parent</artifactId>
<version>0.18-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.14.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,40 @@
/*
* Copyright (C) 2012-2019 Frank Baumann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.erethon.dungeonsxl.adapter.block;
import de.erethon.dungeonsxl.util.DColor;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
/**
* @author Daniel Saukel
*/
public interface BlockAdapter {
boolean isBedHead(Block block);
void openDoor(Block block);
void setBlockWoolColor(Block block, DColor color);
BlockFace getFacing(Block block);
void setFacing(Block block, BlockFace facing);
void setAxis(Block block, boolean z);
}

View File

@ -77,15 +77,6 @@ public enum DColor {
return dye.getColor().asRGB();
}
/**
* @deprecated Use getDyeColor() or getMaterial() instead
* @return the wool DV
*/
@Deprecated
public byte getWoolData() {
return dye.getWoolData();
}
/**
* @return the wool material
*/

26
bukkit_blockdata/pom.xml Normal file
View File

@ -0,0 +1,26 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.erethon.dungeonsxl</groupId>
<artifactId>dungeonsxl-bukkit_blockdata</artifactId>
<version>0.18-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>de.erethon.dungeonsxl</groupId>
<artifactId>dungeonsxl-parent</artifactId>
<version>0.18-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>de.erethon.dungeonsxl</groupId>
<artifactId>dungeonsxl-api</artifactId>
<version>${project.parent.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.14.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,83 @@
/*
* Copyright (C) 2012-2019 Frank Baumann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.erethon.dungeonsxl.adapter.block;
import de.erethon.dungeonsxl.util.DColor;
import org.bukkit.Axis;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.Directional;
import org.bukkit.block.data.Openable;
import org.bukkit.block.data.Orientable;
import org.bukkit.block.data.type.Bed;
/**
* @author Daniel Saukel
*/
public class BlockAdapterBlockData implements BlockAdapter {
@Override
public boolean isBedHead(Block block) {
if (!(block.getBlockData() instanceof Bed)) {
throw new IllegalArgumentException("Block is not Bed");
}
return ((Bed) block.getBlockData()).getPart() == Bed.Part.HEAD;
}
@Override
public void openDoor(Block block) {
if (!(block.getBlockData() instanceof Openable)) {
throw new IllegalArgumentException("Block is not Openable");
}
Openable data = ((Openable) block.getBlockData());
data.setOpen(true);
block.setBlockData(data);
}
@Override
public void setBlockWoolColor(Block block, DColor color) {
block.setType(color.getWoolMaterial().getMaterial());
}
@Override
public BlockFace getFacing(Block block) {
if (!(block.getBlockData() instanceof Directional)) {
throw new IllegalArgumentException("Block is not Directional");
}
return ((Directional) block.getBlockData()).getFacing();
}
@Override
public void setFacing(Block block, BlockFace facing) {
if (!(block.getBlockData() instanceof Directional)) {
throw new IllegalArgumentException("Block is not Directional");
}
Directional data = (Directional) block.getBlockData();
data.setFacing(facing);
block.setBlockData(data, false);
}
@Override
public void setAxis(Block block, boolean z) {
if (!(block.getBlockData() instanceof Orientable)) {
throw new IllegalArgumentException("Block is not Orientable");
}
Orientable data = (Orientable) block.getBlockData();
data.setAxis(z ? Axis.Z : Axis.X);
}
}

View File

@ -0,0 +1,26 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.erethon.dungeonsxl</groupId>
<artifactId>dungeonsxl-bukkit_magicvalues</artifactId>
<version>0.18-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>de.erethon.dungeonsxl</groupId>
<artifactId>dungeonsxl-parent</artifactId>
<version>0.18-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>de.erethon.dungeonsxl</groupId>
<artifactId>dungeonsxl-api</artifactId>
<version>${project.parent.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.12.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,78 @@
/*
* Copyright (C) 2012-2019 Frank Baumann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.erethon.dungeonsxl.adapter.block;
import de.erethon.dungeonsxl.util.DColor;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.material.Bed;
import org.bukkit.material.Directional;
import org.bukkit.material.MaterialData;
/**
* @author Daniel Saukel
*/
public class BlockAdapterMagicValues implements BlockAdapter {
@Override
public boolean isBedHead(Block block) {
MaterialData data = block.getState().getData();
if (!(data instanceof Bed)) {
throw new IllegalArgumentException("Block is not Bed");
}
return ((Bed) data).isHeadOfBed();
}
@Override
public void openDoor(Block block) {
block.setData((byte) (block.getData() + 4));
}
@Override
public void setBlockWoolColor(Block block, DColor color) {
block.setTypeIdAndData(Material.WOOL.getId(), color.getDyeColor().getWoolData(), false);
}
@Override
public BlockFace getFacing(Block block) {
MaterialData data = block.getState().getData();
if (!(data instanceof Directional)) {
throw new IllegalArgumentException("Block is not Directional");
}
return ((Directional) data).getFacing();
}
@Override
public void setFacing(Block block, BlockFace facing) {
BlockState state = block.getState();
MaterialData data = state.getData();
if (!(data instanceof Directional)) {
throw new IllegalArgumentException("Block is not Directional");
}
((Directional) data).setFacingDirection(facing);
state.setData(data);
state.update();
}
@Override
public void setAxis(Block block, boolean z) {
block.setData(z ? (byte) 2 : 1);
}
}

View File

@ -22,6 +22,24 @@
</resources>
</build>
<dependencies>
<dependency>
<groupId>de.erethon.dungeonsxl</groupId>
<artifactId>dungeonsxl-api</artifactId>
<version>${project.parent.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>de.erethon.dungeonsxl</groupId>
<artifactId>dungeonsxl-bukkit_blockdata</artifactId>
<version>${project.parent.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>de.erethon.dungeonsxl</groupId>
<artifactId>dungeonsxl-bukkit_magicvalues</artifactId>
<version>${project.parent.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
@ -34,24 +52,6 @@
<version>1.7</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>de.erethon</groupId>
<artifactId>caliburn</artifactId>
<version>0.5.4</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>de.erethon.commons</groupId>
<artifactId>commons-dist</artifactId>
<version>6.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>de.erethon.vignette</groupId>
<artifactId>vignette-dist</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.github.dre2n</groupId>
<artifactId>commandsxl</artifactId>

View File

@ -19,10 +19,14 @@ package de.erethon.dungeonsxl;
import de.erethon.caliburn.CaliburnAPI;
import de.erethon.caliburn.loottable.LootTable;
import de.erethon.commons.compatibility.Internals;
import de.erethon.commons.compatibility.Version;
import de.erethon.commons.config.MessageConfig;
import de.erethon.commons.javaplugin.DREPlugin;
import de.erethon.commons.javaplugin.DREPluginSettings;
import de.erethon.commons.misc.FileUtil;
import de.erethon.dungeonsxl.adapter.block.BlockAdapter;
import de.erethon.dungeonsxl.adapter.block.BlockAdapterBlockData;
import de.erethon.dungeonsxl.adapter.block.BlockAdapterMagicValues;
import de.erethon.dungeonsxl.announcer.AnnouncerCache;
import de.erethon.dungeonsxl.command.DCommandCache;
import de.erethon.dungeonsxl.config.DMessage;
@ -68,6 +72,8 @@ public class DungeonsXL extends DREPlugin {
private static DungeonsXL instance;
private CaliburnAPI caliburn;
public static final BlockAdapter BLOCK_ADAPTER = Version.isAtLeast(Version.MC1_13) ? new BlockAdapterBlockData() : new BlockAdapterMagicValues();
public static final String[] EXCLUDED_FILES = {"config.yml", "uid.dat", "DXLData.data", "data"};
public static File BACKUPS;
public static File LANGUAGES;

View File

@ -26,7 +26,6 @@ import de.erethon.dungeonsxl.game.Game;
import de.erethon.dungeonsxl.player.DGamePlayer;
import de.erethon.dungeonsxl.player.DGlobalPlayer;
import de.erethon.dungeonsxl.player.DGroup;
import de.erethon.dungeonsxl.util.MagicValueUtil;
import de.erethon.dungeonsxl.world.DGameWorld;
import de.erethon.dungeonsxl.world.DResourceWorld;
import java.util.HashSet;
@ -49,7 +48,7 @@ public class DPortal extends GlobalProtection {
private Block block1;
private Block block2;
private ExItem material = VanillaItem.NETHER_PORTAL;
private byte axis;
private boolean zAxis;
private boolean active;
private Set<Block> blocks;
@ -64,13 +63,13 @@ public class DPortal extends GlobalProtection {
this.active = active;
}
public DPortal(DungeonsXL plugin, int id, Block block1, Block block2, ExItem material, byte axis, boolean active) {
public DPortal(DungeonsXL plugin, int id, Block block1, Block block2, ExItem material, boolean zAxis, boolean active) {
super(plugin, block1.getWorld(), id);
this.block1 = block1;
this.block2 = block2;
this.material = material;
this.axis = axis;
this.zAxis = zAxis;
this.active = active;
}
@ -84,7 +83,7 @@ public class DPortal extends GlobalProtection {
material = VanillaItem.NETHER_PORTAL;
}
String axis = config.getString("axis");
this.axis = (byte) (axis != null && axis.equalsIgnoreCase("z") ? 2 : 1);
zAxis = axis != null && axis.equalsIgnoreCase("z");
active = true;
create(null);
}
@ -145,9 +144,9 @@ public class DPortal extends GlobalProtection {
if (player != null && material == VanillaItem.NETHER_PORTAL) {
float yaw = player.getPlayer().getLocation().getYaw();
if (yaw >= 45 & yaw < 135 || yaw >= 225 & yaw < 315) {
axis = 2;//z;
zAxis = true;
} else if (yaw >= 315 | yaw < 45 || yaw >= 135 & yaw < 225) {
axis = 1;//x;
zAxis = false;
}
}
@ -184,7 +183,7 @@ public class DPortal extends GlobalProtection {
Block block = getWorld().getBlockAt(xx, yy, zz);
block.setType(material.getMaterial(), false);
if (material == VanillaItem.NETHER_PORTAL) {
MagicValueUtil.setBlockData(block, axis);
DungeonsXL.BLOCK_ADAPTER.setAxis(block, zAxis);
}
}
@ -290,7 +289,7 @@ public class DPortal extends GlobalProtection {
configFile.set(preString + ".material", material.getId());
if (material == VanillaItem.NETHER_PORTAL) {
configFile.set(preString + ".axis", axis == 2 ? "z" : "x");
configFile.set(preString + ".axis", zAxis ? "z" : "x");
}
}

View File

@ -27,11 +27,9 @@ import java.util.Set;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.block.Sign;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.material.Attachable;
/**
* @author Daniel Saukel
@ -217,26 +215,15 @@ public abstract class JoinSign extends GlobalProtection {
}
protected static void onCreation(DungeonsXL plugin, Block startSign, String identifier, int maxElements, int startIfElementsAtLeast) {
// TODO: Replace as soon as versions older than 1.13 are dropped
World world = startSign.getWorld();
BlockFace facing = ((Attachable) startSign.getState().getData()).getAttachedFace().getOppositeFace();
//BlockFace facing = ((Directional) startSign.getBlockData()).getFacing().getOppositeFace();
BlockFace facing = DungeonsXL.BLOCK_ADAPTER.getFacing(startSign);
int x = startSign.getX(), y = startSign.getY(), z = startSign.getZ();
int verticalSigns = (int) Math.ceil((float) (1 + maxElements) / 4);
while (verticalSigns > 1) {
Block block = world.getBlockAt(x, y - verticalSigns + 1, z);
block.setType(VanillaItem.WALL_SIGN.getMaterial(), false);
BlockState state = block.getState();
org.bukkit.material.Sign signData = (org.bukkit.material.Sign) state.getData();
signData.setFacingDirection(facing);
state.setData(signData);
state.update(true, false);
// Directional directional = (Directional) block.getBlockData();
// directional.setFacing(facing);
// block.setBlockData(directional, false);
DungeonsXL.BLOCK_ADAPTER.setFacing(block, facing);
verticalSigns--;
}

View File

@ -38,7 +38,7 @@ public abstract class LocationSign extends DSign {
double x = getSign().getX() + 0.5;
double y = getSign().getY();
double z = getSign().getZ() + 0.5;
float yaw = BlockUtil.blockFaceToYaw(((org.bukkit.material.Sign) getSign().getData()).getFacing().getOppositeFace());
float yaw = BlockUtil.blockFaceToYaw(DungeonsXL.BLOCK_ADAPTER.getFacing(getSign().getBlock()).getOppositeFace());
float pitch = 0;
location = new Location(getGameWorld().getWorld(), x, y, z, yaw, pitch);
}

View File

@ -16,6 +16,7 @@
*/
package de.erethon.dungeonsxl.sign;
import de.erethon.caliburn.category.Category;
import de.erethon.caliburn.item.VanillaItem;
import de.erethon.commons.misc.BlockUtil;
import de.erethon.dungeonsxl.DungeonsXL;
@ -24,7 +25,6 @@ import de.erethon.dungeonsxl.world.block.LockedDoor;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Sign;
import org.bukkit.material.Door;
/**
* @author Daniel Saukel
@ -81,7 +81,7 @@ public class OpenDoorSign extends DSign {
@Override
public void onInit() {
Block block = BlockUtil.getAttachedBlock(getSign().getBlock());
if (block.getState().getData() instanceof Door) {
if (Category.DOORS.containsBlock(block)) {
if (block.getRelative(BlockFace.DOWN).getType() == block.getType()) {
door = new LockedDoor(plugin, block.getRelative(BlockFace.DOWN));
} else {

View File

@ -17,7 +17,6 @@
package de.erethon.dungeonsxl.world.block;
import de.erethon.dungeonsxl.DungeonsXL;
import de.erethon.dungeonsxl.util.MagicValueUtil;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.event.block.BlockBreakEvent;
@ -57,10 +56,7 @@ public class LockedDoor extends GameBlock implements MultiBlock {
* Opens the door.
*/
public void open() {
/*Openable data = ((Openable) block.getBlockData());
data.setOpen(true);
block.setBlockData(data);*/
MagicValueUtil.setBlockData(block, (byte) (block.getData() + 4));
DungeonsXL.BLOCK_ADAPTER.openDoor(block);
}
@Override

View File

@ -27,7 +27,6 @@ import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.material.Bed;
/**
* @author Daniel Saukel
@ -86,7 +85,7 @@ public class TeamBed extends TeamBlock implements MultiBlock {
owner.getGameWorld().sendMessage(DMessage.GROUP_BED_DESTROYED.getMessage(owner.getName(), DGamePlayer.getByPlayer(breaker).getName()));
Block block1 = event.getBlock();
if (((Bed) block1.getState().getData()).isHeadOfBed()) {
if (DungeonsXL.BLOCK_ADAPTER.isBedHead(block)) {
Block block2 = getAttachedBlock(block1);
if (block2 != null) {
block2.setType(VanillaItem.AIR.getMaterial());

View File

@ -18,13 +18,10 @@ package de.erethon.dungeonsxl.world.block;
import de.erethon.caliburn.item.VanillaItem;
import de.erethon.commons.chat.MessageUtil;
import de.erethon.commons.compatibility.CompatibilityHandler;
import de.erethon.dungeonsxl.DungeonsXL;
import de.erethon.dungeonsxl.config.DMessage;
import de.erethon.dungeonsxl.player.DGamePlayer;
import de.erethon.dungeonsxl.player.DGroup;
import de.erethon.dungeonsxl.util.DColor;
import de.erethon.dungeonsxl.util.MagicValueUtil;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockBreakEvent;
@ -44,7 +41,7 @@ public class TeamFlag extends TeamBlock {
* Reset a team flag when the capturer dies.
*/
public void reset() {
setBlockWoolColor(block, owner.getDColor());
DungeonsXL.BLOCK_ADAPTER.setBlockWoolColor(block, owner.getDColor());
}
@Override
@ -66,11 +63,4 @@ public class TeamFlag extends TeamBlock {
return true;
}
public static void setBlockWoolColor(Block block, DColor color) {
block.setType(color.getWoolMaterial().getMaterial());
if (!CompatibilityHandler.getInstance().getVersion().useNewMaterials()) {
MagicValueUtil.setBlockData(block, color.getWoolData());
}
}
}

23
pom.xml
View File

@ -8,6 +8,9 @@
<url>https://dre2n.github.io</url>
<description>Create custom dungeons and adventure maps with ease!</description>
<modules>
<module>api</module>
<module>bukkit_blockdata</module>
<module>bukkit_magicvalues</module>
<module>core</module>
<module>dist</module>
</modules>
@ -17,6 +20,26 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>de.erethon.commons</groupId>
<artifactId>commons-dist</artifactId>
<version>6.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>de.erethon</groupId>
<artifactId>caliburn</artifactId>
<version>0.5.4</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>de.erethon.vignette</groupId>
<artifactId>vignette-dist</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spigot-repo</id>