2017-10-28 03:28:00 +00:00
|
|
|
/*
|
|
|
|
* Minecraft Forge
|
2019-02-10 22:57:03 +00:00
|
|
|
* Copyright (c) 2016-2019.
|
2017-10-28 03:28:00 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation version 2.1
|
|
|
|
* of the License.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2018-07-01 21:17:28 +00:00
|
|
|
|
2017-10-28 03:28:00 +00:00
|
|
|
package net.minecraftforge.server.command;
|
|
|
|
|
|
|
|
import java.text.DecimalFormat;
|
2018-09-15 07:59:19 +00:00
|
|
|
import java.util.stream.Collectors;
|
2019-02-14 23:08:53 +00:00
|
|
|
import java.util.stream.StreamSupport;
|
2017-10-28 03:28:00 +00:00
|
|
|
|
2018-09-15 07:59:19 +00:00
|
|
|
import com.mojang.brigadier.builder.ArgumentBuilder;
|
|
|
|
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
|
|
|
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
|
|
|
|
import net.minecraft.command.CommandSource;
|
|
|
|
import net.minecraft.command.Commands;
|
2019-02-14 23:08:53 +00:00
|
|
|
import net.minecraft.command.arguments.DimensionArgument;
|
2019-05-23 23:02:15 +00:00
|
|
|
import net.minecraft.util.text.TranslationTextComponent;
|
2018-09-15 07:59:19 +00:00
|
|
|
import net.minecraft.world.dimension.DimensionType;
|
2017-10-28 03:28:00 +00:00
|
|
|
|
2018-09-15 07:59:19 +00:00
|
|
|
class CommandTps
|
2017-10-28 03:28:00 +00:00
|
|
|
{
|
2019-05-23 23:02:15 +00:00
|
|
|
private static final DynamicCommandExceptionType INVALID_DIMENSION = new DynamicCommandExceptionType(dim -> new TranslationTextComponent("commands.forge.tps.invalid", dim, StreamSupport.stream(DimensionType.getAll().spliterator(), false).map(d -> DimensionType.getKey(d).toString()).sorted().collect(Collectors.joining(", "))));
|
2017-10-28 03:28:00 +00:00
|
|
|
private static final DecimalFormat TIME_FORMATTER = new DecimalFormat("########0.000");
|
|
|
|
|
2018-09-15 07:59:19 +00:00
|
|
|
static ArgumentBuilder<CommandSource, ?> register()
|
2017-10-28 03:28:00 +00:00
|
|
|
{
|
2018-09-22 10:40:22 +00:00
|
|
|
return Commands.literal("tps")
|
|
|
|
.requires(cs->cs.hasPermissionLevel(0)) //permission
|
2019-05-23 23:02:15 +00:00
|
|
|
.then(Commands.argument("dim", DimensionArgument.getDimension())
|
2020-02-26 03:45:14 +00:00
|
|
|
.executes(ctx -> sendTime(ctx.getSource(), DimensionArgument.getDimensionArgument(ctx, "dim")))
|
2018-09-15 07:59:19 +00:00
|
|
|
)
|
|
|
|
.executes(ctx -> {
|
2019-05-23 23:02:15 +00:00
|
|
|
for (DimensionType dim : DimensionType.getAll())
|
2018-09-15 07:59:19 +00:00
|
|
|
sendTime(ctx.getSource(), dim);
|
2017-10-28 03:28:00 +00:00
|
|
|
|
2018-09-22 10:40:22 +00:00
|
|
|
double meanTickTime = mean(ctx.getSource().getServer().tickTimeArray) * 1.0E-6D;
|
2018-09-15 07:59:19 +00:00
|
|
|
double meanTPS = Math.min(1000.0/meanTickTime, 20);
|
2019-05-23 23:02:15 +00:00
|
|
|
ctx.getSource().sendFeedback(new TranslationTextComponent("commands.forge.tps.summary.all", TIME_FORMATTER.format(meanTickTime), TIME_FORMATTER.format(meanTPS)), true);
|
2017-10-28 03:28:00 +00:00
|
|
|
|
2018-09-15 07:59:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
);
|
2017-10-28 03:28:00 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 23:08:53 +00:00
|
|
|
private static int sendTime(CommandSource cs, DimensionType dim) throws CommandSyntaxException
|
2017-10-28 03:28:00 +00:00
|
|
|
{
|
2019-02-14 23:08:53 +00:00
|
|
|
long[] times = cs.getServer().getTickTime(dim);
|
2017-10-28 03:28:00 +00:00
|
|
|
|
2018-09-15 07:59:19 +00:00
|
|
|
if (times == null)
|
2019-05-23 23:02:15 +00:00
|
|
|
throw INVALID_DIMENSION.create(DimensionType.getKey(dim));
|
2017-10-28 03:28:00 +00:00
|
|
|
|
2018-09-15 07:59:19 +00:00
|
|
|
double worldTickTime = mean(times) * 1.0E-6D;
|
|
|
|
double worldTPS = Math.min(1000.0 / worldTickTime, 20);
|
2019-05-23 23:02:15 +00:00
|
|
|
cs.sendFeedback(new TranslationTextComponent("commands.forge.tps.summary.named", dim.getId(), DimensionType.getKey(dim), TIME_FORMATTER.format(worldTickTime), TIME_FORMATTER.format(worldTPS)), true);
|
2018-09-15 07:59:19 +00:00
|
|
|
|
|
|
|
return 1;
|
2017-10-28 03:28:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static long mean(long[] values)
|
|
|
|
{
|
|
|
|
long sum = 0L;
|
|
|
|
for (long v : values)
|
|
|
|
sum += v;
|
|
|
|
return sum / values.length;
|
|
|
|
}
|
|
|
|
}
|