Skip to content
This repository has been archived by the owner on Jan 21, 2023. It is now read-only.

Basic Save/Load functionality. #35

Open
wants to merge 1 commit into
base: master-MC1.12
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions build.properties
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
minecraft.version=1.12
minecraft.mappings=snapshot_20170629
forge.version=14.21.1.2387
minecraft.version=1.12.2
minecraft.mappings=snapshot_20171003
forge.version=14.23.2.2611

mod.name=Architect
mod.group=li.cil.architect
mod.version=1.2.0

chiselsandbits.version=13.0.78
jei.version=4.7.0.68
chiselsandbits.version=14.0.99
jei.version=4.8.5.157

maven.url=file:///home/www/maven.cil.li/web
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void onWorldRender(final RenderWorldLastEvent event) {
} else if (AbstractProvider.isBoundToEntity(stack)) {
if (AbstractProvider.getDimension(stack) == player.getEntityWorld().provider.getDimension()) {
final Entity entity = AbstractProvider.getEntity(stack, mc.world);
if (entity != null && player.getDistanceSqToEntity(entity) <= 64 * 64) {
if (entity != null && player.getDistanceSq(entity) <= 64 * 64) {

GlStateManager.color(0.2f, 0.9f, 0.4f, 0.4f);
renderEntitySelector(entity, dt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
import net.minecraft.item.ItemStack;

public enum ItemColorBlueprint implements IItemColor {
INSTANCE;
INSTANCE;

@Override
public int getColorFromItemstack(final ItemStack stack, final int tintIndex) {
switch (tintIndex) {
case 0:
return 0xFFFFFFF;
case 1: {
final EnumDyeColor color = ItemBlueprint.getColor(stack);
return color == null ? 0x00000000 : (0xFF000000 | color.getColorValue());
}
default:
return 0xFFFF00FF;
}
}
@Override
public int colorMultiplier(ItemStack stack, int tintIndex) {
switch (tintIndex) {
case 0:
return 0xFFFFFFF;
case 1: {
final EnumDyeColor color = ItemBlueprint.getColor(stack);
return color == null ? 0x00000000 : (0xFF000000 | color.getColorValue());
}
default:
return 0xFFFF00FF;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public CommandArchitect() {
addSubCommand(new SubCommandReload());
addSubCommand(new SubCommandCopy());
addSubCommand(new SubCommandPaste());
addSubCommand(new SubCommandSave());
addSubCommand(new SubCommandLoad());
}

private void addSubCommand(final CommandBase command) {
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/li/cil/architect/common/command/SubCommandLoad.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package li.cil.architect.common.command;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;

import org.apache.commons.io.FileUtils;

import com.google.common.base.Charsets;

import li.cil.architect.api.API;
import li.cil.architect.common.Architect;
import li.cil.architect.common.init.Items;
import li.cil.architect.common.network.Network;
import li.cil.architect.common.network.message.MessageClipboard;
import li.cil.architect.common.network.message.MessageRequestBlueprintData;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.command.WrongUsageException;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.server.MinecraftServer;
import net.minecraftforge.fml.common.Loader;

public final class SubCommandLoad extends AbstractSubCommand {

@Override
public String getName() {
return "load";
}

@Override
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
final EntityPlayerMP player = getCommandSenderAsPlayer(sender);
final ItemStack stack = Items.getHeldItem(player, Items::isBlueprint);
if (stack.isEmpty()) {
throw new WrongUsageException(getUsage(sender));
}

final String configDirectory = Loader.instance().getConfigDir().getPath();
final File path = Paths.get(configDirectory, API.MOD_ID, args[0] + ".bp").toFile();
try {
String value = FileUtils.readFileToString(path, Charsets.UTF_8);
Network.INSTANCE.getWrapper().sendTo(new MessageClipboard(value), player);
Network.INSTANCE.getWrapper().sendTo(new MessageRequestBlueprintData(), player);
} catch (IOException e) {
Architect.getLog().warn("Failed reading " + path.toString() + ".", e);
}
}
}
59 changes: 59 additions & 0 deletions src/main/java/li/cil/architect/common/command/SubCommandSave.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package li.cil.architect.common.command;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Base64;

import org.apache.commons.io.FileUtils;

import com.google.common.base.Charsets;

import li.cil.architect.api.API;
import li.cil.architect.common.Architect;
import li.cil.architect.common.config.Constants;
import li.cil.architect.common.init.Items;
import li.cil.architect.common.item.ItemBlueprint;
import li.cil.architect.common.network.Network;
import li.cil.architect.common.network.message.MessageClipboard;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.command.WrongUsageException;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.server.MinecraftServer;
import net.minecraftforge.fml.common.Loader;

public final class SubCommandSave extends AbstractSubCommand {

@Override
public String getName() {
return "save";
}

@Override
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
final EntityPlayerMP player = getCommandSenderAsPlayer(sender);
final ItemStack stack = Items.getHeldItem(player, Items::isBlueprint);
if (stack.isEmpty()) {
throw new WrongUsageException(getUsage(sender));
}

final String configDirectory = Loader.instance().getConfigDir().getPath();
final File path = Paths.get(configDirectory, API.MOD_ID, stack.getDisplayName() + ".bp").toFile();

try {
final NBTTagCompound nbt = ItemBlueprint.getData(stack).serializeNBT();
final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
CompressedStreamTools.writeCompressed(nbt, bytes);
final String value = Base64.getEncoder().encodeToString(bytes.toByteArray());
FileUtils.writeStringToFile(path, value, Charsets.UTF_8);
notifyCommandListener(sender, this, Constants.COMMAND_SAVE_SUCCES);
} catch (final IOException e) {
Architect.getLog().warn("Failed writing " + path.toString() + ".", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public final class Constants {
public static final String COMMAND_RELOAD_SUCCESS = "commands.architect.reload.success";
public static final String COMMAND_RELOAD_ERRORS = "commands.architect.reload.errors";
public static final String COMMAND_RELOAD_ERROR = "commands.architect.reload.error";
public static final String COMMAND_SAVE_SUCCES = "commands.architect.save.success";

// --------------------------------------------------------------------- //
// Messages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ public void addInformation(final ItemStack stack, @Nullable final World world, f
} else if (isBoundToEntity(stack) && world != null) {
final Entity entity = getEntity(stack, world);
if (entity != null) {
final int distance = MathHelper.ceil(player.getDistanceToEntity(entity));
final int distance = MathHelper.ceil(player.getDistance(entity));
tooltip.add(I18n.format(Constants.TOOLTIP_PROVIDER_ENTITY, entity.getDisplayName().getFormattedText(), distance));
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/architect/lang/en_US.lang
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ commands.architect.nbt.no_tile_entity=Not a tile entity.
commands.architect.reload.success=Converter settings reloaded.
commands.architect.reload.errors=§cSome settings could not be reloaded due to errors:
commands.architect.reload.error=%s: %s
commands.architect.save.success=Successfully saved blueprint.

message.architect.grid_snapping.enabled=§aGrid snapping enabled.
message.architect.grid_snapping.disabled=§cGrid snapping disabled.
Expand Down