From 31b5ab9d52f7776852027f5e088fcaae1355a9b6 Mon Sep 17 00:00:00 2001 From: bdew Date: Sun, 10 May 2015 22:37:04 +0300 Subject: [PATCH] Added /oredistribution command --- resources/assets/bdlib/lang/en_US.lang | 5 + src/net/bdew/lib/BdLib.scala | 3 +- src/net/bdew/lib/CommandDumpRegistry.scala | 2 +- src/net/bdew/lib/CommandOreDistribution.scala | 97 +++++++++++++++++++ 4 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 src/net/bdew/lib/CommandOreDistribution.scala diff --git a/resources/assets/bdlib/lang/en_US.lang b/resources/assets/bdlib/lang/en_US.lang index 4a11065..89a6735 100644 --- a/resources/assets/bdlib/lang/en_US.lang +++ b/resources/assets/bdlib/lang/en_US.lang @@ -17,3 +17,8 @@ bdlib.rsmode.rs_off=Disable on redstone bdlib.label.empty=Empty +bdlib.oredistribution.usage=/oredistribution [min height] [max height] +bdlib.oredistribution.start1=Checking ore distribution in (%s,%s,%s) to (%s,%s,%s) dim %s +bdlib.oredistribution.start2=Server may lag for a while +bdlib.oredistribution.entry=%s %s - %s (%s%%) +bdlib.oredistribution.warn=%s Warning: %s has multiple variants generated: \ No newline at end of file diff --git a/src/net/bdew/lib/BdLib.scala b/src/net/bdew/lib/BdLib.scala index 62ebe7f..6794880 100644 --- a/src/net/bdew/lib/BdLib.scala +++ b/src/net/bdew/lib/BdLib.scala @@ -42,7 +42,8 @@ object BdLib { @EventHandler def serverStarting(event: FMLServerStartingEvent) { val commandHandler = event.getServer.getCommandManager.asInstanceOf[CommandHandler] - commandHandler.registerCommand(new CommandDumpRegistry) + commandHandler.registerCommand(CommandDumpRegistry) + commandHandler.registerCommand(CommandOreDistribution) onServerStarting.trigger(event) } diff --git a/src/net/bdew/lib/CommandDumpRegistry.scala b/src/net/bdew/lib/CommandDumpRegistry.scala index 38847a9..8b50410 100644 --- a/src/net/bdew/lib/CommandDumpRegistry.scala +++ b/src/net/bdew/lib/CommandDumpRegistry.scala @@ -19,7 +19,7 @@ import net.minecraft.item.ItemStack import net.minecraftforge.fluids.FluidRegistry import net.minecraftforge.oredict.OreDictionary -class CommandDumpRegistry extends CommandBase { +object CommandDumpRegistry extends CommandBase { def getCommandName = "dumpregistry" override def getRequiredPermissionLevel = 2 def getCommandUsage(c: ICommandSender) = "dumpregistry" diff --git a/src/net/bdew/lib/CommandOreDistribution.scala b/src/net/bdew/lib/CommandOreDistribution.scala new file mode 100644 index 0000000..1043442 --- /dev/null +++ b/src/net/bdew/lib/CommandOreDistribution.scala @@ -0,0 +1,97 @@ +/* + * Copyright (c) bdew, 2013 - 2014 + * https://github.com/bdew/bdlib + * + * This mod is distributed under the terms of the Minecraft Mod Public + * License 1.0, or MMPL. Please check the contents of the license located in + * http://bdew.net/minecraft-mod-public-license/ + */ + +package net.bdew.lib + +import cpw.mods.fml.common.registry.GameData +import net.minecraft.command.{CommandBase, ICommandSender, WrongUsageException} +import net.minecraft.entity.player.EntityPlayerMP +import net.minecraft.item.Item +import net.minecraft.util.EnumChatFormatting +import net.minecraftforge.oredict.OreDictionary + +import scala.collection.mutable + +object CommandOreDistribution extends CommandBase { + def getCommandName = "oredistribution" + override def getRequiredPermissionLevel = 2 + def getCommandUsage(c: ICommandSender) = "bdlib.oredistribution.usage" + + def safeInt(s: String) = + try { + s.toInt + } catch { + case e: NumberFormatException => throw new WrongUsageException("bdlib.oredistribution.usage") + } + + def wrapColor(s: String, c: EnumChatFormatting) = c + s + EnumChatFormatting.RESET + + def processCommand(sender: ICommandSender, params: Array[String]) { + if (params.length < 1 || params.length > 3) + throw new WrongUsageException(getCommandUsage(sender)) + + val (startX, startZ, world) = + sender match { + case p: EntityPlayerMP => (p.posX.toInt, p.posZ.toInt, p.worldObj) + case _ => (0, 0, sender.getEntityWorld) + } + + val radius = safeInt(params(0)) + val minY = if (params.length >= 2) safeInt(params(1)) else 0 + val maxY = if (params.length >= 3) safeInt(params(2)) else world.getHeight - 1 + + if (minY < 0 || radius <= 0 || minY > maxY || maxY > world.getHeight - 1) + throw new WrongUsageException(getCommandUsage(sender)) + + // why java, WHY? + CommandBase.func_152373_a(sender, this, "bdlib.oredistribution.start1", + Array[Integer](startX - radius, minY, startZ - radius, startX + radius, maxY, startZ + radius, world.provider.dimensionId): _*) + + CommandBase.func_152373_a(sender, this, "bdlib.oredistribution.start2") + + import scala.collection.JavaConversions._ + + val ores = (OreDictionary.getOreNames.filter(_.startsWith("ore")) flatMap { name => + for (s <- OreDictionary.getOres(name)) yield (s.getItem, s.getItemDamage) -> name + }).toMap + + val distribution = mutable.Map.empty[String, Int].withDefaultValue(0) + val kinds = mutable.Map.empty[String, Set[(Item, Int)]].withDefaultValue(Set.empty) + + for { + x <- startX - radius to startX + radius + y <- minY to maxY + z <- startZ - radius to startZ + radius + if !world.isAirBlock(x, y, z) + } { + val block = world.getBlock(x, y, z) + val item = Item.getItemFromBlock(block) + val damage = block.getDamageValue(world, x, y, z) + for (ore <- ores.get((item, damage))) { + distribution(ore) += 1 + kinds(ore) += ((item, damage)) + } + } + + for ((id, types) <- kinds.filter(_._2.size > 1).toList.sortBy(_._1)) { + CommandBase.func_152373_a(sender, this, "bdlib.oredistribution.warn", + wrapColor(" !", EnumChatFormatting.RED), wrapColor(id.substring(3), EnumChatFormatting.RED)) + for ((item, dmg) <- types) + CommandBase.func_152373_a(sender, this, wrapColor(" - ", EnumChatFormatting.RED) + GameData.getItemRegistry.getNameForObject(item) + "@" + dmg) + } + + val total = distribution.map(_._2).sum + + for ((id, num) <- distribution.filter(_._2 > 0).toList.sortBy(-_._2)) { + CommandBase.func_152373_a(sender, this, "bdlib.oredistribution.entry", + " *", wrapColor(id.substring(3), EnumChatFormatting.YELLOW), + DecFormat.round(num), DecFormat.short(100F * num / total)) + } + } +} \ No newline at end of file