2014-06-22 16:50:31 +02:00
|
|
|
package com.pahimar.ee3.handler;
|
|
|
|
|
2014-09-04 17:30:22 +02:00
|
|
|
import com.pahimar.ee3.exchange.EnergyValueRegistry;
|
|
|
|
import com.pahimar.ee3.network.PacketHandler;
|
|
|
|
import com.pahimar.ee3.network.message.MessageEnergyValueSync;
|
2014-07-20 00:15:40 +02:00
|
|
|
import com.pahimar.ee3.reference.Reference;
|
|
|
|
import com.pahimar.ee3.util.LogHelper;
|
2014-06-22 16:50:31 +02:00
|
|
|
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
2014-09-04 17:30:22 +02:00
|
|
|
import net.minecraft.entity.player.EntityPlayerMP;
|
2014-06-22 16:50:31 +02:00
|
|
|
import net.minecraftforge.event.entity.player.PlayerEvent;
|
|
|
|
|
2014-07-20 00:15:40 +02:00
|
|
|
import java.io.*;
|
2014-06-22 20:03:30 +02:00
|
|
|
|
2014-06-22 16:50:31 +02:00
|
|
|
public class PlayerEventHandler
|
|
|
|
{
|
|
|
|
@SubscribeEvent
|
2014-06-26 21:19:59 +02:00
|
|
|
public void onPlayerLoadFromFileEvent(PlayerEvent.LoadFromFile event)
|
2014-06-22 16:50:31 +02:00
|
|
|
{
|
2014-06-26 21:19:59 +02:00
|
|
|
if (!event.entityPlayer.worldObj.isRemote)
|
2014-06-22 20:03:30 +02:00
|
|
|
{
|
2014-07-18 18:58:35 +02:00
|
|
|
// Grab the correct directory to be reading/writing player knowledge data to
|
|
|
|
if (PlayerKnowledgeHandler.playerDataDirectory == null || !PlayerKnowledgeHandler.playerDataDirectory.getAbsolutePath().equalsIgnoreCase(event.playerDirectory.getAbsolutePath()))
|
2014-06-22 20:03:30 +02:00
|
|
|
{
|
2014-07-20 00:15:40 +02:00
|
|
|
PlayerKnowledgeHandler.playerDataDirectory = new File(event.playerDirectory, Reference.MOD_ID.toLowerCase());
|
|
|
|
|
|
|
|
if (!PlayerKnowledgeHandler.playerDataDirectory.exists())
|
|
|
|
{
|
|
|
|
PlayerKnowledgeHandler.playerDataDirectory.mkdir();
|
|
|
|
}
|
2014-06-22 20:03:30 +02:00
|
|
|
}
|
2014-06-26 21:19:59 +02:00
|
|
|
|
2014-07-18 18:58:35 +02:00
|
|
|
// If player knowledge data doesn't exist, initialize a file for the player
|
2014-07-20 00:15:40 +02:00
|
|
|
File playerDataFile = new File(PlayerKnowledgeHandler.playerDataDirectory, event.entityPlayer.getUniqueID() + PlayerKnowledgeHandler.KNOWLEDGE_FILE_EXTENSION);
|
2014-07-18 18:58:35 +02:00
|
|
|
if (!playerDataFile.exists())
|
2014-06-22 20:03:30 +02:00
|
|
|
{
|
2014-07-20 00:15:40 +02:00
|
|
|
// Add the player name and the knowledge file to the legend file (makes for easier referencing of files to players)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(new File(PlayerKnowledgeHandler.playerDataDirectory, "legend.txt"), true)));
|
|
|
|
out.println(String.format("Player Name = %s", event.entityPlayer.getCommandSenderName()));
|
|
|
|
out.println(String.format("Knowledge File = %s", event.entityPlayer.getUniqueID() + PlayerKnowledgeHandler.KNOWLEDGE_FILE_EXTENSION));
|
|
|
|
out.println();
|
|
|
|
out.close();
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
LogHelper.warn("Could not add player ");
|
|
|
|
}
|
|
|
|
|
2014-07-18 18:58:35 +02:00
|
|
|
PlayerKnowledgeHandler.writeKnowledgeData(event.entityPlayer);
|
2014-06-22 20:03:30 +02:00
|
|
|
}
|
|
|
|
}
|
2014-06-22 16:50:31 +02:00
|
|
|
}
|
2014-09-04 17:30:22 +02:00
|
|
|
|
|
|
|
@SubscribeEvent
|
|
|
|
public void onPlayerLoggedInEvent(cpw.mods.fml.common.gameevent.PlayerEvent.PlayerLoggedInEvent event)
|
|
|
|
{
|
|
|
|
PacketHandler.INSTANCE.sendTo(new MessageEnergyValueSync(EnergyValueRegistry.getInstance()), (EntityPlayerMP) event.player);
|
|
|
|
}
|
2014-06-22 16:50:31 +02:00
|
|
|
}
|