/ClueManager.java Secret
Created
July 23, 2015 03:19
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.princecat.treasuretrails.common.clue; | |
import com.google.gson.*; | |
import com.google.gson.stream.JsonReader; | |
import com.princecat.treasuretrails.common.lib.Reference; | |
import com.sun.media.jfxmedia.logging.Logger; | |
import cpw.mods.fml.common.FMLLog; | |
import net.minecraft.block.Block; | |
import net.minecraft.entity.player.EntityPlayer; | |
import net.minecraft.entity.player.EntityPlayerMP; | |
import net.minecraft.item.ItemStack; | |
import net.minecraft.nbt.NBTTagCompound; | |
import net.minecraft.server.MinecraftServer; | |
import net.minecraft.util.ChatComponentText; | |
import net.minecraft.world.World; | |
import org.apache.logging.log4j.Level; | |
import java.io.*; | |
import java.lang.reflect.Type; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Random; | |
import java.util.UUID; | |
/** | |
* Created by Liam on 26/06/15. | |
*/ | |
public class ClueManager implements JsonSerializer<ClueManager>, JsonDeserializer<ClueManager> { | |
public static final Gson jsonSerializer = new GsonBuilder().setPrettyPrinting().registerTypeAdapter(ClueManager.class, new ClueManager()).create(); | |
public static final String OWNER_UUID = "playerUUID"; | |
public static final String OWNER_NAME = "playerName"; | |
public static final String CLUE_STAGE = "clueStage"; | |
public static final String CLUE_MAX_STAGE = "maxClueStage"; | |
public static final String CLUE_TYPE = "clueType"; | |
public static final String CLUE_TIER = "clueTier"; | |
public static final String CLUE_X = "clueX"; | |
public static final String CLUE_Y = "clueY"; | |
public static final String CLUE_Z = "clueZ"; | |
public static final String CLUE_BIOME_ID = "clueBiomeID"; | |
private UUID playerUUID; | |
private String playerName; | |
private Random rand; | |
private int clueStage = -1; | |
private int maxStages = -1; | |
private EnumClueType clueType = null; | |
private EnumClueTier clueTier = null; | |
private int clueX = -1; | |
private int clueY = -1; | |
private int clueZ = -1; | |
private int clueBiomeID = -1; | |
private boolean hasChanged; | |
public ClueManager() {} | |
public ClueManager(EntityPlayer player, ItemStack stack) { | |
this.rand = new Random(); | |
this.playerUUID = player.getPersistentID(); | |
this.playerName = player.getCommandSenderName(); | |
this.clueTier = EnumClueTier.values()[Math.min(stack.getItemDamage(), EnumClueTier.values().length)]; | |
writeToFile(); | |
} | |
/** | |
* Should be called when the clue is right clicked by the player. | |
*/ | |
public void initializeClue() { | |
if (this.clueType == null) { | |
this.clueType = this.clueTier.getClueTypes()[rand.nextInt(this.clueTier.getClueTypes().length)]; | |
} | |
if (this.maxStages == -1) { | |
this.maxStages = this.rand.nextInt((this.clueTier.getMaxStages() - 1)) + 1; | |
} | |
advanceStage(); | |
} | |
public boolean checkCoordinates(int x, int y, int z) { | |
switch(this.clueType) { | |
case COORDINATE: | |
case SCAN: | |
System.out.println(String.format("%d:%d:%d / %d:%d:%d", x, y, z, this.clueX, this.clueY, this.clueZ)); | |
if (x == this.clueX && y == this.clueY && z == this.clueZ) { | |
return true; | |
} | |
break; | |
} | |
return false; | |
} | |
public void advanceStage() { | |
this.clueStage++; | |
if (this.clueStage > this.maxStages) { | |
completeClue(); | |
ClueRegistry.unregister(getEntityPlayer()); | |
} | |
this.clueType = EnumClueType.SCAN; | |
switch(this.clueType) { | |
case COORDINATE: | |
generateCoordinate(); | |
break; | |
case SCAN: | |
generateScanBiome(); | |
} | |
writeToFile(); | |
} | |
public void generateScanBiome() { | |
EntityPlayerMP player = getEntityPlayer(); | |
if (player == null) | |
return; | |
int range = this.clueTier.getRange(); | |
int x = (int) Math.floor(player.posX + (rand.nextInt(range * 2) - range)); | |
int z = (int) Math.floor(player.posZ + (rand.nextInt(range * 2) - range)); | |
this.clueBiomeID = player.worldObj.getBiomeGenForCoords(x, z).biomeID; | |
// this.clueBiomeID = BiomeGenBase.getBiomeGenArray()[this.rand.nextInt(BiomeGenBase.getBiomeGenArray().length)].biomeID; | |
} | |
public void generateCoordinate() { | |
EntityPlayerMP player = getEntityPlayer(); | |
if (player == null) | |
return; | |
World world = player.worldObj; | |
int range = this.clueTier.getRange(); | |
List<Integer> yPositions = new ArrayList<>(); | |
int x = (int) Math.floor(player.posX + (rand.nextInt(range * 2) - range)); | |
int z = (int) Math.floor(player.posZ + (rand.nextInt(range * 2) - range)); | |
int topY = world.getTopSolidOrLiquidBlock(x, z); | |
for (int y = 0; y < topY; y++) { | |
Block block = world.getBlock(x, y, z); | |
if (!world.isAirBlock(x, y, z) && block.isBlockSolid(world, x, y, z, 1)) | |
yPositions.add(y); | |
} | |
int validY = (int) yPositions.toArray()[rand.nextInt(yPositions.size())]; | |
this.clueX = x; | |
this.clueY = validY; | |
this.clueZ = z; | |
} | |
public void completeClue() { | |
EntityPlayerMP player = getEntityPlayer(); | |
player.addChatMessage(new ChatComponentText("Completed the clue!")); | |
ClueRegistry.unregister(player); | |
} | |
public EntityPlayerMP getEntityPlayer() { | |
if (this.playerUUID != null) { | |
for (EntityPlayerMP player : ((List<EntityPlayerMP>) MinecraftServer.getServer().getConfigurationManager().playerEntityList)) { | |
if (player.getPersistentID().equals(this.playerUUID)); | |
return player; | |
} | |
} | |
return null; | |
} | |
public UUID getPlayerUUID() { | |
return playerUUID; | |
} | |
public void setPlayerUUID(UUID playerUUID) { | |
this.playerUUID = playerUUID; | |
} | |
public String getPlayerName() { | |
return playerName; | |
} | |
public void setPlayerName(String playerName) { | |
this.playerName = playerName; | |
} | |
public int getClueStage() { | |
return clueStage; | |
} | |
public void setClueStage(int clueStage) { | |
this.clueStage = clueStage; | |
} | |
public EnumClueType getClueType() { | |
return clueType; | |
} | |
public void setClueType(EnumClueType clueType) { | |
this.clueType = clueType; | |
} | |
public EnumClueTier getClueTier() { | |
return clueTier; | |
} | |
public void setClueTier(EnumClueTier clueTier) { | |
this.clueTier = clueTier; | |
} | |
public int getClueX() { | |
return clueX; | |
} | |
public void setClueX(int clueX) { | |
this.clueX = clueX; | |
} | |
public int getClueY() { | |
return clueY; | |
} | |
public void setClueY(int clueY) { | |
this.clueY = clueY; | |
} | |
public int getClueZ() { | |
return clueZ; | |
} | |
public void setClueZ(int clueZ) { | |
this.clueZ = clueZ; | |
} | |
public int getMaxStages() { | |
return maxStages; | |
} | |
public void setMaxStages(int maxStages) { | |
this.maxStages = maxStages; | |
} | |
public int getClueBiomeID() { | |
return clueBiomeID; | |
} | |
public void setClueBiomeID(int clueBiomeID) { | |
this.clueBiomeID = clueBiomeID; | |
} | |
@Override | |
public ClueManager deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { | |
if (json.isJsonObject()) { | |
JsonObject jsonObject = (JsonObject) json; | |
ClueManager manager = new ClueManager(); | |
if (jsonObject.has(OWNER_UUID)) manager.playerUUID = UUID.fromString(jsonObject.get(OWNER_UUID).getAsString()); | |
if (jsonObject.has(OWNER_NAME)) manager.playerName = jsonObject.get(OWNER_NAME).getAsString(); | |
if (jsonObject.has(CLUE_STAGE)) manager.clueStage = jsonObject.get(CLUE_STAGE).getAsInt(); | |
if (jsonObject.has(CLUE_MAX_STAGE)) manager.maxStages = jsonObject.get(CLUE_MAX_STAGE).getAsInt(); | |
if (jsonObject.has(CLUE_TYPE)) manager.clueType = EnumClueType.values()[jsonObject.get(CLUE_TYPE).getAsInt()]; | |
if (jsonObject.has(CLUE_TIER)) manager.clueTier = EnumClueTier.values()[jsonObject.get(CLUE_TIER).getAsInt()]; | |
if (jsonObject.has(CLUE_X)) manager.clueX = jsonObject.get(CLUE_X).getAsInt(); | |
if (jsonObject.has(CLUE_Y)) manager.clueY = jsonObject.get(CLUE_Y).getAsInt(); | |
if (jsonObject.has(CLUE_Z)) manager.clueZ = jsonObject.get(CLUE_Z).getAsInt(); | |
if (jsonObject.has(CLUE_BIOME_ID)) manager.clueBiomeID = jsonObject.get(CLUE_BIOME_ID).getAsInt(); | |
return manager; | |
} | |
return null; | |
} | |
@Override | |
public JsonElement serialize(ClueManager src, Type typeOfSrc, JsonSerializationContext context) { | |
JsonObject json = new JsonObject(); | |
json.addProperty(OWNER_UUID, this.playerUUID.toString()); | |
json.addProperty(OWNER_NAME, this.playerName); | |
json.addProperty(CLUE_STAGE, this.clueStage); | |
json.addProperty(CLUE_MAX_STAGE, this.maxStages); | |
json.addProperty(CLUE_TYPE, this.clueType.ordinal()); | |
json.addProperty(CLUE_TIER, this.clueTier.ordinal()); | |
json.addProperty(CLUE_X, this.clueX); | |
json.addProperty(CLUE_Y, this.clueY); | |
json.addProperty(CLUE_Z, this.clueZ); | |
json.addProperty(CLUE_BIOME_ID, this.clueBiomeID); | |
return json; | |
} | |
public void writeToFile() { | |
if (ClueRegistry.clueDataDirectory != null) { | |
File playerDirectory = new File(ClueRegistry.clueDataDirectory, this.playerUUID.toString()); | |
playerDirectory.mkdirs(); | |
try { | |
FileWriter writer = new FileWriter(new File(playerDirectory, "clue.json")); | |
writer.write(jsonSerializer.toJson(this, ClueManager.class)); | |
writer.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment