PlotSquared/src/main/java/com/plotsquared/sponge/util/SpongeUtil.java

181 lines
6.3 KiB
Java
Raw Normal View History

2015-07-30 16:25:16 +02:00
package com.plotsquared.sponge.util;
import java.lang.reflect.Field;
import java.util.Locale;
2015-10-07 08:33:33 +02:00
import java.util.Optional;
import org.spongepowered.api.block.BlockState;
2015-07-30 16:25:16 +02:00
import org.spongepowered.api.entity.Entity;
2015-10-07 08:33:33 +02:00
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.translation.Translatable;
import org.spongepowered.api.text.translation.Translation;
2015-07-30 16:25:16 +02:00
import org.spongepowered.api.world.World;
import org.spongepowered.api.world.biome.BiomeType;
import org.spongepowered.api.world.biome.BiomeTypes;
2015-07-30 16:25:16 +02:00
import org.spongepowered.api.world.extent.Extent;
import com.flowpowered.math.vector.Vector3d;
import com.flowpowered.math.vector.Vector3i;
import com.intellectualcrafters.plot.object.Location;
import com.intellectualcrafters.plot.object.PlotBlock;
2015-07-30 16:25:16 +02:00
import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.util.MathMan;
import com.intellectualcrafters.plot.util.UUIDHandler;
import com.plotsquared.sponge.SpongeMain;
2015-07-30 19:24:01 +02:00
import com.plotsquared.sponge.object.SpongePlayer;
2015-07-30 16:25:16 +02:00
2015-09-13 06:04:31 +02:00
public class SpongeUtil {
public static Location getLocation(final Entity player) {
2015-09-11 12:09:22 +02:00
final String world = player.getWorld().getName();
final org.spongepowered.api.world.Location loc = player.getLocation();
final Vector3i pos = loc.getBlockPosition();
2015-07-30 16:25:16 +02:00
return new Location(world, pos.getX(), pos.getY(), pos.getZ());
}
2015-09-13 06:04:31 +02:00
private static BiomeType[] biomes;
public static BiomeType getBiome(int index) {
if (biomes == null) {
try {
Field[] fields = BiomeTypes.class.getFields();
biomes = new BiomeType[fields.length];
for (int i = 0; i < fields.length; i++) {
biomes[i] = (BiomeType) fields[i].get(null);
}
} catch (IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
}
return biomes[index];
}
public static Text text(String m) {
return Text.of(m);
}
public static Translation getTranslation(final String m) {
return new Translatable() {
@Override
public Translation getTranslation() {
return new Translation() {
@Override
public String getId() {
return m;
}
@Override
public String get(final Locale l, final Object... args) {
return m;
}
@Override
public String get(final Locale l) {
return m;
}
};
}
}.getTranslation();
}
public static BlockState getBlockState(int id, int data) {
}
public static PlotBlock getPlotBlock(BlockState state) {
}
2015-10-07 08:33:33 +02:00
public static Location getLocation(final org.spongepowered.api.world.Location<World> block) {
return getLocation(block.getExtent().getName(), block);
2015-07-30 16:25:16 +02:00
}
2015-09-13 06:04:31 +02:00
public static Location getLocationFull(final Entity player) {
2015-09-11 12:09:22 +02:00
final String world = player.getWorld().getName();
final Vector3d rot = player.getRotation();
final float[] pitchYaw = MathMan.getPitchAndYaw((float) rot.getX(), (float) rot.getY(), (float) rot.getZ());
final org.spongepowered.api.world.Location loc = player.getLocation();
final Vector3i pos = loc.getBlockPosition();
2015-07-30 16:25:16 +02:00
return new Location(world, pos.getX(), pos.getY(), pos.getZ(), pitchYaw[1], pitchYaw[0]);
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
private static Player lastPlayer = null;
private static PlotPlayer lastPlotPlayer = null;
2015-09-13 06:04:31 +02:00
public static PlotPlayer getPlayer(final Player player) {
if (player == lastPlayer) {
return lastPlotPlayer;
}
2015-09-11 12:09:22 +02:00
final String name = player.getName();
2015-10-19 08:27:51 +02:00
final PlotPlayer pp = UUIDHandler.getPlayer(name);
2015-09-13 06:04:31 +02:00
if (pp != null) {
return pp;
}
2015-07-30 16:25:16 +02:00
lastPlotPlayer = new SpongePlayer(player);
UUIDHandler.getPlayers().put(name, lastPlotPlayer);
lastPlayer = player;
return lastPlotPlayer;
}
2015-09-13 06:04:31 +02:00
public static Player getPlayer(final PlotPlayer player) {
if (player instanceof SpongePlayer) {
return ((SpongePlayer) player).player;
}
2015-07-30 16:25:16 +02:00
return null;
}
2015-09-13 06:04:31 +02:00
2015-08-03 20:20:04 +02:00
private static World lastWorld;
private static String last;
2015-09-13 06:04:31 +02:00
public static World getWorld(final String world) {
if (world == last) {
return lastWorld;
}
2015-09-11 12:09:22 +02:00
final Optional<World> optional = SpongeMain.THIS.getServer().getWorld(world);
2015-09-13 06:04:31 +02:00
if (!optional.isPresent()) {
return null;
}
2015-07-30 16:25:16 +02:00
return optional.get();
}
2015-09-13 06:04:31 +02:00
public static void removePlayer(final String player) {
2015-07-30 16:25:16 +02:00
lastPlayer = null;
lastPlotPlayer = null;
}
2015-09-13 06:04:31 +02:00
public static Location getLocation(final String world, final org.spongepowered.api.world.Location spawn) {
2015-07-30 16:25:16 +02:00
return new Location(world, spawn.getBlockX(), spawn.getBlockY(), spawn.getBlockZ());
}
2015-09-13 06:04:31 +02:00
public static String getWorldName(final org.spongepowered.api.world.Location origin) {
2015-09-11 12:09:22 +02:00
final Extent extent = origin.getExtent();
2015-09-13 06:04:31 +02:00
if (extent == lastWorld) {
return last;
}
if (extent instanceof World) {
lastWorld = (World) extent;
last = ((World) extent).getName();
return last;
}
return null;
}
2015-09-13 06:04:31 +02:00
public static org.spongepowered.api.world.Location getLocation(final Location loc) {
2015-09-11 12:09:22 +02:00
final Optional<World> world = SpongeMain.THIS.getServer().getWorld(loc.getWorld());
2015-09-13 06:04:31 +02:00
if (!world.isPresent()) {
return null;
}
2015-08-14 00:52:31 +02:00
return new org.spongepowered.api.world.Location(world.get(), loc.getX(), loc.getY(), loc.getZ());
}
2015-10-07 08:33:33 +02:00
public static Location getLocation(String world, Vector3i position) {
return new Location(world, position.getX(), position.getY(), position.getZ());
}
public static Location getLocation(String world, Vector3d position) {
return new Location(world, MathMan.roundInt(position.getX()), MathMan.roundInt(position.getY()), MathMan.roundInt(position.getZ()));
}
2015-07-30 16:25:16 +02:00
}