Skip to content

Commit

Permalink
cleanup: commands
Browse files Browse the repository at this point in the history
add a keybinding for celestial map
  • Loading branch information
marcus8448 committed May 25, 2024
1 parent 6fdff06 commit 905f8e0
Show file tree
Hide file tree
Showing 19 changed files with 362 additions and 336 deletions.
10 changes: 5 additions & 5 deletions src/main/generated/assets/galacticraft/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -244,32 +244,31 @@
"chat.galacticraft.rocket.warning": "Press again to launch.",
"commands.galacticraft.debug.registry.dump": "Dumped: %s",
"commands.galacticraft.debug.registry.id": "%s - %s: %s",
"commands.galacticraft.dimensiontp.failure.already_in_dimension": "Already inside dimension %s",
"commands.galacticraft.dimensiontp.failure.entity": "Invalid entity",
"commands.galacticraft.dimensiontp.success.multiple": "Teleported %s entities to %s",
"commands.galacticraft.dimensiontp.success.pos": "Teleported to %s at (%d, %d, %d)",
"commands.galacticraft.dimensiontp.success.single": "Teleported to %s",
"commands.galacticraft.gchouston.cannot_detect_signal": "We cannot locate your signal! Are you sure you're in space?",
"commands.galacticraft.gchouston.confirm": "Er, Houston, we have a problem... (Run this command again to confirm teleport)",
"commands.galacticraft.gchouston.on_earth_already": "I don't need to be rescued!",
"commands.galacticraft.gchouston.success": "You have been rescued. Better luck next time...",
"commands.galacticraft.opencelestialscreen.requires_player": "Requires Player",
"commands.galacticraft.oxygen.get.area.full": "Area is filled with oxygen",
"commands.galacticraft.oxygen.get.area.none": "Area contains no oxygen",
"commands.galacticraft.oxygen.get.area.partial": "Area partially contains oxygen",
"commands.galacticraft.oxygen.get.single.no_oxygen": "No oxygen at block",
"commands.galacticraft.oxygen.get.single.oxygen": "Oxygen exists at block",
"commands.galacticraft.oxygen.set.multiple": "Set oxygen at blocks",
"commands.galacticraft.oxygen.set.single": "Set oxygen at block",
"commands.galacticraft.require_player": "Command requires player",
"config.galacticraft.client": "Client",
"config.galacticraft.client.skybox": "Skybox",
"config.galacticraft.commands": "Commands",
"config.galacticraft.commands.enable_gc_houston": "Enable /gchouston",
"config.galacticraft.debug": "Debug",
"config.galacticraft.debug.hide_alpha_warning": "Hide Alpha Warning",
"config.galacticraft.debug.logging": "Debug Logging",
"config.galacticraft.energy": "Energy",
"config.galacticraft.energy.machines": "Machines",
"config.galacticraft.energy.machines.circuit_fabricator_energy_consumption_rate": "Circuit Fabricator Energy Consumption Rate/t",
"config.galacticraft.energy.machines.coal_generator_energy_production_rate": "Coal Generator Energy Production Rate/t",
"config.galacticraft.energy.machines.electric_compressor_energy_consumption_rate": "Electric Compressor Energy Consumption Rate/t",
"config.galacticraft.energy.machines.electric_furnace_energy_consumption_rate": "Electric Furnace Energy Consumption Rate/t",
"config.galacticraft.energy.machines.energy_storage_module_storage_size": "Energy Storage Module Energy Storage Size",
"config.galacticraft.energy.machines.energy_storage_size": "Default Machine Energy Storage Size",
Expand Down Expand Up @@ -478,6 +477,7 @@
"itemGroup.galacticraft.blocks": "Galacticraft Blocks",
"itemGroup.galacticraft.items": "Galacticraft Items",
"itemGroup.galacticraft.machines": "Galacticraft Machines",
"key.galacticraft.open_celestial_screen": "Open Celestial Map",
"key.galacticraft.rocket.inventory": "Open Rocket Inventory",
"planet.galacticraft.asteroids": "Asteroids",
"planet.galacticraft.asteroids.description": "",
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/galacticraft/mod/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ interface Structure {
interface Command {
String HOUSTON = "gchouston";
String DIMENSION_TP = "dimensiontp";
String DIMENSION_TP_ALIAS = "dimtp";
String DIMTP = "dimtp";
String OPEN_CELESTIAL_SCREEN = "opencelestialscreen";
}

Expand Down
5 changes: 2 additions & 3 deletions src/main/java/dev/galacticraft/mod/GalacticraftClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
import net.fabricmc.fabric.api.client.model.loading.v1.ModelLoadingPlugin;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.client.particle.v1.ParticleFactoryRegistry;
Expand Down Expand Up @@ -181,6 +180,8 @@ public void onInitializeClient() {

});

ModelLoadingPlugin.register(GCModelLoader.INSTANCE);

Constant.LOGGER.info("Client initialization complete. (Took {}ms.)", System.currentTimeMillis() - startInitTime);
}

Expand All @@ -189,8 +190,6 @@ public void onInitializeClient() {
* should be safe to do general initialization here.
*/
public static void init() {
// ModelLoadingPlugin.register(GCModelLoader.INSTANCE);

var helper = ResourceManagerHelper.get(PackType.CLIENT_RESOURCES);
helper.registerReloadListener(RocketTextureManager.INSTANCE);
helper.registerReloadListener(GCModelLoader.INSTANCE);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/dev/galacticraft/mod/api/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public interface Config {

double bossHealthMultiplier();

boolean enableGcHouston();

void load();

void save();
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/dev/galacticraft/mod/client/GCKeyBinds.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,29 @@

package dev.galacticraft.mod.client;

import dev.galacticraft.mod.client.gui.screen.ingame.CelestialSelectionScreen;
import dev.galacticraft.mod.util.Translations;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import org.lwjgl.glfw.GLFW;

public class GCKeyBinds {
public static final KeyMapping OPEN_ROCKET_INVENTORY = KeyBindingHelper.registerKeyBinding(new KeyMapping(Translations.Keybindings.ROCKET_INVENTORY, GLFW.GLFW_KEY_F, "key.categories.inventory"));
public static final KeyMapping OPEN_CELESTIAL_SCREEN = new KeyMapping(Translations.Keybindings.OPEN_CELESTIAL_SCREEN, GLFW.GLFW_KEY_M, KeyMapping.CATEGORY_MISC);
public static final KeyMapping OPEN_ROCKET_INVENTORY = new KeyMapping(Translations.Keybindings.ROCKET_INVENTORY, GLFW.GLFW_KEY_F, KeyMapping.CATEGORY_INVENTORY);

public static void register() {
KeyBindingHelper.registerKeyBinding(OPEN_CELESTIAL_SCREEN);
KeyBindingHelper.registerKeyBinding(OPEN_ROCKET_INVENTORY);
}

public static void handleKeybinds(Minecraft client) {
if (client.level != null && client.player != null) {
if (client.screen == null) {
while (OPEN_CELESTIAL_SCREEN.consumeClick()) {
client.setScreen(new CelestialSelectionScreen(true, null, false, null));
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public class CelestialSelectionScreen extends Screen {
protected static final ResourceLocation TEXTURE_1 = Constant.id("textures/gui/celestial_selection_1.png");
protected static int BORDER_SIZE = 0;
protected static int BORDER_EDGE_SIZE = 0;
protected final boolean mapMode;
public final boolean mapMode;
private final @Nullable RocketData data;
public final boolean canCreateStations;
protected float zoom = 0.0F;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static void register() {
boolean mapMode = buf.readBoolean();
client.execute(() -> client.setScreen(new CelestialSelectionScreen(mapMode, RocketData.fromNbt(GCItems.ROCKET.getDefaultInstance().getTag()), true, null)));
}
default -> Constant.LOGGER.error("No screen found!");
default -> Constant.LOGGER.error("No screen found with id '{}'!", screen);
}
});

Expand Down
137 changes: 137 additions & 0 deletions src/main/java/dev/galacticraft/mod/command/DimensionTpCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Copyright (c) 2019-2024 Team Galacticraft
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package dev.galacticraft.mod.command;

import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.galacticraft.mod.Constant;
import dev.galacticraft.mod.network.GCScreenType;
import dev.galacticraft.mod.util.Translations;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.DimensionArgument;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.commands.arguments.coordinates.BlockPosArgument;
import net.minecraft.commands.arguments.coordinates.Vec3Argument;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.Collections;

public class DimensionTpCommand {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(Commands.literal(Constant.Command.DIMENSION_TP)
.requires(stack -> stack.hasPermission(2))
.executes(DimensionTpCommand::openCelestialScreen)
.then(Commands.argument("dimension", DimensionArgument.dimension())
.executes(ctx -> teleportToDimension(ctx, Collections.singleton(ctx.getSource().getPlayerOrException()), null))
.then(Commands.argument("players", EntityArgument.players())
.executes(ctx -> teleportToDimension(ctx, EntityArgument.getPlayers(ctx, "players"), null))
.then(Commands.argument("pos", Vec3Argument.vec3(true))
.executes(ctx -> teleportToDimension(ctx, EntityArgument.getPlayers(ctx, "players"), Vec3Argument.getVec3(ctx, "pos")))))
.then(Commands.argument("pos", BlockPosArgument.blockPos())
.executes(ctx -> teleportToDimension(ctx, Collections.singleton(ctx.getSource().getPlayerOrException()), Vec3Argument.getVec3(ctx, "pos")))
)
)
);

dispatcher.register(Commands.literal(Constant.Command.DIMTP)
.redirect(dispatcher.getRoot().getChild(Constant.Command.DIMENSION_TP))
);
}

private static int teleportToDimension(CommandContext<CommandSourceStack> context, @NotNull Collection<ServerPlayer> players, @Nullable Vec3 pos) throws CommandSyntaxException {
ServerLevel level = DimensionArgument.getDimension(context, "dimension");

int success = 0;
for (ServerPlayer player : players) {
success += tryTeleport(level, player, pos) ? 1 : 0;
}

if (success > 0) {
if (success == 1) {
context.getSource().sendSuccess(() -> Component.translatable(Translations.DimensionTp.SUCCESS_SINGLE, level.dimension().location()), true);
} else {
int tmp = success;
context.getSource().sendSuccess(() -> Component.translatable(Translations.DimensionTp.SUCCESS_MULTIPLE, tmp, level.dimension().location()), true);
}
return success;
}
return -1;
}

private static boolean tryTeleport(ServerLevel level, ServerPlayer player, @Nullable Vec3 pos) {
if (pos == null) pos = getValidTeleportPos(level, player);
player.teleportTo(level,
pos.x,
pos.y,
pos.z,
player.getYRot(),
player.getXRot()
);
return true;
}

private static int openCelestialScreen(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
var player = context.getSource().getPlayerOrException();

var buf = PacketByteBufs.create();
buf.writeEnum(GCScreenType.CELESTIAL);
buf.writeBoolean(false);
ServerPlayNetworking.send(player, Constant.Packet.OPEN_SCREEN, buf);

return Command.SINGLE_SUCCESS;
}


/**
* Finds the highest solid block in the level to teleport to.
* @param level The level.
* @param entity The entity to teleport.
* @return The highest valid position to teleport to.
*/
static Vec3 getValidTeleportPos(ServerLevel level, Entity entity) {
if (entity.level() == level) return entity.position();
BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
pos.set(entity.blockPosition());

for (int y = level.getMaxBuildHeight(); y > level.getMinBuildHeight(); y--) {
pos.setY(y);
if (!level.getBlockState(pos).isAir()) {
return new Vec3(entity.getX(), y + 1.0, entity.getZ());
}
}
return entity.position();
}
}
Loading

0 comments on commit 905f8e0

Please sign in to comment.