/ClueManager.java Secret
Created
July 19, 2015 01:34
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 net.minecraft.block.Block; | |
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 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 { | |
public static final String TAG_CLUE_UUID = "clueUUID"; | |
public static final String TAG_OWNER_UUID = "playerUUID"; | |
public static final String TAG_OWNER_NAME = "playerName"; | |
public static final String TAG_CLUE_STAGE = "clueStage"; | |
public static final String TAG_CLUE_MAX_STAGE = "maxClueStage"; | |
public static final String TAG_CLUE_TYPE = "clueType"; | |
public static final String TAG_CLUE_TIER = "clueTier"; | |
public static final String TAG_HAS_COORDINATE = "hasCoordinate"; | |
public static final String TAG_CLUE_X = "clueX"; | |
public static final String TAG_CLUE_Y = "clueY"; | |
public static final String TAG_CLUE_Z = "clueZ"; | |
public static final String TAG_CLUE_BIOME_ID = "clueBiomeID"; | |
private ItemStack clueStack; | |
private UUID clueUUID; | |
private UUID playerUUID; | |
private String playerName; | |
private Random rand; | |
private int clueStage = 0; | |
private int maxStages = 0; | |
private EnumClueType clueType = null; | |
private EnumClueTier clueTier = null; | |
private boolean hasCoordinate = false; | |
private int clueX = -1; | |
private int clueY = -1; | |
private int clueZ = -1; | |
private int clueBiomeID = -1; | |
public ClueManager(ItemStack stack, boolean readOnly) { | |
this.clueStack = stack; | |
this.rand = new Random(); | |
if (readOnly) { | |
readFromNBT(); | |
return; | |
} | |
if (clueStack.stackTagCompound == null) | |
clueStack.stackTagCompound = new NBTTagCompound(); | |
if (clueStack.stackTagCompound.hasKey(TAG_CLUE_UUID) && clueStack.stackTagCompound.getString(TAG_CLUE_UUID) != null) { | |
this.clueUUID = UUID.fromString(clueStack.stackTagCompound.getString(TAG_CLUE_UUID)); | |
} else { | |
this.clueUUID = UUID.randomUUID(); | |
} | |
writeToNBT(); | |
} | |
// Remember to call this after making any changes to values or they won't persist! | |
public void writeToNBT() { | |
if (hasStack()) { | |
if (clueStack.stackTagCompound == null) | |
clueStack.stackTagCompound = new NBTTagCompound(); | |
NBTTagCompound compound = clueStack.stackTagCompound; | |
if (clueUUID != null) compound.setString(TAG_CLUE_UUID, clueUUID.toString()); | |
if (clueStage != 0) compound.setInteger(TAG_CLUE_STAGE, clueStage); | |
if (maxStages != 0) compound.setInteger(TAG_CLUE_MAX_STAGE, maxStages); | |
if (clueType != null) compound.setInteger(TAG_CLUE_TYPE, clueType.getID()); | |
if (clueTier != null) compound.setInteger(TAG_CLUE_TIER, clueTier.getTier()); | |
if (playerUUID != null) compound.setString(TAG_OWNER_UUID, playerUUID.toString()); | |
if (playerName != null) compound.setString(TAG_OWNER_NAME, playerName); | |
if (clueBiomeID != -1) compound.setInteger(TAG_CLUE_BIOME_ID, clueBiomeID); | |
compound.setBoolean(TAG_HAS_COORDINATE, hasCoordinate); | |
if (hasCoordinate) { | |
compound.setInteger(TAG_CLUE_X, clueX); | |
compound.setInteger(TAG_CLUE_Y, clueY); | |
compound.setInteger(TAG_CLUE_Z, clueZ); | |
} | |
} | |
} | |
// Should only ever really be used when loading from disk after server reset. | |
public void readFromNBT() { | |
if (hasStack()) { | |
if (clueStack.stackTagCompound != null) { | |
NBTTagCompound compound = clueStack.getTagCompound(); | |
this.clueStage = compound.hasKey(TAG_CLUE_STAGE) ? compound.getInteger(TAG_CLUE_STAGE) : 0; | |
this.maxStages = compound.hasKey(TAG_CLUE_MAX_STAGE) ? compound.getInteger(TAG_CLUE_MAX_STAGE) : 0; | |
this.clueType = compound.hasKey(TAG_CLUE_TYPE) ? EnumClueType.fromID(compound.getInteger(TAG_CLUE_TYPE)) : null; | |
this.clueTier = compound.hasKey(TAG_CLUE_TIER) ? EnumClueTier.fromID(compound.getInteger(TAG_CLUE_TIER)) : null; | |
this.playerUUID = compound.hasKey(TAG_OWNER_UUID) ? UUID.fromString(compound.getString(TAG_OWNER_UUID)) : null; | |
this.playerName = compound.hasKey(TAG_OWNER_NAME) ? compound.getString(TAG_OWNER_NAME) : null; | |
this.hasCoordinate = compound.hasKey(TAG_HAS_COORDINATE) && compound.getBoolean(TAG_HAS_COORDINATE); | |
this.clueX = compound.hasKey(TAG_CLUE_X) ? compound.getInteger(TAG_CLUE_X) : -1; | |
this.clueY = compound.hasKey(TAG_CLUE_Y) ? compound.getInteger(TAG_CLUE_Y) : -1; | |
this.clueZ = compound.hasKey(TAG_CLUE_Z) ? compound.getInteger(TAG_CLUE_Z) : -1; | |
this.clueBiomeID = compound.hasKey(TAG_CLUE_BIOME_ID) ? compound.getInteger(TAG_CLUE_BIOME_ID) : -1; | |
} | |
} | |
} | |
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.maxStages == 0) { | |
this.maxStages = this.rand.nextInt((this.clueTier.getMaxStages() - 1)) + 1; | |
} | |
if (this.clueStage > this.maxStages) { | |
completeClue(); | |
ClueRegistry.unregister(getEntityPlayer()); | |
} | |
this.clueType = EnumClueType.SCAN; | |
switch(this.clueType) { | |
case COORDINATE: | |
generateCoordinate(); | |
break; | |
case SCAN: | |
generateScanBiome(); | |
} | |
this.writeToNBT(); | |
} | |
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; | |
this.hasCoordinate = true; | |
} | |
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 boolean hasStack() { | |
return clueStack != null; | |
} | |
public ItemStack getClueStack() { | |
return clueStack; | |
} | |
public void setClueStack(ItemStack clueStack) { | |
this.clueStack = clueStack; | |
} | |
public void setClueUUID(UUID clueUUID) { | |
this.clueUUID = clueUUID; | |
} | |
public UUID getClueUUID() { | |
return clueUUID; | |
} | |
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 boolean hasCoordinate() { | |
return hasCoordinate; | |
} | |
public void setHasCoordinate(boolean hasCoordinate) { | |
this.hasCoordinate = hasCoordinate; | |
} | |
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 String toString() { | |
return "ClueManager{" + | |
"clueStack=" + clueStack + | |
", clueUUID=" + clueUUID + | |
", playerUUID=" + playerUUID + | |
", playerName='" + playerName + '\'' + | |
", clueStage=" + clueStage + | |
", clueType=" + clueType + | |
", clueTier=" + clueTier + | |
", hasCoordinate=" + hasCoordinate + | |
", clueX=" + clueX + | |
", clueY=" + clueY + | |
", clueZ=" + clueZ + | |
'}'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment