Skip to content

Commit

Permalink
Merge pull request #164 from LandOfRails/feature/issue-159-landofsign…
Browse files Browse the repository at this point in the history
…als-crash-with-java-=-12-due-to-caused-by-javalangnosuchfieldexception-modifiers

Feature/issue 159 landofsignals crash with java = 12 due to caused by javalangnosuchfieldexception modifiers
  • Loading branch information
Danielxs01 authored Sep 28, 2024
2 parents cdfeec5 + 2b03551 commit cc247f3
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 116 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
### Release 1.3.3

* Fixed [#159] : Mod crashed with java version 12 or higher due to "javalangnosuchfieldexception modifiers"
* Fixed [#159] : Blocks would glitch in 1.14 - 1.16 after the GUI was opened on the signal or the signalbox. This resulted in the block not reacting to changed states and staying rendered even after the destruction of the block.

### Release 1.3.2

* Fixed [#151] : Rail switch always points left from either perspective
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
public class GuiSignalBoxComplexSignal implements IScreen {

private static TileSignalBox tsb;
private static String signalId;

private final ItemStack itemStackRight;
private final ItemStack itemStackLeft;
Expand All @@ -41,10 +42,8 @@ public class GuiSignalBoxComplexSignal implements IScreen {
private String originalLeftState;

public GuiSignalBoxComplexSignal() {
final TileComplexSignal tsp = tsb.getTileComplexSignal();
String itemId = tsp.getId();

modes = LOSBlocks.BLOCK_COMPLEX_SIGNAL.getAllGroupStates(tsp.getId());
modes = LOSBlocks.BLOCK_COMPLEX_SIGNAL.getAllGroupStates(signalId);
modeGroups = modes.keySet();
originalSignalGroup = tsb.getGroupId(getFirstValue(modeGroups));
signalGroup = originalSignalGroup;
Expand All @@ -56,19 +55,20 @@ public GuiSignalBoxComplexSignal() {

itemStackLeft = new ItemStack(LOSItems.ITEM_COMPLEX_SIGNAL, 1);
final TagCompound tag = itemStackLeft.getTagCompound();
tag.setString("itemId", itemId);
tag.setString("itemId", signalId);
itemStackLeft.setTagCompound(tag);

itemStackRight = new ItemStack(LOSItems.ITEM_COMPLEX_SIGNAL, 1);
final TagCompound tag2 = itemStackRight.getTagCompound();
tag2.setString("itemId", itemId);
tag2.setString("itemId", signalId);
itemStackRight.setTagCompound(tag2);


}

public static void open(final TileSignalBox tileSignalBox) {
public static void open(final TileSignalBox tileSignalBox, String signalId) {
tsb = tileSignalBox;
GuiSignalBoxComplexSignal.signalId = signalId;
LOSGuis.SIGNAL_BOX_COMPLEX_SIGNAL.open(MinecraftClient.getPlayer());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
public class GuiSignalBoxSignalPart implements IScreen {

private static TileSignalBox tsb;
private static String signalId;

private final ItemStack itemStackRight;
private final ItemStack itemStackLeft;
Expand All @@ -34,10 +35,7 @@ public class GuiSignalBoxSignalPart implements IScreen {

public GuiSignalBoxSignalPart() {

final TileSignalPart tsp = tsb.getTileSignalPart();
String itemId = tsp.getId();

states = LOSBlocks.BLOCK_SIGNAL_PART.getAllStates(tsp.getId());
states = LOSBlocks.BLOCK_SIGNAL_PART.getAllStates(signalId);

originalRightState = tsb.getActiveGroupState("");
rightState = originalRightState;
Expand All @@ -47,19 +45,20 @@ public GuiSignalBoxSignalPart() {

itemStackLeft = new ItemStack(LOSItems.ITEM_SIGNAL_PART, 1);
final TagCompound tag = itemStackLeft.getTagCompound();
tag.setString("itemId", itemId);
tag.setString("itemId", signalId);
itemStackLeft.setTagCompound(tag);

itemStackRight = new ItemStack(LOSItems.ITEM_SIGNAL_PART, 1);
final TagCompound tag2 = itemStackRight.getTagCompound();
tag2.setString("itemId", itemId);
tag2.setString("itemId", signalId);
itemStackRight.setTagCompound(tag2);


}

public static void open(final TileSignalBox tileSignalBox) {
public static void open(final TileSignalBox tileSignalBox, String signalId) {
tsb = tileSignalBox;
GuiSignalBoxSignalPart.signalId = signalId;
LOSGuis.SIGNAL_BOX_SIGNAL_PART.open(MinecraftClient.getPlayer());
}

Expand All @@ -76,7 +75,7 @@ public void init(final IScreenBuilder screen) {
groupButton = new Button(screen, -100, 0, GuiText.LABEL_SIGNALGROUP.toString("default")) {
@Override
public void onClick(Player.Hand hand) {

// Signal parts have only one group - so no logic needed here
}
};
new Button(screen, -100, 50, "<-- " + GuiText.LABEL_NOREDSTONE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,22 @@
import net.landofrails.landofsignals.LOSItems;
import net.landofrails.landofsignals.packet.GuiSignalPrioritizationToServerPacket;
import net.landofrails.landofsignals.serialization.EmptyStringMapper;
import net.landofrails.landofsignals.tile.TileComplexSignal;
import net.landofrails.landofsignals.tile.TileSignalPart;
import util.Matrix4;

import java.text.MessageFormat;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Supplier;

import static net.landofrails.landofsignals.packet.GuiSignalPrioritizationToClientPacket.SIGNAL_PART_ID;

public class GuiSignalPrioritization implements IScreen {

private static TileSignalPart tileSignalPart;
private static TileComplexSignal tileComplexSignal;
private static Integer signalType;
private static Vec3i signalPos;
private static String signalId;
private static String[] orderedStates;
private static Map<String, String[]> orderedGroupStates;

private static final Supplier<GuiRegistry.GUI> GUI = () -> LOSGuis.SIGNAL_PRIORITIZATION;

Expand Down Expand Up @@ -133,8 +135,7 @@ public void onClick(Player.Hand hand) {
};

actions[i] = indexOffset -> {
int index = entriesIndex + indexOffset;
itemIndex = index;
itemIndex = entriesIndex + indexOffset;
updateButtons();
};
}
Expand Down Expand Up @@ -176,7 +177,7 @@ public void onEnterKey(IScreenBuilder builder) {
@Override
public void onClose() {

if (tileSignalPart != null) {
if (signalType == SIGNAL_PART_ID) {
if (Arrays.deepEquals(originalStates, states)) {
return;
}
Expand Down Expand Up @@ -242,29 +243,20 @@ else if (useNext)
}

private static <T> T getFirstValue(T[] values) {
for (T object : values)
return object;
if(values != null && values.length >= 1)
return values[0];
return null;
}

private static Map<String, String[]> cleanCopy(Map<String, String[]> map) {
Map<String, String[]> val = new HashMap<>();
map.forEach((groupId, states) -> {
val.put(groupId, Arrays.copyOf(states, states.length));
});
map.forEach((groupId, states) -> val.put(groupId, Arrays.copyOf(states, states.length)));
return val;
}

// Getters for signal-dependent stuff
private String getId() {
if (tileSignalPart != null)
return tileSignalPart.getId();
return tileComplexSignal.getId();
}

private boolean initStates(IScreenBuilder screen) {
if (tileSignalPart != null) {
states = tileSignalPart.getOrderedStates();
if (signalType == SIGNAL_PART_ID) {
states = orderedStates != null ? Arrays.copyOf(orderedStates, orderedStates.length) : null;
if (states == null || states.length == 0) {
screen.close();
return false;
Expand All @@ -275,15 +267,15 @@ private boolean initStates(IScreenBuilder screen) {

groups = new String[0];
} else {
groupStates = tileComplexSignal.getOrderedGroupStates();
groupStates = orderedGroupStates != null? cleanCopy(orderedGroupStates) : null;
if (groupStates == null || groupStates.isEmpty()) {
screen.close();
return false;
}

originalGroupStates = cleanCopy(groupStates);

Map<String, ContentPackSignalGroup> signalGroups = LOSBlocks.BLOCK_COMPLEX_SIGNAL.getAllGroupStates(tileComplexSignal.getId());
Map<String, ContentPackSignalGroup> signalGroups = LOSBlocks.BLOCK_COMPLEX_SIGNAL.getAllGroupStates(signalId);
groups = signalGroups.keySet().toArray(new String[0]);
groupNames = new HashMap<>();
signalGroups.forEach((groupId, group) ->
Expand All @@ -296,22 +288,21 @@ private boolean initStates(IScreenBuilder screen) {
}

private void createItem() {
String blockId = getId();
if (tileSignalPart != null) {
if (signalType == SIGNAL_PART_ID) {
item = new ItemStack(LOSItems.ITEM_SIGNAL_PART, 1);
TagCompound tag = item.getTagCompound();
tag.setString("itemId", blockId);
tag.setString("itemId", signalId);
item.setTagCompound(tag);
} else {
item = new ItemStack(LOSItems.ITEM_COMPLEX_SIGNAL, 1);
TagCompound tag = item.getTagCompound();
tag.setString("itemId", blockId);
tag.setString("itemId", signalId);
item.setTagCompound(tag);
}
}

private void refreshItem() {
if (tileSignalPart != null) {
if (signalType == SIGNAL_PART_ID) {
final TagCompound rightTag = item.getTagCompound();
rightTag.setString("itemState", states[itemIndex]);
item.setTagCompound(rightTag);
Expand All @@ -336,17 +327,13 @@ private void updateGroup(String newGroup) {

// Open-Functions

public static void open(Vec3i signalPos, TileSignalPart tileSignalPart) {
public static void open(Vec3i signalPos, int signalType, String signalId, String[] orderedStates, Map<String, String[]> orderedGroupStates) {
GuiSignalPrioritization.signalPos = signalPos;
GuiSignalPrioritization.tileSignalPart = tileSignalPart;
GuiSignalPrioritization.tileComplexSignal = null;
GUI.get().open(MinecraftClient.getPlayer());
}
GuiSignalPrioritization.signalType = signalType;
GuiSignalPrioritization.signalId = signalId;
GuiSignalPrioritization.orderedStates = orderedStates;
GuiSignalPrioritization.orderedGroupStates = orderedGroupStates;

public static void open(Vec3i signalPos, TileComplexSignal tileComplexSignal) {
GuiSignalPrioritization.signalPos = signalPos;
GuiSignalPrioritization.tileSignalPart = null;
GuiSignalPrioritization.tileComplexSignal = tileComplexSignal;
GUI.get().open(MinecraftClient.getPlayer());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,60 @@
import cam72cam.mod.net.Packet;
import cam72cam.mod.serialization.TagField;
import net.landofrails.landofsignals.gui.GuiSignalPrioritization;
import net.landofrails.landofsignals.serialization.MapStringStringArrayMapper;
import net.landofrails.landofsignals.serialization.StringArrayMapper;
import net.landofrails.landofsignals.tile.TileComplexSignal;
import net.landofrails.landofsignals.tile.TileSignalPart;

import java.util.Map;

public class GuiSignalPrioritizationToClientPacket extends Packet {

public static final int SIGNAL_PART_ID = 1;
public static final int COMPLEX_SIGNAL_ID = 2;

@TagField("signalPos")
Vec3i signalPos;
@TagField("tileSignalPart")
TileSignalPart tileSignalPart;
@TagField("tileComplexSignal")
TileComplexSignal tileComplexSignal;
@TagField("signalType")
int signalType;
@TagField("signalId")
String signalId;
@TagField(value = "orderedStates", mapper = StringArrayMapper.class)
String[] orderedStates = null;
@TagField(value = "orderedGroupStates", mapper = MapStringStringArrayMapper.class)
Map<String, String[]> orderedGroupStates = null;

public GuiSignalPrioritizationToClientPacket() {

}

public GuiSignalPrioritizationToClientPacket(Vec3i signalPos, TileSignalPart tileSignalPart) {
this.signalPos = signalPos;
this.tileSignalPart = tileSignalPart;
public GuiSignalPrioritizationToClientPacket(TileSignalPart tileSignalPart) {
this.signalPos = tileSignalPart.getPos();
this.signalType = SIGNAL_PART_ID;
this.signalId = tileSignalPart.getId();
this.orderedStates = tileSignalPart.getOrderedStates();
}

public GuiSignalPrioritizationToClientPacket(Vec3i signalPos, TileComplexSignal tileComplexSignal) {
this.signalPos = signalPos;
this.tileComplexSignal = tileComplexSignal;
public GuiSignalPrioritizationToClientPacket(TileComplexSignal tileComplexSignal) {
this.signalPos = tileComplexSignal.getPos();
this.signalType = COMPLEX_SIGNAL_ID;
this.signalId = tileComplexSignal.getId();
this.orderedGroupStates = tileComplexSignal.getOrderedGroupStates();
}

@Override
protected void handle() {
if (tileSignalPart != null) {
getWorld().setBlockEntity(signalPos, tileSignalPart);
GuiSignalPrioritization.open(signalPos, tileSignalPart);
} else if (tileComplexSignal != null) {
getWorld().setBlockEntity(signalPos, tileComplexSignal);
GuiSignalPrioritization.open(signalPos, tileComplexSignal);
}
GuiSignalPrioritization.open(signalPos, signalType, signalId, orderedStates, orderedGroupStates);
}

public static void sendToPlayer(Player player, TileSignalPart tileSignalPart) {
new GuiSignalPrioritizationToClientPacket(tileSignalPart.getPos(), tileSignalPart).sendToPlayer(player);
tileSignalPart.markDirty();
new GuiSignalPrioritizationToClientPacket(tileSignalPart).sendToPlayer(player);
}

public static void sendToPlayer(Player player, TileComplexSignal tileComplexSignal) {
new GuiSignalPrioritizationToClientPacket(tileComplexSignal.getPos(), tileComplexSignal).sendToPlayer(player);
tileComplexSignal.markDirty();
new GuiSignalPrioritizationToClientPacket(tileComplexSignal).sendToPlayer(player);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,34 @@

public class SignalBoxTileSignalPartPacket extends Packet {

@TagField("tileSignalPart")
TileSignalPart tileSignalPart;
@TagField("tileComplexSignal")
TileComplexSignal tileComplexSignal;

@TagField("tileSignalBox")
TileSignalBox tileSignalBox;
@TagField("posSignalBox")
Vec3i posSignalBox;
@TagField("signalType")
Byte signalType;
@TagField("signalId")
String signalId;

public SignalBoxTileSignalPartPacket() {
}

public SignalBoxTileSignalPartPacket(final TileSignalPart tileSignalPart, final TileSignalBox tileSignalBox) {
this.tileSignalPart = tileSignalPart;
this.tileSignalBox = tileSignalBox;
this.signalId = tileSignalPart.getId();
this.posSignalBox = tileSignalBox.getPos();
this.signalType = tileSignalBox.getSignalType();
}

public SignalBoxTileSignalPartPacket(final TileComplexSignal tileComplexSignal, final TileSignalBox tileSignalBox) {
this.tileComplexSignal = tileComplexSignal;
this.tileSignalBox = tileSignalBox;
this.signalId = tileComplexSignal.getId();
this.posSignalBox = tileSignalBox.getPos();
this.signalType = tileSignalBox.getSignalType();
}

@Override
protected void handle() {
if (tileSignalPart != null) {
getWorld().setBlockEntity(posSignalBox, tileSignalBox);
getWorld().getBlockEntity(posSignalBox, TileSignalBox.class).setTileSignalPart(tileSignalPart);
GuiSignalBoxSignalPart.open(tileSignalBox);
} else if (tileComplexSignal != null) {
getWorld().setBlockEntity(posSignalBox, tileSignalBox);
getWorld().getBlockEntity(posSignalBox, TileSignalBox.class).setTileComplexSignal(tileComplexSignal);
GuiSignalBoxComplexSignal.open(tileSignalBox);
if (signalType == 0) {
GuiSignalBoxSignalPart.open(getWorld().getBlockEntity(posSignalBox, TileSignalBox.class), signalId);
} else if (signalType == 1) {
GuiSignalBoxComplexSignal.open(getWorld().getBlockEntity(posSignalBox, TileSignalBox.class), signalId);
} else {
LandOfSignals.error("Can't open Signalbox, no tile entity given.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class TileComplexSignal extends BlockEntity implements IManipulate {
@TagField("blockRotation")
private int blockRotate;
@TagField("id")
private final String id;
private String id;
@TagField(value = "signalGroupStates", mapper = MapStringStringMapper.class)
private Map<String, String> signalGroupStates = new HashMap<>();
// for server only
Expand Down
Loading

0 comments on commit cc247f3

Please sign in to comment.