]> git.lizzy.rs Git - LightOverlay.git/blob - src/main/java/me/shedaniel/lightoverlay/LightOverlayClient.java
f570c942898c5cf742c3e40050bbb5972746dc13
[LightOverlay.git] / src / main / java / me / shedaniel / lightoverlay / LightOverlayClient.java
1 package me.shedaniel.lightoverlay;
2
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.client.event.InputEvent;
30 import net.minecraftforge.client.event.RenderWorldLastEvent;
31 import net.minecraftforge.client.settings.KeyConflictContext;
32 import net.minecraftforge.client.settings.KeyModifier;
33 import net.minecraftforge.common.MinecraftForge;
34 import net.minecraftforge.eventbus.api.SubscribeEvent;
35 import net.minecraftforge.fml.DistExecutor;
36 import net.minecraftforge.fml.client.registry.ClientRegistry;
37
38 import java.io.File;
39 import java.io.FileInputStream;
40 import java.io.FileOutputStream;
41 import java.io.IOException;
42 import java.text.DecimalFormat;
43 import java.util.Properties;
44
45 public class LightOverlayClient {
46     
47     static final DecimalFormat FORMAT = new DecimalFormat("#.#");
48     private static final String KEYBIND_CATEGORY = "key.lightoverlay-forge.category";
49     private static final ResourceLocation ENABLE_OVERLAY_KEYBIND = new ResourceLocation("lightoverlay-forge", "enable_overlay");
50     private static final ResourceLocation INCREASE_REACH_KEYBIND = new ResourceLocation("lightoverlay-forge", "increase_reach");
51     private static final ResourceLocation DECREASE_REACH_KEYBIND = new ResourceLocation("lightoverlay-forge", "decrease_reach");
52     private static final ResourceLocation INCREASE_LINE_WIDTH_KEYBIND = new ResourceLocation("lightoverlay-forge", "increase_line_width");
53     private static final ResourceLocation DECREASE_LINE_WIDTH_KEYBIND = new ResourceLocation("lightoverlay-forge", "decrease_line_width");
54     static int reach = 7;
55     static EntityType<Entity> testingEntityType;
56     static float lineWidth = 1.0F;
57     static int yellowColor = 0xFFFF00, redColor = 0xFF0000;
58     static File configFile = new File(new File(Minecraft.getInstance().gameDir, "config"), "lightoverlay.properties");
59     private static KeyBinding enableOverlay, increaseReach, decreaseReach, increaseLineWidth, decreaseLineWidth;
60     private static boolean enabled = false;
61     
62     public static void register() {
63         // Load Config
64         loadConfig(configFile);
65         
66         // Setup
67         testingEntityType = EntityType.Builder.create(EntityClassification.MONSTER).size(0f, 0f).disableSerialization().build(null);
68         enableOverlay = registerKeybind(ENABLE_OVERLAY_KEYBIND, InputMappings.Type.KEYSYM, 296, KEYBIND_CATEGORY);
69         increaseReach = registerKeybind(INCREASE_REACH_KEYBIND, InputMappings.Type.KEYSYM, -1, KEYBIND_CATEGORY);
70         decreaseReach = registerKeybind(DECREASE_REACH_KEYBIND, InputMappings.Type.KEYSYM, -1, KEYBIND_CATEGORY);
71         increaseLineWidth = registerKeybind(INCREASE_LINE_WIDTH_KEYBIND, InputMappings.Type.KEYSYM, -1, KEYBIND_CATEGORY);
72         decreaseLineWidth = registerKeybind(DECREASE_LINE_WIDTH_KEYBIND, InputMappings.Type.KEYSYM, -1, KEYBIND_CATEGORY);
73         MinecraftForge.EVENT_BUS.register(LightOverlayClient.class);
74         
75         try {
76             DistExecutor.runWhenOn(Dist.CLIENT, () -> () -> LightOverlayCloth.register());
77         } catch (Exception e) {
78             e.printStackTrace();
79         }
80     }
81     
82     public static CrossType getCrossType(BlockPos pos, World world, PlayerEntity playerEntity) {
83         BlockState blockBelowState = world.getBlockState(pos.down());
84         BlockState blockUpperState = world.getBlockState(pos);
85         VoxelShape upperCollisionShape = blockUpperState.getCollisionShape(world, pos, ISelectionContext.forEntity(playerEntity));
86         if (!blockUpperState.getFluidState().isEmpty())
87             return CrossType.NONE;
88         /* WorldEntitySpawner.func_222266_a */
89         // Check if the outline is full
90         if (Block.doesSideFillSquare(upperCollisionShape, Direction.UP))
91             return CrossType.NONE;
92         // Check if there is power
93         if (blockUpperState.canProvidePower())
94             return CrossType.NONE;
95         // Check if the collision has a bump
96         if (upperCollisionShape.getEnd(Direction.Axis.Y) > 0)
97             return CrossType.NONE;
98         if (blockUpperState.getBlock().isIn(BlockTags.RAILS))
99             return CrossType.NONE;
100         // Check block state allow spawning (excludes bedrock and barriers automatically)
101         if (!blockBelowState.canEntitySpawn(world, pos.down(), testingEntityType))
102             return CrossType.NONE;
103         if (world.getLightFor(LightType.BLOCK, pos) >= 8)
104             return CrossType.NONE;
105         if (world.getLightFor(LightType.SKY, pos) >= 8)
106             return CrossType.YELLOW;
107         return CrossType.RED;
108     }
109     
110     public static void renderCross(World world, BlockPos pos, int color, PlayerEntity entity) {
111         ActiveRenderInfo info = Minecraft.getInstance().gameRenderer.getActiveRenderInfo();
112         GlStateManager.lineWidth(lineWidth);
113         GlStateManager.depthMask(false);
114         GlStateManager.disableTexture();
115         Tessellator tessellator = Tessellator.getInstance();
116         BufferBuilder buffer = tessellator.getBuffer();
117         double d0 = info.getProjectedView().x;
118         double d1 = info.getProjectedView().y - .005D;
119         VoxelShape upperOutlineShape = world.getBlockState(pos).getShape(world, pos, ISelectionContext.forEntity(entity));
120         if (!upperOutlineShape.isEmpty())
121             d1 -= upperOutlineShape.getEnd(Direction.Axis.Y);
122         double d2 = info.getProjectedView().z;
123         
124         buffer.begin(1, DefaultVertexFormats.POSITION_COLOR);
125         int red = (color >> 16) & 255;
126         int green = (color >> 8) & 255;
127         int blue = color & 255;
128         buffer.pos(pos.getX() + .01 - d0, pos.getY() - d1, pos.getZ() + .01 - d2).color(red, green, blue, 255).endVertex();
129         buffer.pos(pos.getX() - .01 + 1 - d0, pos.getY() - d1, pos.getZ() - .01 + 1 - d2).color(red, green, blue, 255).endVertex();
130         buffer.pos(pos.getX() - .01 + 1 - d0, pos.getY() - d1, pos.getZ() + .01 - d2).color(red, green, blue, 255).endVertex();
131         buffer.pos(pos.getX() + .01 - d0, pos.getY() - d1, pos.getZ() - .01 + 1 - d2).color(red, green, blue, 255).endVertex();
132         tessellator.draw();
133         GlStateManager.depthMask(true);
134         GlStateManager.enableTexture();
135     }
136     
137     @SubscribeEvent(receiveCanceled = true)
138     public static void handleInput(InputEvent.KeyInputEvent event) {
139         if (enableOverlay.isPressed())
140             enabled = !enabled;
141         if (increaseReach.isPressed()) {
142             if (reach < 50)
143                 reach++;
144             try {
145                 saveConfig(configFile);
146             } catch (IOException e) {
147                 e.printStackTrace();
148             }
149             Minecraft.getInstance().player.sendStatusMessage(new TranslationTextComponent("text.lightoverlay-forge.current_reach", reach), false);
150         }
151         if (decreaseReach.isPressed()) {
152             if (reach > 1)
153                 reach--;
154             try {
155                 saveConfig(configFile);
156             } catch (IOException e) {
157                 e.printStackTrace();
158             }
159             Minecraft.getInstance().player.sendStatusMessage(new TranslationTextComponent("text.lightoverlay-forge.current_reach", reach), false);
160         }
161         if (increaseLineWidth.isPressed()) {
162             if (lineWidth < 7)
163                 lineWidth += 0.1f;
164             try {
165                 saveConfig(configFile);
166             } catch (IOException e) {
167                 e.printStackTrace();
168             }
169             Minecraft.getInstance().player.sendStatusMessage(new TranslationTextComponent("text.lightoverlay-forge.current_line_width", FORMAT.format(lineWidth)), false);
170         }
171         if (decreaseLineWidth.isPressed()) {
172             if (lineWidth > 1)
173                 lineWidth -= 0.1F;
174             try {
175                 saveConfig(configFile);
176             } catch (IOException e) {
177                 e.printStackTrace();
178             }
179             Minecraft.getInstance().player.sendStatusMessage(new TranslationTextComponent("text.lightoverlay-forge.current_line_width", FORMAT.format(lineWidth)), false);
180         }
181     }
182     
183     @SubscribeEvent
184     public static void renderWorldLast(RenderWorldLastEvent event) {
185         if (LightOverlayClient.enabled) {
186             Minecraft client = Minecraft.getInstance();
187             ClientPlayerEntity playerEntity = client.player;
188             World world = client.world;
189             GlStateManager.disableTexture();
190             GlStateManager.disableBlend();
191             BlockPos playerPos = new BlockPos(playerEntity.posX, playerEntity.posY, playerEntity.posZ);
192             BlockPos.getAllInBox(playerPos.add(-reach, -reach, -reach), playerPos.add(reach, reach, reach)).forEach(pos -> {
193                 Biome biome = world.getBiome(pos);
194                 if (biome.getSpawningChance() > 0 && !biome.getSpawns(EntityClassification.MONSTER).isEmpty()) {
195                     CrossType type = LightOverlayClient.getCrossType(pos, world, playerEntity);
196                     if (type != CrossType.NONE) {
197                         VoxelShape shape = world.getBlockState(pos).getCollisionShape(world, pos);
198                         int color = type == CrossType.RED ? redColor : yellowColor;
199                         LightOverlayClient.renderCross(world, pos, color, playerEntity);
200                     }
201                 }
202             });
203             GlStateManager.enableBlend();
204             GlStateManager.enableTexture();
205         }
206     }
207     
208     private static KeyBinding registerKeybind(ResourceLocation resourceLocation, InputMappings.Type type, int keyCode, String category) {
209         KeyBinding keyBinding = new KeyBinding("key." + resourceLocation.getNamespace() + "." + resourceLocation.getPath(), KeyConflictContext.IN_GAME, KeyModifier.NONE, type, keyCode, category);
210         ClientRegistry.registerKeyBinding(keyBinding);
211         return keyBinding;
212     }
213     
214     static void loadConfig(File file) {
215         try {
216             redColor = 0xFF0000;
217             yellowColor = 0xFFFF00;
218             if (!file.exists() || !file.canRead())
219                 saveConfig(file);
220             FileInputStream fis = new FileInputStream(file);
221             Properties properties = new Properties();
222             properties.load(fis);
223             fis.close();
224             reach = Integer.parseInt((String) properties.computeIfAbsent("reach", a -> "7"));
225             lineWidth = Float.valueOf((String) properties.computeIfAbsent("lineWidth", a -> "1"));
226             {
227                 int r, g, b;
228                 r = Integer.parseInt((String) properties.computeIfAbsent("yellowColorRed", a -> "255"));
229                 g = Integer.parseInt((String) properties.computeIfAbsent("yellowColorGreen", a -> "255"));
230                 b = Integer.parseInt((String) properties.computeIfAbsent("yellowColorBlue", a -> "0"));
231                 yellowColor = (r << 16) + (g << 8) + b;
232             }
233             {
234                 int r, g, b;
235                 r = Integer.parseInt((String) properties.computeIfAbsent("redColorRed", a -> "255"));
236                 g = Integer.parseInt((String) properties.computeIfAbsent("redColorGreen", a -> "0"));
237                 b = Integer.parseInt((String) properties.computeIfAbsent("redColorBlue", a -> "0"));
238                 redColor = (r << 16) + (g << 8) + b;
239             }
240             saveConfig(file);
241         } catch (Exception e) {
242             e.printStackTrace();
243             reach = 7;
244             lineWidth = 1.0F;
245             redColor = 0xFF0000;
246             yellowColor = 0xFFFF00;
247             try {
248                 saveConfig(file);
249             } catch (IOException ex) {
250                 ex.printStackTrace();
251             }
252         }
253     }
254     
255     static void saveConfig(File file) throws IOException {
256         FileOutputStream fos = new FileOutputStream(file, false);
257         fos.write("# Light Overlay Config".getBytes());
258         fos.write("\n".getBytes());
259         fos.write(("reach=" + reach).getBytes());
260         fos.write("\n".getBytes());
261         fos.write(("lineWidth=" + FORMAT.format(lineWidth)).getBytes());
262         fos.write("\n".getBytes());
263         fos.write(("yellowColorRed=" + ((yellowColor >> 16) & 255)).getBytes());
264         fos.write("\n".getBytes());
265         fos.write(("yellowColorGreen=" + ((yellowColor >> 8) & 255)).getBytes());
266         fos.write("\n".getBytes());
267         fos.write(("yellowColorBlue=" + (yellowColor & 255)).getBytes());
268         fos.write("\n".getBytes());
269         fos.write(("redColorRed=" + ((redColor >> 16) & 255)).getBytes());
270         fos.write("\n".getBytes());
271         fos.write(("redColorGreen=" + ((redColor >> 8) & 255)).getBytes());
272         fos.write("\n".getBytes());
273         fos.write(("redColorBlue=" + (redColor & 255)).getBytes());
274         fos.close();
275     }
276     
277     private static enum CrossType {
278         YELLOW,
279         RED,
280         NONE
281     }
282     
283 }