Repository: mekanism/mekanism
Issue: [1.11.2] Server Crash

#### Issue description:
 Hello! A few days ago, our official pack server started crashing whenever someone is near a Mekanism machine (tested with a Metallurgic Infuser and Enrichment Chamber). At first we thought that it was all due to Mekanism, but today we figured out that it doesn't happen when the crafting recipes in JEI bug out, as it sometimes happens with the 1.11.2 version. Then we tried changing the JEI version on the client back to the version where all was fine, but it didn't help. Either way, by the crash log we can tell that it has something to do with Mekanism.

#### Version (make sure you are on the latest version before reporting):
**Forge:** 13.20.0.2315
**Mekanism:** 9.3.2.307
**JEI (if it actually has to do with it)** 4.5.0.289

#### If a (crash)log is relevant for this issue, link it here: (_It's almost always relevant_)

https://paste.ee/p/a1wzk

Changed files:
src/main/java/mekanism/common/frequency/Frequency.java, src/main/java/mekanism/common/entity/EntityRobit.java, src/main/java/mekanism/common/tile/TileEntitySecurityDesk.java, src/main/java/mekanism/common/content/transporter/TransporterManager.java, src/main/java/mekanism/common/tile/component/TileComponentSecurity.java, src/main/java/mekanism/common/network/PacketSecurityUpdate.java, src/main/java/mekanism/common/util/MekanismUtils.java

diff --git a/src/main/java/mekanism/common/content/transporter/TransporterManager.java b/src/main/java/mekanism/common/content/transporter/TransporterManager.java
index ca9cd31ed6..9609b648b4 100644
--- a/src/main/java/mekanism/common/content/transporter/TransporterManager.java
+++ b/src/main/java/mekanism/common/content/transporter/TransporterManager.java
@@ -9,6 +9,7 @@ import java.util.Set;
 
 import mekanism.api.Coord4D;
 import mekanism.api.EnumColor;
+import mekanism.common.Mekanism;
 import mekanism.common.base.ISideConfiguration;
 import mekanism.common.content.transporter.TransitRequest.TransitResponse;
 import mekanism.common.content.transporter.TransporterStack.Path;
@@ -113,6 +114,11 @@ public class TransporterManager
 			{
 				int slotID = slots[get];
 
+				if (slotID >= ret.size()){
+					Mekanism.logger.error("Inventory {} gave slot number >= the number of slots it reported! {} >= {} ", inv.getClass().getName(), slotID, ret.size());
+					continue;
+				}
+
 				ret.set(slotID, !sidedInventory.getStackInSlot(slotID).isEmpty() ? sidedInventory.getStackInSlot(slotID).copy() : ItemStack.EMPTY);
 			}
 			
diff --git a/src/main/java/mekanism/common/entity/EntityRobit.java b/src/main/java/mekanism/common/entity/EntityRobit.java
index 97cd975679..110c845a9e 100644
--- a/src/main/java/mekanism/common/entity/EntityRobit.java
+++ b/src/main/java/mekanism/common/entity/EntityRobit.java
@@ -578,7 +578,7 @@ public class EntityRobit extends EntityCreature implements IInventory, ISustaine
 	public void setOwnerUUID(UUID uuid)
 	{
 		dataManager.set(OWNER_UUID, uuid.toString());
-		dataManager.set(OWNER_NAME, UsernameCache.getLastKnownUsername(uuid));
+		dataManager.set(OWNER_NAME, MekanismUtils.getLastKnownUsername(uuid));
 	}
 
 	public boolean getFollowing()
diff --git a/src/main/java/mekanism/common/frequency/Frequency.java b/src/main/java/mekanism/common/frequency/Frequency.java
index b4d76e39e2..059b80db7b 100644
--- a/src/main/java/mekanism/common/frequency/Frequency.java
+++ b/src/main/java/mekanism/common/frequency/Frequency.java
@@ -9,6 +9,7 @@ import java.util.UUID;
 
 import mekanism.api.Coord4D;
 import mekanism.common.PacketHandler;
+import mekanism.common.util.MekanismUtils;
 import net.minecraft.nbt.NBTTagCompound;
 import net.minecraftforge.common.UsernameCache;
 
@@ -118,7 +119,7 @@ public class Frequency
 	{
 		data.add(name);
 		data.add(ownerUUID.toString());
-		data.add(UsernameCache.getLastKnownUsername(ownerUUID));
+		data.add(MekanismUtils.getLastKnownUsername(ownerUUID));
 		data.add(publicFreq);
 	}
 
diff --git a/src/main/java/mekanism/common/network/PacketSecurityUpdate.java b/src/main/java/mekanism/common/network/PacketSecurityUpdate.java
index 4146b91b47..4c4c26e217 100644
--- a/src/main/java/mekanism/common/network/PacketSecurityUpdate.java
+++ b/src/main/java/mekanism/common/network/PacketSecurityUpdate.java
@@ -13,6 +13,7 @@ import mekanism.common.frequency.Frequency;
 import mekanism.common.network.PacketSecurityUpdate.SecurityUpdateMessage;
 import mekanism.common.security.SecurityData;
 import mekanism.common.security.SecurityFrequency;
+import mekanism.common.util.MekanismUtils;
 import net.minecraftforge.common.UsernameCache;
 import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
 import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
@@ -51,7 +52,7 @@ public class PacketSecurityUpdate implements IMessageHandler<SecurityUpdateMessa
 			if(packetType == SecurityPacket.UPDATE)
 			{
 				playerUUID = uuid;
-				playerUsername = UsernameCache.getLastKnownUsername(uuid);
+				playerUsername = MekanismUtils.getLastKnownUsername(uuid);
 				securityData = data;
 			}
 		}
@@ -92,7 +93,7 @@ public class PacketSecurityUpdate implements IMessageHandler<SecurityUpdateMessa
 				for(SecurityFrequency frequency : frequencies)
 				{
 					PacketHandler.writeString(dataStream, frequency.ownerUUID.toString());
-					PacketHandler.writeString(dataStream, UsernameCache.getLastKnownUsername(frequency.ownerUUID));
+					PacketHandler.writeString(dataStream, MekanismUtils.getLastKnownUsername(frequency.ownerUUID));
 					new SecurityData(frequency).write(dataStream);
 				}
 			}
diff --git a/src/main/java/mekanism/common/tile/TileEntitySecurityDesk.java b/src/main/java/mekanism/common/tile/TileEntitySecurityDesk.java
index 1a2c383d11..c4274f92c6 100644
--- a/src/main/java/mekanism/common/tile/TileEntitySecurityDesk.java
+++ b/src/main/java/mekanism/common/tile/TileEntitySecurityDesk.java
@@ -262,7 +262,7 @@ public class TileEntitySecurityDesk extends TileEntityContainerBlock implements
 		if(ownerUUID != null)
 		{
 			data.add(true);
-			data.add(UsernameCache.getLastKnownUsername(ownerUUID));
+			data.add(MekanismUtils.getLastKnownUsername(ownerUUID));
 		}
 		else {
 			data.add(false);
diff --git a/src/main/java/mekanism/common/tile/component/TileComponentSecurity.java b/src/main/java/mekanism/common/tile/component/TileComponentSecurity.java
index a2af50f493..3bf566b347 100644
--- a/src/main/java/mekanism/common/tile/component/TileComponentSecurity.java
+++ b/src/main/java/mekanism/common/tile/component/TileComponentSecurity.java
@@ -214,7 +214,7 @@ public class TileComponentSecurity implements ITileComponent
 		{
 			data.add(true);
 			data.add(ownerUUID.toString());
-			data.add(UsernameCache.getLastKnownUsername(ownerUUID));
+			data.add(MekanismUtils.getLastKnownUsername(ownerUUID));
 		}
 		else {
 			data.add(false);
diff --git a/src/main/java/mekanism/common/util/MekanismUtils.java b/src/main/java/mekanism/common/util/MekanismUtils.java
index 15638a322c..ce2b9d4b8b 100644
--- a/src/main/java/mekanism/common/util/MekanismUtils.java
+++ b/src/main/java/mekanism/common/util/MekanismUtils.java
@@ -1,5 +1,6 @@
 package mekanism.common.util;
 
+import com.mojang.authlib.GameProfile;
 import ic2.api.energy.EnergyNet;
 
 import java.io.BufferedReader;
@@ -12,6 +13,7 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.UUID;
 
 import mekanism.api.Chunk3D;
 import mekanism.api.Coord4D;
@@ -86,18 +88,22 @@ import net.minecraft.world.ChunkCache;
 import net.minecraft.world.EnumSkyBlock;
 import net.minecraft.world.IBlockAccess;
 import net.minecraft.world.World;
+import net.minecraftforge.common.UsernameCache;
 import net.minecraft.world.chunk.Chunk;
 import net.minecraftforge.fluids.BlockFluidBase;
 import net.minecraftforge.fluids.Fluid;
 import net.minecraftforge.fluids.FluidRegistry;
 import net.minecraftforge.fluids.FluidStack;
 import net.minecraftforge.fluids.IFluidBlock;
+import net.minecraftforge.fml.common.FMLCommonHandler;
 import net.minecraftforge.fml.relauncher.Side;
 import net.minecraftforge.fml.relauncher.SideOnly;
 import net.minecraftforge.oredict.OreDictionary;
 import net.minecraftforge.oredict.ShapedOreRecipe;
 import cofh.api.item.IToolHammer;
 
+import javax.annotation.Nonnull;
+
 /**
  * Utilities used by Mekanism. All miscellaneous methods are located here.
  * @author AidanBrady
@@ -109,6 +115,8 @@ public final class MekanismUtils
 
 	public static final Map<String, Class<?>> classesFound = new HashMap<String, Class<?>>();
 
+	private static final List<UUID> warnedFails = new ArrayList<>();
+
 	/**
 	 * Checks for a new version of Mekanism.
 	 */
@@ -1518,6 +1526,23 @@ public final class MekanismUtils
 		return false;
 	}
 
+	@Nonnull
+	public static String getLastKnownUsername(UUID uuid)
+	{
+		String ret = UsernameCache.getLastKnownUsername(uuid);
+		if (ret == null && !warnedFails.contains(uuid) && FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER){ // see if MC/Yggdrasil knows about it?!
+			GameProfile gp = FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerProfileCache().getProfileByUUID(uuid);
+			if (gp != null){
+				ret = gp.getName();
+			}
+		}
+		if (ret == null && !warnedFails.contains(uuid)){
+			Mekanism.logger.warn("Failed to retrieve username for UUID {}, you might want to add it to the JSON cache", uuid);
+			warnedFails.add(uuid);
+		}
+		return ret != null ? ret : "<???>";
+	}
+
 	public static enum ResourceType
 	{
 		GUI("gui"),