Skip to content

Commit

Permalink
Map copy and transcription updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Stormwind99 committed Jan 10, 2020
1 parent 661974b commit f87477c
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/wumple/util/map/MapCreation.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static ItemStack doCreate(World worldIn, List<ItemStack> inputs)
*
* @param worldIn
* @param inputs
* @return new map ItemStack that trys to include data from all input maps
* @return new MapProps that trys to include data from all input maps
*/
@Nullable
public static MapProps getCreateMap(World worldIn, List<ItemStack> inputs)
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/wumple/util/map/MapTranscription.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public static void transcribeMap(final ItemStack dest, final ItemStack src, fina
{
log("transcribeMap begin");

// TODO dest needs new unique map data
final MapData destMapData = XMapAPI.getInstance().getMapData(dest, worldIn);
final MapData srcMapData = XMapAPI.getInstance().getMapData(src, worldIn);

Expand Down Expand Up @@ -180,4 +181,25 @@ public static int doTranscribe(World worldIn, ItemStack targetStack, List<ItemSt

return count;
}

// transcribe src map onto a deep copy of dest's map and make that copy newDest's mapdata
public static void transcribeMapWithCopy(ItemStack newDest, ItemStack dest, ItemStack src, World worldIn)
{
MapUtil.forceDeepCloneMap(newDest, dest, worldIn);

// transcribe map data from src onto newDest
MapTranscription.transcribeMap(newDest, src, worldIn);
}

// copy dest to new ItemStack newDest, deep transcribe src onto newDest, and return newDest
public static ItemStack transcribeMapWithCopy(ItemStack dest, ItemStack src, World worldIn)
{
ItemStack newDest = dest.copy();

transcribeMapWithCopy(newDest, dest, src, worldIn);

return newDest;
}
}


29 changes: 29 additions & 0 deletions src/main/java/com/wumple/util/map/MapUtil.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.wumple.util.map;

import com.wumple.util.xmap.XMapAPI;

import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.world.World;
import net.minecraft.world.storage.MapData;

public class MapUtil
{
Expand All @@ -24,4 +28,29 @@ public static int extractMapScaleDirection(ItemStack stack)

return value;
}

/*
* Copy MapData from dest to a new copy on newDest
*/
public static void forceDeepCloneMap(ItemStack newDest, ItemStack dest, World worldIn)
{
// get original map data
MapData mapdata = XMapAPI.getInstance().getMapData(dest, worldIn);

// force new copy to start with a new MapData
int i = worldIn.getNextMapId();
newDest.getOrCreateTag().putInt("map", i);

// copy dest's original map data to newDest (new copy of original)
MapData mapdata1 = XMapAPI.getInstance().getMapData(newDest, worldIn); // will create new map data due to above map id change
mapdata1.copyFrom(mapdata);
mapdata1.scale = mapdata.scale;
mapdata1.trackingPosition = mapdata.trackingPosition;
mapdata1.unlimitedTracking = mapdata.unlimitedTracking;
mapdata1.dimension = mapdata.dimension;
mapdata1.locked = mapdata.locked;

mapdata1.markDirty();
}

}
19 changes: 19 additions & 0 deletions src/main/java/com/wumple/util/misc/ItemStackUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.wumple.util.misc;

import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;

public class ItemStackUtil
{
/*
* Copy the ItemStack but change its Item to srcItem
*/
public static ItemStack pseudoClone(ItemStack itemstack, Item srcItem)
{
// from ItemStack.copy
ItemStack itemstack2 = new ItemStack(srcItem);
itemstack2.setAnimationsToGo(itemstack.getAnimationsToGo());
if (itemstack.hasTag()) itemstack2.setTag(itemstack.getTag());
return itemstack2;
}
}
9 changes: 8 additions & 1 deletion src/main/java/com/wumple/util/xmap/IXFilledMapItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@ public interface IXFilledMapItem
{
boolean isAMapScaleValid(byte scale);

// create a new map ItemStack with fresh MapData
ItemStack setupANewMap(World worldIn, int worldX, int worldZ, byte scale, boolean trackingPosition,
boolean unlimitedTracking);


// create a new map ItemStack with fresh MapData
default ItemStack setupANewMap(World worldIn, int worldX, int worldZ, byte scale)
{ return setupANewMap(worldIn, worldX, worldZ, scale, true, false); }

String getID();

boolean fillMapData(World worldIn, Entity viewer, ItemStack itemstack);

ItemStack copyMapShallow(ItemStack stack);
ItemStack copyMapDeep(ItemStack stack, World worldIn);

// -------------------------------------------------------------------------------------
// MapData-using API's for backwards compatibility, etc - try not to use these
Expand All @@ -35,4 +40,6 @@ void updateMapDataArea(World worldIn, Entity viewer, MapData data, int startPixe

@Nullable
public MapData getMyMapData(ItemStack stack, World worldIn);
@Nullable
public MapData getMyData(ItemStack stack, World worldIn);
}
61 changes: 46 additions & 15 deletions src/main/java/com/wumple/util/xmap/IXMapAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.wumple.util.map.MapUtil;

import net.minecraft.item.FilledMapItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
Expand All @@ -10,37 +11,67 @@

public interface IXMapAPI
{
// ------------------------------------------------------------------------
// Type and checks

default Item getEmptyMapItem() { return Items.MAP; }
default Item getFilledMapItem() { return Items.FILLED_MAP; }

boolean isEmptyMap(ItemStack itemstack1);
boolean isFilledMap(ItemStack itemstack1);

boolean isExplorationMap(MapData mapData);
boolean isExplorationMap(ItemStack itemstack, World worldIn);

default byte getMaxScale() { return 4; }
default byte getDefaultScale() { return 0; }

default boolean isMapScaleValid(byte scale) { return (scale >= 0) && (scale <= getMaxScale()); }
boolean isMapScaleValid(ItemStack itemstack, byte scale);

// ------------------------------------------------------------------------
// Creation and copying

ItemStack setupNewMap(World worldIn, int worldX, int worldZ, byte scale, boolean trackingPosition, boolean unlimitedTracking);
default ItemStack setupNewMap(World worldIn, int worldX, int worldZ, byte scale)
{ return setupNewMap(worldIn, worldX, worldZ, scale, true, false); }
default ItemStack setupNewMap(World worldIn, int worldX, int worldZ)
{ return setupNewMap(worldIn, worldX, worldZ, getDefaultScale(), true, false); }

ItemStack copyMapShallow(ItemStack itemstack);
default ItemStack copyMapShallow(ItemStack stack, int count)
{ ItemStack s=copyMapShallow(stack); s.setCount(count); return s; }
ItemStack copyMapDeep(ItemStack stack, World worldIn);
default ItemStack copyMapDeep(ItemStack stack, World w, int count)
{ ItemStack s=copyMapDeep(stack, w); s.setCount(count); return s; }
ItemStack copyMapDeepLocked(ItemStack stack, World worldIn);

ItemStack copyMap(ItemStack itemstack, int i);
default ItemStack copyMap(ItemStack itemstack) { return copyMap(itemstack, 1); }
// ------------------------------------------------------------------------
// Scale

default byte getMaxScale() { return 4; }
default byte getDefaultScale() { return 0; }

boolean isExplorationMap(MapData mapData);
boolean isExplorationMap(ItemStack itemstack, World worldIn);
default boolean isMapScaleValid(byte scale) { return (scale >= 0) && (scale <= getMaxScale()); }
boolean isMapScaleValid(ItemStack itemstack, byte scale);

MapData getMapData(ItemStack itemstack, World worldIn);


MapData createMapData(String mapName);

default void mapScaleDirection(ItemStack stack, int direction) { MapUtil.mapScaleDirection(stack, direction); }
default void increaseMapScale(ItemStack stack) { mapScaleDirection(stack, 1); }
default void decreaseMapScale(ItemStack stack) { mapScaleDirection(stack, -1); }

// ------------------------------------------------------------------------
// MapData related

MapData getMapData(ItemStack itemstack, World worldIn);
MapData getMapDataIfExists(ItemStack itemstack, World worldIn);
MapData createMapData(String mapName);

/*
* Deep copy map data from itemstack to newstack
*/
default public void cloneMapData(ItemStack itemstack, ItemStack newstack, World worldIn)
{
MapData mapdata = getMapData(itemstack, worldIn);
MapData mapdata1 = FilledMapItem.getMapData(newstack, worldIn);
mapdata1.copyFrom(mapdata);
mapdata1.scale = mapdata.scale;
mapdata1.trackingPosition = mapdata.trackingPosition;
mapdata1.unlimitedTracking = mapdata.unlimitedTracking;
mapdata1.dimension = mapdata.dimension;
mapdata1.locked = mapdata.locked;
}
}
Loading

0 comments on commit f87477c

Please sign in to comment.