TF-Marriage/src/main/java/com/lenis0012/bukkit/marriage2/commands/CommandTeleport.java

95 lines
3.1 KiB
Java
Raw Normal View History

2015-07-09 03:13:58 +00:00
package com.lenis0012.bukkit.marriage2.commands;
import com.lenis0012.bukkit.marriage2.MData;
import com.lenis0012.bukkit.marriage2.MPlayer;
import com.lenis0012.bukkit.marriage2.Marriage;
import com.lenis0012.bukkit.marriage2.config.Message;
2016-02-26 15:27:07 +00:00
import com.lenis0012.bukkit.marriage2.config.Settings;
2018-06-05 17:09:30 +00:00
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
2015-07-09 03:13:58 +00:00
import org.bukkit.entity.Player;
2018-06-05 17:09:30 +00:00
import java.util.Arrays;
import java.util.List;
2015-07-09 03:13:58 +00:00
/**
* Created by Lennart on 7/9/2015.
*/
public class CommandTeleport extends Command {
2018-06-05 17:09:30 +00:00
private static final List<Material> UNSAFE_TYPES = Arrays.asList(Material.LAVA, Material.STATIONARY_LAVA, Material.CACTUS);
2015-07-09 03:13:58 +00:00
public CommandTeleport(Marriage marriage) {
super(marriage, "tp");
2016-06-12 18:27:52 +00:00
setDescription(Message.COMMAND_TELEPORT.toString());
2016-02-26 15:27:07 +00:00
setExecutionFee(Settings.PRICE_TELEPORT);
2015-07-09 03:13:58 +00:00
}
@Override
public void execute() {
MPlayer mPlayer = marriage.getMPlayer(player);
2015-07-09 03:13:58 +00:00
MData marriage = mPlayer.getMarriage();
2016-02-23 13:56:24 +00:00
if(marriage == null) {
2015-07-09 03:13:58 +00:00
reply(Message.NOT_MARRIED);
2016-02-23 13:56:24 +00:00
return;
2015-07-09 03:13:58 +00:00
}
2016-02-23 13:56:24 +00:00
Player partner = Bukkit.getPlayer(marriage.getOtherPlayer(player.getUniqueId()));
if(partner == null) {
reply(Message.PARTNER_NOT_ONLINE);
return;
}
2016-02-23 16:22:31 +00:00
2018-06-05 17:09:30 +00:00
Location destination = partner.getLocation();
if(player.getGameMode() != GameMode.CREATIVE) {
destination = getSafeLocation(destination);
}
if(destination == null) {
reply(Message.TELEPORT_UNSAFE);
return;
}
2016-02-26 15:27:07 +00:00
if(!payFee()) return;
2018-06-05 17:09:30 +00:00
2018-06-05 18:29:43 +00:00
player.teleport(destination);
2016-02-23 13:56:24 +00:00
reply(Message.TELEPORTED);
partner.sendMessage(ChatColor.translateAlternateColorCodes('&', Message.TELEPORTED_2.toString()));
2015-07-09 03:13:58 +00:00
}
2018-06-05 17:09:30 +00:00
private Location getSafeLocation(Location destination) {
World world = destination.getWorld();
Block block = destination.getBlock();
if(block == null || block.getY() < 0 || block.getY() > world.getMaxHeight()) {
return null; // Out of bounds, cant teleport to void or from a bizarre height.
}
if(isSafeGround(block.getRelative(BlockFace.DOWN))) {
return destination; // Current destination is valid
}
// Find next potentially safe block
2018-06-05 18:29:43 +00:00
while(!(block.getType().isSolid() || block.isLiquid()) && block.getY() > 0) {
2018-06-05 17:09:30 +00:00
block = block.getRelative(BlockFace.DOWN);
if(UNSAFE_TYPES.contains(block.getType())) {
return null; // Obstructed by unsafe block
}
}
if(!isSafeGround(block)) {
return null; // Still not safe
}
// Safe
Location target = destination.clone();
target.setY(block.getY() + 1);
return target;
}
private boolean isSafeGround(Block block) {
2018-06-05 18:29:43 +00:00
return (block.getType().isSolid() || block.getType() == Material.WATER || block.getType() == Material.STATIONARY_WATER)
2018-06-05 17:09:30 +00:00
&& !UNSAFE_TYPES.contains(block.getRelative(0, 1, 0).getType())
2018-06-05 18:29:43 +00:00
&& !UNSAFE_TYPES.contains(block.getRelative(0, 2, 0).getType());
2018-06-05 17:09:30 +00:00
}
2015-07-09 03:13:58 +00:00
}