Advertisement
Ivorius

Psychedelicraft Intercomm

Mar 31st, 2014
517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.18 KB | None | 0 0
  1. package net.ivorius.psychedelicraft;
  2.  
  3. import cpw.mods.fml.common.FMLCommonHandler;
  4. import cpw.mods.fml.common.event.FMLInterModComms;
  5. import cpw.mods.fml.relauncher.Side;
  6. import net.ivorius.psychedelicraft.entities.DrugHelper;
  7. import net.ivorius.psychedelicraft.entities.DrugInfluence;
  8. import net.minecraft.client.Minecraft;
  9. import net.minecraft.entity.Entity;
  10. import net.minecraft.item.ItemStack;
  11. import net.minecraft.nbt.NBTTagCompound;
  12. import net.minecraftforge.common.DimensionManager;
  13.  
  14. /**
  15.  * Created by Lukas Tenbrink on 17.03.14.
  16.  * <p/>
  17.  * Receives an IMCMessage. Most messages use the keys 'worldID' for the dimension ID and 'entityID' for the entity ID, if drug values are involved.
  18.  * Also note that messages that expect a return value set the value as their appropriate tag, and otherwise send back the whole NBTTagCompound to the sender, with the key set as 'replyKey'.
  19.  * Messages are read at Client / Server tick END Forge event.
  20.  * If you need any more messages added, feel free to contact me about it.
  21.  */
  22. public class PsycheCommunicationHandler
  23. {
  24.     public void onIMCMessage(FMLInterModComms.IMCMessage message, boolean runtime)
  25.     {
  26.         try
  27.         {
  28.             if (isMessage("drugAddValue", message, NBTTagCompound.class))
  29.             {
  30.                 NBTTagCompound cmp = message.getNBTValue();
  31.                 getDrugHelper(cmp).addToDrug(cmp.getString("drugName"), cmp.getFloat("drugValue"));
  32.             }
  33.             else if (isMessage("drugAddInfluence", message, NBTTagCompound.class))
  34.             {
  35.                 NBTTagCompound cmp = message.getNBTValue();
  36.                 DrugInfluence influence = new DrugInfluence(cmp.getString("drugName"), cmp.getInteger("drugInfluenceDelay"), cmp.getDouble("drugInfluenceSpeed"), cmp.getDouble("drugInfluenceSpeedAdd"), cmp.getDouble("drugTotalEffect"));
  37.                 getDrugHelper(cmp).addToDrug(influence);
  38.             }
  39.             else if (isMessage("drugSetValue", message, NBTTagCompound.class))
  40.             {
  41.                 NBTTagCompound cmp = message.getNBTValue();
  42.                 getDrugHelper(cmp).setDrugValue(cmp.getString("drugName"), cmp.getFloat("drugValue"));
  43.             }
  44.             else if (isMessage("drugSetLocked", message, NBTTagCompound.class))
  45.             {
  46.                 NBTTagCompound cmp = message.getNBTValue();
  47.                 getDrugHelper(cmp).getDrug(cmp.getString("drugName")).setLocked(cmp.getBoolean("drugLocked"));
  48.             }
  49.             else if (isMessage("drugGetValue", message, NBTTagCompound.class))
  50.             {
  51.                 NBTTagCompound cmp = message.getNBTValue();
  52.                 NBTTagCompound response = (NBTTagCompound) cmp.copy();
  53.                 response.setFloat("drugValue", getDrugHelper(cmp).getDrugValue(cmp.getString("drugName")));
  54.                 sendReply(message, response);
  55.             }
  56.             else if (isMessage("drugIsLocked", message, NBTTagCompound.class))
  57.             {
  58.                 NBTTagCompound cmp = message.getNBTValue();
  59.                 NBTTagCompound response = (NBTTagCompound) cmp.copy();
  60.                 response.setBoolean("drugLocked", getDrugHelper(cmp).getDrug(cmp.getString("drugName")).isLocked());
  61.                 sendReply(message, response);
  62.             }
  63.         }
  64.         catch (Exception ex)
  65.         {
  66.             Psychedelicraft.logger.error("Message error! Exception on message with key '" + message.key + "' of type '" + message.getMessageType().getName() + "'");
  67.             ex.printStackTrace();
  68.         }
  69.     }
  70.  
  71.     private boolean isMessage(String key, FMLInterModComms.IMCMessage message, Class expectedType)
  72.     {
  73.         if (key.equals(message.key))
  74.         {
  75.             if (message.getMessageType().isAssignableFrom(expectedType))
  76.             {
  77.                 return true;
  78.             }
  79.  
  80.             faultyMessage(message, expectedType);
  81.         }
  82.  
  83.         return false;
  84.     }
  85.  
  86.     private Entity getEntity(NBTTagCompound compound)
  87.     {
  88.         return getEntity(compound, "worldID", "entityID");
  89.     }
  90.  
  91.     private Entity getEntity(NBTTagCompound compound, String worldKey, String entityKey)
  92.     {
  93.         if (FMLCommonHandler.instance().getEffectiveSide() == Side.CLIENT)
  94.         {
  95.             return Minecraft.getMinecraft().theWorld.getEntityByID(compound.getInteger(entityKey));
  96.         }
  97.         else
  98.         {
  99.             return DimensionManager.getWorld(compound.getInteger(worldKey)).getEntityByID(compound.getInteger(entityKey));
  100.         }
  101.     }
  102.  
  103.     private DrugHelper getDrugHelper(NBTTagCompound compound)
  104.     {
  105.         return DrugHelper.getDrugHelper(getEntity(compound));
  106.     }
  107.  
  108.     private boolean sendReply(FMLInterModComms.IMCMessage message, String value)
  109.     {
  110.         if (message.getSender() == null)
  111.         {
  112.             return false;
  113.         }
  114.  
  115.         NBTTagCompound cmp = message.getNBTValue();
  116.         FMLInterModComms.sendRuntimeMessage(Psychedelicraft.MODID, message.getSender(), cmp.getString("replyKey"), value);
  117.         return true;
  118.     }
  119.  
  120.     private boolean sendReply(FMLInterModComms.IMCMessage message, NBTTagCompound value)
  121.     {
  122.         if (message.getSender() == null)
  123.         {
  124.             return false;
  125.         }
  126.  
  127.         NBTTagCompound cmp = message.getNBTValue();
  128.         FMLInterModComms.sendRuntimeMessage(Psychedelicraft.MODID, message.getSender(), cmp.getString("replyKey"), value);
  129.         return true;
  130.     }
  131.  
  132.     private boolean sendReply(FMLInterModComms.IMCMessage message, ItemStack value)
  133.     {
  134.         if (message.getSender() == null)
  135.         {
  136.             Psychedelicraft.logger.error("Message error! Could not reply to message with key '" + message.key + "' - No sender found");
  137.             return false;
  138.         }
  139.  
  140.         NBTTagCompound cmp = message.getNBTValue();
  141.         FMLInterModComms.sendRuntimeMessage(Psychedelicraft.MODID, message.getSender(), cmp.getString("replyKey"), value);
  142.         return true;
  143.     }
  144.  
  145.     private void faultyMessage(FMLInterModComms.IMCMessage message, Class expectedType)
  146.     {
  147.         Psychedelicraft.logger.error("Message error! Got message with key '" + message.key + "' of type '" + message.getMessageType().getName() + "'; Expected type: '" + expectedType.getName() + "'");
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement