blockgoblin31
blockgoblin31
JCHJava Community | Help. Code. Learn.
Created by blockgoblin31 on 10/31/2024 in #java-help
How would I have a thread wait on a lambda?
This is my first time messing with threads. The project takes user input with a lambda, so I need to wait until that lambda gets valid data to resume the thread. From my research I havent found a good way to do that other than while (var == null) {} which doesn't seem ideal.
55 replies
JCHJava Community | Help. Code. Learn.
Created by blockgoblin31 on 7/27/2024 in #java-help
Why/how is displayMap null
I have a class here and it seems like nothing in the constructor is running except the super() call. None of the prints run and when repaint is called it throws an error for displayMap being null. What is wrong?
5 replies
JCHJava Community | Help. Code. Learn.
Created by blockgoblin31 on 7/11/2024 in #java-help
How to add blank lines to the end of a Document?
I'm working on a text editor and it needs to copy some text from one JTextArea to another at the same line, but when one adds a line I can't copy it over with the code I'm using because the document doesnt have enough Elements. My code is
addCaretListener(new CaretListener() {
public void caretUpdate(CaretEvent ce) {
JTextComponent comp = (JTextComponent)ce.getSource();
int pos = comp.getCaretPosition();
Element root = comp.getDocument().getDefaultRootElement();
lineNum = root.getElementIndex(pos);
Element line = root.getElement(lineNum);
try {
String text = comp.getDocument().getText(line.getStartOffset(), line.getEndOffset() - line.getStartOffset());
CodeDoc document = (CodeDoc) other.getDocument();
text = ((CodeView) comp).hasAssm ? CodeView.fromAssm(text) : CodeView.toAssm(text);
document.replace(document.getDefaultRootElement().getElement(lineNum).getStartOffset(), document.getDefaultRootElement().getElement(lineNum).getEndOffset(), text, null);
} catch (BadLocationException e) {
throw new RuntimeException(e);
}
}
});
addCaretListener(new CaretListener() {
public void caretUpdate(CaretEvent ce) {
JTextComponent comp = (JTextComponent)ce.getSource();
int pos = comp.getCaretPosition();
Element root = comp.getDocument().getDefaultRootElement();
lineNum = root.getElementIndex(pos);
Element line = root.getElement(lineNum);
try {
String text = comp.getDocument().getText(line.getStartOffset(), line.getEndOffset() - line.getStartOffset());
CodeDoc document = (CodeDoc) other.getDocument();
text = ((CodeView) comp).hasAssm ? CodeView.fromAssm(text) : CodeView.toAssm(text);
document.replace(document.getDefaultRootElement().getElement(lineNum).getStartOffset(), document.getDefaultRootElement().getElement(lineNum).getEndOffset(), text, null);
} catch (BadLocationException e) {
throw new RuntimeException(e);
}
}
});
48 replies
JCHJava Community | Help. Code. Learn.
Created by blockgoblin31 on 7/10/2024 in #java-help
Text color not changing
I have a custom class that extends JTextArea, I'm eventually going to use it to copy information between two different views but figured I might as well implement a quick dark mode. The problem is my new AttributeSet isn't applied, and I can't tell why. I've used StyledDocument.insertString() before for setting text and background color(working with a JTextPane then), but here it doesnt seem to work the same way. Do I need to extend a different class?
15 replies
JCHJava Community | Help. Code. Learn.
Created by blockgoblin31 on 7/9/2024 in #java-help
Not sure what even is the problem
No description
5 replies
JCHJava Community | Help. Code. Learn.
Created by blockgoblin31 on 6/27/2024 in #java-help
Use a Class for a generic type?
is there a way to for example do
String clazz = "java.lang.String";
List<Class.forName(clazz)> list = new List<>();
String clazz = "java.lang.String";
List<Class.forName(clazz)> list = new List<>();
In case this is xy problem: I have a class called DataObject for storing some extra data on things, and it has a generic type for what type of data it's storing. There's another class called DataMap that takes its own generic type and extends DataObject with a HashMap<DataObject<T>, DataObject<?>>. I'm then serializing it to json with gson as my method of storing it in a file, but I need a custom deserializer for it, and I'm getting errors for it not being able to convert HashMap<DataObject<?>, DataObject<?>> to HashMap<DataObject<T>, DataObject<T>>. Storing the class name in the json is my current idea for a workaround.
24 replies
JCHJava Community | Help. Code. Learn.
Created by blockgoblin31 on 2/25/2024 in #java-help
How do I append text to a JTextPane?
I've only used JTextAreas in the past, but my current project needs colored text, so I need to use JTextPane or JEditorPane. From googling JTextPane seems simpler but I still cant understand how I actually add text
24 replies
JCHJava Community | Help. Code. Learn.
Created by blockgoblin31 on 2/23/2024 in #java-help
How would I listen for keypresses in a window?
I'm working on a game and want to grab keyboard input while a specific window is focused. It wont have many components, should just be a JTextArea(that can't be edited) though Ill probably add more as I get better with JFrames. How would I listen for the keyboard input? A key listener?
5 replies
JCHJava Community | Help. Code. Learn.
Created by blockgoblin31 on 2/7/2024 in #java-help
Exclude files from jar intellij
I'm working on something that adds on to a project that I can't run easily for testing, so I have some test files that emulate it through command prompt(roughly), and I was wondering how I could exclude those files from the final jar. They're already in a test directory. The main problem is they wont compile when I'm making a build, because I need to change the type from my test class to the actual production class which breaks things in the test class.
30 replies
JCHJava Community | Help. Code. Learn.
Created by blockgoblin31 on 2/6/2024 in #java-help
How would I compare HashMap keys with .equals() instead of ==?
HashMap.getOrDefault compares keys with == instead of .equals, is there an equivalent method or something else I could use to compare the keys with .equals() instead?(I assumed it was .equals and would have to recode about half my project if there isnt a workaround)
12 replies
JCHJava Community | Help. Code. Learn.
Created by blockgoblin31 on 2/4/2024 in #java-help
gson really doesn't like my class, does anyone know why?
its only calling .toString() on it instead of convering it to json it can parse back into an object. Class is attached.
9 replies
JCHJava Community | Help. Code. Learn.
Created by blockgoblin31 on 2/1/2024 in #java-help
How do I input longs
I have my own class that extends Random that I made so I can save the seed and use it again later if something weird happens, but it doesn't let me paste the number in from console. Specifically the error I'm getting is java: integer number too large. The number is 3449410621531671124.
8 replies
JCHJava Community | Help. Code. Learn.
Created by blockgoblin31 on 11/28/2023 in #java-help
How does KeyListener work?
I have two(relevant) classes which ive made trying to figure out how this works.
package com.blockgoblin31.game;

import javax.swing.*;
import java.awt.*;

public class Main {
public static int xSize;
public static int ySize;
public static void main(String[] args) {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JTextArea area = new JTextArea();
JScrollPane pane = new JScrollPane(area);
pane.setPreferredSize(new Dimension(375, 125));
area.setEditable(false);
frame.getContentPane().add(pane);
Game game = new Game(area, frame);
frame.addKeyListener(game);
frame.pack();
frame.setVisible(true);
System.out.println("running");
}
}
package com.blockgoblin31.game;

import javax.swing.*;
import java.awt.*;

public class Main {
public static int xSize;
public static int ySize;
public static void main(String[] args) {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JTextArea area = new JTextArea();
JScrollPane pane = new JScrollPane(area);
pane.setPreferredSize(new Dimension(375, 125));
area.setEditable(false);
frame.getContentPane().add(pane);
Game game = new Game(area, frame);
frame.addKeyListener(game);
frame.pack();
frame.setVisible(true);
System.out.println("running");
}
}
package com.blockgoblin31.game;

import com.blockgoblin31.game.input.KeyContainer;

import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;

public class Game implements KeyListener {

private JTextArea area;

private JFrame frame;

public Game(JTextArea area, JFrame frame) {
this.area = area;
this.frame = frame;
}

public boolean close = false;

@Override
public void keyTyped(KeyEvent e) {
turn(new KeyContainer(e));
System.out.println("Text");
}

@Override
public void keyPressed(KeyEvent e) {

}

@Override
public void keyReleased(KeyEvent e) {

}

public void turn(KeyContainer key) {
if (key.isKey("A")) {
area.append("A Pressed");
area.setCaretPosition(area.getDocument().getLength());
}
else if (key.isKeyCombination("C", new KeyContainer.ModifierType[]{KeyContainer.ModifierType.CONTROL})) frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
}
}
package com.blockgoblin31.game;

import com.blockgoblin31.game.input.KeyContainer;

import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;

public class Game implements KeyListener {

private JTextArea area;

private JFrame frame;

public Game(JTextArea area, JFrame frame) {
this.area = area;
this.frame = frame;
}

public boolean close = false;

@Override
public void keyTyped(KeyEvent e) {
turn(new KeyContainer(e));
System.out.println("Text");
}

@Override
public void keyPressed(KeyEvent e) {

}

@Override
public void keyReleased(KeyEvent e) {

}

public void turn(KeyContainer key) {
if (key.isKey("A")) {
area.append("A Pressed");
area.setCaretPosition(area.getDocument().getLength());
}
else if (key.isKeyCombination("C", new KeyContainer.ModifierType[]{KeyContainer.ModifierType.CONTROL})) frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
}
}
39 replies
JCHJava Community | Help. Code. Learn.
Created by blockgoblin31 on 11/21/2023 in #java-help
Check for a specific generic type
I have a variable of the type Supplier<? extends Item> and want to check if it’s Supplier<? extends BlockItem>. Is there a way to do that? instanceOf doesn’t seem to work according to IntelliJ. Minecraft forge 1.16.5 so java 8.
15 replies
JCHJava Community | Help. Code. Learn.
Created by blockgoblin31 on 5/4/2023 in #java-help
Specify generics?
Can I do something like class ArrayList<String> extends ArrayList<E> {? I have a class with a generic and I want to override a method if the type is string, so I can use .matches() instead of ==. Being able to check the type of a generic would also work.
16 replies
JCHJava Community | Help. Code. Learn.
Created by blockgoblin31 on 5/1/2023 in #java-help
Is there a way to return a different type in a subclass method?
I have this
public class List<E> extends java.util.ArrayList<E> {
public List<E> add(E input) {
super.add(input);
return this;
}
public class List<E> extends java.util.ArrayList<E> {
public List<E> add(E input) {
super.add(input);
return this;
}
which seems to not be valid because the method .add(E) in ArrayList returns a boolean. Is there any way I can implement this?
28 replies
JCHJava Community | Help. Code. Learn.
Created by blockgoblin31 on 5/1/2023 in #java-help
Intellij is complaining about this code, not sure why
public K get(int i) {
int j = 0;
this.forEach((K key, V value) -> {
if (j == i) {
return key;
}
j++;
});
return null;
}
public K get(int i) {
int j = 0;
this.forEach((K key, V value) -> {
if (j == i) {
return key;
}
j++;
});
return null;
}
its saying "variable used in lambda expression should be final or effectively final" (about j), and gives the option to convert j to atomic. Im wondering what that means, and what atomic is. (K and V are generics, this class extends HashMap)
16 replies
JCHJava Community | Help. Code. Learn.
Created by blockgoblin31 on 4/18/2023 in #java-help
I am trying to get the index of the lowest value in a array/ArrayList. How do I do this?
I have an array of length 4(which I can make an ArrayList if that would be easier), and I need to find the index of the lowest value in the array, to remove that same index from an ArrayList.
26 replies
JCHJava Community | Help. Code. Learn.
Created by blockgoblin31 on 4/17/2023 in #java-help
Why does one error and not the other?
I am using Intellij, and it is completely fine with this code
@Override
void updateStats() {
this.armor = this.baseArmor;
this.power = this.basePower + this.weapon.power;
this.attackBonus = this.baseAttackBonus;
for (ToolItem t : equippedArmor) {
this.armor += t.armor;
}
for (ToolItem t : equippedTrinkets) {
this.attackBonus += t.bonus;
}
}
@Override
void updateStats() {
this.armor = this.baseArmor;
this.power = this.basePower + this.weapon.power;
this.attackBonus = this.baseAttackBonus;
for (ToolItem t : equippedArmor) {
this.armor += t.armor;
}
for (ToolItem t : equippedTrinkets) {
this.attackBonus += t.bonus;
}
}
but says this does not work, with the error "method does not override method from it's superclass."
@Override
void applyEffects(ArrayList<String> keys) {
for (String key: keys) {
switch (key) {
case "frozen" -> this.power = 0;
case "stuck" -> this.armor /= 2;
case "disarmed" -> this.power -= this.weapon.power;
case "disarmored" -> {
for (ToolItem i : this.equippedArmor) {
if (i != null) this.armor -= i.armor;
}
}
}
}
}
@Override
void applyEffects(ArrayList<String> keys) {
for (String key: keys) {
switch (key) {
case "frozen" -> this.power = 0;
case "stuck" -> this.armor /= 2;
case "disarmed" -> this.power -= this.weapon.power;
case "disarmored" -> {
for (ToolItem i : this.equippedArmor) {
if (i != null) this.armor -= i.armor;
}
}
}
}
}
Both of these methods are overriding equivalent methods from the class this one extends.
26 replies
JCHJava Community | Help. Code. Learn.
Created by blockgoblin31 on 4/6/2023 in #java-help
What is returning false?
With this code, when I run the test method and case two runs, no matter what keys[i] and hide[i] are, it returns false. What is going wrong?(Not sure if it happens with the other cases, my current code sets list to only 0s and 2s)
class Tester {
String[] keys;
boolean[] hide;
int[] list;
Map map;
Room room;
public Tester(Path path, String[] keys, boolean[] hide, int[] list) {
this.keys = keys;
this.room = path.start;
this.map = this.room.map;
this.hide = hide;
this.list = list;
}

boolean test() {
for (int i = 0; i < keys.length; i++) {
switch (list[i]) {
case 1:
if (room.keys.contains(keys[i]) && hide[i]) {
return false;
} else if (!room.keys.contains(keys[i]) && !hide[i]) {
return false;
}
case 2:
if (!map.globals.contains(keys[i]) && hide[i]) {
return false;
} else if (map.globals.contains(keys[i]) && !hide[i]) {
return false;
}
case 3:
if (!room.keys.contains(keys[i])) {
if (map.player.inventory.contains(keys[i])) {

} else {
return false;
}
}
}
}
return true;
}
}
class Tester {
String[] keys;
boolean[] hide;
int[] list;
Map map;
Room room;
public Tester(Path path, String[] keys, boolean[] hide, int[] list) {
this.keys = keys;
this.room = path.start;
this.map = this.room.map;
this.hide = hide;
this.list = list;
}

boolean test() {
for (int i = 0; i < keys.length; i++) {
switch (list[i]) {
case 1:
if (room.keys.contains(keys[i]) && hide[i]) {
return false;
} else if (!room.keys.contains(keys[i]) && !hide[i]) {
return false;
}
case 2:
if (!map.globals.contains(keys[i]) && hide[i]) {
return false;
} else if (map.globals.contains(keys[i]) && !hide[i]) {
return false;
}
case 3:
if (!room.keys.contains(keys[i])) {
if (map.player.inventory.contains(keys[i])) {

} else {
return false;
}
}
}
}
return true;
}
}
8 replies