Moving to source sets

This commit is contained in:
Sebastian Hartte
2020-07-01 23:36:51 +02:00
parent f2e3d81fd7
commit 2642ced86b
2924 changed files with 794 additions and 796 deletions
@@ -1,60 +0,0 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 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, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 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 Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.server;
import static net.minecraft.server.command.CommandManager.literal;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraftforge.fml.server.ServerLifecycleHooks;
import appeng.api.features.AEFeature;
import appeng.core.AEConfig;
public final class AECommand {
public void register(CommandDispatcher<ServerCommandSource> dispatcher) {
LiteralArgumentBuilder<ServerCommandSource> builder = literal("ae2");
for (Commands command : Commands.values()) {
if (command.test && !AEConfig.instance().isFeatureEnabled(AEFeature.UNSUPPORTED_DEVELOPER_TOOLS)) {
continue;
}
add(builder, command);
}
dispatcher.register(builder);
}
private void add(LiteralArgumentBuilder<net.minecraft.server.command.ServerCommandSource> builder, Commands subCommand) {
LiteralArgumentBuilder<ServerCommandSource> subCommandBuilder = literal(subCommand.name().toLowerCase())
.requires(src -> src.hasPermissionLevel(subCommand.level));
subCommand.command.addArguments(subCommandBuilder);
subCommandBuilder.executes(ctx -> {
subCommand.command.call(ServerLifecycleHooks.getCurrentServer(), ctx, ctx.getSource());
return 1;
});
builder.then(subCommandBuilder);
}
}
@@ -1,51 +0,0 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 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, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 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 Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.server;
public enum AccessType {
/**
* allows basic access to manipulate the block via gui, or other.
*/
BLOCK_ACCESS,
/**
* Can player deposit items into the network.
*/
NETWORK_DEPOSIT,
/**
* can player withdraw items from the network.
*/
NETWORK_WITHDRAW,
/**
* can player issue crafting requests?
*/
NETWORK_CRAFT,
/**
* can player add new blocks to the network.
*/
NETWORK_BUILD,
/**
* can player manipulate security settings.
*/
NETWORK_SECURITY
}
@@ -0,0 +1,82 @@
package appeng.server;
import appeng.api.parts.CableRenderMode;
import appeng.block.AEBaseBlock;
import appeng.client.ActionKey;
import appeng.client.EffectType;
import appeng.core.AppEngBase;
import appeng.core.sync.network.ServerNetworkHandler;
import appeng.hooks.TickHandler;
import net.fabricmc.fabric.api.server.PlayerStream;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.client.util.InputUtil;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.hit.HitResult;
import net.minecraft.world.World;
import javax.annotation.Nonnull;
import java.util.List;
import java.util.Random;
import java.util.stream.Stream;
public final class AppEngServer extends AppEngBase {
private final MinecraftServer server;
private final ServerNetworkHandler networkHandler;
private final TickHandler tickHandler;
public AppEngServer(MinecraftServer server) {
this.server = server;
this.networkHandler = new ServerNetworkHandler();
this.tickHandler = new TickHandler();
}
@Override
public Stream<? extends PlayerEntity> getPlayers() {
return PlayerStream.all(server);
}
@Override
public void spawnEffect(EffectType effect, World world, double posX, double posY, double posZ, Object extra) {
}
@Override
public boolean shouldAddParticles(Random r) {
return false;
}
@Override
public HitResult getRTR() {
return null;
}
@Override
public void postInit() {
}
@Override
public CableRenderMode getRenderMode() {
return null;
}
@Override
public void updateRenderMode(PlayerEntity player) {
}
@Override
public boolean isActionKey(@Nonnull ActionKey key, InputUtil.Key input) {
return false;
}
@Override
public MinecraftServer getServer() {
return server;
}
}
-45
View File
@@ -1,45 +0,0 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 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, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 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 Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.server;
import appeng.server.subcommands.ChunkLogger;
import appeng.server.subcommands.Supporters;
import appeng.server.subcommands.TestMeteoritesCommand;
import appeng.server.subcommands.TestOreGenCommand;
public enum Commands {
Chunklogger(4, new ChunkLogger(), false), Supporters(0, new Supporters(), false),
TestOreGen(4, new TestOreGenCommand(), true), TestMeteorites(4, new TestMeteoritesCommand(), true);
public final int level;
public final ISubCommand command;
public boolean test;
Commands(final int level, final ISubCommand w, boolean test) {
this.level = level;
this.command = w;
this.test = test;
}
@Override
public String toString() {
return this.name();
}
}
@@ -1,33 +0,0 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 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, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 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 Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.server;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.MinecraftServer;
public interface ISubCommand {
default void addArguments(LiteralArgumentBuilder<ServerCommandSource> builder) {
}
void call(MinecraftServer srv, CommandContext<ServerCommandSource> ctx, ServerCommandSource sender);
}
@@ -1,122 +0,0 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 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, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 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 Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.server;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.client.util.InputMappings;
import net.minecraft.client.util.InputUtil;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.hit.HitResult;
import net.minecraft.world.World;
import net.minecraftforge.fml.server.ServerLifecycleHooks;
import appeng.api.parts.CableRenderMode;
import appeng.block.AEBaseBlock;
import appeng.client.ActionKey;
import appeng.client.EffectType;
import appeng.core.CommonHelper;
import appeng.core.sync.BasePacket;
import appeng.core.sync.network.NetworkHandler;
import appeng.items.tools.NetworkToolItem;
import appeng.util.Platform;
public class ServerHelper extends CommonHelper {
@Override
public World getWorld() {
throw new UnsupportedOperationException("This is a server...");
}
@Override
public void bindTileEntitySpecialRenderer(final Class<? extends BlockEntity> tile, final AEBaseBlock blk) {
throw new UnsupportedOperationException("This is a server...");
}
@Override
public List<? extends PlayerEntity> getPlayers() {
if (!Platform.isClient()) {
final MinecraftServer server = ServerLifecycleHooks.getCurrentServer();
if (server != null) {
return server.getPlayerList().getPlayers();
}
}
return new ArrayList<>();
}
@Override
public void sendToAllNearExcept(final PlayerEntity p, final double x, final double y, final double z,
final double dist, final World w, final BasePacket packet) {
if (w.isClient()) {
return;
}
for (final PlayerEntity o : this.getPlayers()) {
final ServerPlayerEntity entityplayermp = (ServerPlayerEntity) o;
if (entityplayermp != p && entityplayermp.world == w) {
final double dX = x - entityplayermp.getX();
final double dY = y - entityplayermp.getY();
final double dZ = z - entityplayermp.getZ();
if (dX * dX + dY * dY + dZ * dZ < dist * dist) {
NetworkHandler.instance().sendTo(packet, entityplayermp);
}
}
}
}
@Override
public void spawnEffect(final EffectType type, final World world, final double posX, final double posY,
final double posZ, final Object o) {
// :P
}
@Override
public boolean shouldAddParticles(final Random r) {
return false;
}
@Override
public HitResult getRTR() {
return null;
}
@Override
public void postInit() {
}
@Override
public CableRenderMode getRenderMode() {
}
@Override
public boolean isActionKey(ActionKey key, InputUtil.Key input) {
return false;
}
}
@@ -1,81 +0,0 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 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, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 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 Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.server.subcommands;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.MinecraftServer;
import net.minecraft.text.TranslatableText;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.world.ChunkEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import appeng.api.features.AEFeature;
import appeng.core.AEConfig;
import appeng.core.AELog;
import appeng.server.ISubCommand;
public class ChunkLogger implements ISubCommand {
private boolean enabled = false;
private void displayStack() {
if (AEConfig.instance().isFeatureEnabled(AEFeature.CHUNK_LOGGER_TRACE)) {
boolean output = false;
for (final StackTraceElement e : Thread.currentThread().getStackTrace()) {
if (output) {
AELog.info(
" " + e.getClassName() + '.' + e.getMethodName() + " (" + e.getLineNumber() + ')');
} else {
output = e.getClassName().contains("EventBus") && e.getMethodName().contains("post");
}
}
}
}
@SubscribeEvent
public void onChunkLoadEvent(final ChunkEvent.Load event) {
if (!event.getWorld().isClient()) {
AELog.info("Chunk Loaded: " + event.getChunk().getPos().x + ", " + event.getChunk().getPos().z);
this.displayStack();
}
}
@SubscribeEvent
public void onChunkUnloadEvent(final ChunkEvent.Unload unload) {
if (!unload.getWorld().isClient()) {
AELog.info("Chunk Unloaded: " + unload.getChunk().getPos().x + ", " + unload.getChunk().getPos().z);
this.displayStack();
}
}
@Override
public void call(final MinecraftServer srv, final CommandContext<ServerCommandSource> data, final ServerCommandSource sender) {
this.enabled = !this.enabled;
if (this.enabled) {
MinecraftForge.EVENT_BUS.register(this);
sender.sendFeedback(new TranslatableText("commands.ae2.ChunkLoggerOn"), true);
} else {
MinecraftForge.EVENT_BUS.unregister(this);
sender.sendFeedback(new TranslatableText("commands.ae2.ChunkLoggerOff"), true);
}
}
}
@@ -1,38 +0,0 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 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, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 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 Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.server.subcommands;
import com.google.common.base.Joiner;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.MinecraftServer;
import net.minecraft.text.LiteralText;
import appeng.server.ISubCommand;
public class Supporters implements ISubCommand {
@Override
public void call(final MinecraftServer srv, final CommandContext<ServerCommandSource> data, final ServerCommandSource sender) {
final String[] who = { "Stig Halvorsen", "Josh Ricker", "Jenny \"Othlon\" Sutherland", "Hristo Bogdanov",
"BevoLJ" };
sender.sendFeedback(new LiteralText("Special thanks to " + Joiner.on(", ").join(who)), true);
}
}
@@ -1,213 +0,0 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 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, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 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 Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.server.subcommands;
import static net.minecraft.server.command.CommandManager.literal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import com.google.common.math.StatsAccumulator;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.MinecraftServer;
import net.minecraft.text.ClickEvent;
import net.minecraft.util.Formatting;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.text.Text;
import net.minecraft.text.LiteralText;
import net.minecraft.util.text.event.ClickEvent;
import net.minecraft.util.text.event.HoverEvent;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.ChunkStatus;
import net.minecraft.world.gen.ChunkGenerator;
import net.minecraft.world.gen.Heightmap;
import net.minecraft.world.gen.feature.structure.StructureStart;
import net.minecraft.server.world.ServerWorld;
import net.minecraftforge.fml.server.ServerLifecycleHooks;
import appeng.server.ISubCommand;
import appeng.worldgen.meteorite.MeteoriteStructure;
import appeng.worldgen.meteorite.MeteoriteStructurePiece;
import appeng.worldgen.meteorite.PlacedMeteoriteSettings;
/**
* This is a testing command to validate quartz ore generation.
*/
public class TestMeteoritesCommand implements ISubCommand {
@Override
public void addArguments(LiteralArgumentBuilder<ServerCommandSource> builder) {
builder.then(literal("force").executes(ctx -> {
test(ServerLifecycleHooks.getCurrentServer(), ctx.getSource(), true);
return 1;
}));
}
@Override
public void call(final MinecraftServer srv, final CommandContext<ServerCommandSource> ctx, final ServerCommandSource sender) {
test(srv, sender, false);
}
private static void test(MinecraftServer srv, final ServerCommandSource sender, boolean force) {
int radius = 100;
ServerPlayerEntity player = null;
try {
player = sender.asPlayer();
} catch (CommandSyntaxException ignored) {
}
ServerWorld world;
BlockPos centerBlock;
if (player != null) {
world = player.getServerWorld();
centerBlock = new BlockPos(player.getX(), 0, player.getZ());
} else {
world = srv.getOverworld();
centerBlock = world.getSpawnPoint();
}
ChunkPos center = new ChunkPos(centerBlock);
ChunkGenerator<?> generator = world.getChunkManager().getChunkGenerator();
// Find all meteorites in the given rectangle
List<PlacedMeteoriteSettings> found = new ArrayList<>();
int chunksChecked = 0;
for (int cx = center.x - radius; cx <= center.x + radius; cx++) {
for (int cz = center.z - radius; cz <= center.z + radius; cz++) {
chunksChecked++;
ChunkPos cp = new ChunkPos(cx, cz);
BlockPos p = new BlockPos(cp.getXStart(), 0, cp.getZStart());
BlockPos nearest = MeteoriteStructure.INSTANCE.findNearest(world, generator, p, 0, false);
if (nearest != null) {
Chunk chunk = world.getChunk(cx, cz, ChunkStatus.STRUCTURE_STARTS);
// The actual relevant information is in the structure piece
MeteoriteStructurePiece piece = getMeteoritePieceFromChunk(chunk);
if (piece != null) {
found.add(piece.getSettings());
}
}
}
}
// Create stats on how far apart the meteorites are
StatsAccumulator stats = new StatsAccumulator();
for (PlacedMeteoriteSettings settings : found) {
double closestOther = Double.NaN;
for (PlacedMeteoriteSettings otherSettings : found) {
if (otherSettings != settings) {
double d = settings.getPos().distanceSq(otherSettings.getPos());
if (Double.isNaN(closestOther) || d < closestOther) {
closestOther = d;
}
}
}
if (!Double.isNaN(closestOther)) {
stats.add(Math.sqrt(closestOther));
}
}
found.sort(Comparator.comparingDouble(settings -> settings.getPos().distanceSq(centerBlock)));
sendLine(sender, "Chunks checked: %d", chunksChecked);
sendLine(sender, "Meteorites found: %d", found.size());
sendLine(sender, "Closest: min=%.2f max=%.2f mean=%.2f stddev=%.2f", stats.min(), stats.max(), stats.mean(),
stats.populationStandardDeviation());
int closestCount = Math.min(10, found.size());
for (int i = 0; i < closestCount; i++) {
PlacedMeteoriteSettings settings = found.get(i);
BlockPos pos = settings.getPos();
String state = "not final";
if (force && settings.getFallout() == null) {
Chunk chunk = world.getChunk(pos);
MeteoriteStructurePiece piece = getMeteoritePieceFromChunk(chunk);
if (piece == null) {
state = "removed";
} else {
settings = piece.getSettings();
pos = settings.getPos();
}
}
Text restOfLine;
if (settings.getFallout() == null) {
restOfLine = new LiteralText(
String.format(Locale.ROOT, ", radius=%.2f [%s]", settings.getMeteoriteRadius(), state));
} else {
restOfLine = new LiteralText(String.format(Locale.ROOT, ", radius=%.2f, crater=%s, fallout=%s",
settings.getMeteoriteRadius(), settings.getCraterType().name().toLowerCase(),
settings.getFallout().name().toLowerCase()));
}
Text msg = new LiteralText(" #" + (i + 1) + " ");
msg.append(getClickablePosition(world, settings, pos)).append(restOfLine);
// Add a tooltip
Text tooltip = new LiteralText(settings.toString() + "\nBiome: ")
.append(world.getBiome(pos).getName());
msg.formatted(style -> style.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, tooltip)));
sender.sendFeedback(msg, true);
}
}
// Add a clickable link to teleport the user to the Meteorite
private static Text getClickablePosition(ServerWorld world, PlacedMeteoriteSettings settings,
BlockPos pos) {
BlockPos tpPos = pos.up((int) Math.ceil(settings.getMeteoriteRadius()));
int surfaceY = world.getHeight(Heightmap.Type.WORLD_SURFACE, tpPos).getY();
if (surfaceY > tpPos.getY()) {
tpPos = new BlockPos(tpPos.getX(), surfaceY, tpPos.getZ());
}
String displayText = String.format(Locale.ROOT, "pos=%d,%d,%d", tpPos.getX(), tpPos.getY(), tpPos.getZ());
String tpCommand = String.format(Locale.ROOT, "/tp @s %d %d %d", tpPos.getX(), tpPos.getY(), tpPos.getZ());
return new LiteralText(displayText)
.formatted(Formatting.UNDERLINE)
.styled(style -> style.withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, tpCommand)));
}
private static MeteoriteStructurePiece getMeteoritePieceFromChunk(Chunk chunk) {
StructureStart start = chunk.getStructureStart(MeteoriteStructure.INSTANCE.getStructureName());
if (start != null && start.getComponents().size() > 0
&& start.getComponents().get(0) instanceof MeteoriteStructurePiece) {
return (MeteoriteStructurePiece) start.getComponents().get(0);
}
return null;
}
private static void sendLine(ServerCommandSource sender, String text, Object... args) {
sender.sendFeedback(new LiteralText(String.format(Locale.ROOT, text, args)), true);
}
}
@@ -1,182 +0,0 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 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, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 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 Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.server.subcommands;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.function.ToDoubleFunction;
import java.util.stream.Collectors;
import com.google.common.math.StatsAccumulator;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.block.BlockState;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.MinecraftServer;
import net.minecraft.text.LiteralText;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.world.chunk.ChunkStatus;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.server.world.ServerWorld;
import appeng.core.Api;
import appeng.server.ISubCommand;
/**
* This is a testing command to validate quartz ore generation.
*/
public class TestOreGenCommand implements ISubCommand {
private final BlockState quartzOre;
private final BlockState chargedQuartzOre;
public TestOreGenCommand() {
quartzOre = Api.INSTANCE.definitions().blocks().quartzOre().block().getDefaultState();
chargedQuartzOre = Api.INSTANCE.definitions().blocks().quartzOreCharged().block().getDefaultState();
}
@Override
public void call(final MinecraftServer srv, final CommandContext<ServerCommandSource> data, final ServerCommandSource sender) {
int radius = 1000;
ServerWorld world;
BlockPos center;
try {
ServerPlayerEntity player = sender.asPlayer();
world = player.getServerWorld();
center = new BlockPos(player.getX(), 0, player.getZ());
} catch (CommandSyntaxException e) {
world = srv.getOverworld();
center = world.getSpawnPoint();
}
ChunkPos tl = new ChunkPos(center.add(-radius, 0, -radius));
ChunkPos br = new ChunkPos(center.add(radius, 0, radius));
Stats stats = new Stats();
for (int cx = tl.x; cx <= br.x; cx++) {
for (int cz = tl.z; cz <= br.z; cz++) {
ChunkPos cp = new ChunkPos(cx, cz);
checkChunk(sender, world, cp, stats);
}
}
AggregatedStats oreCount = AggregatedStats.create(stats.chunks, cs -> (double) cs.quartzOreCount);
List<ChunkStats> chunksWithOre = stats.chunks.stream().filter(c -> c.quartzOreCount > 0)
.collect(Collectors.toList());
AggregatedStats minHeight = AggregatedStats.create(chunksWithOre, cs -> (double) cs.minHeight);
AggregatedStats maxHeight = AggregatedStats.create(chunksWithOre, cs -> (double) cs.maxHeight);
AggregatedStats chargedCount = AggregatedStats.create(chunksWithOre, cs -> (double) cs.chargedOreCount);
sendLine(sender, "Checked %d chunks", stats.chunks.size());
sendLine(sender, " Count: %s", oreCount);
sendLine(sender, " Min-Height: %s", minHeight);
sendLine(sender, " Max-Height: %s", maxHeight);
sendLine(sender, " Sub-Type Count: %s", chargedCount);
}
private void checkChunk(ServerCommandSource sender, ServerWorld world, ChunkPos cp, Stats stats) {
Chunk chunk = world.getChunk(cp.x, cp.z, ChunkStatus.FULL, false);
if (chunk == null) {
sendLine(sender, "Skipping chunk %s", cp);
return;
}
ChunkStats chunkStats = new ChunkStats();
BlockPos.Mutable blockPos = new BlockPos.Mutable();
sendLine(sender, "Checking chunk %s", cp);
for (int x = cp.getXStart(); x <= cp.getXEnd(); x++) {
blockPos.setX(x);
for (int z = cp.getZStart(); z <= cp.getZEnd(); z++) {
blockPos.setZ(z);
for (int y = 0; y < world.getMaxHeight(); y++) {
blockPos.setY(y);
BlockState state = chunk.getBlockState(blockPos);
if (state == quartzOre || state == chargedQuartzOre) {
chunkStats.minHeight = Math.min(chunkStats.minHeight, y);
chunkStats.maxHeight = Math.max(chunkStats.maxHeight, y);
chunkStats.quartzOreCount++;
if (state == chargedQuartzOre) {
chunkStats.chargedOreCount++;
}
}
}
}
}
stats.chunks.add(chunkStats);
}
private static void sendLine(ServerCommandSource sender, String text, Object... args) {
sender.sendFeedback(new LiteralText(String.format(Locale.ROOT, text, args)), true);
}
private static class Stats {
public final List<ChunkStats> chunks = new ArrayList<>();
}
private static class ChunkStats {
public int quartzOreCount = 0;
public int chargedOreCount = 0;
public int minHeight = Integer.MAX_VALUE;
public int maxHeight = Integer.MIN_VALUE;
}
private static class AggregatedStats {
public final double min;
public final double max;
public final double mean;
public final double stdDev;
public AggregatedStats(double min, double max, double mean, double stdDev) {
this.min = min;
this.max = max;
this.mean = mean;
this.stdDev = stdDev;
}
public static <T> AggregatedStats create(List<T> values, ToDoubleFunction<T> getter) {
if (values.isEmpty()) {
return new AggregatedStats(Double.NaN, Double.NaN, Double.NaN, Double.NaN);
}
StatsAccumulator accumulator = new StatsAccumulator();
for (T value : values) {
accumulator.add(getter.applyAsDouble(value));
}
return new AggregatedStats(accumulator.min(), accumulator.max(), accumulator.mean(),
accumulator.populationStandardDeviation());
}
@Override
public String toString() {
if (Double.isNaN(min)) {
return "Invalid";
}
return String.format(Locale.ROOT, "min=%.2f, max=%.2f, mean=%.2f, stdDev=%.2f", min, max, mean, stdDev);
}
}
}