|
1 | | -package com.mcmoddev.orespawn; |
2 | | - |
3 | | -import java.util.ArrayList; |
4 | | -import java.util.Collection; |
5 | | -import java.util.Iterator; |
6 | | -import java.util.List; |
7 | | -import java.util.Map.Entry; |
8 | | -import java.util.Random; |
9 | | -import java.util.Set; |
10 | | - |
11 | | -import com.mcmoddev.orespawn.api.os3.BuilderLogic; |
12 | | -import com.mcmoddev.orespawn.api.os3.SpawnBuilder; |
13 | | -import com.mcmoddev.orespawn.data.Config; |
14 | | -import com.mcmoddev.orespawn.data.Constants; |
15 | | - |
16 | | -import net.minecraft.nbt.NBTTagCompound; |
17 | | -import net.minecraft.nbt.NBTTagList; |
18 | | -import net.minecraft.nbt.NBTTagString; |
19 | | -import net.minecraft.util.math.ChunkPos; |
20 | | -import net.minecraft.world.World; |
21 | | -import net.minecraft.world.chunk.IChunkGenerator; |
22 | | -import net.minecraft.world.gen.ChunkProviderServer; |
23 | | -import net.minecraftforge.event.terraingen.OreGenEvent; |
24 | | -import net.minecraftforge.event.world.ChunkDataEvent; |
25 | | -import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; |
26 | | -import net.minecraftforge.fml.common.registry.GameRegistry; |
27 | | -import net.minecraftforge.fml.common.IWorldGenerator; |
28 | | -import net.minecraftforge.fml.common.ObfuscationReflectionHelper; |
29 | | -import net.minecraftforge.fml.common.eventhandler.Event; |
30 | | - |
31 | | -public class EventHandlers { |
32 | | - private List<ChunkPos> chunks; |
33 | | - |
34 | | - public EventHandlers() { |
35 | | - chunks = new ArrayList<>(); |
36 | | - } |
37 | | - |
38 | | - @SubscribeEvent |
39 | | - public void onGenerateMinable(OreGenEvent.GenerateMinable event) { |
40 | | - if( Config.getBoolean(Constants.REPLACE_VANILLA_OREGEN) ) { |
41 | | - event.setResult(Event.Result.DENY); |
42 | | - } |
43 | | - } |
44 | | - |
45 | | - @SubscribeEvent |
46 | | - public void onChunkSave(ChunkDataEvent.Save ev) { |
47 | | - NBTTagCompound dataTag = ev.getData().getCompoundTag(Constants.CHUNK_TAG_NAME); |
48 | | - NBTTagList ores = new NBTTagList(); |
49 | | - NBTTagList features = new NBTTagList(); |
50 | | - features.appendTag( new NBTTagString("orespawn:default")); |
51 | | - |
52 | | - for( Entry<String, BuilderLogic> ent : OreSpawn.API.getSpawns().entrySet() ) { |
53 | | - BuilderLogic log = ent.getValue(); |
54 | | - if( log.getAllDimensions().containsKey(ev.getWorld().provider.getDimension()) ) { |
55 | | - Collection<SpawnBuilder> vals = log.getDimension(ev.getWorld().provider.getDimension()).getAllSpawns(); |
56 | | - for( SpawnBuilder s : vals ) { |
57 | | - ores.appendTag( new NBTTagString( s.getOres().get(0).getOre().getBlock().getRegistryName().toString() ) ); |
58 | | - } |
59 | | - } |
60 | | - if( log.getAllDimensions().containsKey(OreSpawn.API.dimensionWildcard()) ) { |
61 | | - Collection<SpawnBuilder> vals = log.getDimension(OreSpawn.API.dimensionWildcard()).getAllSpawns(); |
62 | | - for( SpawnBuilder s : vals ) { |
63 | | - ores.appendTag( new NBTTagString( s.getOres().get(0).getOre().getBlock().getRegistryName().toString() ) ); |
64 | | - } |
65 | | - } |
66 | | - } |
67 | | - |
68 | | - dataTag.setTag(Constants.ORE_TAG, ores); |
69 | | - dataTag.setTag(Constants.FEATURES_TAG, features); |
70 | | - ev.getData().setTag(Constants.CHUNK_TAG_NAME, dataTag); |
71 | | - } |
72 | | - |
73 | | - @SubscribeEvent |
74 | | - public void onChunkLoad(ChunkDataEvent.Load ev) { |
75 | | - World world = ev.getWorld(); |
76 | | - ChunkPos chunkCoords = new ChunkPos(ev.getChunk().x, ev.getChunk().z); |
77 | | - int chunkX = ev.getChunk().x; |
78 | | - int chunkZ = ev.getChunk().z; |
79 | | - |
80 | | - if( chunks.contains(chunkCoords) ) { |
81 | | - return; |
82 | | - } |
83 | | - |
84 | | - if( Config.getBoolean(Constants.RETROGEN_KEY) ) { |
85 | | - chunks.add(chunkCoords); |
86 | | - Set<IWorldGenerator> worldGens = ObfuscationReflectionHelper.getPrivateValue(GameRegistry.class, null, "worldGenerators"); |
87 | | - NBTTagCompound chunkTag = ev.getData().getCompoundTag(Constants.CHUNK_TAG_NAME); |
88 | | - int count = chunkTag==null?0:chunkTag.getTagList(Constants.ORE_TAG, 8).tagCount(); |
89 | | - if( count != countOres(ev.getWorld().provider.getDimension()) || |
90 | | - Config.getBoolean(Constants.FORCE_RETROGEN_KEY)) { |
91 | | - for (Iterator<IWorldGenerator> iterator = worldGens.iterator(); iterator.hasNext();) |
92 | | - { |
93 | | - IWorldGenerator wg = iterator.next(); |
94 | | - long worldSeed = world.getSeed(); |
95 | | - Random fmlRandom = new Random(worldSeed); |
96 | | - long xSeed = fmlRandom.nextLong() >> 2 + 1L; |
97 | | - long zSeed = fmlRandom.nextLong() >> 2 + 1L; |
98 | | - long chunkSeed = (xSeed * chunkCoords.x + zSeed * chunkCoords.z) ^ worldSeed; |
99 | | - |
100 | | - fmlRandom.setSeed(chunkSeed); |
101 | | - ChunkProviderServer chunkProvider = (ChunkProviderServer) world.getChunkProvider(); |
102 | | - IChunkGenerator chunkGenerator = ObfuscationReflectionHelper.getPrivateValue(ChunkProviderServer.class, chunkProvider, "field_186029_c", "chunkGenerator"); |
103 | | - wg.generate(fmlRandom, chunkX, chunkZ, world, chunkGenerator, chunkProvider); |
104 | | - } |
105 | | - } |
106 | | - } |
107 | | - } |
108 | | - |
109 | | - |
110 | | - private int countOres(int dim) { |
111 | | - int acc = 0; |
112 | | - for( Entry<String, BuilderLogic> sL : OreSpawn.API.getSpawns().entrySet() ) { |
113 | | - if( sL.getValue().getAllDimensions().containsKey(dim) ) { |
114 | | - acc += sL.getValue().getAllDimensions().get(dim).getAllSpawns().size(); |
115 | | - } |
116 | | - if( sL.getValue().getAllDimensions().containsKey(OreSpawn.API.dimensionWildcard()) ) { |
117 | | - acc += sL.getValue().getAllDimensions().get(OreSpawn.API.dimensionWildcard()).getAllSpawns().size(); |
118 | | - } |
119 | | - } |
120 | | - return acc; |
121 | | - } |
122 | | - |
123 | | - |
124 | | -} |
| 1 | +package com.mcmoddev.orespawn; |
| 2 | + |
| 3 | +import java.util.LinkedList; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.Deque; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map.Entry; |
| 8 | +import java.util.Random; |
| 9 | +import java.util.concurrent.ConcurrentLinkedDeque; |
| 10 | +import java.util.stream.Collectors; |
| 11 | + |
| 12 | +import com.mcmoddev.orespawn.api.os3.BuilderLogic; |
| 13 | +import com.mcmoddev.orespawn.api.os3.DimensionBuilder; |
| 14 | +import com.mcmoddev.orespawn.api.os3.FeatureBuilder; |
| 15 | +import com.mcmoddev.orespawn.api.os3.OreBuilder; |
| 16 | +import com.mcmoddev.orespawn.api.os3.SpawnBuilder; |
| 17 | +import com.mcmoddev.orespawn.data.Config; |
| 18 | +import com.mcmoddev.orespawn.data.Constants; |
| 19 | + |
| 20 | +import net.minecraft.nbt.NBTTagCompound; |
| 21 | +import net.minecraft.nbt.NBTTagList; |
| 22 | +import net.minecraft.nbt.NBTTagString; |
| 23 | +import net.minecraft.util.math.ChunkPos; |
| 24 | +import net.minecraft.world.World; |
| 25 | +import net.minecraft.world.chunk.IChunkGenerator; |
| 26 | +import net.minecraft.world.gen.ChunkProviderServer; |
| 27 | +import net.minecraftforge.event.terraingen.OreGenEvent; |
| 28 | +import net.minecraftforge.event.terraingen.OreGenEvent.GenerateMinable.EventType; |
| 29 | +import net.minecraftforge.event.world.ChunkDataEvent; |
| 30 | +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; |
| 31 | +import net.minecraftforge.fml.common.gameevent.TickEvent.Phase; |
| 32 | +import net.minecraftforge.fml.common.gameevent.TickEvent.WorldTickEvent; |
| 33 | +import net.minecraftforge.fml.relauncher.Side; |
| 34 | +import net.minecraftforge.fml.common.ObfuscationReflectionHelper; |
| 35 | +import net.minecraftforge.fml.common.eventhandler.Event; |
| 36 | +import net.minecraftforge.fml.common.eventhandler.EventPriority; |
| 37 | + |
| 38 | +public class EventHandlers { |
| 39 | + private Deque<ChunkPos> chunks; |
| 40 | + private Deque<ChunkPos> retroChunks; |
| 41 | + |
| 42 | + EventHandlers() { |
| 43 | + chunks = new ConcurrentLinkedDeque<>(); |
| 44 | + retroChunks = new ConcurrentLinkedDeque<>(); |
| 45 | + } |
| 46 | + |
| 47 | + private List<EventType> vanillaEvents = Arrays.asList(EventType.ANDESITE, EventType.COAL, EventType.DIAMOND, EventType.DIORITE, EventType.DIRT, |
| 48 | + EventType.EMERALD, EventType.GOLD, EventType.GRANITE, EventType.GRAVEL, EventType.IRON, EventType.LAPIS, EventType.REDSTONE, |
| 49 | + EventType.QUARTZ, EventType.SILVERFISH); |
| 50 | + |
| 51 | + @SubscribeEvent(priority = EventPriority.HIGHEST, receiveCanceled = true) |
| 52 | + public void onGenerateMinable(OreGenEvent.GenerateMinable event) { |
| 53 | + if (Config.getBoolean(Constants.REPLACE_VANILLA_OREGEN) && vanillaEvents.contains(event.getType())) { |
| 54 | + event.setResult(Event.Result.DENY); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @SubscribeEvent |
| 59 | + public void onChunkSave(ChunkDataEvent.Save ev) { |
| 60 | + NBTTagCompound dataTag = ev.getData().getCompoundTag(Constants.CHUNK_TAG_NAME); |
| 61 | + NBTTagList ores = new NBTTagList(); |
| 62 | + NBTTagList features = new NBTTagList(); |
| 63 | + |
| 64 | + features.appendTag(new NBTTagString("orespawn:default")); |
| 65 | + |
| 66 | + List<DimensionBuilder> spawns = OreSpawn.API.getSpawns().entrySet().stream() |
| 67 | + .filter(ent -> ent.getValue().getAllDimensions().containsKey(ev.getWorld().provider.getDimension())) |
| 68 | + .map(ent -> ent.getValue().getDimension(ev.getWorld().provider.getDimension())) |
| 69 | + .collect(Collectors.toList()); |
| 70 | + |
| 71 | + if (ev.getWorld().provider.getDimension() > 0 && ev.getWorld().provider.getDimension() != 1) { |
| 72 | + spawns.addAll(OreSpawn.API.getSpawns().entrySet().stream() |
| 73 | + .filter(ent -> ent.getValue().getAllDimensions().containsKey(OreSpawn.API.dimensionWildcard())) |
| 74 | + .map(ent -> ent.getValue().getDimension(OreSpawn.API.dimensionWildcard())) |
| 75 | + .collect(Collectors.toList())); |
| 76 | + } |
| 77 | + |
| 78 | + List<SpawnBuilder> spc = new LinkedList<>(); |
| 79 | + List<OreBuilder> oreList = new LinkedList<>(); |
| 80 | + spawns.stream().map(DimensionBuilder::getAllSpawns).forEach(spc::addAll); |
| 81 | + spc.stream().map(SpawnBuilder::getOres).forEach(oreList::addAll); |
| 82 | + |
| 83 | + oreList.stream() |
| 84 | + .map(oreEnt -> new NBTTagString(oreEnt.getOre().getBlock().getRegistryName().toString())) |
| 85 | + .forEach(ores::appendTag); |
| 86 | + |
| 87 | + List<FeatureBuilder> featureList = new LinkedList<>(); |
| 88 | + |
| 89 | + spawns.forEach(sp -> featureList.addAll(sp.getAllSpawns().stream().map(SpawnBuilder::getFeatureGen).collect(Collectors.toList()))); |
| 90 | + |
| 91 | + featureList.stream() |
| 92 | + .map(feat -> new NBTTagString(feat.getFeatureName())) |
| 93 | + .forEach(features::appendTag); |
| 94 | + |
| 95 | + ChunkPos chunkCoords = new ChunkPos(ev.getChunk().x, ev.getChunk().z); |
| 96 | + |
| 97 | + if (!Config.getBoolean(Constants.RETROGEN_KEY) || chunks.contains(chunkCoords)) { |
| 98 | + dataTag.setTag(Constants.ORE_TAG, ores); |
| 99 | + dataTag.setTag(Constants.FEATURES_TAG, features); |
| 100 | + } |
| 101 | + |
| 102 | + ev.getData().setTag(Constants.CHUNK_TAG_NAME, dataTag); |
| 103 | + } |
| 104 | + |
| 105 | + @SubscribeEvent |
| 106 | + public void onChunkLoad(ChunkDataEvent.Load ev) { |
| 107 | + World world = ev.getWorld(); |
| 108 | + ChunkPos chunkCoords = new ChunkPos(ev.getChunk().x, ev.getChunk().z); |
| 109 | + |
| 110 | + doBedrockRetrogen(chunkCoords); |
| 111 | + |
| 112 | + if (chunks.contains(chunkCoords)) { |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + if (Config.getBoolean(Constants.RETROGEN_KEY)) { |
| 117 | + NBTTagCompound chunkTag = ev.getData().getCompoundTag(Constants.CHUNK_TAG_NAME); |
| 118 | + |
| 119 | + if (featuresAreDifferent(chunkTag, world.provider.getDimension()) || Config.getBoolean(Constants.FORCE_RETROGEN_KEY)) { |
| 120 | + chunks.addLast(chunkCoords); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + private boolean featuresAreDifferent(NBTTagCompound chunkTag, int dim) { |
| 127 | + return ((countOres(dim) != chunkTag.getTagList(Constants.ORE_TAG, 8).tagCount()) || |
| 128 | + compFeatures(chunkTag.getTagList(Constants.FEATURES_TAG, 8), dim)); |
| 129 | + } |
| 130 | + |
| 131 | + private boolean compFeatures(NBTTagList tagList, int dim) { |
| 132 | + List<DimensionBuilder> spawns = OreSpawn.API.getSpawns().entrySet().stream() |
| 133 | + .filter(ent -> ent.getValue().getAllDimensions().containsKey(dim)) |
| 134 | + .map(ent -> ent.getValue().getDimension(dim)) |
| 135 | + .collect(Collectors.toList()); |
| 136 | + |
| 137 | + if (dim > 0 && dim != 1) { |
| 138 | + spawns.addAll(OreSpawn.API.getSpawns().entrySet().stream() |
| 139 | + .filter(ent -> ent.getValue().getAllDimensions().containsKey(OreSpawn.API.dimensionWildcard())) |
| 140 | + .map(ent -> ent.getValue().getDimension(OreSpawn.API.dimensionWildcard())) |
| 141 | + .collect(Collectors.toList())); |
| 142 | + } |
| 143 | + |
| 144 | + List<FeatureBuilder> featureList = new LinkedList<>(); |
| 145 | + |
| 146 | + spawns.forEach(sp -> featureList.addAll(sp.getAllSpawns().stream().map(SpawnBuilder::getFeatureGen).collect(Collectors.toList()))); |
| 147 | + |
| 148 | + return featureList.size() == tagList.tagCount(); |
| 149 | + } |
| 150 | + |
| 151 | + private void doBedrockRetrogen(ChunkPos chunkCoords) { |
| 152 | + if (retroChunks.contains(chunkCoords)) { |
| 153 | + return; |
| 154 | + } |
| 155 | + |
| 156 | + if (Config.getBoolean(Constants.RETRO_BEDROCK)) { |
| 157 | + retroChunks.addLast(chunkCoords); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + private int countOres(int dim) { |
| 162 | + int acc = 0; |
| 163 | + |
| 164 | + for (Entry<String, BuilderLogic> sL : OreSpawn.API.getSpawns().entrySet()) { |
| 165 | + if (sL.getValue().getAllDimensions().containsKey(dim)) { |
| 166 | + acc += sL.getValue().getAllDimensions().get(dim).getAllSpawns().size(); |
| 167 | + } |
| 168 | + |
| 169 | + if (sL.getValue().getAllDimensions().containsKey(OreSpawn.API.dimensionWildcard())) { |
| 170 | + acc += sL.getValue().getAllDimensions().get(OreSpawn.API.dimensionWildcard()).getAllSpawns().size(); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + return acc; |
| 175 | + } |
| 176 | + |
| 177 | + @SubscribeEvent |
| 178 | + public void worldTick(WorldTickEvent ev) { |
| 179 | + if (ev.side != Side.SERVER) { |
| 180 | + return; |
| 181 | + } |
| 182 | + |
| 183 | + World world = ev.world; |
| 184 | + |
| 185 | + if (ev.phase == Phase.END) { |
| 186 | + for (int c = 0; c < 5 && !chunks.isEmpty(); c++) { |
| 187 | + ChunkPos p = chunks.pop(); |
| 188 | + Random random = new Random(world.getSeed()); |
| 189 | + // re-seed with something totally new :P |
| 190 | + random.setSeed((((random.nextLong() >> 4 + 1) + p.x) + ((random.nextLong() >> 2 + 1) + p.z)) ^ world.getSeed()); |
| 191 | + ChunkProviderServer chunkProvider = (ChunkProviderServer) world.getChunkProvider(); |
| 192 | + IChunkGenerator chunkGenerator = ObfuscationReflectionHelper.getPrivateValue(ChunkProviderServer.class, chunkProvider, "field_186029_c", "chunkGenerator"); |
| 193 | + OreSpawn.API.getGenerator().generate(random, p.x, p.z, world, chunkGenerator, chunkProvider); |
| 194 | + } |
| 195 | + |
| 196 | + for (int c = 0; c < 5 && !retroChunks.isEmpty(); c++) { |
| 197 | + ChunkPos p = retroChunks.pop(); |
| 198 | + OreSpawn.flatBedrock.retrogen(world, p.x, p.z); |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments