/* * 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 . */ package appeng.items.storage; import appeng.api.implementations.TransitionResult; import appeng.api.implementations.items.ISpatialStorageCell; import appeng.api.storage.ISpatialDimension; import appeng.api.util.WorldCoord; import appeng.core.AELog; import appeng.core.localization.GuiText; import appeng.core.worlddata.SpatialDimensionManager; import appeng.items.AEBaseItem; import appeng.spatial.StorageHelper; import net.minecraft.client.util.ITooltipFlag; import net.minecraft.item.ItemStack; import net.minecraft.nbt.CompoundNBT; import net.minecraft.util.ResourceLocation; import net.minecraft.util.math.BlockPos; import net.minecraft.util.text.ITextComponent; import net.minecraft.world.World; import net.minecraft.world.dimension.DimensionType; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; import java.util.List; public class ItemSpatialStorageCell extends AEBaseItem implements ISpatialStorageCell { private static final String TAG_DIMENSION_ID = "dimension_id"; private final int maxRegion; public ItemSpatialStorageCell( Properties props, final int spatialScale ) { super(props); this.maxRegion = spatialScale; } @OnlyIn( Dist.CLIENT ) @Override public void addInformation(final ItemStack stack, final World world, final List lines, final ITooltipFlag advancedTooltips ) { if (advancedTooltips.isAdvanced()) { final DimensionType dimType = this.getStoredDimension(stack); if (dimType != null && dimType.getRegistryName() != null) { String ae2Id = dimType.getRegistryName().getPath(); lines.add(GuiText.CellId.textComponent().appendText(": " + ae2Id)); } } } @Override public boolean isSpatialStorage( final ItemStack is ) { return true; } @Override public int getMaxStoredDim( final ItemStack is ) { return this.maxRegion; } @Override public DimensionType getStoredDimension(final ItemStack is ) { final CompoundNBT c = is.getTag(); if( c != null && c.contains(TAG_DIMENSION_ID) ) { try { ResourceLocation dimTypeId = new ResourceLocation(c.getString(TAG_DIMENSION_ID) ); return DimensionType.byName(dimTypeId); } catch (Exception e) { AELog.warn("Failed to retrieve storage cell dimension.", e); } } return null; } @Override public TransitionResult doSpatialTransition( final ItemStack is, final World w, final WorldCoord min, final WorldCoord max, int playerId ) { final int targetX = max.x - min.x - 1; final int targetY = max.y - min.y - 1; final int targetZ = max.z - min.z - 1; final int maxSize = this.getMaxStoredDim( is ); final BlockPos targetSize = new BlockPos( targetX, targetY, targetZ ); ISpatialDimension manager = new SpatialDimensionManager(); DimensionType storedDim = this.getStoredDimension( is ); if( storedDim == null ) { storedDim = manager.createNewCellDimension( targetSize); } if (storedDim == null) { // Failed to create the dimension return new TransitionResult(false, 0); } try { if( manager.isCellDimension( storedDim ) ) { World cellWorld = manager.getWorld(storedDim); BlockPos scale = manager.getCellContentSize( storedDim ); if( scale.equals( targetSize ) ) { if( targetX <= maxSize && targetY <= maxSize && targetZ <= maxSize ) { BlockPos offset = manager.getCellDimensionOrigin( storedDim ); this.setStoredDimension( is, storedDim); StorageHelper.getInstance() .swapRegions( w, min.x + 1, min.y + 1, min.z + 1, cellWorld, offset.getX(), offset.getY(), offset.getZ(), targetX - 1, targetY - 1, targetZ - 1 ); return new TransitionResult( true, 0 ); } } } return new TransitionResult( false, 0 ); } finally { // clean up newly created dimensions that failed transfer if( manager.isCellDimension( storedDim ) && this.getStoredDimension( is ) == null ) { manager.deleteCellDimension( storedDim ); } } } private void setStoredDimension(final ItemStack is, DimensionType dim) { final CompoundNBT c = is.getOrCreateTag(); c.putString(TAG_DIMENSION_ID, dim.getRegistryName().toString() ); } }