Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Gui List to rift based upon the work of Danielshe #13

Open
wants to merge 6 commits into
base: jitpack
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Changed Tabs to Space, moved classes, and edited lang file
Unknown authored and Waterpicker committed Jan 1, 2019
commit 38ce7363d2deb53934b332e59e96fbeec6e4c88c
69 changes: 35 additions & 34 deletions src/main/java/org/dimdev/rift/mixin/core/client/MixinMainMenu.java
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
import net.minecraft.client.gui.GuiMainMenu;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.resources.I18n;
import org.dimdev.utils.ModList;
import org.dimdev.rift.modlist.ModList;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
@@ -14,37 +14,38 @@

@Mixin(GuiMainMenu.class)
public class MixinMainMenu extends GuiScreen {

@Shadow
private void switchToRealms() {}

@ModifyArg(
method = "addSingleplayerMultiplayerButtons",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/client/gui/GuiMainMenu;addButton(Lnet/minecraft/client/gui/GuiButton;)Lnet/minecraft/client/gui/GuiButton;",
ordinal = 2
)
)
private GuiButton getRealmsButton(GuiButton original) {
GuiButton button = new GuiButton(original.id, width / 2 + 2, original.y, 98, 20, I18n.format("menu.online")) {
@Override
public void onClick(double mouseX, double mouseY) {
switchToRealms();
}
};
return button;
}

@Inject(method = "addSingleplayerMultiplayerButtons", at = @At("RETURN"))
private void onAddSingleplayerMultiplayerButtons(int y, int dy, CallbackInfo ci) {
GuiButton button = new GuiButton(100, width / 2 - 100, y + dy * 2, 98, 20, I18n.format("riftmodlist.mods")) {
@Override
public void onClick(double mouseX, double mouseY) {
ModList.displayModList();
}
};
addButton(button);
}


@Shadow
private void switchToRealms() {
}

@ModifyArg(
method = "addSingleplayerMultiplayerButtons",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/client/gui/GuiMainMenu;addButton(Lnet/minecraft/client/gui/GuiButton;)Lnet/minecraft/client/gui/GuiButton;",
ordinal = 2
)
)
private GuiButton getRealmsButton(GuiButton original) {
GuiButton button = new GuiButton(original.id, width / 2 + 2, original.y, 98, 20, I18n.format("menu.online")) {
@Override
public void onClick(double mouseX, double mouseY) {
switchToRealms();
}
};
return button;
}

@Inject(method = "addSingleplayerMultiplayerButtons", at = @At("RETURN"))
private void onAddSingleplayerMultiplayerButtons(int y, int dy, CallbackInfo ci) {
GuiButton button = new GuiButton(100, width / 2 - 100, y + dy * 2, 98, 20, I18n.format("rift.modlist.mods")) {
@Override
public void onClick(double mouseX, double mouseY) {
ModList.displayModList();
}
};
addButton(button);
}

}
108 changes: 108 additions & 0 deletions src/main/java/org/dimdev/rift/modlist/GuiMods.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package org.dimdev.rift.modlist;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.GuiTextField;
import net.minecraft.client.resources.I18n;
import net.minecraft.util.Util;
import org.dimdev.riftloader.ModInfo;
import org.dimdev.riftloader.RiftLoader;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class GuiMods extends GuiScreen {

private final String searchBoxSuggestion = I18n.format("rift.modlist.search_mods");
public static List<ModInfo> modList = new ArrayList<>();

@Nullable
private GuiModsContent guiModListContent;
private GuiTextField searchBox;
public GuiScreen previousGui;

public GuiMods() {
regenerateMods();
}

private void regenerateMods() {
modList = new ArrayList<>();
RiftLoader.instance.getMods().forEach(modInfo -> {
ModInfo cloned = new ModInfo();
cloned.id = modInfo.id;
cloned.name = modInfo.name != null ? modInfo.name : modInfo.id;
cloned.source = modInfo.source;
cloned.authors = modInfo.authors;
cloned.listeners = modInfo.listeners;
modList.add(modInfo);
});
Collections.sort(modList, (riftMod, anotherMod) -> {
return riftMod.name.compareTo(anotherMod.name);
});
}

@Override
protected void initGui() {
Minecraft.getInstance().keyboardListener.enableRepeatEvents(true);
addButton(new GuiButton(501, this.width / 2 - 100, this.height - 30, 93, 20, I18n.format("rift.modlist.openFolder")) {
@Override
public void onClick(double mouseX, double mouseY) {
Util.getOSType().openFile(RiftLoader.instance.modsDir);
}
});
this.guiModListContent = new GuiModsContent(this, "");

this.searchBox = new GuiTextField(103, this.fontRenderer, this.width / 2 - 100, 32, 200, 20) {
@Override
public void setFocused(boolean var1) {
super.setFocused(true);
}
};
this.searchBox.setTextAcceptHandler((p_212350_1_, p_212350_2_) ->
{
this.guiModListContent.searchFilter(p_212350_2_);
});
addButton(new GuiButton(104, this.width / 2 - 3, this.height - 30, 93, 20, I18n.format("rift.modlist.done")) {
@Override
public void onClick(double var1, double var3) {
close();
Minecraft.getInstance().displayGuiScreen(previousGui);
}
});
this.eventListeners.add(searchBox);
this.eventListeners.add(guiModListContent);
this.searchBox.setFocused(true);
this.searchBox.setCanLoseFocus(false);
}

public void reloadSearch() {
try {
this.searchBox.setText("");
this.guiModListContent.searchFilter("");
} catch (Exception e) {
}
}

@Override
public void tick() {
this.searchBox.tick();
this.searchBox.setSuggestion(searchBox.getText().equals("") ? searchBoxSuggestion : null);
}

@Override
public void render(int mouseX, int mouseY, float partialTicks) {
this.guiModListContent.drawScreen(mouseX, mouseY, partialTicks);
this.searchBox.drawTextField(mouseX, mouseY, partialTicks);
this.drawCenteredString(this.fontRenderer, I18n.format("rift.modlist.mods"), this.width / 2, 16, 16777215);
super.render(mouseX, mouseY, partialTicks);
}

@Nullable
public GuiModsContent getGuiModListContent() {
return guiModListContent;
}

}
175 changes: 175 additions & 0 deletions src/main/java/org/dimdev/rift/modlist/GuiModsContent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package org.dimdev.rift.modlist;

import net.minecraft.client.Minecraft;
import net.minecraft.client.audio.PositionedSoundRecord;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiSlot;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.resources.I18n;
import net.minecraft.init.SoundEvents;
import org.dimdev.riftloader.ModInfo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class GuiModsContent extends GuiSlot {

private List<ModInfo> modList = new ArrayList<>();
private GuiMods parent;
private FontRenderer fontRenderer;
private int currentIndex = -1;

public GuiModsContent(GuiMods parent, String searchTerm) {
super(
Minecraft.getInstance(),
parent.width,
parent.height,
60,
parent.height - 40,
40
);
this.parent = parent;
this.modList = new ArrayList<>();
this.fontRenderer = mc.fontRenderer;
setShowSelectionBox(true);
searchFilter(searchTerm);
}

@Override
public int getListWidth() {
return this.width - 48 * 2;
}

public int getCurrentIndex() {
return currentIndex;
}

public List<ModInfo> getModList() {
return modList;
}

@Override
protected int getSize() {
return modList.size();
}

@Override
protected boolean isSelected(int slotIndex) {
return slotIndex == currentIndex;
}

@Override
protected void drawBackground() {
parent.drawDefaultBackground();
}

protected int getY(int index) {
return top + 4 - getAmountScrolled() + index * slotHeight + headerPadding;
}

protected int getX(int index) {
return left + width / 2 - getListWidth() / 2 + 2;
}

@Override
protected void drawSlot(int slotIndex, int xPos, int yPos, int heightIn, int mouseXIn, int mouseYIn, float partialTicks) {
int i = this.getY(slotIndex);
int j = this.getX(slotIndex);
ModInfo mod = modList.get(slotIndex);
Minecraft.getInstance().getTextureManager().bindTexture(ModList.getModIcon(mod.id, mod.source));
GlStateManager.color4f(1.0F, 1.0F, 1.0F, 1.0F);
Gui.drawModalRectWithCustomSizedTexture(j, i, 0, 0, 32, 32, 32, 32);
String modName = mod.name;
int j1 = fontRenderer.getStringWidth(modName);
if (j1 > 157)
modName = fontRenderer.trimStringToWidth(modName, 157 - fontRenderer.getStringWidth("...")) + "...";
this.fontRenderer.drawStringWithShadow(modName, (float) (j + 32 + 2), (float) (i + 1), 16777215);
String authors = I18n.format("rift.modlist.authors");
for(String name : mod.authors) {
authors += " " + name;
}
if (mod.authors.size() == 0) authors += " " + I18n.format("rift.modlist.noone");
List<String> list = fontRenderer.listFormattedStringToWidth(authors, 157);
for(String l : list) {
fontRenderer.drawStringWithShadow(l, (float) (j + 32 + 2), (float) (i + 12 + 10 * list.indexOf(l)), 8421504);
}
GuiButton viewButton = new GuiButton(700 + slotIndex, left + width / 2 + getListWidth() / 2 - 61, i + 6, 50, 20,
I18n.format("rift.modlist.view")) {
};
viewButton.render(mouseXIn, mouseYIn, partialTicks);
}

@Override
protected int getContentHeight() {
return this.getSize() * 40;
}

@Override
public boolean mouseClicked(double p_mouseClicked_1_, double p_mouseClicked_3_, int p_mouseClicked_5_) {
int x = left + width / 2 + getListWidth() / 2 - 61, y = 0, index = 0;
while (true) {
if (getY(index) > p_mouseClicked_3_) {
index--;
y = getY(index);
break;
}
index++;
}
if (p_mouseClicked_1_ > x && p_mouseClicked_1_ < x + 50 && p_mouseClicked_3_ > y + 6 && p_mouseClicked_3_ < y + 26) {
Minecraft.getInstance().getSoundHandler().play(PositionedSoundRecord.getMasterRecord(SoundEvents.UI_BUTTON_CLICK, 1.0F));
Minecraft.getInstance().displayGuiScreen(new GuiModsView(modList.get(index)));
currentIndex = -1;
} else if (isMouseInList(p_mouseClicked_1_, p_mouseClicked_3_)) {
Minecraft.getInstance().getSoundHandler().play(PositionedSoundRecord.getMasterRecord(SoundEvents.UI_BUTTON_CLICK, 1.0F));
currentIndex = index;
}
return super.mouseClicked(p_mouseClicked_1_, p_mouseClicked_3_, p_mouseClicked_5_);
}

private boolean isMouseInList(double p_mouseClicked_1_, double p_mouseClicked_3_) {
return p_mouseClicked_3_ > 40 && p_mouseClicked_3_ < parent.height - 40;
}

@Override
protected int getScrollBarX() {
return this.width - 46;
}

public void setCurrentIndex(int currentIndex) {
this.currentIndex = currentIndex;
}

public void searchFilter(String searchTerm) {
String currentId = currentIndex > 0 ? modList.get(currentIndex).id : "";
List<ModInfo> modList = new ArrayList<>();
if (searchTerm.replaceAll(" ", "").equals(""))
modList = new LinkedList<>(parent.modList);
else
for(ModInfo mod : parent.modList) {
List<String> list = new LinkedList<>(mod.authors);
list.addAll(Arrays.asList(new String[]{mod.id, mod.name, mod.source.getName()}));
if (hasMatch(searchTerm, list.toArray(new String[list.size()])))
modList.add(mod);
}
this.modList = new LinkedList<>(modList);
int i = -1;
for(int j = 0; j < modList.size(); j++) {
ModInfo mod = modList.get(j);
if (mod.id.equals(currentId))
i = j;
}
setCurrentIndex(i);
}

private boolean hasMatch(String searchTerm, String... items) {
for(String i : items)
if (i.toLowerCase().contains(searchTerm.toLowerCase()))
return true;
return false;
}

}
132 changes: 132 additions & 0 deletions src/main/java/org/dimdev/rift/modlist/GuiModsView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package org.dimdev.rift.modlist;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.client.resources.I18n;
import net.minecraft.util.Util;
import org.dimdev.riftloader.ModInfo;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

public class GuiModsView extends GuiScreen {

private ModInfo mod;

public GuiModsView(ModInfo mod) {
this.mod = mod;
}

@Override
protected void initGui() {
if (mod.url != null) {
addButton(new GuiButton(601, this.width - 80, 20, 70, 20, I18n.format("rift.modlist.visit_website")) {
@Override
public void onClick(double mouseX, double mouseY) {
super.onClick(mouseX, mouseY);
try {
Util.getOSType().openURL(new URL(mod.url));
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
});
}
}

@Override
public void render(int mouseX, int mouseY, float partialTicks) {
this.overlayBackground(0, 52, 64, 64, 64, 255, 255);
Minecraft.getInstance().getTextureManager().bindTexture(ModList.getModIcon(mod.id, mod.source));
GlStateManager.color4f(1.0F, 1.0F, 1.0F, 1.0F);
Gui.drawModalRectWithCustomSizedTexture(10, 10, 0, 0, 32, 32, 32, 32);

//Draw Dark thing
Tessellator tessellator = Tessellator.getInstance();
BufferBuilder bufferbuilder = tessellator.getBuffer();
this.overlayBackground(52, this.height, 32, 32, 32, 255, 255);

//Draw Shade
GlStateManager.enableBlend();
GlStateManager.blendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ZERO, GlStateManager.DestFactor.ONE);
GlStateManager.disableAlphaTest();
GlStateManager.shadeModel(7425);
GlStateManager.disableTexture2D();
bufferbuilder.begin(7, DefaultVertexFormats.POSITION_TEX_COLOR);
bufferbuilder.pos(0, 52D + 4, 0.0D).tex(0.0D, 1.0D).color(0, 0, 0, 0).endVertex();
bufferbuilder.pos(this.width, 52D + 4, 0.0D).tex(1.0D, 1.0D).color(0, 0, 0, 0).endVertex();
bufferbuilder.pos(this.width, 52D, 0.0D).tex(1.0D, 0.0D).color(0, 0, 0, 255).endVertex();
bufferbuilder.pos(0, 52D, 0.0D).tex(0.0D, 0.0D).color(0, 0, 0, 255).endVertex();
tessellator.draw();
GlStateManager.enableTexture2D();
GlStateManager.shadeModel(7424);
GlStateManager.enableAlphaTest();
GlStateManager.disableBlend();

//Render info
String modName = mod.name != null ? mod.name : mod.id;
int j1 = fontRenderer.getStringWidth(modName);
if (j1 > this.width - 48 - 90)
modName = fontRenderer.trimStringToWidth(modName, this.width - 48 - 90 - 3) + "...";
this.fontRenderer.drawStringWithShadow(modName, 48, 11, 16777215);
String versions = I18n.format("rift.modlist.versions", (mod.version != null ? mod.version : "Unidentified"));
j1 = fontRenderer.getStringWidth(versions);
if (j1 > this.width - 48 - 90)
versions = fontRenderer.trimStringToWidth(versions, this.width - 48 - 90 - 3) + "...";
this.fontRenderer.drawStringWithShadow(versions, 48, 21, 8421504);
if (mod.url != null) {
String url = I18n.format("rift.modlist.url", (mod.url != null ? mod.url : "Unidentified"));
j1 = fontRenderer.getStringWidth(url);
if (j1 > this.width - 48 - 90)
url = fontRenderer.trimStringToWidth(url, this.width - 48 - 90 - 3) + "...";
this.fontRenderer.drawStringWithShadow(url, 48, 31, 8421504);
}
String id = I18n.format("rift.modlist.id", mod.id);
j1 = fontRenderer.getStringWidth(id);
if (j1 > this.width - 48 * 2)
id = fontRenderer.trimStringToWidth(id, this.width - 48 * 2 - 3) + "...";
this.fontRenderer.drawStringWithShadow(id, 48, (float) (60), 8421504);
String description = mod.description != null ? mod.description : "";
List<String> list = this.mc.fontRenderer.listFormattedStringToWidth(description, this.width - 48 * 2);
for(int i1 = 0; i1 < list.size(); ++i1)
this.fontRenderer.drawStringWithShadow(list.get(i1), 48, (float) (75 + 10 * i1), 16777215);

super.render(mouseX, mouseY, partialTicks);
}

/**
* Overlays the background to hide scrolled items
*/
protected void overlayBackground(int startY, int endY, int red, int green, int blue, int startAlpha, int endAlpha) {
Tessellator tessellator = Tessellator.getInstance();
BufferBuilder bufferbuilder = tessellator.getBuffer();
this.mc.getTextureManager().bindTexture(Gui.OPTIONS_BACKGROUND);
GlStateManager.color4f(1.0F, 1.0F, 1.0F, 1.0F);
float f = 32.0F;
bufferbuilder.begin(7, DefaultVertexFormats.POSITION_TEX_COLOR);
bufferbuilder.pos((double) 0, (double) endY, 0.0D).tex(0.0D, (double) ((float) endY / 32.0F)).color(red, green, blue, endAlpha).endVertex();
bufferbuilder.pos((double) (0 + this.width), (double) endY, 0.0D).tex((double) ((float) this.width / 32.0F), (double) ((float) endY / 32.0F)).color(red, green, blue, endAlpha).endVertex();
bufferbuilder.pos((double) (0 + this.width), (double) startY, 0.0D).tex((double) ((float) this.width / 32.0F), (double) ((float) startY / 32.0F)).color(red, green, blue, startAlpha).endVertex();
bufferbuilder.pos((double) 0, (double) startY, 0.0D).tex(0.0D, (double) ((float) startY / 32.0F)).color(red, green, blue, startAlpha).endVertex();
tessellator.draw();
}

@Override
public boolean keyPressed(int p_keyPressed_1_, int p_keyPressed_2_, int p_keyPressed_3_) {
if (p_keyPressed_1_ == 256 && this.allowCloseWithEscape()) {
this.close();
this.mc.displayGuiScreen(ModList.guiModsList);
return true;
} else {
return super.keyPressed(p_keyPressed_1_, p_keyPressed_2_, p_keyPressed_3_);
}
}

}
48 changes: 48 additions & 0 deletions src/main/java/org/dimdev/rift/modlist/ModList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.dimdev.rift.modlist;

import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.DynamicTexture;
import net.minecraft.client.renderer.texture.NativeImage;
import net.minecraft.util.ResourceLocation;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class ModList {

static GuiMods guiModsList;
private static Map<String, ResourceLocation> modIconMap = new HashMap<>();
private static final ResourceLocation packIcon = new ResourceLocation("textures/misc/unknown_pack.png");

public static void displayModList() {
if (guiModsList == null)
guiModsList = new GuiMods();
else
guiModsList.getGuiModListContent().setCurrentIndex(-1);
guiModsList.previousGui = Minecraft.getInstance().currentScreen;
guiModsList.reloadSearch();
Minecraft.getInstance().displayGuiScreen(guiModsList);
}

public static ResourceLocation getModIcon(String modid, File source) {
if (modIconMap.containsKey(modid))
return modIconMap.get(modid);
if (!source.isFile()) return packIcon;
try (JarFile jar = new JarFile(source)) {
JarEntry entry = jar.getJarEntry("pack.png");
if (entry != null) {
InputStream inputStream = jar.getInputStream(entry);
NativeImage nativeImage = NativeImage.read(inputStream);
return Minecraft.getInstance().getTextureManager().getDynamicTextureLocation("modpackicon", new DynamicTexture(nativeImage));
}
} catch (Exception e) {
e.printStackTrace();
}
return packIcon;
}

}
110 changes: 0 additions & 110 deletions src/main/java/org/dimdev/utils/GuiMods.java

This file was deleted.

175 changes: 0 additions & 175 deletions src/main/java/org/dimdev/utils/GuiModsContent.java

This file was deleted.

132 changes: 0 additions & 132 deletions src/main/java/org/dimdev/utils/GuiModsView.java

This file was deleted.

48 changes: 0 additions & 48 deletions src/main/java/org/dimdev/utils/ModList.java

This file was deleted.

20 changes: 10 additions & 10 deletions src/main/resources/assets/rift/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"riftmodlist.mods": "Mods List",
"riftmodlist.openFolder": "Open Mods Folder",
"riftmodlist.authors": "By",
"riftmodlist.noone": "No One",
"riftmodlist.view": "View",
"riftmodlist.done": "Done",
"riftmodlist.search_mods": "Search Mods...",
"riftmodlist.versions": "Version: %s",
"riftmodlist.url": "Website: %s",
"riftmodlist.id": "Mod ID: %s"
"rift.modlist.mods": "Mods List",
"rift.modlist.openFolder": "Open Mods Folder",
"rift.modlist.authors": "By",
"rift.modlist.noone": "No One",
"rift.modlist.view": "View",
"rift.modlist.done": "Done",
"rift.modlist.search_mods": "Search Mods...",
"rift.modlist.versions": "Version: %s",
"rift.modlist.url": "Website: %s",
"rift.modlist.id": "Mod ID: %s"
}