From c4c5e0553acda296261a5ee70cf01571a9885a3a Mon Sep 17 00:00:00 2001 From: Adubbz Date: Sun, 29 Mar 2015 19:39:22 +1100 Subject: [PATCH] Added commands for teleporting to a biome of a given id, and finding the biome associated with an id --- .../common/command/BOPCommand.java | 120 ++++++++++++++++++ .../common/util/biome/BiomeUtils.java | 69 ++++++++++ .../biomesoplenty/core/BiomesOPlenty.java | 15 ++- .../assets/biomesoplenty/lang/en_US.lang | 7 + 4 files changed, 208 insertions(+), 3 deletions(-) create mode 100644 src/main/java/biomesoplenty/common/command/BOPCommand.java create mode 100644 src/main/java/biomesoplenty/common/util/biome/BiomeUtils.java diff --git a/src/main/java/biomesoplenty/common/command/BOPCommand.java b/src/main/java/biomesoplenty/common/command/BOPCommand.java new file mode 100644 index 000000000..962b58d22 --- /dev/null +++ b/src/main/java/biomesoplenty/common/command/BOPCommand.java @@ -0,0 +1,120 @@ +/******************************************************************************* + * Copyright 2015, the Biomes O' Plenty Team + * + * This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International Public License. + * + * To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/. + ******************************************************************************/ + +package biomesoplenty.common.command; + +import java.util.List; + +import net.minecraft.command.CommandBase; +import net.minecraft.command.CommandException; +import net.minecraft.command.ICommandSender; +import net.minecraft.command.WrongUsageException; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.util.BlockPos; +import net.minecraft.util.ChatComponentTranslation; +import net.minecraft.world.World; +import net.minecraft.world.biome.BiomeGenBase; +import biomesoplenty.common.util.biome.BiomeUtils; + +import com.google.common.collect.Lists; + +public class BOPCommand extends CommandBase +{ + @Override + public String getCommandName() + { + return "biomesoplenty"; + } + + @Override + public List getCommandAliases() + { + return Lists.newArrayList("bop", "biomesop"); + } + + @Override + public String getCommandUsage(ICommandSender sender) + { + return "commands.biomesoplenty.usage"; + } + + @Override + public int getRequiredPermissionLevel() + { + return 2; + } + + @Override + public void processCommand(ICommandSender sender, String[] args) throws CommandException + { + if (args.length < 1) + { + throw new WrongUsageException("commands.biomesoplenty.usage"); + } + else if ("biomename".equals(args[0])) + { + getBiomeName(sender, args); + } + else if ("tpbiome".equals(args[0])) + { + teleportFoundBiome(sender, args); + } + } + + private void getBiomeName(ICommandSender sender, String[] args) throws CommandException + { + if (args.length < 2) + { + throw new WrongUsageException("commands.biomesoplenty.biomename.usage"); + } + + int biomeId = parseInt(args[1], 0, 255); + BiomeGenBase biome = BiomeGenBase.getBiome(biomeId); + + sender.addChatMessage(new ChatComponentTranslation("commands.biomesoplenty.biomename.success", biomeId, biome == null ? "Undefined" : biome.biomeName)); + } + + private void teleportFoundBiome(ICommandSender sender, String[] args) throws CommandException + { + if (args.length < 2) + { + throw new WrongUsageException("commands.biomesoplenty.tpbiome.usage"); + } + + int biomeId = parseInt(args[1], 0, 255); + BiomeGenBase biome = BiomeGenBase.getBiome(biomeId); + EntityPlayerMP player = getCommandSenderAsPlayer(sender); + World world = player.worldObj; + BlockPos closestBiomePos = biome == null ? null : BiomeUtils.findBiome(world, biome, player.getPosition()); + + if (closestBiomePos != null) + { + double x = (double)closestBiomePos.getX(); + double y = (double)world.getTopSolidOrLiquidBlock(closestBiomePos).getY(); + double z = (double)closestBiomePos.getZ(); + + player.playerNetServerHandler.setPlayerLocation(x, y, z, player.rotationYaw, player.rotationPitch); + sender.addChatMessage(new ChatComponentTranslation("commands.biomesoplenty.tpbiome.success", player.getCommandSenderName(), biome.biomeName, x, y, z)); + } + else + { + sender.addChatMessage(new ChatComponentTranslation("commands.biomesoplenty.tpbiome.error", biome == null ? "Undefined" : biome.biomeName)); + } + } + + @Override + public List addTabCompletionOptions(ICommandSender sender, String[] args, BlockPos pos) + { + if (args.length == 1) + { + return getListOfStringsMatchingLastWord(args, "biomename", "tpbiome"); + } + + return null; + } +} diff --git a/src/main/java/biomesoplenty/common/util/biome/BiomeUtils.java b/src/main/java/biomesoplenty/common/util/biome/BiomeUtils.java new file mode 100644 index 000000000..935ea4121 --- /dev/null +++ b/src/main/java/biomesoplenty/common/util/biome/BiomeUtils.java @@ -0,0 +1,69 @@ +/******************************************************************************* + * Copyright 2015, the Biomes O' Plenty Team + * + * This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International Public License. + * + * To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/. + ******************************************************************************/ + +package biomesoplenty.common.util.biome; + +import java.util.Arrays; + +import net.minecraft.util.BlockPos; +import net.minecraft.world.World; +import net.minecraft.world.biome.BiomeGenBase; +import net.minecraft.world.biome.WorldChunkManager; + +public class BiomeUtils +{ + public static BlockPos findBiome(World world, BiomeGenBase biome, BlockPos startPos) + { + int radius = 256; + WorldChunkManager chunkManager = world.getWorldChunkManager(); + BlockPos pos1 = null; + BlockPos pos2 = null; + + for (int x = -10; x <= 10; x++) + { + for (int z = -10; z <= 10; z++) + { + if (pos1 == null) + { + BlockPos foundPos = chunkManager.findBiomePosition(startPos.getX() + (x * 512), startPos.getZ() + (z * 512), 256, Arrays.asList(biome), world.rand); + + if (foundPos != null && world.getBiomeGenForCoords(foundPos) == biome) + { + pos1 = foundPos; + } + } + + if (pos2 == null) + { + BlockPos foundPos = chunkManager.findBiomePosition(startPos.getX() + (x * 512), startPos.getZ() + (-z * 512), 256, Arrays.asList(biome), world.rand); + + if (foundPos != null && world.getBiomeGenForCoords(foundPos) == biome) + { + pos2 = foundPos; + } + } + + if (pos1 != null && pos2 != null) + { + break; + } + } + } + + if (pos1 != null && pos2 != null) + { + if (startPos.distanceSq(pos1) < startPos.distanceSq(pos2)) return pos1; + else return pos2; + } + else + { + if (pos1 != null) return pos1; + else return pos2; + } + } +} diff --git a/src/main/java/biomesoplenty/core/BiomesOPlenty.java b/src/main/java/biomesoplenty/core/BiomesOPlenty.java index a889c460b..1015b9e38 100644 --- a/src/main/java/biomesoplenty/core/BiomesOPlenty.java +++ b/src/main/java/biomesoplenty/core/BiomesOPlenty.java @@ -10,9 +10,6 @@ package biomesoplenty.core; import java.io.File; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.Mod.EventHandler; import net.minecraftforge.fml.common.Mod.Instance; @@ -20,6 +17,12 @@ import net.minecraftforge.fml.common.SidedProxy; import net.minecraftforge.fml.common.event.FMLInitializationEvent; import net.minecraftforge.fml.common.event.FMLPostInitializationEvent; import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; +import net.minecraftforge.fml.common.event.FMLServerStartingEvent; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import biomesoplenty.common.command.BOPCommand; import biomesoplenty.common.init.ModBiomes; import biomesoplenty.common.init.ModBlocks; import biomesoplenty.common.init.ModConfiguration; @@ -69,6 +72,12 @@ public class BiomesOPlenty { } + + @EventHandler + public void serverStarting(FMLServerStartingEvent event) + { + event.registerServerCommand(new BOPCommand()); + } public File getConfigDirectory() { diff --git a/src/main/resources/assets/biomesoplenty/lang/en_US.lang b/src/main/resources/assets/biomesoplenty/lang/en_US.lang index 31a5414bc..7a18a3db8 100644 --- a/src/main/resources/assets/biomesoplenty/lang/en_US.lang +++ b/src/main/resources/assets/biomesoplenty/lang/en_US.lang @@ -1,3 +1,10 @@ +commands.biomesoplenty.usage=/biomesoplenty [args] +commands.biomesoplenty.biomename.usage=/biomesoplenty biomename [biomeId] +commands.biomesoplenty.biomename.success=Biome ID %s is associated with %s +commands.biomesoplenty.tpbiome.usage=/biomesoplenty tpbiome [biomeId] +commands.biomesoplenty.tpbiome.success=Teleported %s to biome %s at (%s, %s, %s) +commands.biomesoplenty.tpbiome.error=Couldn't find biome %s! + generator.BIOMESOP=Biomes O' Plenty generator.BIOMESOP.info=Notice: Biomes O' Plenty 1.8 is in a very early state