Advertisement
Gamebuster

Untitled

May 14th, 2019
847
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 44.85 KB | None | 0 0
  1. /*
  2. * Minecraft Forge
  3. * Copyright (c) 2016-2019.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation version 2.1
  8. * of the License.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19.  
  20. package net.minecraftforge.client;
  21.  
  22. import static net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType.BOSSINFO;
  23. import static net.minecraftforge.fml.VersionChecker.Status.BETA;
  24. import static net.minecraftforge.fml.VersionChecker.Status.BETA_OUTDATED;
  25. import static org.lwjgl.opengl.GL11.*;
  26. import static org.lwjgl.opengl.GL20.*;
  27.  
  28. import java.io.File;
  29. import java.lang.reflect.Field;
  30. import java.nio.ByteBuffer;
  31. import java.nio.FloatBuffer;
  32. import java.util.ArrayList;
  33. import java.util.List;
  34. import java.util.Map;
  35. import java.util.Optional;
  36. import java.util.Random;
  37. import java.util.concurrent.FutureTask;
  38.  
  39. import javax.annotation.Nonnull;
  40. import javax.vecmath.Matrix3f;
  41. import javax.vecmath.Matrix4f;
  42. import javax.vecmath.Vector3f;
  43. import javax.vecmath.Vector4f;
  44.  
  45. import net.minecraft.client.MouseHelper;
  46. import net.minecraftforge.client.event.RecipesUpdatedEvent;
  47. import org.apache.commons.lang3.tuple.Pair;
  48. import org.apache.logging.log4j.LogManager;
  49. import org.apache.logging.log4j.Logger;
  50. import org.apache.logging.log4j.core.async.ThreadNameCachingStrategy;
  51. import org.apache.logging.log4j.core.impl.ReusableLogEventFactory;
  52. import org.lwjgl.BufferUtils;
  53. import org.lwjgl.opengl.GL11;
  54.  
  55. import net.minecraft.block.state.IBlockState;
  56. import net.minecraft.client.GameSettings;
  57. import net.minecraft.client.MainWindow;
  58. import net.minecraft.client.Minecraft;
  59. import net.minecraft.client.audio.ISound;
  60. import net.minecraft.client.audio.SoundManager;
  61. import net.minecraft.client.gui.BossInfoClient;
  62. import net.minecraft.client.gui.FontRenderer;
  63. import net.minecraft.client.gui.GuiGameOver;
  64. import net.minecraft.client.gui.GuiMainMenu;
  65. import net.minecraft.client.gui.GuiScreen;
  66. import net.minecraft.client.gui.GuiScreenWorking;
  67. import net.minecraft.client.renderer.BufferBuilder;
  68. import net.minecraft.client.renderer.GameRenderer;
  69. import net.minecraft.client.renderer.FogRenderer;
  70. import net.minecraft.client.renderer.GlStateManager;
  71. import net.minecraft.client.renderer.ItemRenderer;
  72. import net.minecraft.client.renderer.OpenGlHelper;
  73. import net.minecraft.client.renderer.RenderHelper;
  74. import net.minecraft.client.renderer.Tessellator;
  75. import net.minecraft.client.renderer.WorldRenderer;
  76. import net.minecraft.client.renderer.model.BakedQuad;
  77. import net.minecraft.client.renderer.model.BlockFaceUV;
  78. import net.minecraft.client.renderer.model.IBakedModel;
  79. import net.minecraft.client.renderer.model.ItemCameraTransforms;
  80. import net.minecraft.client.renderer.model.ItemTransformVec3f;
  81. import net.minecraft.client.renderer.model.ModelManager;
  82. import net.minecraft.client.renderer.model.ModelResourceLocation;
  83. import net.minecraft.client.renderer.model.ModelRotation;
  84. import net.minecraft.client.renderer.model.SimpleBakedModel;
  85. import net.minecraft.client.renderer.color.BlockColors;
  86. import net.minecraft.client.renderer.color.ItemColors;
  87. import net.minecraft.client.renderer.entity.model.ModelBiped;
  88. import net.minecraft.client.renderer.texture.NativeImage;
  89. import net.minecraft.client.renderer.texture.TextureAtlasSprite;
  90. import net.minecraft.client.renderer.texture.TextureMap;
  91. import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
  92. import net.minecraft.client.renderer.vertex.VertexFormat;
  93. import net.minecraft.client.renderer.vertex.VertexFormatElement;
  94. import net.minecraft.client.renderer.vertex.VertexFormatElement.EnumUsage;
  95. import net.minecraft.client.resources.I18n;
  96. import net.minecraft.entity.Entity;
  97. import net.minecraft.entity.EntityLivingBase;
  98. import net.minecraft.entity.player.EntityPlayer;
  99. import net.minecraft.entity.player.EntityPlayerMP;
  100. import net.minecraft.fluid.IFluidState;
  101. import net.minecraft.inventory.EntityEquipmentSlot;
  102. import net.minecraft.item.ItemStack;
  103. import net.minecraft.nbt.NBTTagCompound;
  104. import net.minecraft.server.integrated.IntegratedServer;
  105. import net.minecraft.util.BlockRenderLayer;
  106. import net.minecraft.util.EnumFacing;
  107. import net.minecraft.util.EnumHand;
  108. import net.minecraft.util.MovementInput;
  109. import net.minecraft.util.ResourceLocation;
  110. import net.minecraft.util.math.BlockPos;
  111. import net.minecraft.util.math.MathHelper;
  112. import net.minecraft.util.math.RayTraceResult;
  113. import net.minecraft.util.registry.IRegistry;
  114. import net.minecraft.util.text.TextFormatting;
  115. import net.minecraft.world.GameType;
  116. import net.minecraft.world.IWorldReader;
  117. import net.minecraft.world.World;
  118. import net.minecraft.world.biome.Biome;
  119. import net.minecraft.world.dimension.DimensionType;
  120. import net.minecraft.world.storage.ISaveFormat;
  121. import net.minecraft.world.storage.WorldInfo;
  122. import net.minecraft.world.storage.WorldSummary;
  123. import net.minecraftforge.client.event.ColorHandlerEvent;
  124. import net.minecraftforge.client.event.DrawBlockHighlightEvent;
  125. import net.minecraftforge.client.event.EntityViewRenderEvent;
  126. import net.minecraftforge.client.event.FOVUpdateEvent;
  127. import net.minecraftforge.client.event.GuiScreenEvent;
  128. import net.minecraftforge.client.event.InputUpdateEvent;
  129. import net.minecraftforge.client.event.ModelBakeEvent;
  130. import net.minecraftforge.client.event.RenderGameOverlayEvent;
  131. import net.minecraftforge.client.event.RenderHandEvent;
  132. import net.minecraftforge.client.event.RenderSpecificHandEvent;
  133. import net.minecraftforge.client.event.RenderWorldLastEvent;
  134. import net.minecraftforge.client.event.ScreenshotEvent;
  135. import net.minecraftforge.client.event.TextureStitchEvent;
  136. import net.minecraftforge.client.event.sound.PlaySoundEvent;
  137. import net.minecraftforge.client.model.ModelDynBucket;
  138. import net.minecraftforge.client.model.ModelLoader;
  139. import net.minecraftforge.client.model.animation.Animation;
  140. import net.minecraftforge.client.model.pipeline.QuadGatheringTransformer;
  141. import net.minecraftforge.client.event.InputEvent;
  142. import net.minecraftforge.common.ForgeMod;
  143. import net.minecraftforge.common.MinecraftForge;
  144. import net.minecraftforge.common.model.IModelPart;
  145. import net.minecraftforge.common.model.ITransformation;
  146. import net.minecraftforge.common.model.TRSRTransformation;
  147. import net.minecraftforge.fml.VersionChecker;
  148. import net.minecraftforge.fml.client.registry.ClientRegistry;
  149. import net.minecraftforge.fml.server.ServerLifecycleHooks;
  150. import net.minecraftforge.eventbus.api.Event;
  151. import net.minecraftforge.resource.ReloadRequirements;
  152. import net.minecraftforge.resource.SelectiveReloadStateHandler;
  153. import net.minecraftforge.resource.VanillaResourceType;
  154. import net.minecraftforge.versions.forge.ForgeVersion;
  155.  
  156. public class ForgeHooksClient
  157. {
  158. private static final Logger LOGGER = LogManager.getLogger();
  159.  
  160. //private static final ResourceLocation ITEM_GLINT = new ResourceLocation("textures/misc/enchanted_item_glint.png");
  161.  
  162. public static String getArmorTexture(Entity entity, ItemStack armor, String _default, EntityEquipmentSlot slot, String type)
  163. {
  164. String result = armor.getItem().getArmorTexture(armor, entity, slot, type);
  165. return result != null ? result : _default;
  166. }
  167.  
  168. public static boolean onDrawBlockHighlight(WorldRenderer context, EntityPlayer player, RayTraceResult target, int subID, float partialTicks)
  169. {
  170. return MinecraftForge.EVENT_BUS.post(new DrawBlockHighlightEvent(context, player, target, subID, partialTicks));
  171. }
  172.  
  173. public static void dispatchRenderLast(WorldRenderer context, float partialTicks)
  174. {
  175. MinecraftForge.EVENT_BUS.post(new RenderWorldLastEvent(context, partialTicks));
  176. }
  177.  
  178. public static boolean renderFirstPersonHand(WorldRenderer context, float partialTicks)
  179. {
  180. return MinecraftForge.EVENT_BUS.post(new RenderHandEvent(context, partialTicks));
  181. }
  182.  
  183. public static boolean renderSpecificFirstPersonHand(EnumHand hand, float partialTicks, float interpPitch, float swingProgress, float equipProgress, ItemStack stack)
  184. {
  185. return MinecraftForge.EVENT_BUS.post(new RenderSpecificHandEvent(hand, partialTicks, interpPitch, swingProgress, equipProgress, stack));
  186. }
  187.  
  188. public static void onTextureStitchedPre(TextureMap map)
  189. {
  190. MinecraftForge.EVENT_BUS.post(new TextureStitchEvent.Pre(map));
  191. // ModelLoader.White.INSTANCE.register(map); // TODO Custom TAS
  192. ModelDynBucket.LoaderDynBucket.INSTANCE.register(map);
  193. }
  194.  
  195. public static void onTextureStitchedPost(TextureMap map)
  196. {
  197. MinecraftForge.EVENT_BUS.post(new TextureStitchEvent.Post(map));
  198. }
  199.  
  200. public static void onBlockColorsInit(BlockColors blockColors)
  201. {
  202. MinecraftForge.EVENT_BUS.post(new ColorHandlerEvent.Block(blockColors));
  203. }
  204.  
  205. public static void onItemColorsInit(ItemColors itemColors, BlockColors blockColors)
  206. {
  207. MinecraftForge.EVENT_BUS.post(new ColorHandlerEvent.Item(itemColors, blockColors));
  208. }
  209.  
  210. static int renderPass = -1;
  211. public static void setRenderPass(int pass)
  212. {
  213. renderPass = pass;
  214. }
  215.  
  216. static final ThreadLocal<BlockRenderLayer> renderLayer = new ThreadLocal<BlockRenderLayer>();
  217.  
  218. public static void setRenderLayer(BlockRenderLayer layer)
  219. {
  220. renderLayer.set(layer);
  221. }
  222.  
  223. public static ModelBiped getArmorModel(EntityLivingBase entityLiving, ItemStack itemStack, EntityEquipmentSlot slot, ModelBiped _default)
  224. {
  225. ModelBiped model = itemStack.getItem().getArmorModel(entityLiving, itemStack, slot, _default);
  226. return model == null ? _default : model;
  227. }
  228.  
  229. //This properly moves the domain, if provided, to the front of the string before concatenating
  230. public static String fixDomain(String base, String complex)
  231. {
  232. int idx = complex.indexOf(':');
  233. if (idx == -1)
  234. {
  235. return base + complex;
  236. }
  237.  
  238. String name = complex.substring(idx + 1, complex.length());
  239. if (idx > 1)
  240. {
  241. String domain = complex.substring(0, idx);
  242. return domain + ':' + base + name;
  243. }
  244. else
  245. {
  246. return base + name;
  247. }
  248. }
  249. /* TODO mouse input
  250. public static boolean postMouseEvent()
  251. {
  252. return MinecraftForge.EVENT_BUS.post(new MouseEvent());
  253. }
  254. */
  255. public static float getOffsetFOV(EntityPlayer entity, float fov)
  256. {
  257. FOVUpdateEvent fovUpdateEvent = new FOVUpdateEvent(entity, fov);
  258. MinecraftForge.EVENT_BUS.post(fovUpdateEvent);
  259. return fovUpdateEvent.getNewfov();
  260. }
  261.  
  262. public static double getFOVModifier(GameRenderer renderer, Entity entity, IBlockState blockState, IFluidState fluidState, double renderPartialTicks, double fov) {
  263. EntityViewRenderEvent.FOVModifier event = new EntityViewRenderEvent.FOVModifier(renderer, entity, blockState, fluidState, renderPartialTicks, fov);
  264. MinecraftForge.EVENT_BUS.post(event);
  265. return event.getFOV();
  266. }
  267.  
  268. private static int skyX, skyZ;
  269.  
  270. private static boolean skyInit;
  271. private static int skyRGBMultiplier;
  272.  
  273. public static int getSkyBlendColour(World world, BlockPos center)
  274. {
  275. if (center.getX() == skyX && center.getZ() == skyZ && skyInit)
  276. {
  277. return skyRGBMultiplier;
  278. }
  279. skyInit = true;
  280.  
  281. GameSettings settings = Minecraft.getInstance().gameSettings;
  282. int[] ranges = ForgeMod.blendRanges;
  283. int distance = 0;
  284. if (settings.fancyGraphics && ranges.length > 0)
  285. {
  286. distance = ranges[MathHelper.clamp(settings.renderDistanceChunks, 0, ranges.length-1)];
  287. }
  288.  
  289. int r = 0;
  290. int g = 0;
  291. int b = 0;
  292.  
  293. int divider = 0;
  294. for (int x = -distance; x <= distance; ++x)
  295. {
  296. for (int z = -distance; z <= distance; ++z)
  297. {
  298. BlockPos pos = center.add(x, 0, z);
  299. Biome biome = world.getBiome(pos);
  300. int colour = biome.getSkyColorByTemp(biome.getTemperature(pos));
  301. r += (colour & 0xFF0000) >> 16;
  302. g += (colour & 0x00FF00) >> 8;
  303. b += colour & 0x0000FF;
  304. divider++;
  305. }
  306. }
  307.  
  308. int multiplier = (r / divider & 255) << 16 | (g / divider & 255) << 8 | b / divider & 255;
  309.  
  310. skyX = center.getX();
  311. skyZ = center.getZ();
  312. skyRGBMultiplier = multiplier;
  313. return skyRGBMultiplier;
  314. }
  315. /**
  316. * Initialization of Forge Renderers.
  317. */
  318. static
  319. {
  320. //FluidRegistry.renderIdFluid = RenderingRegistry.getNextAvailableRenderId();
  321. //RenderingRegistry.registerBlockHandler(RenderBlockFluid.instance);
  322. }
  323.  
  324. public static void renderMainMenu(GuiMainMenu gui, FontRenderer font, int width, int height)
  325. {
  326. VersionChecker.Status status = ForgeVersion.getStatus();
  327. if (status == BETA || status == BETA_OUTDATED)
  328. {
  329. // render a warning at the top of the screen,
  330. String line = I18n.format("forge.update.beta.1", TextFormatting.RED, TextFormatting.RESET);
  331. gui.drawString(font, line, (width - font.getStringWidth(line)) / 2, 4 + (0 * (font.FONT_HEIGHT + 1)), -1);
  332. line = I18n.format("forge.update.beta.2");
  333. gui.drawString(font, line, (width - font.getStringWidth(line)) / 2, 4 + (1 * (font.FONT_HEIGHT + 1)), -1);
  334. }
  335.  
  336. String line = null;
  337. switch(status)
  338. {
  339. //case FAILED: line = " Version check failed"; break;
  340. //case UP_TO_DATE: line = "Forge up to date"}; break;
  341. //case AHEAD: line = "Using non-recommended Forge build, issues may arise."}; break;
  342. case OUTDATED:
  343. case BETA_OUTDATED: line = I18n.format("forge.update.newversion", ForgeVersion.getTarget()); break;
  344. default: break;
  345. }
  346.  
  347. if (line != null)
  348. {
  349. // if we have a line, render it in the bottom right, above Mojang's copyright line
  350. gui.drawString(font, line, width - font.getStringWidth(line) - 2, height - (2 * (font.FONT_HEIGHT + 1)), -1);
  351. }
  352. }
  353.  
  354. public static ISound playSound(SoundManager manager, ISound sound)
  355. {
  356. PlaySoundEvent e = new PlaySoundEvent(manager, sound);
  357. MinecraftForge.EVENT_BUS.post(e);
  358. return e.getResultSound();
  359. }
  360.  
  361. //static RenderBlocks VertexBufferRB;
  362. static int worldRenderPass;
  363.  
  364. public static int getWorldRenderPass()
  365. {
  366. return worldRenderPass;
  367. }
  368.  
  369. public static void drawScreen(GuiScreen screen, int mouseX, int mouseY, float partialTicks)
  370. {
  371. if (!MinecraftForge.EVENT_BUS.post(new GuiScreenEvent.DrawScreenEvent.Pre(screen, mouseX, mouseY, partialTicks)))
  372. screen.render(mouseX, mouseY, partialTicks);
  373. MinecraftForge.EVENT_BUS.post(new GuiScreenEvent.DrawScreenEvent.Post(screen, mouseX, mouseY, partialTicks));
  374. }
  375.  
  376. public static float getFogDensity(FogRenderer fogRenderer, GameRenderer renderer, Entity entity, IBlockState state, IFluidState fluidState, float partial, float density)
  377. {
  378. EntityViewRenderEvent.FogDensity event = new EntityViewRenderEvent.FogDensity(fogRenderer, renderer, entity, state, fluidState, partial, density);
  379. if (MinecraftForge.EVENT_BUS.post(event)) return event.getDensity();
  380. return -1;
  381. }
  382.  
  383. public static void onFogRender(FogRenderer fogRenderer, GameRenderer renderer, Entity entity, IBlockState state, IFluidState fluidState, float partial, int mode, float distance)
  384. {
  385. MinecraftForge.EVENT_BUS.post(new EntityViewRenderEvent.RenderFogEvent(fogRenderer, renderer, entity, state, fluidState, partial, mode, distance));
  386. }
  387.  
  388. public static void onModelBake(ModelManager modelManager, Map<ModelResourceLocation, IBakedModel> modelRegistry, ModelLoader modelLoader)
  389. {
  390. MinecraftForge.EVENT_BUS.post(new ModelBakeEvent(modelManager, modelRegistry, modelLoader));
  391. modelLoader.onPostBakeEvent(modelRegistry);
  392. }
  393.  
  394. @SuppressWarnings("deprecation")
  395. public static Matrix4f getMatrix(net.minecraft.client.renderer.model.ItemTransformVec3f transform)
  396. {
  397. javax.vecmath.Matrix4f m = new javax.vecmath.Matrix4f(), t = new javax.vecmath.Matrix4f();
  398. m.setIdentity();
  399. m.setTranslation(TRSRTransformation.toVecmath(transform.translation));
  400. t.setIdentity();
  401. t.rotY(transform.rotation.getY());
  402. m.mul(t);
  403. t.setIdentity();
  404. t.rotX(transform.rotation.getX());
  405. m.mul(t);
  406. t.setIdentity();
  407. t.rotZ(transform.rotation.getZ());
  408. m.mul(t);
  409. t.setIdentity();
  410. t.m00 = transform.scale.getX();
  411. t.m11 = transform.scale.getY();
  412. t.m22 = transform.scale.getZ();
  413. m.mul(t);
  414. return m;
  415. }
  416.  
  417. private static final Matrix4f flipX;
  418. static {
  419. flipX = new Matrix4f();
  420. flipX.setIdentity();
  421. flipX.m00 = -1;
  422. }
  423.  
  424. public static IBakedModel handleCameraTransforms(IBakedModel model, ItemCameraTransforms.TransformType cameraTransformType, boolean leftHandHackery)
  425. {
  426. Pair<? extends IBakedModel, Matrix4f> pair = model.handlePerspective(cameraTransformType);
  427.  
  428. if (pair.getRight() != null)
  429. {
  430. Matrix4f matrix = new Matrix4f(pair.getRight());
  431. if (leftHandHackery)
  432. {
  433. matrix.mul(flipX, matrix);
  434. matrix.mul(matrix, flipX);
  435. }
  436. multiplyCurrentGlMatrix(matrix);
  437. }
  438. return pair.getLeft();
  439. }
  440.  
  441. private static final FloatBuffer matrixBuf = BufferUtils.createFloatBuffer(16);
  442.  
  443. public static void multiplyCurrentGlMatrix(Matrix4f matrix)
  444. {
  445. matrixBuf.clear();
  446. float[] t = new float[4];
  447. for(int i = 0; i < 4; i++)
  448. {
  449. matrix.getColumn(i, t);
  450. matrixBuf.put(t);
  451. }
  452. matrixBuf.flip();
  453. glMultMatrixf(matrixBuf);
  454. }
  455.  
  456. // moved and expanded from WorldVertexBufferUploader.draw
  457.  
  458. public static void preDraw(EnumUsage attrType, VertexFormat format, int element, int stride, ByteBuffer buffer)
  459. {
  460. VertexFormatElement attr = format.getElement(element);
  461. int count = attr.getElementCount();
  462. int constant = attr.getType().getGlConstant();
  463. buffer.position(format.getOffset(element));
  464. switch(attrType)
  465. {
  466. case POSITION:
  467. glVertexPointer(count, constant, stride, buffer);
  468. glEnableClientState(GL_VERTEX_ARRAY);
  469. break;
  470. case NORMAL:
  471. if(count != 3)
  472. {
  473. throw new IllegalArgumentException("Normal attribute should have the size 3: " + attr);
  474. }
  475. glNormalPointer(constant, stride, buffer);
  476. glEnableClientState(GL_NORMAL_ARRAY);
  477. break;
  478. case COLOR:
  479. glColorPointer(count, constant, stride, buffer);
  480. glEnableClientState(GL_COLOR_ARRAY);
  481. break;
  482. case UV:
  483. OpenGlHelper.glClientActiveTexture(OpenGlHelper.GL_TEXTURE0 + attr.getIndex());
  484. glTexCoordPointer(count, constant, stride, buffer);
  485. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  486. OpenGlHelper.glClientActiveTexture(OpenGlHelper.GL_TEXTURE0);
  487. break;
  488. case PADDING:
  489. break;
  490. case GENERIC:
  491. glEnableVertexAttribArray(attr.getIndex());
  492. glVertexAttribPointer(attr.getIndex(), count, constant, false, stride, buffer);
  493. break;
  494. default:
  495. LOGGER.fatal("Unimplemented vanilla attribute upload: {}", attrType.getDisplayName());
  496. }
  497. }
  498.  
  499. public static void postDraw(EnumUsage attrType, VertexFormat format, int element, int stride, ByteBuffer buffer)
  500. {
  501. VertexFormatElement attr = format.getElement(element);
  502. switch(attrType)
  503. {
  504. case POSITION:
  505. glDisableClientState(GL_VERTEX_ARRAY);
  506. break;
  507. case NORMAL:
  508. glDisableClientState(GL_NORMAL_ARRAY);
  509. break;
  510. case COLOR:
  511. glDisableClientState(GL_COLOR_ARRAY);
  512. // is this really needed?
  513. GlStateManager.resetColor();
  514. break;
  515. case UV:
  516. OpenGlHelper.glClientActiveTexture(OpenGlHelper.GL_TEXTURE0 + attr.getIndex());
  517. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  518. OpenGlHelper.glClientActiveTexture(OpenGlHelper.GL_TEXTURE0);
  519. break;
  520. case PADDING:
  521. break;
  522. case GENERIC:
  523. glDisableVertexAttribArray(attr.getIndex());
  524. break;
  525. default:
  526. LOGGER.fatal("Unimplemented vanilla attribute upload: {}", attrType.getDisplayName());
  527. }
  528. }
  529.  
  530. public static void transform(net.minecraft.client.renderer.Vector3f vec, Matrix4f m)
  531. {
  532. Vector4f tmp = new Vector4f(vec.getX(), vec.getY(), vec.getZ(), 1f);
  533. m.transform(tmp);
  534. if(Math.abs(tmp.w - 1f) > 1e-5) tmp.scale(1f / tmp.w);
  535. vec.set(tmp.x, tmp.y, tmp.z);
  536. }
  537.  
  538. public static Matrix4f getMatrix(ModelRotation modelRotation)
  539. {
  540. Matrix4f ret = new Matrix4f(TRSRTransformation.toVecmath(modelRotation.getMatrix()), new Vector3f(), 1), tmp = new Matrix4f();
  541. tmp.setIdentity();
  542. tmp.m03 = tmp.m13 = tmp.m23 = .5f;
  543. ret.mul(tmp, ret);
  544. tmp.invert();
  545. //tmp.m03 = tmp.m13 = tmp.m23 = -.5f;
  546. ret.mul(tmp);
  547. return ret;
  548. }
  549.  
  550. public static void putQuadColor(BufferBuilder renderer, BakedQuad quad, int color)
  551. {
  552. float cb = color & 0xFF;
  553. float cg = (color >>> 8) & 0xFF;
  554. float cr = (color >>> 16) & 0xFF;
  555. float ca = (color >>> 24) & 0xFF;
  556. VertexFormat format = quad.getFormat();
  557. int size = format.getIntegerSize();
  558. int offset = format.getColorOffset() / 4; // assumes that color is aligned
  559. boolean hasColor = format.hasColor();
  560. for(int i = 0; i < 4; i++)
  561. {
  562. int vc = hasColor ? quad.getVertexData()[offset + size * i] : 0xFFFFFFFF;
  563. float vcr = vc & 0xFF;
  564. float vcg = (vc >>> 8) & 0xFF;
  565. float vcb = (vc >>> 16) & 0xFF;
  566. float vca = (vc >>> 24) & 0xFF;
  567. int ncr = Math.min(0xFF, (int)(cr * vcr / 0xFF));
  568. int ncg = Math.min(0xFF, (int)(cg * vcg / 0xFF));
  569. int ncb = Math.min(0xFF, (int)(cb * vcb / 0xFF));
  570. int nca = Math.min(0xFF, (int)(ca * vca / 0xFF));
  571. renderer.putColorRGBA(renderer.getColorIndex(4 - i), ncr, ncg, ncb, nca);
  572. }
  573. }
  574.  
  575. private static class LightGatheringTransformer extends QuadGatheringTransformer {
  576.  
  577. private static final VertexFormat FORMAT = new VertexFormat().addElement(DefaultVertexFormats.TEX_2F).addElement(DefaultVertexFormats.TEX_2S);
  578.  
  579. int blockLight, skyLight;
  580.  
  581. { setVertexFormat(FORMAT); }
  582.  
  583. boolean hasLighting()
  584. {
  585. return dataLength[1] >= 2;
  586. }
  587.  
  588. @Override
  589. protected void processQuad()
  590. {
  591. // Reset light data
  592. blockLight = 0;
  593. skyLight = 0;
  594. // Compute average light for all 4 vertices
  595. for (int i = 0; i < 4; i++)
  596. {
  597. blockLight += (int) ((quadData[1][i][0] * 0xFFFF) / 0x20);
  598. skyLight += (int) ((quadData[1][i][1] * 0xFFFF) / 0x20);
  599. }
  600. // Values must be multiplied by 16, divided by 4 for average => x4
  601. blockLight *= 4;
  602. skyLight *= 4;
  603. }
  604.  
  605. // Dummy overrides
  606.  
  607. @Override
  608. public void setQuadTint(int tint) {}
  609.  
  610. @Override
  611. public void setQuadOrientation(EnumFacing orientation) {}
  612.  
  613. @Override
  614. public void setApplyDiffuseLighting(boolean diffuse) {}
  615.  
  616. @Override
  617. public void setTexture(TextureAtlasSprite texture) {}
  618. }
  619.  
  620. private static final LightGatheringTransformer lightGatherer = new LightGatheringTransformer();
  621.  
  622. public static void renderLitItem(ItemRenderer ri, IBakedModel model, int color, ItemStack stack)
  623. {
  624. List<BakedQuad> allquads = new ArrayList<>();
  625. Random random = new Random();
  626. long seed = 42L;
  627.  
  628. for (EnumFacing enumfacing : EnumFacing.values())
  629. {
  630. random.setSeed(seed);
  631. allquads.addAll(model.getQuads(null, enumfacing, random));
  632. }
  633.  
  634. random.setSeed(seed);
  635. allquads.addAll(model.getQuads(null, null, random));
  636.  
  637. if (allquads.isEmpty()) return;
  638.  
  639. // Current list of consecutive quads with the same lighting
  640. List<BakedQuad> segment = new ArrayList<>();
  641.  
  642. // Lighting of the current segment
  643. int segmentBlockLight = 0;
  644. int segmentSkyLight = 0;
  645. // Diffuse lighting state
  646. boolean segmentShading = true;
  647. // State changed by the current segment
  648. boolean segmentLightingDirty = false;
  649. boolean segmentShadingDirty = false;
  650. // If the current segment contains lighting data
  651. boolean hasLighting = false;
  652.  
  653. for (int i = 0; i < allquads.size(); i++)
  654. {
  655. BakedQuad q = allquads.get(i);
  656.  
  657. // Lighting of the current quad
  658. int bl = 0;
  659. int sl = 0;
  660.  
  661. // Fail-fast on ITEM, as it cannot have light data
  662. if (q.getFormat() != DefaultVertexFormats.ITEM && q.getFormat().hasUv(1))
  663. {
  664. q.pipe(lightGatherer);
  665. if (lightGatherer.hasLighting())
  666. {
  667. bl = lightGatherer.blockLight;
  668. sl = lightGatherer.skyLight;
  669. }
  670. }
  671.  
  672. boolean shade = q.shouldApplyDiffuseLighting();
  673.  
  674. boolean lightingDirty = segmentBlockLight != bl || segmentSkyLight != sl;
  675. boolean shadeDirty = shade != segmentShading;
  676.  
  677. // If lighting or color data has changed, draw the segment and flush it
  678. if (lightingDirty || shadeDirty)
  679. {
  680. if (i > 0) // Make sure this isn't the first quad being processed
  681. {
  682. drawSegment(ri, color, stack, segment, segmentBlockLight, segmentSkyLight, segmentShading, segmentLightingDirty && (hasLighting || segment.size() < i), segmentShadingDirty);
  683. }
  684. segmentBlockLight = bl;
  685. segmentSkyLight = sl;
  686. segmentShading = shade;
  687. segmentLightingDirty = lightingDirty;
  688. segmentShadingDirty = shadeDirty;
  689. hasLighting = segmentBlockLight > 0 || segmentSkyLight > 0 || !segmentShading;
  690. }
  691.  
  692. segment.add(q);
  693. }
  694.  
  695. drawSegment(ri, color, stack, segment, segmentBlockLight, segmentSkyLight, segmentShading, segmentLightingDirty && (hasLighting || segment.size() < allquads.size()), segmentShadingDirty);
  696.  
  697. // Clean up render state if necessary
  698. if (hasLighting)
  699. {
  700. OpenGlHelper.glMultiTexCoord2f(OpenGlHelper.GL_TEXTURE0, OpenGlHelper.lastBrightnessX, OpenGlHelper.lastBrightnessY);
  701. GlStateManager.enableLighting();
  702. }
  703. }
  704.  
  705. private static void drawSegment(ItemRenderer ri, int baseColor, ItemStack stack, List<BakedQuad> segment, int bl, int sl, boolean shade, boolean updateLighting, boolean updateShading)
  706. {
  707. BufferBuilder bufferbuilder = Tessellator.getInstance().getBuffer();
  708. bufferbuilder.begin(GL11.GL_QUADS, DefaultVertexFormats.ITEM);
  709.  
  710. float lastBl = OpenGlHelper.lastBrightnessX;
  711. float lastSl = OpenGlHelper.lastBrightnessY;
  712.  
  713. if (updateShading)
  714. {
  715. if (shade)
  716. {
  717. // (Re-)enable lighting for normal look with shading
  718. GlStateManager.enableLighting();
  719. }
  720. else
  721. {
  722. // Disable lighting to simulate a lack of diffuse lighting
  723. GlStateManager.disableLighting();
  724. }
  725. }
  726.  
  727. if (updateLighting)
  728. {
  729. // Force lightmap coords to simulate synthetic lighting
  730. OpenGlHelper.glMultiTexCoord2f(OpenGlHelper.GL_TEXTURE0, Math.max(bl, lastBl), Math.max(sl, lastSl));
  731. }
  732.  
  733. ri.renderQuads(bufferbuilder, segment, baseColor, stack);
  734. Tessellator.getInstance().draw();
  735.  
  736. // Preserve this as it represents the "world" lighting
  737. OpenGlHelper.lastBrightnessX = lastBl;
  738. OpenGlHelper.lastBrightnessY = lastSl;
  739.  
  740. segment.clear();
  741. }
  742.  
  743. /**
  744. * internal, relies on fixed format of FaceBakery
  745. */
  746. public static void fillNormal(int[] faceData, EnumFacing facing)
  747. {
  748. Vector3f v1 = getVertexPos(faceData, 3);
  749. Vector3f t = getVertexPos(faceData, 1);
  750. Vector3f v2 = getVertexPos(faceData, 2);
  751. v1.sub(t);
  752. t.set(getVertexPos(faceData, 0));
  753. v2.sub(t);
  754. v1.cross(v2, v1);
  755. v1.normalize();
  756.  
  757. int x = ((byte) Math.round(v1.x * 127)) & 0xFF;
  758. int y = ((byte) Math.round(v1.y * 127)) & 0xFF;
  759. int z = ((byte) Math.round(v1.z * 127)) & 0xFF;
  760.  
  761. int normal = x | (y << 0x08) | (z << 0x10);
  762.  
  763. for(int i = 0; i < 4; i++)
  764. {
  765. faceData[i * 7 + 6] = normal;
  766. }
  767. }
  768.  
  769. private static Vector3f getVertexPos(int[] data, int vertex)
  770. {
  771. int idx = vertex * 7;
  772.  
  773. float x = Float.intBitsToFloat(data[idx]);
  774. float y = Float.intBitsToFloat(data[idx + 1]);
  775. float z = Float.intBitsToFloat(data[idx + 2]);
  776.  
  777. return new Vector3f(x, y, z);
  778. }
  779.  
  780. @SuppressWarnings("deprecation")
  781. public static Optional<TRSRTransformation> applyTransform(ItemTransformVec3f transform, Optional<? extends IModelPart> part)
  782. {
  783. if(part.isPresent()) return Optional.empty();
  784. return Optional.of(TRSRTransformation.blockCenterToCorner(TRSRTransformation.from(transform)));
  785. }
  786.  
  787. public static Optional<TRSRTransformation> applyTransform(ModelRotation rotation, Optional<? extends IModelPart> part)
  788. {
  789. if(part.isPresent()) return Optional.empty();
  790. return Optional.of(TRSRTransformation.from(rotation));
  791. }
  792.  
  793. public static Optional<TRSRTransformation> applyTransform(Matrix4f matrix, Optional<? extends IModelPart> part)
  794. {
  795. if(part.isPresent()) return Optional.empty();
  796. return Optional.of(new TRSRTransformation(matrix));
  797. }
  798.  
  799. public static void loadEntityShader(Entity entity, GameRenderer entityRenderer)
  800. {
  801. if (entity != null)
  802. {
  803. ResourceLocation shader = ClientRegistry.getEntityShader(entity.getClass());
  804. if (shader != null)
  805. {
  806. entityRenderer.loadShader(shader);
  807. }
  808. }
  809. }
  810.  
  811. public static IBakedModel getDamageModel(IBakedModel ibakedmodel, TextureAtlasSprite texture, IBlockState state, IWorldReader world, BlockPos pos)
  812. {
  813. state = state.getBlock().getExtendedState(state, world, pos);
  814. return (new SimpleBakedModel.Builder(state, ibakedmodel, texture, new Random(), 42)).build();
  815. }
  816.  
  817. private static int slotMainHand = 0;
  818.  
  819. public static boolean shouldCauseReequipAnimation(@Nonnull ItemStack from, @Nonnull ItemStack to, int slot)
  820. {
  821. boolean fromInvalid = from.isEmpty();
  822. boolean toInvalid = to.isEmpty();
  823.  
  824. if (fromInvalid && toInvalid) return false;
  825. if (fromInvalid || toInvalid) return true;
  826.  
  827. boolean changed = false;
  828. if (slot != -1)
  829. {
  830. changed = slot != slotMainHand;
  831. slotMainHand = slot;
  832. }
  833. return from.getItem().shouldCauseReequipAnimation(from, to, changed);
  834. }
  835.  
  836. public static BlockFaceUV applyUVLock(BlockFaceUV blockFaceUV, EnumFacing originalSide, ITransformation rotation)
  837. {
  838. TRSRTransformation global = new TRSRTransformation(rotation.getMatrixVec());
  839. Matrix4f uv = global.getUVLockTransform(originalSide).getMatrixVec();
  840. Vector4f vec = new Vector4f(0, 0, 0, 1);
  841. float u0 = blockFaceUV.getVertexU(blockFaceUV.getVertexRotatedRev(0));
  842. float v0 = blockFaceUV.getVertexV(blockFaceUV.getVertexRotatedRev(0));
  843. vec.x = u0 / 16;
  844. vec.y = v0 / 16;
  845. uv.transform(vec);
  846. float uMin = 16 * vec.x; // / vec.w;
  847. float vMin = 16 * vec.y; // / vec.w;
  848. float u1 = blockFaceUV.getVertexU(blockFaceUV.getVertexRotatedRev(2));
  849. float v1 = blockFaceUV.getVertexV(blockFaceUV.getVertexRotatedRev(2));
  850. vec.x = u1 / 16;
  851. vec.y = v1 / 16;
  852. vec.z = 0;
  853. vec.w = 1;
  854. uv.transform(vec);
  855. float uMax = 16 * vec.x; // / vec.w;
  856. float vMax = 16 * vec.y; // / vec.w;
  857. if (uMin > uMax && u0 < u1 || uMin < uMax && u0 > u1)
  858. {
  859. float t = uMin;
  860. uMin = uMax;
  861. uMax = t;
  862. }
  863. if (vMin > vMax && v0 < v1 || vMin < vMax && v0 > v1)
  864. {
  865. float t = vMin;
  866. vMin = vMax;
  867. vMax = t;
  868. }
  869. float a = (float)Math.toRadians(blockFaceUV.rotation);
  870. Vector3f rv = new Vector3f(MathHelper.cos(a), MathHelper.sin(a), 0);
  871. Matrix3f rot = new Matrix3f();
  872. uv.getRotationScale(rot);
  873. rot.transform(rv);
  874. int angle = MathHelper.normalizeAngle(-(int)Math.round(Math.toDegrees(Math.atan2(rv.y, rv.x)) / 90) * 90, 360);
  875. return new BlockFaceUV(new float[]{ uMin, vMin, uMax, vMax }, angle);
  876. }
  877.  
  878. public static RenderGameOverlayEvent.BossInfo bossBarRenderPre(MainWindow res, BossInfoClient bossInfo, int x, int y, int increment)
  879. {
  880. RenderGameOverlayEvent.BossInfo evt = new RenderGameOverlayEvent.BossInfo(new RenderGameOverlayEvent(Animation.getPartialTickTime(), res),
  881. BOSSINFO, bossInfo, x, y, increment);
  882. MinecraftForge.EVENT_BUS.post(evt);
  883. return evt;
  884. }
  885.  
  886. public static void bossBarRenderPost(MainWindow res)
  887. {
  888. MinecraftForge.EVENT_BUS.post(new RenderGameOverlayEvent.Post(new RenderGameOverlayEvent(Animation.getPartialTickTime(), res), BOSSINFO));
  889. }
  890.  
  891. public static ScreenshotEvent onScreenshot(NativeImage image, File screenshotFile)
  892. {
  893. ScreenshotEvent event = new ScreenshotEvent(image, screenshotFile);
  894. MinecraftForge.EVENT_BUS.post(event);
  895. return event;
  896. }
  897.  
  898. @SuppressWarnings("deprecation")
  899. public static Pair<? extends IBakedModel,Matrix4f> handlePerspective(IBakedModel model, ItemCameraTransforms.TransformType type)
  900. {
  901. TRSRTransformation tr = TRSRTransformation.from(model.getItemCameraTransforms().getTransform(type));
  902. Matrix4f mat = null;
  903. if(!tr.equals(TRSRTransformation.identity())) mat = tr.getMatrixVec();
  904. return Pair.of(model, mat);
  905. }
  906.  
  907. public static void onInputUpdate(EntityPlayer player, MovementInput movementInput)
  908. {
  909. MinecraftForge.EVENT_BUS.post(new InputUpdateEvent(player, movementInput));
  910. }
  911.  
  912. public static void refreshResources(Minecraft mc, VanillaResourceType... types) {
  913. SelectiveReloadStateHandler.INSTANCE.beginReload(ReloadRequirements.include(types));
  914. mc.refreshResources();
  915. SelectiveReloadStateHandler.INSTANCE.endReload();
  916. }
  917.  
  918. public static boolean onGuiMouseClickedPre(GuiScreen guiScreen, double mouseX, double mouseY, int button)
  919. {
  920. Event event = new GuiScreenEvent.MouseClickedEvent.Pre(guiScreen, mouseX, mouseY, button);
  921. return MinecraftForge.EVENT_BUS.post(event);
  922. }
  923.  
  924. public static boolean onGuiMouseClickedPost(GuiScreen guiScreen, double mouseX, double mouseY, int button)
  925. {
  926. Event event = new GuiScreenEvent.MouseClickedEvent.Post(guiScreen, mouseX, mouseY, button);
  927. return MinecraftForge.EVENT_BUS.post(event);
  928. }
  929.  
  930. public static boolean onGuiMouseReleasedPre(GuiScreen guiScreen, double mouseX, double mouseY, int button)
  931. {
  932. Event event = new GuiScreenEvent.MouseReleasedEvent.Pre(guiScreen, mouseX, mouseY, button);
  933. return MinecraftForge.EVENT_BUS.post(event);
  934. }
  935.  
  936. public static boolean onGuiMouseReleasedPost(GuiScreen guiScreen, double mouseX, double mouseY, int button)
  937. {
  938. Event event = new GuiScreenEvent.MouseReleasedEvent.Post(guiScreen, mouseX, mouseY, button);
  939. return MinecraftForge.EVENT_BUS.post(event);
  940. }
  941.  
  942. public static boolean onGuiMouseDragPre(GuiScreen guiScreen, double mouseX, double mouseY, int mouseButton, double dragX, double dragY)
  943. {
  944. Event event = new GuiScreenEvent.MouseDragEvent.Pre(guiScreen, mouseX, mouseY, mouseButton, dragX, dragY);
  945. return MinecraftForge.EVENT_BUS.post(event);
  946. }
  947.  
  948. public static boolean onGuiMouseDragPost(GuiScreen guiScreen, double mouseX, double mouseY, int mouseButton, double dragX, double dragY)
  949. {
  950. Event event = new GuiScreenEvent.MouseDragEvent.Post(guiScreen, mouseX, mouseY, mouseButton, dragX, dragY);
  951. return MinecraftForge.EVENT_BUS.post(event);
  952. }
  953.  
  954. public static boolean onGuiMouseScrollPre(MouseHelper mouseHelper, GuiScreen guiScreen, double scrollDelta)
  955. {
  956. MainWindow mainWindow = guiScreen.mc.mainWindow;
  957. double mouseX = mouseHelper.getMouseX() * (double) mainWindow.getScaledWidth() / (double) mainWindow.getWidth();
  958. double mouseY = mouseHelper.getMouseY() * (double) mainWindow.getScaledHeight() / (double) mainWindow.getHeight();
  959. Event event = new GuiScreenEvent.MouseScrollEvent.Pre(guiScreen, mouseX, mouseY, scrollDelta);
  960. return MinecraftForge.EVENT_BUS.post(event);
  961. }
  962.  
  963. public static boolean onGuiMouseScrollPost(MouseHelper mouseHelper, GuiScreen guiScreen, double scrollDelta)
  964. {
  965. MainWindow mainWindow = guiScreen.mc.mainWindow;
  966. double mouseX = mouseHelper.getMouseX() * (double) mainWindow.getScaledWidth() / (double) mainWindow.getWidth();
  967. double mouseY = mouseHelper.getMouseY() * (double) mainWindow.getScaledHeight() / (double) mainWindow.getHeight();
  968. Event event = new GuiScreenEvent.MouseScrollEvent.Post(guiScreen, mouseX, mouseY, scrollDelta);
  969. return MinecraftForge.EVENT_BUS.post(event);
  970. }
  971.  
  972. public static boolean onGuiKeyPressedPre(GuiScreen guiScreen, int keyCode, int scanCode, int modifiers)
  973. {
  974. Event event = new GuiScreenEvent.KeyboardKeyPressedEvent.Pre(guiScreen, keyCode, scanCode, modifiers);
  975. return MinecraftForge.EVENT_BUS.post(event);
  976. }
  977.  
  978. public static boolean onGuiKeyPressedPost(GuiScreen guiScreen, int keyCode, int scanCode, int modifiers)
  979. {
  980. Event event = new GuiScreenEvent.KeyboardKeyPressedEvent.Post(guiScreen, keyCode, scanCode, modifiers);
  981. return MinecraftForge.EVENT_BUS.post(event);
  982. }
  983.  
  984. public static boolean onGuiKeyReleasedPre(GuiScreen guiScreen, int keyCode, int scanCode, int modifiers)
  985. {
  986. Event event = new GuiScreenEvent.KeyboardKeyReleasedEvent.Pre(guiScreen, keyCode, scanCode, modifiers);
  987. return MinecraftForge.EVENT_BUS.post(event);
  988. }
  989.  
  990. public static boolean onGuiKeyReleasedPost(GuiScreen guiScreen, int keyCode, int scanCode, int modifiers)
  991. {
  992. Event event = new GuiScreenEvent.KeyboardKeyReleasedEvent.Post(guiScreen, keyCode, scanCode, modifiers);
  993. return MinecraftForge.EVENT_BUS.post(event);
  994. }
  995.  
  996. public static boolean onGuiCharTypedPre(GuiScreen guiScreen, char codePoint, int modifiers)
  997. {
  998. Event event = new GuiScreenEvent.KeyboardCharTypedEvent.Pre(guiScreen, codePoint, modifiers);
  999. return MinecraftForge.EVENT_BUS.post(event);
  1000. }
  1001.  
  1002. public static boolean onGuiCharTypedPost(GuiScreen guiScreen, char codePoint, int modifiers)
  1003. {
  1004. Event event = new GuiScreenEvent.KeyboardCharTypedEvent.Post(guiScreen, codePoint, modifiers);
  1005. return MinecraftForge.EVENT_BUS.post(event);
  1006. }
  1007.  
  1008. public static void onRecipesUpdated()
  1009. {
  1010. Event event = new RecipesUpdatedEvent();
  1011. MinecraftForge.EVENT_BUS.post(event);
  1012. }
  1013.  
  1014. // Resets cached thread fields in ThreadNameCachingStrategy and ReusableLogEventFactory to be repopulated during their next access.
  1015. // This serves a workaround for no built-in method of triggering this type of refresh as brought up by LOG4J2-2178.
  1016. public static void invalidateLog4jThreadCache()
  1017. {
  1018. try
  1019. {
  1020. Field nameField = ThreadNameCachingStrategy.class.getDeclaredField("THREADLOCAL_NAME");
  1021. Field logEventField = ReusableLogEventFactory.class.getDeclaredField("mutableLogEventThreadLocal");
  1022. nameField.setAccessible(true);
  1023. logEventField.setAccessible(true);
  1024. ((ThreadLocal<?>) nameField.get(null)).set(null);
  1025. ((ThreadLocal<?>) logEventField.get(null)).set(null);
  1026. }
  1027. catch (ReflectiveOperationException | NoClassDefFoundError e)
  1028. {
  1029. LOGGER.error("Unable to invalidate log4j thread cache, thread fields in logs may be inaccurate", e);
  1030. }
  1031. }
  1032.  
  1033. public static void fireMouseInput(int button, int action, int mods)
  1034. {
  1035. MinecraftForge.EVENT_BUS.post(new InputEvent.MouseInputEvent(button, action, mods));
  1036. }
  1037.  
  1038. public static void fireKeyInput(int key, int scanCode, int action, int modifiers)
  1039. {
  1040. MinecraftForge.EVENT_BUS.post(new InputEvent.KeyInputEvent(key, scanCode, action, modifiers));
  1041. }
  1042.  
  1043. /**
  1044. * Fixes MC-96521 directly, and marks the current world to be deleted by
  1045. * {@link #deleteMarkedHardcoreWorld(WorldSummary)}
  1046. *
  1047. * @see net.minecraft.client.gui.GuiGameOver
  1048. * @see #deleteMarkedHardcoreWorld
  1049. */
  1050. public static void fixHardcore()
  1051. {
  1052. Minecraft mc = Minecraft.getInstance();
  1053. IntegratedServer server = ((IntegratedServer) ServerLifecycleHooks.getCurrentServer());
  1054. if(server != null) {
  1055. server.addScheduledTask(() -> {
  1056. String ownerName = server.getServerOwner();
  1057. EntityPlayerMP owner = server.getPlayerList().getPlayerByUsername(ownerName);
  1058. owner.setGameType(GameType.NOT_SET);
  1059. mc.world.sendQuittingDisconnectingPacket();
  1060. });
  1061. }
  1062. else {
  1063. mc.world.sendQuittingDisconnectingPacket();
  1064. }
  1065. while(mc.getConnection().getNetworkManager().isChannelOpen()) {}
  1066. mc.loadWorld(null);
  1067. }
  1068.  
  1069. /**
  1070. * Used to fix MC-30646<p>
  1071. * <p>
  1072. * Deletes the world specified by the provided WorldSummary under the following
  1073. * conditions:<p>
  1074. *
  1075. * 1. The world is Hardcore <p>
  1076. * 2. The player's health is <= 0 <p>
  1077. * 3. The player's game type is set to {@link GameType#NOT_SET}<p>
  1078. *
  1079. * @param world the WorldSummary to delete
  1080. *
  1081. * @see #fixHardcore
  1082. * @see net.minecraft.client.gui.GuiListWorldSelection#func_212330_a
  1083. *
  1084. * @return true if the world was just deleted, or the world has already been deleted
  1085. */
  1086. public static boolean deleteMarkedHardcoreWorld(WorldSummary world)
  1087. {
  1088. ISaveFormat saveLoader = Minecraft.getInstance().getSaveLoader();
  1089. if(!saveLoader.getWorldFolder(world.getFileName()).toFile().exists())
  1090. {
  1091. return true;
  1092. }
  1093. if(world.isHardcoreModeEnabled())
  1094. {
  1095. WorldInfo worldInfo = Minecraft.getInstance().getSaveLoader().getWorldInfo(world.getFileName());
  1096. if(worldInfo != null)
  1097. {
  1098. NBTTagCompound playerNBT = worldInfo.getPlayerNBTTagCompound();
  1099. if(playerNBT != null)
  1100. {
  1101. if(playerNBT.hasKey("playerGameType") && playerNBT.hasKey("Health"))
  1102. {
  1103. if(playerNBT.getShort("playerGameType") == -1 && playerNBT.getFloat("Health") <= 0)
  1104. {
  1105. return Minecraft.getInstance().getSaveLoader().deleteWorldDirectory(world.getFileName());
  1106. }
  1107. }
  1108. }
  1109. }
  1110. }
  1111. return false;
  1112. }
  1113.  
  1114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement