Skip to content

Commit

Permalink
Switch the compass to storing dimension IDs instead of types
Browse files Browse the repository at this point in the history
Now supports checking worlds besides the current to update the compass target, and supports the chance that multiple dimensions are registered with the same type
  • Loading branch information
KnightMiner committed Jul 7, 2019
1 parent 125b329 commit b09d059
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 54 deletions.
Expand Up @@ -9,7 +9,6 @@
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.DimensionType;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
Expand Down Expand Up @@ -49,9 +48,9 @@ public float apply(ItemStack stack, @Nullable World world, @Nullable EntityLivin

@SideOnly(Side.CLIENT)
private double getAngle(ItemStack stack, @Nullable World world, @Nullable Entity entity, boolean isHeld) {
DimensionType dimension = ItemWaypointCompass.getDimension(stack);
Integer dimension = ItemWaypointCompass.getDimension(stack);
if (dimension != null) {
BlockPos pos = ItemWaypointCompass.getPos(stack, dimension, world.provider.getDimensionType());
BlockPos pos = ItemWaypointCompass.getPos(stack, dimension, world.provider.getDimension());
if (pos != null) {
double entityAngle = isHeld ? (double)entity.rotationYaw : this.getFrameRotation((EntityItemFrame)entity);
entityAngle = MathHelper.positiveModulo(entityAngle / 360.0D, 1.0D);
Expand Down
Expand Up @@ -3,22 +3,24 @@
import knightminer.inspirations.common.Config;
import knightminer.inspirations.library.Util;
import knightminer.inspirations.library.util.TagUtil;
import knightminer.inspirations.tools.InspirationsTools;
import knightminer.inspirations.tools.client.WaypointCompassGetter;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.init.Items;
import net.minecraft.item.EnumDyeColor;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityBeacon;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.StringUtils;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.DimensionType;
import net.minecraft.world.World;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

Expand All @@ -44,34 +46,46 @@ public void onUpdate(ItemStack stack, World world, Entity entityIn, int itemSlot
return;
}

// TODO: handle other dimensions
DimensionType type = getDimension(stack);
if (type == world.provider.getDimensionType()) {
BlockPos pos = getPos(stack, null, null);
if (pos != null && world.isBlockLoaded(pos, false)) {
TileEntity te = world.getTileEntity(pos);
if (te instanceof TileEntityBeacon) {
// if the beacon beam is just missing, wait another cycle before clearing, this sometimes happens on world load
NBTTagCompound tags = stack.getTagCompound();
if (((TileEntityBeacon)te).isComplete) {
tags.removeTag(TAG_CHECK_BEACON);
} else if (tags.getBoolean(TAG_CHECK_BEACON)) {
stack.setTagCompound(null);
} else {
stack.getTagCompound().setBoolean(TAG_CHECK_BEACON, true);
}
Integer dimension = getDimension(stack);
if (dimension != null) {
if (dimension == world.provider.getDimension()) {
checkPos(stack, world, getPos(stack));
// only update other dimensions half as often
} else if (Config.waypointCompassCrossDimension && world.getTotalWorldTime() % 320 == 20) {
World other = DimensionManager.getWorld(dimension);
// TODO: clear NBT if null?
if (other != null) {
checkPos(stack, other, getPos(stack, dimension, other.provider.getDimension()));
}
}
}
}

/** Checks a position in the world to see if the compass is valid */
private void checkPos(ItemStack stack, World world, BlockPos pos) {
if (pos != null && world.isBlockLoaded(pos, false)) {
TileEntity te = world.getTileEntity(pos);
if (te instanceof TileEntityBeacon) {
// if the beacon beam is just missing, wait another cycle before clearing, this sometimes happens on world load
NBTTagCompound tags = stack.getTagCompound();
if (((TileEntityBeacon)te).isComplete) {
tags.removeTag(TAG_CHECK_BEACON);
} else if (tags.getBoolean(TAG_CHECK_BEACON)) {
clearNBT(stack);
} else {
// if the beacon is missing, clear immediately
stack.setTagCompound(null);
stack.getTagCompound().setBoolean(TAG_CHECK_BEACON, true);
}
} else {
// if the beacon is missing, clear immediately
clearNBT(stack);
}
}
}

@Override
@SideOnly(Side.CLIENT)
public void addInformation(ItemStack stack, @Nullable World worldIn, List<String> tooltip, ITooltipFlag flag) {
DimensionType type = getDimension(stack);
DimensionType type = getDimensionType(stack);
if (type != null) {
String dimension = type.getName();
String key = "dimension." + dimension + ".name";
Expand All @@ -80,7 +94,7 @@ public void addInformation(ItemStack stack, @Nullable World worldIn, List<String
}
String dimensionTooltip = dimension;
if (Config.waypointCompassAdvTooltip && flag == ITooltipFlag.TooltipFlags.ADVANCED) {
BlockPos pos = getPos(stack, null, null);
BlockPos pos = getPos(stack);
if (pos != null) {
dimensionTooltip = Util.translateFormatted(getUnlocalizedName() + ".pos.tooltip", dimension, pos.getX(), pos.getZ());
}
Expand All @@ -95,53 +109,74 @@ public void addInformation(ItemStack stack, @Nullable World worldIn, List<String
/* Utilities */

/**
* Gets the dimension from a given waypoint compass
* @param stack Compass stack
* @return Dimension type compass points to
* Gets the dimension ID from a stack, or null if it has none
* @param stack Stack containing dimension
* @return Dimension ID, or null if not present+
*/
public static DimensionType getDimension(ItemStack stack) {
@Nullable
public static Integer getDimension(ItemStack stack) {
if (!stack.hasTagCompound()) {
return null;
}
String dimension = stack.getTagCompound().getString(ItemWaypointCompass.TAG_DIMENSION);
if (StringUtils.isNullOrEmpty(dimension)) {
return null;
}
try {
return DimensionType.byName(dimension);
} catch (IllegalArgumentException e) {
return null;
NBTTagCompound tags = stack.getTagCompound();
return tags.hasKey(TAG_DIMENSION, 99) ? tags.getInteger(TAG_DIMENSION) : null;
}

/**
* Gets the dimension from a given waypoint compass
* @param stack Compass stack
* @return Dimension type compass points to
*/
@Nullable
public static DimensionType getDimensionType(ItemStack stack) {
Integer dimension = getDimension(stack);
if (dimension != null) {
try {
return DimensionManager.getProviderType(dimension);
} catch (IllegalArgumentException e) {}
}
return null;
}

/**
* Gets the position from the given compass
* @param stack Compass stack
* @param compassType Dimension type on the compass
* @param worldType Dimension type in the player's world
* @return Position for the compass, adjusted for the overworld/nether difference
* Gets the position from the given compass, ignoring dimension differences
* @param stack Compass stack
* @return Position from the compass
*/
public static BlockPos getPos(ItemStack stack, @Nullable DimensionType compassType, @Nullable DimensionType worldType) {
@Nullable
public static BlockPos getPos(ItemStack stack) {
if (!stack.hasTagCompound()) {
return null;
}
BlockPos pos = TagUtil.readPos(stack.getTagCompound().getCompoundTag(ItemWaypointCompass.TAG_POS));
return TagUtil.readPos(stack.getTagCompound().getCompoundTag(ItemWaypointCompass.TAG_POS));
}

/**
* Gets the position from the given compass, taking dimension differences into account
* @param stack Compass stack
* @param compassDimension Dimension on the compass
* @param worldDimension Dimension in the player's world
* @return Position for the compass, adjusted for the overworld/nether difference
*/
@Nullable
public static BlockPos getPos(ItemStack stack, int compassDimension, int worldDimension) {
BlockPos pos = getPos(stack);
if (pos == null) {
return null;
}

// if the types differ, we may have special logic
if (compassType != worldType) {
if (compassDimension != worldDimension) {
if (!Config.waypointCompassCrossDimension) {
return null;
}

// from nether coords
if (compassType == DimensionType.NETHER) {
if (compassDimension == DimensionType.NETHER.getId()) {
return new BlockPos(pos.getX() * 8, pos.getY(), pos.getZ() * 8);
}
// to nether coords
if (worldType == DimensionType.NETHER) {
if (worldDimension == DimensionType.NETHER.getId()) {
return new BlockPos(Math.round(pos.getX() / 8f), pos.getY(), Math.round(pos.getZ() / 8f));
}
}
Expand All @@ -156,10 +191,21 @@ public static BlockPos getPos(ItemStack stack, @Nullable DimensionType compassTy
*/
public static void setNBT(@Nonnull ItemStack stack, @Nullable World world, @Nullable BlockPos pos) {
if (world == null || pos == null) {
stack.setTagCompound(null);
clearNBT(stack);
return;
}
setNBT(stack, world.provider.getDimensionType(), pos);
setNBT(stack, world.provider.getDimension(), pos);
}

/** Removes compass related NBT, but keeps the display name */
private static void clearNBT(ItemStack stack) {
if (stack.hasDisplayName()) {
String name = stack.getDisplayName();
stack.setTagCompound(null);
stack.setStackDisplayName(name);
} else {
stack.setTagCompound(null);
}
}

/**
Expand All @@ -168,19 +214,19 @@ public static void setNBT(@Nonnull ItemStack stack, @Nullable World world, @Null
* @param waypoint Stack to copy from
*/
public static void copyNBT(@Nonnull ItemStack stack, @Nonnull ItemStack waypoint) {
setNBT(stack, getDimension(waypoint), getPos(waypoint, null, null));
if(waypoint.hasDisplayName()) {
stack.setStackDisplayName(waypoint.getDisplayName());
if (!waypoint.hasTagCompound()) {
return;
}
setNBT(stack, getDimension(waypoint), getPos(waypoint));
}

private static void setNBT(@Nonnull ItemStack stack, DimensionType type, BlockPos pos) {
if (type == null || pos == null) {
private static void setNBT(@Nonnull ItemStack stack, int dimension, BlockPos pos) {
if (pos == null) {
stack.setTagCompound(null);
return;
}
NBTTagCompound tags = TagUtil.getTagSafe(stack);
tags.setString(ItemWaypointCompass.TAG_DIMENSION, type.getName());;
tags.setInteger(ItemWaypointCompass.TAG_DIMENSION, dimension);
tags.setTag(ItemWaypointCompass.TAG_POS, TagUtil.writePos(pos));
stack.setTagCompound(tags);
}
Expand Down

0 comments on commit b09d059

Please sign in to comment.