1 package me.shedaniel.lightoverlay;
3 import com.mojang.blaze3d.platform.GlStateManager;
4 import net.minecraft.block.Block;
5 import net.minecraft.block.BlockState;
6 import net.minecraft.client.Minecraft;
7 import net.minecraft.client.entity.player.ClientPlayerEntity;
8 import net.minecraft.client.renderer.ActiveRenderInfo;
9 import net.minecraft.client.renderer.BufferBuilder;
10 import net.minecraft.client.renderer.Tessellator;
11 import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
12 import net.minecraft.client.settings.KeyBinding;
13 import net.minecraft.client.util.InputMappings;
14 import net.minecraft.entity.Entity;
15 import net.minecraft.entity.EntityClassification;
16 import net.minecraft.entity.EntityType;
17 import net.minecraft.entity.player.PlayerEntity;
18 import net.minecraft.tags.BlockTags;
19 import net.minecraft.util.Direction;
20 import net.minecraft.util.ResourceLocation;
21 import net.minecraft.util.math.BlockPos;
22 import net.minecraft.util.math.shapes.ISelectionContext;
23 import net.minecraft.util.math.shapes.VoxelShape;
24 import net.minecraft.util.text.TranslationTextComponent;
25 import net.minecraft.world.LightType;
26 import net.minecraft.world.World;
27 import net.minecraft.world.biome.Biome;
28 import net.minecraftforge.api.distmarker.Dist;
29 import net.minecraftforge.api.distmarker.OnlyIn;
30 import net.minecraftforge.client.event.InputEvent;
31 import net.minecraftforge.client.event.RenderWorldLastEvent;
32 import net.minecraftforge.client.settings.KeyConflictContext;
33 import net.minecraftforge.client.settings.KeyModifier;
34 import net.minecraftforge.common.MinecraftForge;
35 import net.minecraftforge.eventbus.api.SubscribeEvent;
36 import net.minecraftforge.fml.client.registry.ClientRegistry;
37 import net.minecraftforge.fml.common.Mod;
41 import java.io.FileInputStream;
42 import java.io.FileOutputStream;
43 import java.io.IOException;
44 import java.text.DecimalFormat;
46 import java.util.Properties;
49 @Mod("lightoverlay-forge")
50 public class LightOverlay {
52 private static final String KEYBIND_CATEGORY = "key.lightoverlay-forge.category";
53 private static final ResourceLocation ENABLE_OVERLAY_KEYBIND = new ResourceLocation("lightoverlay-forge", "enable_overlay");
54 private static final ResourceLocation INCREASE_REACH_KEYBIND = new ResourceLocation("lightoverlay-forge", "increase_reach");
55 private static final ResourceLocation DECREASE_REACH_KEYBIND = new ResourceLocation("lightoverlay-forge", "decrease_reach");
56 private static final ResourceLocation INCREASE_LINE_WIDTH_KEYBIND = new ResourceLocation("lightoverlay-forge", "increase_line_width");
57 private static final ResourceLocation DECREASE_LINE_WIDTH_KEYBIND = new ResourceLocation("lightoverlay-forge", "decrease_line_width");
58 private static final DecimalFormat FORMAT = new DecimalFormat("#.#");
59 private static KeyBinding enableOverlay, increaseReach, decreaseReach, increaseLineWidth, decreaseLineWidth;
60 private static boolean enabled = false;
61 private static int reach = 12;
62 private static EntityType<Entity> testingEntityType;
63 private static float lineWidth = 1.0F;
64 private static File configFile = new File(new File(Minecraft.getInstance().gameDir, "config"), "lightoverlay.properties");
66 public LightOverlay() {
68 loadConfig(configFile);
71 testingEntityType = EntityType.Builder.create(EntityClassification.MONSTER).size(0f, 0f).disableSerialization().build(null);
72 enableOverlay = registerKeybind(ENABLE_OVERLAY_KEYBIND, InputMappings.Type.KEYSYM, 296, KEYBIND_CATEGORY);
73 increaseReach = registerKeybind(INCREASE_REACH_KEYBIND, InputMappings.Type.KEYSYM, -1, KEYBIND_CATEGORY);
74 decreaseReach = registerKeybind(DECREASE_REACH_KEYBIND, InputMappings.Type.KEYSYM, -1, KEYBIND_CATEGORY);
75 increaseLineWidth = registerKeybind(INCREASE_LINE_WIDTH_KEYBIND, InputMappings.Type.KEYSYM, -1, KEYBIND_CATEGORY);
76 decreaseLineWidth = registerKeybind(DECREASE_LINE_WIDTH_KEYBIND, InputMappings.Type.KEYSYM, -1, KEYBIND_CATEGORY);
77 MinecraftForge.EVENT_BUS.register(this);
80 public static CrossType getCrossType(BlockPos pos, World world, PlayerEntity playerEntity) {
81 BlockState blockBelowState = world.getBlockState(pos.down());
82 BlockState blockUpperState = world.getBlockState(pos);
83 VoxelShape upperCollisionShape = blockUpperState.getCollisionShape(world, pos, ISelectionContext.forEntity(playerEntity));
84 /* WorldEntitySpawner.func_222266_a */
85 // Check if the outline is full
86 if (Block.doesSideFillSquare(upperCollisionShape, Direction.UP))
87 return CrossType.NONE;
88 // Check if there is power
89 if (blockUpperState.canProvidePower())
90 return CrossType.NONE;
91 // Check if the collision has a bump
92 if (upperCollisionShape.getEnd(Direction.Axis.Y) > 0)
93 return CrossType.NONE;
94 if (blockUpperState.getBlock().isIn(BlockTags.RAILS))
95 return CrossType.NONE;
96 // Check block state allow spawning (excludes bedrock and barriers automatically)
97 if (!blockBelowState.canEntitySpawn(world, pos.down(), testingEntityType))
98 return CrossType.NONE;
99 if (world.getLightFor(LightType.BLOCK, pos) >= 8)
100 return CrossType.NONE;
101 if (world.getLightFor(LightType.SKY, pos) >= 8)
102 return CrossType.YELLOW;
103 return CrossType.RED;
106 public static void renderCross(World world, BlockPos pos, Color color, PlayerEntity entity) {
107 ActiveRenderInfo info = Minecraft.getInstance().gameRenderer.getActiveRenderInfo();
108 GlStateManager.lineWidth(lineWidth);
109 GlStateManager.depthMask(false);
110 GlStateManager.disableTexture();
111 Tessellator tessellator = Tessellator.getInstance();
112 BufferBuilder buffer = tessellator.getBuffer();
113 double d0 = info.getProjectedView().x;
114 double d1 = info.getProjectedView().y - .005D;
115 VoxelShape upperOutlineShape = world.getBlockState(pos).getShape(world, pos, ISelectionContext.forEntity(entity));
116 if (!upperOutlineShape.isEmpty())
117 d1 -= upperOutlineShape.getEnd(Direction.Axis.Y);
118 double d2 = info.getProjectedView().z;
120 buffer.begin(1, DefaultVertexFormats.POSITION_COLOR);
121 buffer.pos(pos.getX() + .01 - d0, pos.getY() - d1, pos.getZ() + .01 - d2).color(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha()).endVertex();
122 buffer.pos(pos.getX() - .01 + 1 - d0, pos.getY() - d1, pos.getZ() - .01 + 1 - d2).color(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha()).endVertex();
123 buffer.pos(pos.getX() - .01 + 1 - d0, pos.getY() - d1, pos.getZ() + .01 - d2).color(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha()).endVertex();
124 buffer.pos(pos.getX() + .01 - d0, pos.getY() - d1, pos.getZ() - .01 + 1 - d2).color(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha()).endVertex();
126 GlStateManager.depthMask(true);
127 GlStateManager.enableTexture();
130 @SubscribeEvent(receiveCanceled = true)
131 public void handleInput(InputEvent.KeyInputEvent event) {
132 if (enableOverlay.isPressed())
134 if (increaseReach.isPressed()) {
138 saveConfig(configFile);
139 } catch (IOException e) {
142 Minecraft.getInstance().player.sendStatusMessage(new TranslationTextComponent("text.lightoverlay-forge.current_reach", reach), false);
144 if (decreaseReach.isPressed()) {
148 saveConfig(configFile);
149 } catch (IOException e) {
152 Minecraft.getInstance().player.sendStatusMessage(new TranslationTextComponent("text.lightoverlay-forge.current_reach", reach), false);
154 if (increaseLineWidth.isPressed()) {
158 saveConfig(configFile);
159 } catch (IOException e) {
162 Minecraft.getInstance().player.sendStatusMessage(new TranslationTextComponent("text.lightoverlay-forge.current_line_width", FORMAT.format(lineWidth)), false);
164 if (decreaseLineWidth.isPressed()) {
168 saveConfig(configFile);
169 } catch (IOException e) {
172 Minecraft.getInstance().player.sendStatusMessage(new TranslationTextComponent("text.lightoverlay-forge.current_line_width", FORMAT.format(lineWidth)), false);
177 public void renderWorldLast(RenderWorldLastEvent event) {
178 if (LightOverlay.enabled) {
179 Minecraft client = Minecraft.getInstance();
180 ClientPlayerEntity playerEntity = client.player;
181 World world = client.world;
182 GlStateManager.disableTexture();
183 GlStateManager.disableBlend();
184 BlockPos playerPos = new BlockPos(playerEntity.posX, playerEntity.posY, playerEntity.posZ);
185 BlockPos.getAllInBox(playerPos.add(-reach, -reach, -reach), playerPos.add(reach, reach, reach)).forEach(pos -> {
186 Biome biome = world.getBiome(pos);
187 if (biome.getSpawningChance() > 0 && !biome.getSpawns(EntityClassification.MONSTER).isEmpty()) {
188 CrossType type = LightOverlay.getCrossType(pos, world, playerEntity);
189 if (type != CrossType.NONE) {
190 VoxelShape shape = world.getBlockState(pos).getCollisionShape(world, pos);
191 Color color = type == CrossType.RED ? Color.RED : Color.YELLOW;
192 LightOverlay.renderCross(world, pos, color, playerEntity);
196 GlStateManager.enableBlend();
197 GlStateManager.enableTexture();
201 private KeyBinding registerKeybind(ResourceLocation resourceLocation, InputMappings.Type type, int keyCode, String category) {
202 KeyBinding keyBinding = new KeyBinding("key." + resourceLocation.getNamespace() + "." + resourceLocation.getPath(), KeyConflictContext.IN_GAME, KeyModifier.NONE, type, keyCode, category);
203 ClientRegistry.registerKeyBinding(keyBinding);
207 private void loadConfig(File file) {
209 if (!file.exists() || !file.canRead())
211 FileInputStream fis = new FileInputStream(file);
212 Properties properties = new Properties();
213 properties.load(fis);
215 for(Map.Entry<Object, Object> entry : properties.entrySet()) {
216 if (entry.getKey() instanceof String && entry.getValue() instanceof String) {
217 String key = (String) entry.getKey();
218 String value = (String) entry.getValue();
219 if (key.equals("reach")) {
221 reach = Integer.valueOf(value);
222 } catch (NumberFormatException e) {
226 } else if (key.equals("lineWidth")) {
228 lineWidth = Float.valueOf(value);
229 } catch (NumberFormatException e) {
237 } catch (Exception e) {
244 private void saveConfig(File file) throws IOException {
245 FileOutputStream fos = new FileOutputStream(file, false);
246 fos.write(("reach=" + String.valueOf(reach)).getBytes());
247 fos.write("\n".getBytes());
248 fos.write(("lineWidth=" + FORMAT.format(lineWidth)).getBytes());
252 private static enum CrossType {