Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.38 KB | None | 0 0
  1. package fr.mff.facmod.core;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import java.util.Map.Entry;
  8.  
  9. import com.sun.scenario.effect.impl.state.GaussianRenderState;
  10.  
  11. import java.util.UUID;
  12.  
  13. import fr.mff.facmod.achievements.AchievementRegistry;
  14. import fr.mff.facmod.blocks.BlockRegistry;
  15. import fr.mff.facmod.config.ConfigFaction;
  16. import fr.mff.facmod.entity.EntityFactionGuardian;
  17. import net.minecraft.block.state.IBlockState;
  18. import net.minecraft.client.Minecraft;
  19. import net.minecraft.entity.player.EntityPlayer;
  20. import net.minecraft.init.Blocks;
  21. import net.minecraft.item.ItemStack;
  22. import net.minecraft.nbt.NBTTagCompound;
  23. import net.minecraft.nbt.NBTTagList;
  24. import net.minecraft.server.MinecraftServer;
  25. import net.minecraft.tileentity.MobSpawnerBaseLogic;
  26. import net.minecraft.util.BlockPos;
  27. import net.minecraft.util.ChatComponentTranslation;
  28. import net.minecraft.util.EnumChatFormatting;
  29. import net.minecraft.world.ChunkCoordIntPair;
  30. import net.minecraft.world.World;
  31. import net.minecraftforge.common.DimensionManager;
  32. import net.minecraftforge.event.entity.living.LivingHurtEvent;
  33. import net.minecraftforge.fml.common.gameevent.TickEvent;
  34.  
  35. public class Homes {
  36.  
  37.     private static final HashMap<String, BlockPos> homes = new HashMap<String, BlockPos>();
  38.     private static final HashMap<String, EntityFactionGuardian> mobMap = new HashMap<String, EntityFactionGuardian>();
  39.  
  40.     private static final HashMap<EntityPlayer, Object[]> tpTimers = new HashMap<EntityPlayer, Object[]>();
  41.  
  42.     public static void clearHomes() {
  43.         homes.clear();
  44.         }
  45.     public static void clearMob(){
  46.         mobMap.clear();
  47.     }
  48.  
  49.     public static HashMap<String, BlockPos> getHomes() {
  50.         return homes;
  51.     }
  52.  
  53.     public static EnumResult setHome(EntityPlayer player, BlockPos position) {
  54.         UUID uuid = player.getUniqueID();
  55.         Faction faction = Faction.Registry.getPlayerFaction(uuid);
  56.         if (faction != null) {
  57.             Member member = faction.getMember(uuid);
  58.             if (member != null) {
  59.                 if (member.getRank().hasPermission(Permission.FACTION_HANDLING)) {
  60.                     ChunkCoordIntPair pair = MinecraftServer.getServer().getEntityWorld().getChunkFromBlockCoords(position).getChunkCoordIntPair();
  61.                     String factionName = Lands.getLandFaction().get(pair);
  62.                     if (factionName != null) {
  63.                         if (faction.getName().equalsIgnoreCase(factionName)) {
  64.                             EntityFactionGuardian gardian = new EntityFactionGuardian(player.getEntityWorld());
  65.                             IBlockState state = MinecraftServer.getServer().getEntityWorld().getBlockState(position.down());
  66.                             if (state.getBlock() != Blocks.bedrock && state.getBlock() != BlockRegistry.homeBase) {
  67.                                 BlockPos lastPos = homes.remove(factionName);
  68.                                 EntityFactionGuardian lastmob = mobMap.get(factionName);
  69.                                 System.out.println(mobMap);
  70.                                 System.out.println(lastmob);
  71.                                 lastmob.setPositionAndUpdate(position.getX(), position.getY(), position.getZ());
  72.                                 lastmob.sync();
  73.                                 lastmob.setDead();
  74.                                 if(lastmob != null){
  75.                                     lastmob.sync();
  76.                                     lastmob.setDead();
  77.                                 }
  78.                                 if (lastPos != null) {
  79.                                     MinecraftServer.getServer().getEntityWorld().setBlockState(lastPos.down(), Blocks.air.getDefaultState());
  80.                                 }
  81.                                 homes.put(factionName, position);
  82.                                 mobMap.put(factionName, gardian);
  83.                                 System.out.println(mobMap);
  84.                                 System.out.println(homes);
  85.                                 System.out.println(gardian.getEntityId());
  86.  
  87.                                 state.getBlock().onBlockDestroyedByPlayer(MinecraftServer.getServer().getEntityWorld(), position.down(), state);
  88.                                 MinecraftServer.getServer().getEntityWorld().setBlockState(position.down(), BlockRegistry.homeBase.getDefaultState());
  89.                                 FactionSaver.save();
  90.                                 gardian.setLocationAndAngles(position.getX() + 0.5D, position.getY() + 2, position.getZ() + 0.5D, 0.0F, 0.0F);
  91.                                 gardian.setOwner(player);
  92.                                 gardian.setGuardianName(faction.getName());
  93.                                 player.getEntityWorld().spawnEntityInWorld(gardian);
  94.                                
  95.                                 player.triggerAchievement(AchievementRegistry.homeSet);
  96.                                 return EnumResult.HOME_SET.clear().addInformation(EnumChatFormatting.WHITE.toString() + position.getX()).addInformation(EnumChatFormatting.WHITE.toString() + position.getY()).addInformation(EnumChatFormatting.WHITE.toString() + position.getZ());
  97.                             }
  98.                             return EnumResult.NO_PERMISSION;
  99.                         }
  100.                         return EnumResult.LAND_OF_THE_FACTION.clear().addInformation(EnumChatFormatting.GOLD + factionName);
  101.                     }
  102.                     return EnumResult.NOT_CLAIMED_LAND;
  103.                 }
  104.                 return EnumResult.NO_PERMISSION;
  105.             }
  106.             return EnumResult.ERROR;
  107.         }
  108.         return EnumResult.NOT_IN_A_FACTION;
  109.     }
  110.  
  111.     public static void writeToNBT(NBTTagCompound compound) {
  112.         NBTTagList homesList = new NBTTagList();
  113.         NBTTagList mobList = new NBTTagList();
  114.  
  115.         Iterator<Entry<String, BlockPos>> iterator = homes.entrySet().iterator();
  116.         while (iterator.hasNext()) {
  117.             NBTTagCompound homeTag = new NBTTagCompound();
  118.             Entry<String, BlockPos> entry = iterator.next();
  119.             homeTag.setString("faction", entry.getKey());
  120.             homeTag.setInteger("x", entry.getValue().getX());
  121.             homeTag.setInteger("y", entry.getValue().getY());
  122.             homeTag.setInteger("z", entry.getValue().getZ());
  123.             homesList.appendTag(homeTag);
  124.         }
  125.  
  126.         compound.setTag("homes", homesList);
  127.  
  128.         Iterator<Entry<String, EntityFactionGuardian>> iterator2 = mobMap.entrySet().iterator();
  129.         while (iterator2.hasNext()) {
  130.             NBTTagCompound mobTag = new NBTTagCompound();
  131.             Entry<String, EntityFactionGuardian> entry2 = iterator2.next();
  132.             mobTag.setString("nom", entry2.getKey());
  133.             mobTag.setInteger("id", entry2.getValue().getEntityId());
  134.             mobTag.setInteger("world", entry2.getValue().getEntityWorld().provider.getDimensionId());
  135.             mobTag.setInteger("x", entry2.getValue().getPosition().getX());
  136.             mobTag.setInteger("y", entry2.getValue().getPosition().getY());
  137.             mobTag.setInteger("z", entry2.getValue().getPosition().getZ());
  138.             mobList.appendTag(mobTag);
  139.             System.out.println(mobTag);
  140.  
  141.  
  142.         }
  143.         compound.setTag("mobs", mobList);
  144.     }
  145.  
  146.     public static void readFromNBT(NBTTagCompound compound) {
  147.         System.out.println("1");
  148.         Homes.clearHomes();
  149.         Homes.clearMob();
  150.  
  151.         NBTTagList homesList = (NBTTagList) compound.getTag("homes");
  152.         NBTTagList mobList = (NBTTagList) compound.getTag("mobs");
  153.  
  154.         for (int i = 0; i < homesList.tagCount(); i++) {
  155.             System.out.println("2");
  156.  
  157.             NBTTagCompound homeTag = homesList.getCompoundTagAt(i);
  158.             BlockPos pos = new BlockPos(homeTag.getInteger("x"), homeTag.getInteger("y"), homeTag.getInteger("z"));
  159.             homes.put(homeTag.getString("faction"), pos);
  160.             FactionSaver.save();
  161.         }
  162.         for (int i = 0; i < mobList.tagCount(); i++) {
  163.             System.out.println("3");
  164.  
  165.             NBTTagCompound mobTag = mobList.getCompoundTagAt(i);
  166.             World world = DimensionManager.getWorld(mobTag.getInteger("world"));
  167.             EntityFactionGuardian entity = new EntityFactionGuardian(world);
  168.             entity.setEntityId(mobTag.getInteger("id"));
  169.             entity.posX = mobTag.getInteger("x");
  170.             entity.posY = mobTag.getInteger("y");
  171.             entity.posZ = mobTag.getInteger("z");
  172.             mobMap.put(mobTag.getString("nom"), entity);
  173.             FactionSaver.save();
  174.             System.out.println(mobMap);
  175.         }
  176.     }
  177.  
  178.  
  179.     public static EnumResult goToHome(EntityPlayer player) {
  180.         Faction faction = Faction.Registry.getPlayerFaction(player.getUniqueID());
  181.         if (faction != null) {
  182.             BlockPos pos = homes.get(faction.getName());
  183.             if (pos != null) {
  184.                 tpTimers.put(player, new Object[] { player.getPosition(), 0 });
  185.                 return EnumResult.TP_LAUNCHED.clear().addInformation(ConfigFaction.TP_DELAY);
  186.             }
  187.             return EnumResult.HOME_NOT_SET;
  188.         }
  189.         return EnumResult.NOT_IN_A_FACTION;
  190.     }
  191.  
  192.     public static void onLandUnclaimedPre(ChunkCoordIntPair pair) {
  193.         String factionName = Lands.getLandFaction().get(pair);
  194.         BlockPos pos = Homes.getHomes().get(factionName);
  195.         EntityFactionGuardian lastmob = mobMap.get(factionName);
  196.         if (pos != null) {
  197.             ChunkCoordIntPair homeChunk = MinecraftServer.getServer().getEntityWorld().getChunkFromBlockCoords(pos).getChunkCoordIntPair();
  198.             if (homeChunk.equals(pair)) {
  199.                 MinecraftServer.getServer().getEntityWorld().setBlockToAir(pos.down());
  200.                 Homes.getHomes().remove(factionName);
  201.                 lastmob.sync();
  202.                 lastmob.setDead();
  203.             }
  204.         }
  205.     }
  206.  
  207.     public static void onWorldTick(TickEvent.WorldTickEvent event) {
  208.         if (!event.world.isRemote && event.world.equals(MinecraftServer.getServer().getEntityWorld())) {
  209.             List<EntityPlayer> remove = new ArrayList<EntityPlayer>();
  210.  
  211.             Iterator<Entry<EntityPlayer, Object[]>> iterator = tpTimers.entrySet().iterator();
  212.             if (iterator.hasNext()) {
  213.                 Entry<EntityPlayer, Object[]> entry = iterator.next();
  214.  
  215.                 if (entry.getKey().getPosition().equals(entry.getValue()[0])) {
  216.                     Integer tick = (Integer) entry.getValue()[1];
  217.                     tick += 1;
  218.                     entry.getValue()[1] = tick;
  219.                     if (tick % 20 == 0) {
  220.                         if (tick / 20 >= ConfigFaction.TP_DELAY) {
  221.                             Faction faction = Faction.Registry.getPlayerFaction(entry.getKey().getUniqueID());
  222.                             if (faction != null) {
  223.                                 BlockPos pos = homes.get(faction.getName());
  224.                                 if (pos != null) {
  225.                                     entry.getKey().setPositionAndUpdate(pos.getX() + 0.5d, pos.getY(), pos.getZ() + 0.5d);
  226.                                     remove.add(entry.getKey());
  227.                                 } else {
  228.                                     remove.add(entry.getKey());
  229.                                     entry.getKey().addChatComponentMessage(new ChatComponentTranslation(EnumResult.HOME_NOT_SET.getLanguageKey(), new Object[0]));
  230.                                 }
  231.                             } else {
  232.                                 remove.add(entry.getKey());
  233.                                 entry.getKey().addChatComponentMessage(new ChatComponentTranslation(EnumResult.NOT_IN_A_FACTION.getLanguageKey(), new Object[0]));
  234.                             }
  235.                         } else {
  236.                             entry.getKey().addChatComponentMessage(new ChatComponentTranslation("msg.tpTimer", String.valueOf(ConfigFaction.TP_DELAY - tick / 20)));
  237.                         }
  238.                     }
  239.                 } else {
  240.                     remove.add(entry.getKey());
  241.                     entry.getKey().addChatComponentMessage(new ChatComponentTranslation("msg.tpCanceled", new Object[0]));
  242.                 }
  243.             }
  244.  
  245.             for (EntityPlayer player : remove) {
  246.                 tpTimers.remove(player);
  247.             }
  248.         }
  249.     }
  250.  
  251.     public static void onLivingHurt(LivingHurtEvent event) {
  252.         if (!event.entity.worldObj.isRemote) {
  253.             if (event.entity instanceof EntityPlayer) {
  254.                 Object o = tpTimers.remove(event.entity);
  255.                 if (o != null) {
  256.                     ((EntityPlayer) event.entity).addChatComponentMessage(new ChatComponentTranslation("msg.tpCanceled", new Object[0]));
  257.                 }
  258.             }
  259.         }
  260.     }
  261.  
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement