Skip to content

Commit

Permalink
Code cleanup (#15)
Browse files Browse the repository at this point in the history
* update buildscript

* guard clause

* remove code commented out

* java style array declaration

* unecessary modifiers

* unecessary semicolons

* simplify logic

* inline variables

* access static members via class

* spotlessApply

* fix memory allocation spam
  • Loading branch information
boubou19 authored Feb 15, 2024
1 parent b7072c1 commit 73cd895
Show file tree
Hide file tree
Showing 57 changed files with 827 additions and 713 deletions.
9 changes: 5 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ channel = stable
mappingsVersion = 12

# Defines other MCP mappings for dependency deobfuscation.
remoteMappings = https://raw.githubusercontent.com/MinecraftForge/FML/1.7.10/conf/
remoteMappings = https\://raw.githubusercontent.com/MinecraftForge/FML/1.7.10/conf/

# Select a default username for testing your mod. You can always override this per-run by running
# `./gradlew runClient --username=AnotherPlayer`, or configuring this command in your IDE.
Expand Down Expand Up @@ -61,6 +61,9 @@ gradleTokenModId = GRADLETOKEN_MODID
# [DEPRECATED] Mod name replacement token.
gradleTokenModName = GRADLETOKEN_MODNAME

# [DEPRECATED] Mod Group replacement token.
gradleTokenGroupName =

# [DEPRECATED]
# Multiple source files can be defined here by providing a comma-separated list: Class1.java,Class2.java,Class3.java
# public static final String VERSION = "GRADLETOKEN_VERSION";
Expand Down Expand Up @@ -123,7 +126,7 @@ includeWellKnownRepositories = true
usesMavenPublishing = true

# Maven repository to publish the mod to.
# mavenPublishUrl = https://nexus.gtnewhorizons.com/repository/releases/
# mavenPublishUrl = https\://nexus.gtnewhorizons.com/repository/releases/

# Publishing to Modrinth requires you to set the MODRINTH_TOKEN environment variable to your current Modrinth API token.
#
Expand Down Expand Up @@ -187,5 +190,3 @@ curseForgeRelations =
# This is meant to be set in $HOME/.gradle/gradle.properties.
# ideaCheckSpotlessOnBuild = true

# Non-GTNH properties
gradleTokenGroupName =
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pluginManagement {
}

plugins {
id 'com.gtnewhorizons.gtnhsettingsconvention' version '1.0.8'
id 'com.gtnewhorizons.gtnhsettingsconvention' version '1.0.14'
}


3 changes: 1 addition & 2 deletions src/main/java/gcewing/sg/BaseBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,7 @@ public boolean hasTileEntity(IBlockState state) {
}

public TileEntity getTileEntity(IBlockAccess world, BlockPos pos) {
TileEntity te = world.getTileEntity(pos.x, pos.y, pos.z);
return te;
return world.getTileEntity(pos.x, pos.y, pos.z);
}

public SGBaseTE getSGBaseTE(IBlockAccess world, BlockPos pos) {
Expand Down
99 changes: 58 additions & 41 deletions src/main/java/gcewing/sg/BaseGLRenderTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,38 +60,52 @@ public void start(boolean usingLightmap) {

@Override
public void setTexture(ITexture tex) {
if (texture != tex) {
super.setTexture(tex);
ResourceLocation loc = tex.location();
if (loc != null) {
setGLMode(0);
if (debugGL) SGCraft.log.debug("BaseGLRenderTarget: bindTexture(" + loc + ")");
BaseModClient.bindTexture(loc);
}
setTexturedMode(!tex.isSolid());
setEmissiveMode(tex.isEmissive());
if (texture == tex) {
return;
}

super.setTexture(tex);
ResourceLocation loc = tex.location();
if (loc != null) {
setGLMode(0);
if (debugGL) SGCraft.log.debug("BaseGLRenderTarget: bindTexture(" + loc + ")");
BaseModClient.bindTexture(loc);
}
setTexturedMode(!tex.isSolid());
setEmissiveMode(tex.isEmissive());

}

protected void setEmissiveMode(boolean state) {
int mode = state ? 1 : 0;
if (emissiveMode != mode) {
if (debugGL) SGCraft.log.debug("BaseGLRenderTarget: glSetEnabled(GL_LIGHTING, " + !state + ")");
glSetEnabled(GL_LIGHTING, !state);
if (usingLightmap) setLightmapEnabled(!state);
emissiveMode = mode;
if (emissiveMode == mode) {
return;
}

if (debugGL) SGCraft.log.debug("BaseGLRenderTarget: glSetEnabled(GL_LIGHTING, " + !state + ")");

glSetEnabled(GL_LIGHTING, !state);

if (usingLightmap) setLightmapEnabled(!state);

emissiveMode = mode;

}

protected void setTexturedMode(boolean state) {
int mode = state ? 1 : 0;
if (texturedMode != mode) {
if (debugGL) SGCraft.log.debug("BaseGLRenderTarget.setTexturedMode: " + state);
setGLMode(0);
if (debugGL) SGCraft.log.debug("BaseGLRenderTarget: glSetEnabled(GL_TEXTURE_2D, " + state + ")");
glSetEnabled(GL_TEXTURE_2D, state);
texturedMode = mode;
if (texturedMode == mode) {
return;
}

if (debugGL) SGCraft.log.debug("BaseGLRenderTarget.setTexturedMode: " + state);

setGLMode(0);

if (debugGL) SGCraft.log.debug("BaseGLRenderTarget: glSetEnabled(GL_TEXTURE_2D, " + state + ")");

glSetEnabled(GL_TEXTURE_2D, state);
texturedMode = mode;
}

protected void setLightmapEnabled(boolean state) {
Expand Down Expand Up @@ -127,27 +141,30 @@ protected void rawAddVertex(Vector3 p, double u, double v) {
}

protected void setGLMode(int mode) {
if (glMode != mode) {
if (glMode != 0) {
if (debugGL) SGCraft.log.debug("BaseGLRenderTarget: glEnd()");
glEnd();
}
glMode = mode;
switch (glMode) {
case 0:
break;
case 3:
if (debugGL) SGCraft.log.debug("BaseGLRenderTarget: glBegin(GL_TRIANGLES)");
glBegin(GL_TRIANGLES);
break;
case 4:
if (debugGL) SGCraft.log.debug("BaseGLRenderTarget: glBegin(GL_QUADS)");
glBegin(GL_QUADS);
break;
default:
throw new IllegalStateException(String.format("Invalid glMode %s", glMode));
}
if (glMode == mode) {
return;
}

if (glMode != 0) {
if (debugGL) SGCraft.log.debug("BaseGLRenderTarget: glEnd()");
glEnd();
}
glMode = mode;
switch (glMode) {
case 0:
break;
case 3:
if (debugGL) SGCraft.log.debug("BaseGLRenderTarget: glBegin(GL_TRIANGLES)");
glBegin(GL_TRIANGLES);
break;
case 4:
if (debugGL) SGCraft.log.debug("BaseGLRenderTarget: glBegin(GL_QUADS)");
glBegin(GL_QUADS);
break;
default:
throw new IllegalStateException(String.format("Invalid glMode %s", glMode));
}

}

@Override
Expand Down
110 changes: 65 additions & 45 deletions src/main/java/gcewing/sg/BaseGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ public static class Screen extends GuiContainer implements BaseMod.ISetMod {
final static int defaultTextColor = 0x404040;

protected BaseMod mod;
// double uscale, vscale;
// float red = 1.0F, green = 1.0F, blue = 1.0F;
// public int textColor = defaultTextColor;
// public boolean textShadow = false;

protected Root root;
protected String title;
protected IWidget mouseWidget;
Expand Down Expand Up @@ -160,9 +157,10 @@ public void gRestore() {
if (gstate.previous != null) {
gstate = gstate.previous;
mc.getTextureManager().bindTexture(gstate.texture);
} else {
SGCraft.log.warn("BaseGui: Warning: Graphics state stack underflow");
return;
}

SGCraft.log.warn("BaseGui: Warning: Graphics state stack underflow");
}

public void drawRect(double x, double y, double w, double h) {
Expand Down Expand Up @@ -316,14 +314,18 @@ public static String playerInventoryName() {
@Override
protected void mouseMovedOrUp(int x, int y, int button) {
super.mouseMovedOrUp(x, y, button);
if (mouseWidget != null) {
MouseCoords m = new MouseCoords(mouseWidget, x, y);
if (button == -1) mouseWidget.mouseMoved(m);
else {
mouseWidget.mouseReleased(m, button);
mouseWidget = null;
}
if (mouseWidget == null) {
return;
}

MouseCoords m = new MouseCoords(mouseWidget, x, y);
if (button == -1) {
mouseWidget.mouseMoved(m);
return;
}

mouseWidget.mouseReleased(m, button);
mouseWidget = null;
}

@Override
Expand All @@ -334,19 +336,23 @@ public void mouseClicked(int x, int y, int button) {

protected void mousePressed(int x, int y, int button) {
mouseWidget = root.dispatchMousePress(x, y, button);
if (mouseWidget != null) {
closeOldFocus(mouseWidget);
focusOn(mouseWidget);
mouseWidget.mousePressed(new MouseCoords(mouseWidget, x, y), button);
if (mouseWidget == null) {
return;
}

closeOldFocus(mouseWidget);
focusOn(mouseWidget);
mouseWidget.mousePressed(new MouseCoords(mouseWidget, x, y), button);
}

void closeOldFocus(IWidget clickedWidget) {
if (!isFocused(clickedWidget)) {
IWidgetContainer parent = clickedWidget.parent();
while (!isFocused(parent)) parent = parent.parent();
dispatchClosure(parent.getFocus());
if (isFocused(clickedWidget)) {
return;
}

IWidgetContainer parent = clickedWidget.parent();
while (!isFocused(parent)) parent = parent.parent();
dispatchClosure(parent.getFocus());
}

void dispatchClosure(IWidget target) {
Expand All @@ -358,36 +364,44 @@ void dispatchClosure(IWidget target) {

IWidget getFocusOf(IWidget widget) {
if (widget instanceof IWidgetContainer) return ((IWidgetContainer) widget).getFocus();
else return null;
return null;
}

@Override
public void keyTyped(char c, int key) {
if (!root.dispatchKeyPress(c, key)) {
if (key == 1 || key == mc.gameSettings.keyBindInventory.getKeyCode()) close();
else super.keyTyped(c, key);
if (root.dispatchKeyPress(c, key)) {
return;
}

if (key == 1 || key == mc.gameSettings.keyBindInventory.getKeyCode()) {
close();
return;
}

super.keyTyped(c, key);
}

public void focusOn(IWidget newFocus) {
SGCraft.log.trace(String.format("BaseGui.Screen.focusOn: %s", name(newFocus)));
IWidgetContainer parent = newFocus.parent();
if (parent != null) {
IWidget oldFocus = parent.getFocus();
SGCraft.log.trace(String.format("BaseGui.Screen.focusOn: Old parent focus = %s", name(oldFocus)));
if (isFocused(parent)) {
SGCraft.log.trace("BaseGui.Screen.focusOn: Parent is focused");
if (oldFocus != newFocus) {
tellFocusChanged(oldFocus, false);
parent.setFocus(newFocus);
tellFocusChanged(newFocus, true);
}
} else {
SGCraft.log.trace("BaseGui.Screen.focusOn: Parent is not focused");
if (parent == null) {
return;
}
IWidget oldFocus = parent.getFocus();
SGCraft.log.trace(String.format("BaseGui.Screen.focusOn: Old parent focus = %s", name(oldFocus)));
if (isFocused(parent)) {
SGCraft.log.trace("BaseGui.Screen.focusOn: Parent is focused");
if (oldFocus != newFocus) {
tellFocusChanged(oldFocus, false);
parent.setFocus(newFocus);
focusOn(parent);
tellFocusChanged(newFocus, true);
}
return;
}

SGCraft.log.trace("BaseGui.Screen.focusOn: Parent is not focused");
parent.setFocus(newFocus);
focusOn(parent);
}

public void focusChanged(boolean state) {}
Expand All @@ -405,9 +419,13 @@ static boolean isFocused(IWidget widget) {

static void tellFocusChanged(IWidget widget, boolean state) {
SGCraft.log.trace(String.format("BaseGui.tellFocusChanged: to %s for %s", state, name(widget)));
if (widget != null) {
widget.focusChanged(state);
if (widget instanceof IWidgetContainer) tellFocusChanged(((IWidgetContainer) widget).getFocus(), state);
if (widget == null) {
return;
}

widget.focusChanged(state);
if (widget instanceof IWidgetContainer) {
tellFocusChanged(((IWidgetContainer) widget).getFocus(), state);
}
}

Expand Down Expand Up @@ -609,10 +627,12 @@ public void add(int left, int top, IWidget widget) {

public void remove(IWidget widget) {
widgets.remove(widget);
if (getFocus() == widget) {
if (isFocused(this)) tellFocusChanged(widget, false);
setFocus(null);
if (getFocus() != widget) {
return;
}

if (isFocused(this)) tellFocusChanged(widget, false);
setFocus(null);
}

@Override
Expand Down Expand Up @@ -800,7 +820,7 @@ public static Ref ref(Object target, String getterName, String setterName) {

public interface Action {

public void perform();
void perform();
}

public static class MethodAction implements Action {
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/gcewing/sg/BaseInventoryUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,17 @@ public boolean set(int i, ItemStack stack) {
if (base.canInsertItem(slot, stack, side)) {
base.setInventorySlotContents(slot, stack);
return true;
} else return false;
}

return false;
}

public ItemStack extract(int i) {
int slot = slots[i];
ItemStack stack = base.getStackInSlot(slot);
if (base.canExtractItem(slot, stack, side)) return stack;
else return null;

return null;
}

}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/gcewing/sg/BaseItemBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ else if (world.canPlaceEntityOnSide(this.field_150939_a, x, y, z, false, side, p
--stack.stackSize;
}
return true;
} else return false;
}

return false;
}

@Override
Expand Down
Loading

0 comments on commit 73cd895

Please sign in to comment.