Skip to content

Commit

Permalink
fix(various): fixed various things related to ui. Added support for r…
Browse files Browse the repository at this point in the history
…l-names in SchematicaMappings

Signed-off-by: TimMayr <[email protected]>
  • Loading branch information
TimMayr committed Feb 9, 2025
1 parent 066b413 commit 0bce20e
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ default FluidState getFluidState(@NotNull BlockPos pos) {
return isPosInside(pos) ? getBlockState(pos).getFluidState() : Fluids.EMPTY.defaultFluidState();
}

@Override
default int getMaxY() {
return getMinY() + getHeight();
}

/**
* @param pos
* tested pos
Expand All @@ -55,11 +60,11 @@ default FluidState getFluidState(@NotNull BlockPos pos) {
*
* @see #isOutsideBuildHeight(BlockPos) extension of
*/
default boolean isPosInside(BlockPos pos) {
default boolean isPosInside(@NotNull BlockPos pos) {
return getMinX() <= pos.getX()
&& pos.getX() < getMaxX()
&& getMinY() <= pos.getY()
&& pos.getY() < getMaxY()
&& pos.getY() <= getMaxY()
&& getMinZ() <= pos.getZ()
&& pos.getZ() < getMaxZ();
}
Expand Down Expand Up @@ -205,7 +210,9 @@ default int getMaxZ() {
*/
int getHeight();

default int getMinY() {return 0;}
default int getMinY() {
return 0;
}

/**
* @param pos
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ public void render(@NotNull GuiGraphics guiGraphics, int index, int left, int to
parent.getParent()
.setTooltipForNextRenderPass(Screen.getTooltipFromItem(parent.getMinecraft(), itemStack)
.stream()
.reduce(Component.empty(),
MutableComponent::append,
MutableComponent::append));
.findFirst().orElse(Component.empty()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ public List<WrappedItemStack> getList(Player player, FakeLevel world, Level mcWo
}

BlockState blockState = world.getBlockState(pos);

Block block = blockState.getBlock();

if (world.getBlockState(pos).isAir()) {
if (blockState.isAir()) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package com.github.lunatrius.schematica.handler.client;

import com.github.lunatrius.core.util.math.MBlockPos;
import com.github.lunatrius.core.util.math.MathHelper;
import com.github.lunatrius.schematica.client.gui.control.SchematicControlScreen;
import com.github.lunatrius.schematica.client.gui.load.SchematicLoadScreen;
import com.github.lunatrius.schematica.client.gui.save.SchematicSaveScreen;
import com.github.lunatrius.schematica.client.printer.SchematicPrinter;
import com.github.lunatrius.schematica.proxy.ClientProxy;
import com.github.lunatrius.schematica.reference.Names;
import com.github.lunatrius.schematica.world.FakeLevel;
import com.mojang.blaze3d.platform.InputConstants;
import com.mojang.serialization.Codec;
import dev.architectury.event.events.client.ClientTickEvent;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.network.chat.Component;
import net.minecraft.util.Mth;
import net.minecraft.world.phys.HitResult;
import org.lwjgl.glfw.GLFW;

Expand All @@ -40,89 +40,89 @@ public class InputHandler {
new KeyMapping(Names.Keys.MOVE_HERE, GLFW.GLFW_KEY_UNKNOWN, Names.Keys.CATEGORY);
private static final KeyMapping KEY_BINDING_PICK_BLOCK =
new KeyMapping(Names.Keys.PICK_BLOCK, GLFW.GLFW_KEY_UNKNOWN, Names.Keys.CATEGORY);
public static final Codec<InputHandler> CODEC = Codec.unit(InputHandler::new);
public static final KeyMapping[] KEY_BINDINGS = new KeyMapping[] {KEY_BINDING_LOAD,
KEY_BINDING_SAVE,
KEY_BINDING_CONTROL,
KEY_BINDING_LAYER_INC,
KEY_BINDING_LAYER_DEC,
KEY_BINDING_LAYER_TOGGLE,
KEY_BINDING_RENDER_TOGGLE,
KEY_BINDING_PRINTER_TOGGLE,
KEY_BINDING_MOVE_HERE,
KEY_BINDING_PICK_BLOCK};
public static final KeyMapping[] KEY_BINDINGS = new KeyMapping[]{KEY_BINDING_LOAD,
KEY_BINDING_SAVE,
KEY_BINDING_CONTROL,
KEY_BINDING_LAYER_INC,
KEY_BINDING_LAYER_DEC,
KEY_BINDING_LAYER_TOGGLE,
KEY_BINDING_RENDER_TOGGLE,
KEY_BINDING_PRINTER_TOGGLE,
KEY_BINDING_MOVE_HERE,
KEY_BINDING_PICK_BLOCK};

private InputHandler() {
ClientTickEvent.CLIENT_POST.register(instance -> {
ClientTickEvent.CLIENT_PRE.register(instance -> {
if (instance.screen == null) {
if (KEY_BINDING_LOAD.isDown()) {
instance.setScreen(new com.github.lunatrius.schematica.client.gui.load.SchematicLoadScreen(instance.screen));
while (KEY_BINDING_LOAD.consumeClick()) {
instance.setScreen(new SchematicLoadScreen(instance.screen));
}

if (KEY_BINDING_SAVE.isDown()) {
while (KEY_BINDING_SAVE.consumeClick()) {
instance.setScreen(new SchematicSaveScreen(instance.screen));
}

if (KEY_BINDING_CONTROL.isDown()) {
while (KEY_BINDING_CONTROL.consumeClick()) {
instance.setScreen(new SchematicControlScreen(instance.screen));
}

if (KEY_BINDING_LAYER_INC.isDown()) {
while (KEY_BINDING_LAYER_INC.isDown()) {
FakeLevel schematic = ClientProxy.schematic;
if (schematic != null && schematic.layerMode != FakeLevel.LayerMode.ALL) {

schematic.renderLayer =
MathHelper.clamp(schematic.renderLayer + 1, 0, schematic.getHeight() - 1);
Mth.clamp(schematic.renderLayer + 1, 0, schematic.getHeight() - 1);
}
}

if (KEY_BINDING_LAYER_DEC.isDown()) {
while (KEY_BINDING_LAYER_DEC.isDown()) {
FakeLevel schematic = ClientProxy.schematic;
if (schematic != null && schematic.layerMode != FakeLevel.LayerMode.ALL) {
schematic.renderLayer =
MathHelper.clamp(schematic.renderLayer - 1, 0, schematic.getHeight() - 1);
Mth.clamp(schematic.renderLayer - 1, 0, schematic.getHeight() - 1);
}
}

if (KEY_BINDING_LAYER_TOGGLE.isDown()) {
while (KEY_BINDING_LAYER_TOGGLE.consumeClick()) {
FakeLevel schematic = ClientProxy.schematic;
if (schematic != null) {
schematic.layerMode = FakeLevel.LayerMode.next(schematic.layerMode);
}
}

if (KEY_BINDING_RENDER_TOGGLE.isDown()) {
while (KEY_BINDING_RENDER_TOGGLE.consumeClick()) {
FakeLevel schematic = ClientProxy.schematic;
if (schematic != null) {
schematic.setRendering(!schematic.isRendering());
}
}

if (KEY_BINDING_PRINTER_TOGGLE.isDown()) {
while (KEY_BINDING_PRINTER_TOGGLE.consumeClick()) {
if (ClientProxy.schematic != null) {
boolean printing = SchematicPrinter.INSTANCE.togglePrinting();
if (instance.player != null) {
instance.player.displayClientMessage(Component.translatable(Names.Messages.TOGGLE_PRINTER,
printing
? Names.Gui.ON
: Names.Gui.OFF), false);
printing
? Names.Gui.ON
: Names.Gui.OFF), false);
}
}
}

if (KEY_BINDING_MOVE_HERE.isDown()) {
while (KEY_BINDING_MOVE_HERE.consumeClick()) {
FakeLevel schematic = ClientProxy.schematic;
if (schematic != null) {
ClientProxy.moveSchematicToPlayer(schematic);
}
}

if (KEY_BINDING_PICK_BLOCK.isDown()) {
while (KEY_BINDING_PICK_BLOCK.consumeClick()) {
FakeLevel schematic = ClientProxy.schematic;
if (schematic != null && schematic.isRendering()) {
pickBlock(schematic, ClientProxy.objectMouseOver, instance);
}
}

}
});
}
Expand All @@ -139,10 +139,9 @@ private void pickBlock(FakeLevel schematic, HitResult objectMouseOver, Minecraft
LocalPlayer player = instance.player;

if (player != null && player.isCreative()) {
int slot = player.getInventory().items.size() - 10 + player.getInventory().selected;
if (instance.gameMode != null) {
instance.gameMode.handlePickItemFromBlock(new MBlockPos(objectMouseOver.getLocation()),
player.input.keyPresses.sprint());
player.input.keyPresses.sprint());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,6 @@ public void init() {
Reference.proxy.createFolders();
SchematicaClientConfig.populateExtraAirBlocks();
SchematicaClientConfig.normalizeSchematicPath();

for (KeyMapping keyMapping : InputHandler.KEY_BINDINGS) {
KeyMappingRegistry.register(keyMapping);
}

// NeoForge.EVENT_BUS.register(new WorldHandler());
Reference.proxy.resetSettings();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ public static class NBT {
public static final String FORMAT_ALPHA = "Alpha";
public static final String FORMAT_STRUCTURE = "Structure";

public static final String ROOT = "Schematic";
public static final String ICON = "Icon";
public static final String BLOCKS = "Blocks";
public static final String WIDTH = "Width";
Expand All @@ -213,6 +214,7 @@ public static class NBT {
public static final String MAPPING_SCHEMATICA = "SchematicaMapping";
public static final String BLOCK_ENTITIES = "TileEntities";
public static final String ENTITIES = "Entities";
public static final String AUTHOR = "Author";
}

public static class Formats {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
Expand All @@ -36,17 +37,40 @@ public ISchematic readFromNBT(CompoundTag tagCompound, Level level) {
int length = tagCompound.getInt(Names.NBT.LENGTH);
int height = tagCompound.getInt(Names.NBT.HEIGHT);

String author = tagCompound.getString(Names.NBT.AUTHOR);

Map<BlockState, BlockState> oldToNew = new HashMap<>();
if (tagCompound.contains(Names.NBT.MAPPING_SCHEMATICA)) {
CompoundTag mapping = tagCompound.getCompound(Names.NBT.MAPPING_SCHEMATICA);
Set<String> names = mapping.getAllKeys();
for (String id : names) {
oldToNew.put(Block.stateById(Integer.parseInt(id)), Block.stateById(mapping.getInt(id)));
BlockState toReplace;

try {
toReplace = Block.stateById(Integer.parseInt(id));
} catch (NumberFormatException n) {
toReplace = BuiltInRegistries.BLOCK.getValue(ResourceLocation.tryParse(id)).defaultBlockState();
}

BlockState replaceWith;

String value = mapping.getString(id);

if (value.isEmpty()) {
replaceWith = Block.stateById(mapping.getInt(id));
} else {
ResourceLocation location = ResourceLocation.tryParse(mapping.getString(id));
replaceWith = BuiltInRegistries.BLOCK.getValue(location).defaultBlockState();
}

oldToNew.put(toReplace, replaceWith);
}
}

MBlockPos pos = new MBlockPos();
ISchematic schematic = new Schematic(icon, width, height, length);
schematic.setAuthor(author);

for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
for (int z = 0; z < length; z++) {
Expand Down Expand Up @@ -174,12 +198,13 @@ public void writeToNBT(CompoundTag tagCompoundIn, @NotNull ISchematic schematic)
}
}

tagCompound.putString(Names.NBT.AUTHOR, schematic.getAuthor());
tagCompound.putString(Names.NBT.FORMAT, Names.NBT.FORMAT_ALPHA);
tagCompound.putIntArray(Names.NBT.BLOCKS, Arrays.stream(localBlocks).mapToInt(Block::getId).toArray());
tagCompound.put(Names.NBT.ENTITIES, entityList);
tagCompound.put(Names.NBT.BLOCK_ENTITIES, blockEntities);
tagCompound.put(Names.NBT.MAPPING_SCHEMATICA, nbtMapping);
tagCompoundIn.put("root", tagCompound);
tagCompoundIn.put(Names.NBT.ROOT, tagCompound);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ public void writeToNBT(@NotNull CompoundTag tagCompoundIn, ISchematic schematic)

template.save(writeTo);
writeTo.putString(Names.NBT.FORMAT, Names.NBT.FORMAT_STRUCTURE);
writeTo.putString(Names.NBT.AUTHOR, schematic.getAuthor());

tagCompoundIn.put("root", writeTo);
tagCompoundIn.put(Names.NBT.ROOT, writeTo);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.github.lunatrius.schematica.fabric.client;

import com.github.lunatrius.schematica.handler.client.InputHandler;
import com.github.lunatrius.schematica.proxy.ClientProxy;
import com.github.lunatrius.schematica.world.FakeLevel;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
Expand Down Expand Up @@ -32,6 +35,10 @@ public void onInitializeClient() {
FakeLevel schematic = ClientProxy.schematic;
ClientProxy.objectMouseOver = schematic != null ? rayTrace(schematic, 1.0f) : null;
});

for (KeyMapping keyBinding : InputHandler.KEY_BINDINGS) {
KeyBindingHelper.registerKeyBinding(keyBinding);
}
}

@SuppressWarnings("SameParameterValue")
Expand All @@ -49,18 +56,18 @@ public void onInitializeClient() {
double posZ = renderViewEntity.getZ();

renderViewEntity.setPos(posX - schematic.getWorldPos().x, posY - schematic.getWorldPos().y,
posZ - schematic.getWorldPos().z);
posZ - schematic.getWorldPos().z);

Vec3 vecPosition = renderViewEntity.getEyePosition(partialTicks);
Vec3 vecLook = renderViewEntity.getLookAngle();
Vec3 vecExtendedLook = vecPosition.add(vecLook.x * blockReachDistance, vecLook.y * blockReachDistance,
vecLook.z * blockReachDistance);
vecLook.z * blockReachDistance);

renderViewEntity.setPos(posX, posY, posZ);

return schematic.clip(
new ClipContext(vecPosition, vecExtendedLook, ClipContext.Block.OUTLINE, ClipContext.Fluid.NONE,
renderViewEntity));
renderViewEntity));
}

throw new IllegalStateException("Error rendering Schematic");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
package com.github.lunatrius.schematica.neoforge.client;

import com.github.lunatrius.schematica.handler.client.InputHandler;
import com.github.lunatrius.schematica.neoforge.SchematicaNeoForge;
import com.github.lunatrius.schematica.neoforge.client.handler.PlayerHandlerNeo;
import com.github.lunatrius.schematica.neoforge.client.handler.RenderTickHandlerNeo;
import com.github.lunatrius.schematica.reference.Reference;
import net.minecraft.client.KeyMapping;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.client.event.RegisterKeyMappingsEvent;
import net.neoforged.neoforge.common.NeoForge;
import org.jetbrains.annotations.NotNull;

@Mod(value = Reference.MOD_ID, dist = Dist.CLIENT)
public class SchematicaNeoClient {
public SchematicaNeoClient() {
public SchematicaNeoClient(@NotNull IEventBus modBus) {
NeoForge.EVENT_BUS.register(PlayerHandlerNeo.INSTANCE);
NeoForge.EVENT_BUS.register(RenderTickHandlerNeo.INSTANCE);
modBus.register(this);
}

//TODO: Maybe move this back to arch in case that works
@SubscribeEvent
public void registerKeys(@NotNull RegisterKeyMappingsEvent event) {
for (KeyMapping keyBinding : InputHandler.KEY_BINDINGS) {
event.register(keyBinding);
}
}
}
Loading

0 comments on commit 0bce20e

Please sign in to comment.