Repaired loading and placement of old DimDoors Pockets

-Added separate Block class for Chaos Door
-Set up Personal DimDoor to create its own type of tile-entity
-Added flag "2" to update blocks upon PocketTemplate placement to
prevent non-defaultstate doors from breaking upon placement.
-Used write- and read- Compressed instead of GZIP Streams
-Closed the output stream
-Added canRiftBePaired field to Rift tile entities
-Added Chaos- and Personal- Door Rift Tile Entities
-Removed statement where I falsely assumed that a meta of 0 meant that
the blockstate was the default.
-Corrected several other mistakes in Schematic.java
-Had to make sure that the blockstate of ancient fabric didn't get
turned into fabric of reality again...
This commit is contained in:
Mathijs Riezebos 2017-02-16 14:28:28 +01:00
parent d0af9178f0
commit 86eb14c408
11 changed files with 120 additions and 51 deletions

View file

@ -95,7 +95,7 @@ public class PocketTemplate { //there is exactly one pocket placer for each diff
for (int x = 0; x < schematic.getWidth(); x++) {
for (int y = 0; y < schematic.getHeight(); y++) {
for (int z = 0; z < schematic.getWidth(); z++) {
world.setBlockState(new BlockPos(xBase + x, yBase + y, zBase + z), schematic.getPallette().get(schematic.getBlockData()[x][y][z]));
world.setBlockState(new BlockPos(xBase + x, yBase + y, zBase + z), schematic.getPallette().get(schematic.getBlockData()[x][y][z]), 2); //the "2" is to make non-default door-halves not break upon placement
}
}
}

View file

@ -28,8 +28,6 @@ import java.util.List;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.CompressedStreamTools;
import org.apache.commons.io.IOUtils;
@ -140,42 +138,50 @@ public class SchematicHandler {
File oldVersionSchematicFile = new File(schematicFolder, "/" + template.getName() + ".schematic");
NBTTagCompound schematicNBT;
//@todo make the following block less repetitious.
//try to load the schematic from 4 different locations/formats
Schematic schematic = null;
if (schematicStream != null) {
try {
GZIPInputStream schematicZipStream = new GZIPInputStream(schematicStream);
schematicNBT = CompressedStreamTools.read(new DataInputStream(schematicZipStream));
schematic = Schematic.loadFromNBT(schematicNBT);
try (DataInputStream schematicDataStream = new DataInputStream(schematicStream)) {
schematicNBT = CompressedStreamTools.readCompressed(schematicDataStream);
schematic = Schematic.loadFromNBT(schematicNBT);
}
} catch (IOException ex) {
Logger.getLogger(SchematicHandler.class.getName()).log(Level.SEVERE, "Schematic file " + template.getName() + ".schem did not load correctly from jar.", ex);
} finally {
}
} else if (oldVersionSchematicStream != null) {
try {
GZIPInputStream schematicZipStream = new GZIPInputStream(oldVersionSchematicStream);
schematicNBT = CompressedStreamTools.read(new DataInputStream(schematicZipStream));
schematic = Schematic.loadFromNBT(schematicNBT);
try (DataInputStream schematicDataStream = new DataInputStream(oldVersionSchematicStream)) {
schematicNBT = CompressedStreamTools.readCompressed(schematicDataStream);
schematic = Schematic.loadFromNBT(schematicNBT);
}
} catch (IOException ex) {
Logger.getLogger(SchematicHandler.class.getName()).log(Level.SEVERE, "Schematic file " + template.getName() + ".schematic did not load correctly from jar.", ex);
}
} else if (schematicFile.exists()) {
try {
GZIPInputStream schematicZipStream = new GZIPInputStream(new FileInputStream(schematicFile));
schematicNBT = CompressedStreamTools.read(new DataInputStream(schematicZipStream));
schematic = Schematic.loadFromNBT(schematicNBT);
try (DataInputStream schematicDataStream = new DataInputStream(new FileInputStream(schematicFile))) {
schematicNBT = CompressedStreamTools.readCompressed(schematicDataStream);
schematic = Schematic.loadFromNBT(schematicNBT);
}
} catch (IOException ex) {
Logger.getLogger(SchematicHandler.class.getName()).log(Level.SEVERE, "Schematic file " + template.getName() + ".schem did not load correctly from config folder.", ex);
}
} else if (oldVersionSchematicFile.exists()) {
try {
GZIPInputStream schematicZipStream = new GZIPInputStream(new FileInputStream(oldVersionSchematicFile));
schematicNBT = CompressedStreamTools.read(new DataInputStream(schematicZipStream));
schematic = Schematic.loadFromNBT(schematicNBT);
try (DataInputStream schematicDataStream = new DataInputStream(new FileInputStream(oldVersionSchematicFile))) {
schematicNBT = CompressedStreamTools.readCompressed(schematicDataStream);
schematic = Schematic.loadFromNBT(schematicNBT);
}
} catch (IOException ex) {
Logger.getLogger(SchematicHandler.class.getName()).log(Level.SEVERE, "Schematic file " + template.getName() + ".schematic did not load correctly from config folder.", ex);
}
} else {
DimDoors.warn(SchematicHandler.class, "Schematic '" + template.getName() + "' was not found in the jar or config directory, neither with the .schem extension, nor with the .schematic extension.");
}
if (schematic != null
&& (schematic.getWidth() > (template.getSize()) * 16 || schematic.getLength() > (template.getSize()) * 16)) {
schematic = null;
@ -277,10 +283,10 @@ public class SchematicHandler {
File saveFile = new File(saveFolder.getAbsolutePath() + "/" + name + ".schem");
try {
saveFile.createNewFile();
GZIPOutputStream schematicZipStream = new GZIPOutputStream(new FileOutputStream(saveFile));
CompressedStreamTools.write(schematicNBT, new DataOutputStream(schematicZipStream));
schematicZipStream.flush();
schematicZipStream.close();
DataOutputStream schematicDataStream = new DataOutputStream(new FileOutputStream(saveFile));
CompressedStreamTools.writeCompressed(schematicNBT, schematicDataStream);
schematicDataStream.flush();
schematicDataStream.close();
} catch (IOException ex) {
Logger.getLogger(SchematicHandler.class.getName()).log(Level.SEVERE, null, ex);
}

View file

@ -1,17 +1,20 @@
package com.zixiken.dimdoors.shared.blocks;
import com.zixiken.dimdoors.shared.items.ModItems;
import com.zixiken.dimdoors.shared.tileentities.TileEntityDimDoorChaos;
import java.util.Random;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
public class BlockDimDoorUnstable extends BlockDimDoorBase {
public class BlockDimDoorChaos extends BlockDimDoorBase {
public static final String ID = "blockDimDoorChaos";
public BlockDimDoorUnstable() {
public BlockDimDoorChaos() {
super(Material.IRON);
setHardness(.2F);
setUnlocalizedName(ID);
@ -28,4 +31,9 @@ public class BlockDimDoorUnstable extends BlockDimDoorBase {
public Item getItemDropped(IBlockState state, Random random, int fortune) {
return Items.IRON_DOOR;
}
@Override
public TileEntity createNewTileEntity(World world, int metadata) {
return new TileEntityDimDoorChaos();
}
}

View file

@ -1,8 +1,11 @@
package com.zixiken.dimdoors.shared.blocks;
import com.zixiken.dimdoors.shared.items.ModItems;
import com.zixiken.dimdoors.shared.tileentities.TileEntityDimDoorPersonal;
import net.minecraft.block.material.Material;
import net.minecraft.item.Item;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
public class BlockDimDoorPersonal extends BlockDimDoorBase {
@ -19,5 +22,10 @@ public class BlockDimDoorPersonal extends BlockDimDoorBase {
public Item getItemDoor() {
return ModItems.itemDimDoorPersonal;
}
@Override
public TileEntity createNewTileEntity(World world, int metadata) {
return new TileEntityDimDoorPersonal();
}
}

View file

@ -10,7 +10,7 @@ public class ModBlocks {
public static BlockDimDoorTransient blockDimDoorTransient;
public static BlockDimDoorWarp blockDimDoorWarp;
public static BlockDimDoorGold blockDimDoorGold;
public static BlockDimDoorUnstable blockDimDoorChaos;
public static BlockDimDoorChaos blockDimDoorChaos;
public static BlockDimDoor blockDimDoor;
public static BlockTransTrapdoor blockDimHatch;
public static BlockDimWall blockDimWall;
@ -22,7 +22,7 @@ public class ModBlocks {
GameRegistry.register(blockDimDoorPersonal = new BlockDimDoorPersonal());
GameRegistry.register(blockDoorGold = new BlockDoorGold());
GameRegistry.register(blockDimDoorGold = new BlockDimDoorGold());
GameRegistry.register(blockDimDoorChaos = new BlockDimDoorUnstable());
GameRegistry.register(blockDimDoorChaos = new BlockDimDoorChaos());
GameRegistry.register(blockDimDoorWarp = new BlockDimDoorWarp());
GameRegistry.register(blockDimDoor = new BlockDimDoor());
GameRegistry.register(blockDimHatch = new BlockTransTrapdoor());

View file

@ -50,6 +50,7 @@ public class ItemRiftBlade extends ItemSword {
if (world.isRemote) {
return new ActionResult(EnumActionResult.FAIL, stack);
}
//SchematicHandler.Instance.getPersonalPocketTemplate().place(0, 20, 0, 20, 0, 0, 1, EnumPocketType.DUNGEON); //this line can be activated for testing purposes
RayTraceResult hit = rayTrace(world, player, true);
if (RayTraceHelper.isRift(hit, world)) {
EnumActionResult canDoorBePlacedOnGroundBelowRift
@ -59,7 +60,7 @@ public class ItemRiftBlade extends ItemSword {
stack.damageItem(1, player);
}
return new ActionResult(canDoorBePlacedOnGroundBelowRift, stack);
} else if (RayTraceHelper.isLivingEntity(hit)) {
TeleportHelper.teleport(player, new Location(world, hit.getBlockPos())); //@todo teleport to a location 1 or 2 blocks distance from the entity
return new ActionResult(EnumActionResult.PASS, stack);

View file

@ -12,6 +12,7 @@ import net.minecraft.world.World;
public abstract class DDTileEntityBase extends TileEntity {
public boolean canRiftBePaired = true;
private boolean isPaired = false;
private int riftID = -1; //should not start at 0
private int pairedRiftID = -1;

View file

@ -0,0 +1,18 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.zixiken.dimdoors.shared.tileentities;
/**
*
* @author Robijnvogel
*/
public class TileEntityDimDoorChaos extends TileEntityDimDoor {
public TileEntityDimDoorChaos() {
canRiftBePaired = true;
}
}

View file

@ -0,0 +1,18 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.zixiken.dimdoors.shared.tileentities;
/**
*
* @author Robijnvogel
*/
public class TileEntityDimDoorPersonal extends TileEntityDimDoor {
public TileEntityDimDoorPersonal() {
canRiftBePaired = true;
}
}

View file

@ -35,9 +35,9 @@ public class Schematic {
private static final String[] NEWDIMDOORBLOCKNAMES = new String[]{
"blockDimDoor",
"blockDimWall", //I think [type=fabric] is the default blockstate
"blockDimWall", //[type=fabric] is the default blockstate
"blockDimDoorTransient",
"blockDimDoorWarp"}; //@todo make these lists complete, possibly with specific blockstate as well?
"blockDimDoorWarp"};
int version = Integer.parseInt("1"); //@todo set in build.gradle ${spongeSchematicVersion}
String author = "DimDoors"; //@todo set in build.gradle ${modID}
@ -121,7 +121,7 @@ public class Schematic {
blockstate = getBlockStateWithProperties(block, properties);
} else {
}
schematic.pallette.add(blockstate);
schematic.pallette.add(blockstate); //@todo, can we assume that a schematic file always has all palette integers used from 0 to pallettemax-1?
}
if (nbt.hasKey("PaletteMax")) { //PaletteMax is not required
schematic.paletteMax = nbt.getInteger("PaletteMax");
@ -175,6 +175,7 @@ public class Schematic {
Map<Integer, String> paletteMap = new HashMap();
for (int i = 0; i < schematic.pallette.size(); i++) {
IBlockState state = schematic.pallette.get(i);
DimDoors.log(Schematic.class, "Saving BlockState: " + state.toString());
String blockStateString = getBlockStateStringFromState(state);
paletteNBT.setInteger(blockStateString, i);
}
@ -214,7 +215,7 @@ public class Schematic {
if (property != null) {
Comparable<?> value = null;
for (Comparable<?> object : property.getAllowedValues()) {
if (object.equals(entry.getValue())) {
if (object.toString().equals(entry.getValue())) {
value = object;
break;
}
@ -228,19 +229,26 @@ public class Schematic {
}
private static String getBlockStateStringFromState(IBlockState state) {
String blockNameString = state.getBlock().getLocalizedName(); //@todo, check if this is the correct method
Block block = state.getBlock();
String blockNameString = "" + Block.REGISTRY.getNameForObject(block);
String blockStateString = "";
String totalString;
if (state.equals(state.getBlock().getDefaultState())) {
IBlockState defaultState = block.getDefaultState();
if (state == defaultState) {
totalString = blockNameString;
} else {
BlockStateContainer container = state.getBlock().getBlockState();
container.getProperties();
for (IProperty property : container.getProperties()) {
String firstHalf = property.getName();
String secondHalf = state.getProperties().get(property).toString();
String propertyString = firstHalf + "=" + secondHalf;
blockStateString += propertyString + ",";
} else { //there is at least one property not equal to the default state's property
BlockStateContainer container = block.getBlockState();
for (IProperty property : container.getProperties()) { //for every property that is valid for this type of Block
String defaultPropertyValue = defaultState.getProperties().get(property).toString();
String thisPropertyValue = state.getProperties().get(property).toString();
if (defaultPropertyValue.equals(thisPropertyValue)) {
//do nothing
} else {
String firstHalf = property.getName();
String secondHalf = state.getProperties().get(property).toString();
String propertyString = firstHalf + "=" + secondHalf;
blockStateString += propertyString + ",";
}
}
blockStateString = blockStateString.substring(0, blockStateString.length() - 1); //removes the last comma
totalString = blockNameString + "[" + blockStateString + "]";
@ -300,7 +308,7 @@ public class Schematic {
return tileEntities;
}
public static Schematic loadOldDimDoorSchematicFromNBT(NBTTagCompound nbt) { //@todo, maybe make this a separate class, so values can be final so they HAVE TO be set in a newly designed constructor
public static Schematic loadOldDimDoorSchematicFromNBT(NBTTagCompound nbt) { //@todo, maybe make this a separate class, so values can be final so they HAVE TO be set in a newly designed constructor?
Schematic schematic = new Schematic();
//schematic.version = 1; //already the default value
@ -315,7 +323,8 @@ public class Schematic {
//schematic.offset = new int[]{0, 0, 0}; //already the default value
NBTTagList paletteNBT = (NBTTagList) nbt.getTag("Palette");
for (int i = 0; i <= paletteNBT.tagCount(); i++) {
for (int i = 0; i < paletteNBT.tagCount(); i++) {
DimDoors.log(Schematic.class, "reading pallete from schematic... i = " + i);
String blockString = paletteNBT.getStringTagAt(i);
boolean isAncientFabric = false;
if (blockString.startsWith("dimdoors")) {
@ -332,13 +341,12 @@ public class Schematic {
Block block = ForgeRegistries.BLOCKS.getValue(new ResourceLocation(blockString));
blockstate = block.getDefaultState();
} else {
Block block = ForgeRegistries.BLOCKS.getValue(new ResourceLocation("blockDimWall"));
Block block = ForgeRegistries.BLOCKS.getValue(new ResourceLocation("dimdoors:blockDimWall"));
blockstate = getBlockStateWithProperties(block, new String[]{"type=ancient"});
}
schematic.pallette.add(blockstate);
}
//check whether or not this blockstate is already in the list
byte[] blockIntArray = nbt.getByteArray("Blocks");
byte[] dataIntArray = nbt.getByteArray("Data");
schematic.blockData = new int[schematic.width][schematic.height][schematic.length];
@ -347,19 +355,20 @@ public class Schematic {
for (int z = 0; z < schematic.length; z++) {
int blockInt = blockIntArray[x + z * schematic.width + y * schematic.width * schematic.length]; //according to the documentation on https://github.com/SpongePowered/Schematic-Specification/blob/master/versions/schematic-1.md
int metadata = dataIntArray[x + z * schematic.width + y * schematic.width * schematic.length]; //according to the documentation on https://github.com/SpongePowered/Schematic-Specification/blob/master/versions/schematic-1.md
if (metadata != 0) {
IBlockState basesState = schematic.pallette.get(blockInt); //this is needed for the various "types" that a block can have
IBlockState additionalState = basesState.getBlock().getStateFromMeta(metadata);
for (IProperty property : basesState.getProperties().keySet()) {
Comparable value = basesState.getProperties().get(property);
additionalState = additionalState.withProperty(property, value);
}
if (schematic.pallette.contains(additionalState)) {
IBlockState baseState = schematic.pallette.get(blockInt); //this is the default blockstate except for ancient fabric
if (baseState == baseState.getBlock().getDefaultState()) { //should only be false if {@code baseState} is ancient fabric
IBlockState additionalState = baseState.getBlock().getStateFromMeta(metadata);
if (schematic.pallette.contains(additionalState)) { //check whether or not this blockstate is already in the list
blockInt = schematic.pallette.indexOf(additionalState);
} else {
schematic.pallette.add(additionalState);
DimDoors.log(Schematic.class, "New blockstate detected. Original blockInt = " + blockInt + " and baseState is " + baseState.toString());
blockInt = schematic.pallette.size() - 1;
}
} else { //if this is ancient fabric
//DimDoors.log(Schematic.class, "Non-default blockstate in palette detected. Original blockInt = " + blockInt + " and baseState is " + baseState.toString()); //@todo should only print a line on load of ancient fabric
blockInt = schematic.pallette.indexOf(baseState);
}
schematic.blockData[x][y][z] = blockInt;
}
@ -378,7 +387,7 @@ public class Schematic {
private static String convertOldDimDoorsBlockNameToNewDimDoorsBlockName(String dimdoorsBlockName) {
if (OLDDIMDOORBLOCKNAMES.length != NEWDIMDOORBLOCKNAMES.length) {
DimDoors.warn(Schematic.class, "The array of old dimdoors block names, somehow isn't the same length as the array of new names, therefore the dimdoors blocks in this schematic will not be loaded.");
DimDoors.warn(Schematic.class, "The array of old dimdoors block names somehow isn't the same length as the array of new names, therefore the dimdoors blocks in this schematic will not be loaded. This is a bug in the DimDoors mod itself.");
return null;
}