Advertisement
Guest User

HumanbenderSpawn

a guest
Aug 24th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.63 KB | None | 0 0
  1. /*
  2.   This file is part of AvatarMod.
  3.    
  4.   AvatarMod is free software: you can redistribute it and/or modify
  5.   it under the terms of the GNU General Public License as published by
  6.   the Free Software Foundation, either version 3 of the License, or
  7.   (at your option) any later version.
  8.  
  9.   AvatarMod is distributed in the hope that it will be useful,
  10.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.   GNU General Public License for more details.
  13.  
  14.   You should have received a copy of the GNU General Public License
  15.   along with AvatarMod. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package com.crowsofwar.avatar.common;
  18.  
  19. import com.crowsofwar.avatar.AvatarInfo;
  20. import com.crowsofwar.avatar.common.entity.mob.EntityAirbender;
  21. import com.crowsofwar.avatar.common.entity.mob.EntityFirebender;
  22. import com.crowsofwar.avatar.common.entity.mob.EntityHumanBender;
  23. import net.minecraft.entity.passive.EntityVillager;
  24. import net.minecraft.util.math.AxisAlignedBB;
  25. import net.minecraft.util.math.ChunkPos;
  26. import net.minecraft.village.Village;
  27. import net.minecraft.world.World;
  28. import net.minecraft.world.gen.structure.MapGenVillage;
  29. import net.minecraftforge.event.terraingen.InitMapGenEvent;
  30. import net.minecraftforge.event.terraingen.InitMapGenEvent.EventType;
  31. import net.minecraftforge.fml.common.Mod;
  32. import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
  33.  
  34. import java.util.List;
  35. import java.util.Map;
  36. import java.util.Random;
  37.  
  38. /**
  39.  * @author CrowsOfWar
  40.  */
  41. @Mod.EventBusSubscriber(modid = AvatarInfo.MOD_ID)
  42. public class HumanBenderSpawner {
  43.  
  44.     @SubscribeEvent
  45.     public static void modifyVillageSpawner(InitMapGenEvent e) {
  46.  
  47.         //if (e.getType() == EventType.VILLAGE) {
  48.             // TODO See if this messes up superflat world options
  49.             e.setNewGen(new MapGenVillageWithHumanbenders());
  50.         //}
  51.  
  52.     }
  53.  
  54.     private static class MapGenVillageWithHumanbenders extends MapGenVillage {
  55.  
  56.         public MapGenVillageWithHumanbenders() {
  57.             super();
  58.         }
  59.  
  60.         public MapGenVillageWithHumanbenders(Map<String, String> map) {
  61.             super(map);
  62.         }
  63.  
  64.         @Override
  65.         public synchronized boolean generateStructure(World worldIn, Random randomIn, ChunkPos chunkCoord) {
  66.             boolean result = super.generateStructure(worldIn, randomIn, chunkCoord);
  67.             if (result) {
  68.  
  69.                 // This list contains villagers in that structure
  70.                 List<EntityVillager> villagers = worldIn.getEntities(EntityVillager.class, villager -> {
  71.                     return new ChunkPos(villager.getPosition()).equals(chunkCoord);
  72.                 });
  73.  
  74.                 // To attempt to have all humanbenders be same type, check if
  75.                 // there are nearby humanbenders
  76.                 // If there are just copy their type
  77.                 AxisAlignedBB aabb = new AxisAlignedBB(chunkCoord.getBlock(-30, 50, -30),
  78.                         chunkCoord.getBlock(30, 150, 30));
  79.                 List<EntityHumanBender> nearbyBenders = worldIn.getEntitiesWithinAABB(EntityHumanBender.class,
  80.                         aabb);
  81.  
  82.                 double chance = 100;
  83.                 Random rand = new Random();
  84.                 if (!villagers.isEmpty() && rand.nextDouble() * 100 < chance) {
  85.  
  86.                     Village village = worldIn.getVillageCollection()
  87.                             .getNearestVillage(chunkCoord.getBlock(0, 0, 0), 200);
  88.  
  89.                     boolean firebender;
  90.  
  91.                     if (nearbyBenders.isEmpty()) {
  92.                         firebender = new Random().nextBoolean();
  93.                     } else {
  94.                         firebender = nearbyBenders.get(0) instanceof EntityFirebender;
  95.                     }
  96.  
  97.                     EntityHumanBender bender = firebender ? new EntityFirebender(worldIn)
  98.                             : new EntityAirbender(worldIn);
  99.                     bender.copyLocationAndAnglesFrom(villagers.get(0));
  100.                     worldIn.spawnEntity(bender);
  101.  
  102.                 }
  103.  
  104.             }
  105.             return result;
  106.         }
  107.  
  108.     }
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement