Skip to content

Commit

Permalink
Added Forge Config button
Browse files Browse the repository at this point in the history
  • Loading branch information
Nixuge committed Mar 14, 2023
1 parent 0d432c1 commit 6692fb4
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 22 deletions.
6 changes: 4 additions & 2 deletions src/main/java/me/nixuge/worlddownloader/McMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import wdl.WDL;

@Mod(
modid = McMod.MOD_ID,
name = McMod.NAME,
version = McMod.VERSION,
clientSideOnly = true
// guiFactory = "me.nixuge.worlddownloader.gui.GuiFactory"
clientSideOnly = true,
guiFactory = "me.nixuge.worlddownloader.gui.GuiFactory"
)
@Getter
@Setter
public class McMod {
public static final String MOD_ID = "worlddownloader";
public static final String NAME = "World Downloader";
public static final String VERSION = "4.1.1.0";
public static WDL wdl;

@Getter
@Mod.Instance(value = McMod.MOD_ID)
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/me/nixuge/worlddownloader/gui/GuiFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package me.nixuge.worlddownloader.gui;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraftforge.fml.client.IModGuiFactory;

import java.util.HashSet;
import java.util.Set;

public class GuiFactory implements IModGuiFactory {
@Override
public void initialize(final Minecraft minecraftInstance) {
}

@Override
public Class<? extends GuiScreen> mainConfigGuiClass() {
return ModGuiConfig.class;
}

@Override
public Set<RuntimeOptionCategoryElement> runtimeGuiCategories() {
return new HashSet<>();
}

@Override
public RuntimeOptionGuiHandler getHandlerFor(final RuntimeOptionCategoryElement element) {
return null;
}
}
11 changes: 11 additions & 0 deletions src/main/java/me/nixuge/worlddownloader/gui/ModGuiConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package me.nixuge.worlddownloader.gui;

import me.nixuge.worlddownloader.McMod;
import net.minecraft.client.gui.GuiScreen;
import wdl.gui.GuiWDL;

public class ModGuiConfig extends GuiWDL {
public ModGuiConfig(final GuiScreen guiScreen) {
super(guiScreen, McMod.wdl);
}
}
11 changes: 2 additions & 9 deletions src/main/java/wdl/WDL.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;


import me.nixuge.worlddownloader.McMod;
import net.minecraft.block.Block;
import net.minecraft.block.BlockAir;
import net.minecraft.block.BlockBed;
Expand Down Expand Up @@ -296,6 +296,7 @@ public static void bootstrap(Minecraft minecraft) {
}
if (INSTANCE == null) {
INSTANCE = new WDL(Minecraft.getMinecraft());
McMod.wdl = INSTANCE;
}
}
private WDL(Minecraft minecraft) {
Expand Down Expand Up @@ -1486,14 +1487,6 @@ public String getServerName() {

return name;
}
// else if (minecraft.isConnectedToRealms()) {
// String realmName = getRealmName();
// if (realmName != null) {
// return realmName;
// } else {
// LOGGER.warn("getServerName: getRealmName returned null!");
// }
// }
else {
LOGGER.warn("getServerName: Not connected to either a real server or realms!");
}
Expand Down
65 changes: 54 additions & 11 deletions src/main/java/wdl/gui/GuiWDL.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ private class ButtonEntry extends GuiList.GuiListEntry<ButtonEntry> {
* Constructor.
*
* @param key
* The I18n key, which will have the base for this GUI
* prepended.
* The I18n key, which will have the base for this GUI
* prepended.
* @param openFunc
* Supplier that constructs a GuiScreen to open based off
* of this screen (the one to open when that screen is
* closed) and the WDL instance
* Supplier that constructs a GuiScreen to open based off
* of this screen (the one to open when that screen is
* closed) and the WDL instance
* @param needsPerms
* Whether the player needs download permission to use
* this button.
* Whether the player needs download permission to use
* this button.
*/
public ButtonEntry(String key, BiFunction<GuiScreen, WDL, GuiScreen> openFunc, boolean needsPerms) {
this.button = this.addButton(new ButtonDisplayGui(0, 0, 200, 20,
Expand Down Expand Up @@ -111,26 +111,45 @@ public void drawEntry(int x, int y, int width, int height, int mouseX, int mouse
private final IConfiguration config;

private WDLTextField worldname;
private boolean isServer;

public GuiWDL(@Nullable GuiScreen parent, WDL wdl) {
public GuiWDL(@Nullable GuiScreen parent, @Nullable WDL wdl) {
super(new ChatComponentTranslation("wdl.gui.wdl.title", WDL.baseFolderName));
this.parent = parent;
this.wdl = wdl;
this.config = WDL.serverProps;
this.isServer = getIsServer();
}

public boolean getIsServer() {
if (wdl == null)
return false;

try {
return (wdl != null && wdl.minecraft.getCurrentServerData() != null);
} catch (Exception e) {
return false;
}
}

/**
* Adds the buttons (and other controls) to the screen in question.
*/
@Override
public void initGui() {
// Done button
this.addButton(new ButtonDisplayGui(this.width / 2 - 100, this.height - 29,
200, 20, parent));

if (isServer)
initGuiServer();
}

public void initGuiServer() {
this.worldname = this.addTextField(new WDLTextField(this.fontRendererObj,
this.width / 2 - 155, 19, 150, 18, new ChatComponentTranslation("wdl.gui.wdl.worldname")));
this.worldname.setText(this.config.getValue(MiscSettings.SERVER_NAME));

this.addButton(new ButtonDisplayGui(this.width / 2 - 100, this.height - 29,
200, 20, parent));

this.addList(new GuiWDLButtonList());
}

Expand Down Expand Up @@ -159,10 +178,34 @@ public void drawScreen(int mouseX, int mouseY, float partialTicks) {

super.drawScreen(mouseX, mouseY, partialTicks);

if (this.worldname != null)
drawScreenServer();
else
drawScreenElse();
}

public void drawScreenServer() {
String name = I18n.format("wdl.gui.wdl.worldname");
this.drawString(this.fontRendererObj, name, this.worldname.xPosition
- this.fontRendererObj.getStringWidth(name + " "), 26, 0xFFFFFF);

Utils.drawGuiInfoBox(displayedTooltip, width, height, 48);
}

public void drawScreenElse() {
String[] strings = {
"Looks like you're trying to access the WorldDownloader config",
"outside of a server.",
"Please use this config only when connected to a server."
};
for (int i = 0; i < strings.length; i++) {
String text = strings[i];
this.drawCenteredString(
this.fontRendererObj,
text,
this.width / 2,
((this.height / 2) - 40 + (15 * i)),
0xFFFFFF);
}
}
}

0 comments on commit 6692fb4

Please sign in to comment.