From 5fa732d22383523e1670e901a365981d7e66976a Mon Sep 17 00:00:00 2001 From: mezz Date: Mon, 25 Mar 2019 12:06:20 -0700 Subject: [PATCH] Remove uses of java.awt (#5591) `java.awt` will statically load itself when `java.awt.Color`, `java.awt.Dimension`, etc are loaded. This conflicts with lwjgl3 and must be avoided (except for in mojang's server gui). --- .../minecraftforge/client/GuiIngameForge.java | 3 +- .../client/event/ScreenshotEvent.java | 3 +- .../client/model/ModelLoader.java | 3 - .../minecraftforge/common/util/Size2i.java | 71 +++++++++++++++++++ .../java/net/minecraftforge/fluids/Fluid.java | 18 ----- .../fml/client/SplashProgress.java | 1 - .../fml/client/gui/GuiBackupFailed.java | 1 - .../fml/client/gui/GuiModList.java | 14 ++-- .../debug/fluid/ColoredFluidTest.java | 4 +- .../debug/gameplay/DynamicBannerTest.java | 2 +- .../debug/util/TextureDumper.java | 2 +- 11 files changed, 83 insertions(+), 39 deletions(-) create mode 100644 src/main/java/net/minecraftforge/common/util/Size2i.java diff --git a/src/main/java/net/minecraftforge/client/GuiIngameForge.java b/src/main/java/net/minecraftforge/client/GuiIngameForge.java index a27ee037e0..2c13b332ba 100644 --- a/src/main/java/net/minecraftforge/client/GuiIngameForge.java +++ b/src/main/java/net/minecraftforge/client/GuiIngameForge.java @@ -21,7 +21,6 @@ package net.minecraftforge.client; import static net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType.*; -import java.awt.Color; import java.util.ArrayList; import java.util.List; @@ -676,7 +675,7 @@ public class GuiIngameForge extends GuiIngame GlStateManager.translatef((float)(width / 2), (float)(height - 68), 0.0F); GlStateManager.enableBlend(); GlStateManager.blendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO); - int color = (animateOverlayMessageColor ? Color.HSBtoRGB(hue / 50.0F, 0.7F, 0.6F) & WHITE : WHITE); + int color = (animateOverlayMessageColor ? MathHelper.hsvToRGB(hue / 50.0F, 0.7F, 0.6F) & WHITE : WHITE); fontrenderer.drawStringWithShadow(overlayMessage, -fontrenderer.getStringWidth(overlayMessage) / 2, -4, color | (opacity << 24)); GlStateManager.disableBlend(); GlStateManager.popMatrix(); diff --git a/src/main/java/net/minecraftforge/client/event/ScreenshotEvent.java b/src/main/java/net/minecraftforge/client/event/ScreenshotEvent.java index 2e04f535d2..77b8e24bc9 100644 --- a/src/main/java/net/minecraftforge/client/event/ScreenshotEvent.java +++ b/src/main/java/net/minecraftforge/client/event/ScreenshotEvent.java @@ -25,7 +25,6 @@ import net.minecraft.util.text.TextComponentString; import net.minecraftforge.eventbus.api.Cancelable; import net.minecraftforge.eventbus.api.Event; -import java.awt.image.BufferedImage; import java.io.File; /** @@ -34,7 +33,7 @@ import java.io.File; * This event is {@link Cancelable} * * {@link #screenshotFile} contains the file the screenshot will be/was saved to - * {@link #image} contains the {@link BufferedImage} that will be saved + * {@link #image} contains the {@link NativeImage} that will be saved * {@link #resultMessage} contains the {@link ITextComponent} to be returned. If {@code null}, the default vanilla message will be used instead */ @Cancelable diff --git a/src/main/java/net/minecraftforge/client/model/ModelLoader.java b/src/main/java/net/minecraftforge/client/model/ModelLoader.java index 2b2dfa2944..515b7e383e 100644 --- a/src/main/java/net/minecraftforge/client/model/ModelLoader.java +++ b/src/main/java/net/minecraftforge/client/model/ModelLoader.java @@ -19,9 +19,6 @@ package net.minecraftforge.client.model; -import java.awt.Color; -import java.awt.Graphics2D; -import java.awt.image.BufferedImage; import java.io.IOException; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; diff --git a/src/main/java/net/minecraftforge/common/util/Size2i.java b/src/main/java/net/minecraftforge/common/util/Size2i.java new file mode 100644 index 0000000000..ea4d8d2172 --- /dev/null +++ b/src/main/java/net/minecraftforge/common/util/Size2i.java @@ -0,0 +1,71 @@ +/* + * Minecraft Forge + * Copyright (c) 2016-2019. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package net.minecraftforge.common.util; + +import com.google.common.base.MoreObjects; +import com.google.common.base.Preconditions; + +import javax.annotation.Nonnegative; + +public final class Size2i +{ + @Nonnegative + public final int width; + @Nonnegative + public final int height; + + @SuppressWarnings("ConstantConditions") + public Size2i(@Nonnegative int width, @Nonnegative int height) + { + Preconditions.checkArgument(width >= 0, "width must be greater or equal 0"); + Preconditions.checkArgument(height >= 0, "height must be greater or equal 0"); + this.width = width; + this.height = height; + } + + @Override + public boolean equals(Object obj) + { + if (obj instanceof Size2i) + { + Size2i other = (Size2i)obj; + return (width == other.width) && (height == other.height); + } + return false; + } + + @Override + public int hashCode() + { + int hash = 17; + hash = hash * 31 + width; + hash = hash * 31 + height; + return hash; + } + + @Override + public String toString() + { + return MoreObjects.toStringHelper(this) + .add("width", width) + .add("height", height) + .toString(); + } +} diff --git a/src/main/java/net/minecraftforge/fluids/Fluid.java b/src/main/java/net/minecraftforge/fluids/Fluid.java index d41cc53c6b..9837eab629 100644 --- a/src/main/java/net/minecraftforge/fluids/Fluid.java +++ b/src/main/java/net/minecraftforge/fluids/Fluid.java @@ -24,7 +24,6 @@ import javax.annotation.Nullable; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import java.awt.Color; import java.util.Locale; import net.minecraft.block.Block; import net.minecraft.block.material.Material; @@ -142,17 +141,6 @@ public class Fluid */ protected int color = 0xFFFFFFFF; - public Fluid(String fluidName, ResourceLocation still, ResourceLocation flowing, Color color) - { - this(fluidName, still, flowing, null, color); - } - - public Fluid(String fluidName, ResourceLocation still, ResourceLocation flowing, @Nullable ResourceLocation overlay, Color color) - { - this(fluidName, still, flowing, overlay); - this.setColor(color); - } - public Fluid(String fluidName, ResourceLocation still, ResourceLocation flowing, int color) { this(fluidName, still, flowing, null, color); @@ -246,12 +234,6 @@ public class Fluid return this; } - public Fluid setColor(Color color) - { - this.color = color.getRGB(); - return this; - } - public Fluid setColor(int color) { this.color = color; diff --git a/src/main/java/net/minecraftforge/fml/client/SplashProgress.java b/src/main/java/net/minecraftforge/fml/client/SplashProgress.java index bc497a9c95..2b94a99d76 100644 --- a/src/main/java/net/minecraftforge/fml/client/SplashProgress.java +++ b/src/main/java/net/minecraftforge/fml/client/SplashProgress.java @@ -24,7 +24,6 @@ import static net.minecraftforge.fml.Logging.fmlLog; import static org.lwjgl.opengl.GL11.*; import static org.lwjgl.opengl.GL12.*; -import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; diff --git a/src/main/java/net/minecraftforge/fml/client/gui/GuiBackupFailed.java b/src/main/java/net/minecraftforge/fml/client/gui/GuiBackupFailed.java index 65fe7aaebc..32c9a32792 100644 --- a/src/main/java/net/minecraftforge/fml/client/gui/GuiBackupFailed.java +++ b/src/main/java/net/minecraftforge/fml/client/gui/GuiBackupFailed.java @@ -19,7 +19,6 @@ package net.minecraftforge.fml.client.gui; -import java.awt.Desktop; import java.io.File; import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.GuiScreen; diff --git a/src/main/java/net/minecraftforge/fml/client/gui/GuiModList.java b/src/main/java/net/minecraftforge/fml/client/gui/GuiModList.java index 3bcb148a25..74af2a2b13 100644 --- a/src/main/java/net/minecraftforge/fml/client/gui/GuiModList.java +++ b/src/main/java/net/minecraftforge/fml/client/gui/GuiModList.java @@ -21,7 +21,6 @@ package net.minecraftforge.fml.client.gui; import static net.minecraft.util.StringUtils.stripControlCodes; -import java.awt.Dimension; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; @@ -58,6 +57,7 @@ import net.minecraft.util.ResourceLocation; import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.TextComponentString; import net.minecraftforge.common.ForgeHooks; +import net.minecraftforge.common.util.Size2i; import net.minecraftforge.fml.ForgeI18n; import net.minecraftforge.fml.MavenVersionStringHelper; import net.minecraftforge.fml.ModContainer; @@ -184,10 +184,10 @@ public class GuiModList extends GuiScreen class Info extends GuiListExtended.IGuiListEntry { private ResourceLocation logoPath; - private Dimension logoDims; + private Size2i logoDims; private List lines; - public Info(GuiListExtended parent, List lines, @Nullable ResourceLocation logoPath, Dimension logoDims) + public Info(GuiListExtended parent, List lines, @Nullable ResourceLocation logoPath, Size2i logoDims) { this.list = parent; this.lines = resizeContent(lines); @@ -439,7 +439,7 @@ public class GuiModList extends GuiScreen List lines = new ArrayList<>(); VersionChecker.CheckResult vercheck = VersionChecker.getResult(selectedMod); - Pair logoData = selectedMod.getLogoFile().map(logoFile-> + Pair logoData = selectedMod.getLogoFile().map(logoFile-> { TextureManager tm = mc.getTextureManager(); final ModFileResourcePack resourcePack = ResourcePackLoader.getResourcePackFor(selectedMod.getModId()) @@ -453,12 +453,12 @@ public class GuiModList extends GuiScreen logo = NativeImage.read(logoResource); if (logo != null) { - return Pair.of(tm.getDynamicTextureLocation("modlogo", new DynamicTexture(logo)), new Dimension(logo.getWidth(), logo.getHeight())); + return Pair.of(tm.getDynamicTextureLocation("modlogo", new DynamicTexture(logo)), new Size2i(logo.getWidth(), logo.getHeight())); } } catch (IOException e) { } - return Pair.of(null, new Dimension(0, 0)); - }).orElse(Pair.of(null, new Dimension(0, 0))); + return Pair.of(null, new Size2i(0, 0)); + }).orElse(Pair.of(null, new Size2i(0, 0))); lines.add(selectedMod.getDisplayName()); lines.add(ForgeI18n.parseMessage("fml.menu.mods.info.version", MavenVersionStringHelper.artifactVersionToString(selectedMod.getVersion()))); diff --git a/src/test/java/net/minecraftforge/debug/fluid/ColoredFluidTest.java b/src/test/java/net/minecraftforge/debug/fluid/ColoredFluidTest.java index 9fcea9b37f..d04968a77c 100644 --- a/src/test/java/net/minecraftforge/debug/fluid/ColoredFluidTest.java +++ b/src/test/java/net/minecraftforge/debug/fluid/ColoredFluidTest.java @@ -19,8 +19,6 @@ package net.minecraftforge.debug.fluid; -import java.awt.Color; - import net.minecraft.block.Block; import net.minecraft.block.material.Material; import net.minecraft.util.ResourceLocation; @@ -41,7 +39,7 @@ import net.minecraftforge.fml.common.registry.GameRegistry.ObjectHolder; public class ColoredFluidTest { static final boolean ENABLED = false; // <-- enable mod - static final Color COLOR = Color.PINK; // <-- change this to try other colors + static final int COLOR = 0xFFAFAF; // <-- change this to try other colors static final String MODID = "fluidadditionalfields"; static final ResourceLocation RES_LOC = new ResourceLocation(MODID, "slime"); diff --git a/src/test/java/net/minecraftforge/debug/gameplay/DynamicBannerTest.java b/src/test/java/net/minecraftforge/debug/gameplay/DynamicBannerTest.java index fb09efa417..498ac28683 100644 --- a/src/test/java/net/minecraftforge/debug/gameplay/DynamicBannerTest.java +++ b/src/test/java/net/minecraftforge/debug/gameplay/DynamicBannerTest.java @@ -19,7 +19,6 @@ package net.minecraftforge.debug.gameplay; -import java.awt.image.BufferedImage; import java.io.IOException; import java.io.InputStream; @@ -44,6 +43,7 @@ import net.minecraftforge.fml.event.FMLPreInitializationEvent; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; +// TODO 1.13: implement without java.awt @Mod(modid = DynamicBannerTest.MODID, name = "ForgeDebugDynamicBanner", version = DynamicBannerTest.VERSION, acceptableRemoteVersions = "*") public class DynamicBannerTest { diff --git a/src/test/java/net/minecraftforge/debug/util/TextureDumper.java b/src/test/java/net/minecraftforge/debug/util/TextureDumper.java index 3117dee004..53c96ce7b7 100644 --- a/src/test/java/net/minecraftforge/debug/util/TextureDumper.java +++ b/src/test/java/net/minecraftforge/debug/util/TextureDumper.java @@ -31,11 +31,11 @@ import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL12; import javax.imageio.ImageIO; -import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.nio.IntBuffer; +// TODO 1.13: implement without java.awt @Mod(modid = TextureDumper.MODID, name = "Forge Texture Atlas Dump", version = TextureDumper.VERSION, clientSideOnly = true) public class TextureDumper {