Reduce complexity

This commit is contained in:
tastybento 2023-07-10 22:48:33 -07:00
parent a6bb78c80f
commit 585f720f6f

View File

@ -65,153 +65,117 @@ public class TemplatedPanel extends Panel
private void generatePanel()
{
Map<Integer, PanelItem> items = switch (this.panelTemplate.type())
{
{
case INVENTORY -> this.populateInventoryPanel();
case HOPPER -> this.populateHopperPanel();
case DROPPER -> this.populateDropperPanel();
};
};
super.makePanel(this.user.getTranslation(this.panelTemplate.title(), this.parameters),
items,
items.keySet().stream().max(Comparator.naturalOrder()).orElse(9),
this.user,
this.getListener().orElse(null),
this.panelTemplate.type());
super.makePanel(this.user.getTranslation(this.panelTemplate.title(), this.parameters),
items,
items.keySet().stream().max(Comparator.naturalOrder()).orElse(9),
this.user,
this.getListener().orElse(null),
this.panelTemplate.type());
}
/**
* This method creates map with item indexes and their icons that will be added into
* Inventory Panel.
* @return Map that contains indexes linked to the correct panel item.
*/
@NonNull
private Map<Integer, PanelItem> populateInventoryPanel()
{
// Init item array with the max available size.
private Map<Integer, PanelItem> populateInventoryPanel() {
PanelItem[][] itemArray = new PanelItem[6][9];
processItemData(itemArray);
removeEmptyLines(itemArray);
fillBorder(itemArray);
fillBackground(itemArray);
return createItemMap(itemArray);
}
private void processItemData(PanelItem[][] itemArray) {
// Analyze the GUI button layout a bit.
for (int i = 0; i < this.panelTemplate.content().length; i++)
{
for (int k = 0; k < this.panelTemplate.content()[i].length; k++)
{
ItemTemplateRecord rec = this.panelTemplate.content()[i][k];
if (rec != null && rec.dataMap().containsKey("type"))
{
for (int i = 0; i < panelTemplate.content().length; i++) {
for (int k = 0; k < panelTemplate.content()[i].length; k++) {
ItemTemplateRecord rec = panelTemplate.content()[i][k];
if (rec != null && rec.dataMap().containsKey("type")) {
String type = String.valueOf(rec.dataMap().get("type"));
int counter = this.typeSlotMap.computeIfAbsent(type, key -> 0);
this.typeSlotMap.put(type, counter + 1);
int counter = typeSlotMap.computeIfAbsent(type, key -> 0);
typeSlotMap.put(type, counter + 1);
}
// Make buttons for the GUI
itemArray[i][k] = makeButton(panelTemplate.content()[i][k]);
}
}
}
// Make buttons for the GUI
for (int i = 0; i < this.panelTemplate.content().length; i++)
{
for (int k = 0; k < this.panelTemplate.content()[i].length; k++)
{
itemArray[i][k] = this.makeButton(this.panelTemplate.content()[i][k]);
}
}
// After items are created, remove empty lines.
boolean[] showLine = this.panelTemplate.forcedRows();
for (int i = 0; i < this.panelTemplate.content().length; i++)
{
private void removeEmptyLines(PanelItem[][] itemArray) {
boolean[] showLine = panelTemplate.forcedRows();
for (int i = 0; i < panelTemplate.content().length; i++) {
boolean emptyLine = true;
for (int k = 0; emptyLine && k < this.panelTemplate.content()[i].length; k++)
{
for (int k = 0; emptyLine && k < panelTemplate.content()[i].length; k++) {
emptyLine = itemArray[i][k] == null;
}
// Do not generate fallback for "empty" lines.
showLine[i] = showLine[i] || !emptyLine;
}
}
// Now fill the border.
if (this.panelTemplate.border() != null)
{
PanelItem template = this.makeTemplate(this.panelTemplate.border());
// Hard codded 6
for (int i = 0; i < 6; i++)
{
if (i == 0 || i == 5)
{
private void fillBorder(PanelItem[][] itemArray) {
if (panelTemplate.border() != null) {
PanelItem template = makeTemplate(panelTemplate.border());
// Hard coded 6
for (int i = 0; i < 6; i++) {
if (i == 0 || i == 5) {
// Fill first and last row completely with border.
for (int k = 0; k < 9; k++)
{
if (itemArray[i][k] == null)
{
for (int k = 0; k < 9; k++) {
if (itemArray[i][k] == null) {
itemArray[i][k] = template;
}
}
}
else
{
} else {
// Fill first and last element in row with border.
if (itemArray[i][0] == null)
{
if (itemArray[i][0] == null) {
itemArray[i][0] = template;
}
if (itemArray[i][8] == null)
{
if (itemArray[i][8] == null) {
itemArray[i][8] = template;
}
}
}
showLine[0] = true;
showLine[5] = true;
panelTemplate.forcedRows()[0] = true;
panelTemplate.forcedRows()[5] = true;
}
}
// Now fill the background.
if (this.panelTemplate.background() != null)
{
PanelItem template = this.makeTemplate(this.panelTemplate.background());
for (int i = 0; i < 6; i++)
{
for (int k = 0; k < 9; k++)
{
if (itemArray[i][k] == null)
{
private void fillBackground(PanelItem[][] itemArray) {
if (panelTemplate.background() != null) {
PanelItem template = makeTemplate(panelTemplate.background());
for (int i = 0; i < 6; i++) {
for (int k = 0; k < 9; k++) {
if (itemArray[i][k] == null) {
itemArray[i][k] = template;
}
}
}
}
}
// Now place all panel items with their indexes into item map.
private Map<Integer, PanelItem> createItemMap(PanelItem[][] itemArray) {
Map<Integer, PanelItem> itemMap = new HashMap<>(6 * 9);
int correctIndex = 0;
for (int i = 0; i < itemArray.length; i++)
{
final boolean iterate = showLine[i];
for (int k = 0; iterate && k < itemArray[i].length; k++)
{
if (itemArray[i][k] != null)
{
for (int i = 0; i < itemArray.length; i++) {
final boolean iterate = panelTemplate.forcedRows()[i];
for (int k = 0; iterate && k < itemArray[i].length; k++) {
if (itemArray[i][k] != null) {
itemMap.put(correctIndex, itemArray[i][k]);
}
correctIndex++;
}
}
return itemMap;
}
/**
* This method creates map with item indexes and their icons that will be added into
* hopper Panel.
@ -394,8 +358,8 @@ public class TemplatedPanel extends Panel
// If there are generic click handlers that could be added, then this is a place
// where to process them.
// Click Handlers are managed by custom addon buttons.
return itemBuilder.build();
// Click Handlers are managed by custom addon buttons.
return itemBuilder.build();
}
}
@ -421,8 +385,8 @@ public class TemplatedPanel extends Panel
// Get next slot index.
ItemSlot itemSlot = this.typeIndex.containsKey(type) ?
this.typeIndex.get(type) :
new ItemSlot(0, this.typeSlotMap);
this.typeIndex.get(type) :
new ItemSlot(0, this.typeSlotMap);
this.typeIndex.put(type, itemSlot.nextItemSlot());
// Try to get next object.
@ -475,9 +439,9 @@ public class TemplatedPanel extends Panel
}
// ---------------------------------------------------------------------
// Section: Classes
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
// Section: Classes
// ---------------------------------------------------------------------
/**
@ -500,9 +464,9 @@ public class TemplatedPanel extends Panel
}
// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------
/**