Skip to content

Commit

Permalink
[#23/#25] Fixed new item selection
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielxs01 committed Nov 21, 2021
1 parent b45982e commit bd65f5d
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.landofrails.stellwand.content.guis;

import cam72cam.mod.MinecraftClient;
import cam72cam.mod.entity.Player;
import cam72cam.mod.gui.GuiRegistry;
import cam72cam.mod.gui.helpers.ItemPickerGUI;
Expand All @@ -13,32 +14,35 @@
import net.landofrails.stellwand.contentpacks.Content;
import net.landofrails.stellwand.contentpacks.entries.ContentPack;
import net.landofrails.stellwand.contentpacks.types.EntryType;
import net.landofrails.stellwand.utils.exceptions.ContentPackException;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.function.Consumer;

public class SelectItem implements IScreen {

// Only initialized once
private static final Map<EntryType, Map.Entry<ItemStack, Consumer<ItemStack>>> ENTRYTYPES = new HashMap<>();
private static GuiRegistry.GUI gui;

// Initialized every call
private final List<Button> contentPackButtons = new LinkedList<>();
private Consumer<ItemStack> selectedItem;
private ItemStack current = null;
private ItemStack current;
private EntryType entryType;
private ItemStack defaultItem;

public static void init(GuiRegistry.GUI gui) {
SelectItem.gui = gui;
}

public static void setEntryType(EntryType type, Map.Entry<ItemStack, Consumer<ItemStack>> defaultAndSelectedItem) {
ENTRYTYPES.putIfAbsent(type, defaultAndSelectedItem);
}

@Override
public void init(IScreenBuilder screen) {

ItemStack itemStack = MinecraftClient.getPlayer().getHeldItem(Player.Hand.PRIMARY);
this.entryType = getEntryTypeFromItem(itemStack);

generateContentPackButtons(screen, Content.getContentPacksFor(entryType));

}
Expand All @@ -51,13 +55,9 @@ public void onEnterKey(IScreenBuilder builder) {
@Override
public void onClose() {
if (current != null)
selectedItem.accept(current);
ENTRYTYPES.get(entryType).getValue().accept(current);
// Reset variables
contentPackButtons.clear();
selectedItem = null;
entryType = null;
current = null;
defaultItem = null;
}

@Override
Expand All @@ -75,7 +75,7 @@ private List<ItemStack> getItemStackForContentPack(ContentPack pack, EntryType t
items.add(is);
});
if (items.isEmpty())
items.add(defaultItem);
items.add(ENTRYTYPES.get(entryType).getKey());
return items;
}

Expand All @@ -88,21 +88,25 @@ private CustomItem getCustomItemForEntryType(EntryType type) {
case BLOCKSIGNAL:
return CustomItems.ITEMBLOCKSIGNAL;
case BLOCKFILLER:
default:
return CustomItems.ITEMBLOCKFILLER;
case BLOCKRECEIVER:
return CustomItems.ITEMBLOCKRECEIVER;
default:
throw new ContentPackException("I must have forgot to add this EntryType to this method: " + type);
}
}

private void generateContentPackButtons(IScreenBuilder screen, List<ContentPack> contentPacks) {
for (int i = 0; i < contentPacks.size(); i++) {
ContentPack contentPack = contentPacks.get(i);
private void generateContentPackButtons(IScreenBuilder screen, Collection<ContentPack> contentPacks) {
int index = 0;
for (ContentPack contentPack : contentPacks) {
int height = 20;
int width = 200;
int x = -width / 2;
int marginY = 5;
int offsetY = -50;
int y = ((height + marginY) * i) + offsetY;
Button button = new Button(screen, x, y, width, height, contentPack.getButtonName()) {
int y = ((height + marginY) * index) + offsetY;

new Button(screen, x, y, width, height, contentPack.getButtonName()) {
@Override
public void onClick(Player.Hand hand) {
List<ItemStack> selection = getItemStackForContentPack(contentPack, entryType);
Expand All @@ -113,8 +117,24 @@ public void onClick(Player.Hand hand) {
ip.show();
}
};
contentPackButtons.add(button);

index++;
}
}

private EntryType getEntryTypeFromItem(ItemStack itemStack) {
if (itemStack.is(CustomItems.ITEMBLOCKFILLER)) {
return EntryType.BLOCKFILLER;
} else if (itemStack.is(CustomItems.ITEMBLOCKMULTISIGNAL)) {
return EntryType.BLOCKMULTISIGNAL;
} else if (itemStack.is(CustomItems.ITEMBLOCKRECEIVER)) {
return EntryType.BLOCKRECEIVER;
} else if (itemStack.is(CustomItems.ITEMBLOCKSENDER)) {
return EntryType.BLOCKSENDER;
} else if (itemStack.is(CustomItems.ITEMBLOCKSIGNAL)) {
return EntryType.BLOCKSIGNAL;
}
return null;
}

/**
Expand All @@ -125,10 +145,8 @@ public void onClick(Player.Hand hand) {
* @param defaultItem Default item if no items are found
* @param selectedItem Consumer for returning itemstack
*/
public void open(Player player, EntryType type, ItemStack defaultItem, Consumer<ItemStack> selectedItem) {
this.selectedItem = selectedItem;
entryType = type;
this.defaultItem = defaultItem;
public static void open(Player player, EntryType type, ItemStack defaultItem, Consumer<ItemStack> selectedItem) {
setEntryType(type, new AbstractMap.SimpleEntry<>(defaultItem, selectedItem));
gui.open(player);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static void register() {

}

// Clientsided
// Clientside
public static void registerRenderers() {
for (CustomItem item : itemList) {
if (item instanceof ICustomTexturePath) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.lwjgl.opengl.GL11;

import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;

public class ItemBlockButtonReceiver extends CustomItem {
Expand Down Expand Up @@ -67,7 +68,7 @@ public static void init() {
}

// ContentPack
for (Map.Entry<ContentPackEntry, String> entry : Content.getBlockReceivers().entrySet()) {
for (Entry<ContentPackEntry, String> entry : Content.getBlockReceivers().entrySet()) {
try {
ContentPackEntry cpe = entry.getKey();
String packId = entry.getValue();
Expand Down Expand Up @@ -110,9 +111,9 @@ public List<ItemStack> getItemVariants(CreativeTab creativeTab) {

public ItemStack getFirstVarient() {
ItemStack is = null;
Iterator<Map.Entry<ContentPackEntry, String>> it = Content.getBlockReceivers().entrySet().iterator();
Iterator<Entry<ContentPackEntry, String>> it = Content.getBlockReceivers().entrySet().iterator();
if (it.hasNext()) {
Map.Entry<ContentPackEntry, String> entry = it.next();
Entry<ContentPackEntry, String> entry = it.next();

ContentPackEntry cpe = entry.getKey();
is = new ItemStack(CustomItems.ITEMBLOCKRECEIVER, 1);
Expand All @@ -132,8 +133,7 @@ public void onClickAir(Player player, World world, Player.Hand hand) {
}

int sizeInHand = player.getHeldItem(hand).getCount();
SelectItem si = new SelectItem();
si.open(player, EntryType.BLOCKRECEIVER, new ItemStack(CustomItems.ITEMBLOCKRECEIVER, 1), item -> {
SelectItem.open(player, EntryType.BLOCKRECEIVER, new ItemStack(CustomItems.ITEMBLOCKRECEIVER, 1), item -> {
if (item != null) {
player.setHeldItem(hand, item);
item.setCount(sizeInHand);
Expand Down Expand Up @@ -232,11 +232,7 @@ public ClickResult onClickBlock(Player player, World world, Vec3i pos, Player.Ha
blockEntity.setContentBlockId(MISSING);
blockEntity.renderEntity
.setRotation(player.getRotationYawHead());
if (facing.equals(Facing.DOWN) || facing.equals(Facing.UP)) {
blockEntity.setWallMounted(false);
} else {
blockEntity.setWallMounted(true);
}
blockEntity.setWallMounted(!facing.equals(Facing.DOWN) && !facing.equals(Facing.UP));
}
//

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ public void onClickAir(Player player, World world, Hand hand) {
return;

int sizeInHand = player.getHeldItem(hand).getCount();
SelectItem si = new SelectItem();
si.open(player, EntryType.BLOCKFILLER, new ItemStack(CustomItems.ITEMBLOCKFILLER, 1), item -> {
SelectItem.open(player, EntryType.BLOCKFILLER, new ItemStack(CustomItems.ITEMBLOCKFILLER, 1), item -> {
if (item != null) {
player.setHeldItem(hand, item);
item.setCount(sizeInHand);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.lwjgl.opengl.GL11;

import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;

public class ItemBlockMultisignal extends CustomItem {
Expand Down Expand Up @@ -67,7 +68,7 @@ public static void init() {
}

// ContentPack
for (Map.Entry<ContentPackEntry, String> entry : Content.getBlockMultisignals().entrySet()) {
for (Entry<ContentPackEntry, String> entry : Content.getBlockMultisignals().entrySet()) {
try {
ContentPackEntry cpe = entry.getKey();
String packId = entry.getValue();
Expand Down Expand Up @@ -110,9 +111,9 @@ public List<ItemStack> getItemVariants(CreativeTab creativeTab) {

public ItemStack getFirstVarient() {
ItemStack is = null;
Iterator<Map.Entry<ContentPackEntry, String>> it = Content.getBlockMultisignals().entrySet().iterator();
Iterator<Entry<ContentPackEntry, String>> it = Content.getBlockMultisignals().entrySet().iterator();
if (it.hasNext()) {
Map.Entry<ContentPackEntry, String> entry = it.next();
Entry<ContentPackEntry, String> entry = it.next();

ContentPackEntry cpe = entry.getKey();
is = new ItemStack(CustomItems.ITEMBLOCKMULTISIGNAL, 1);
Expand All @@ -130,10 +131,9 @@ public void onClickAir(Player player, World world, Player.Hand hand) {
if (world.isServer) {
return;
}

int sizeInHand = player.getHeldItem(hand).getCount();
SelectItem si = new SelectItem();
si.open(player, EntryType.BLOCKMULTISIGNAL, new ItemStack(CustomItems.ITEMBLOCKMULTISIGNAL, 1), item -> {
SelectItem.open(player, EntryType.BLOCKMULTISIGNAL, new ItemStack(CustomItems.ITEMBLOCKMULTISIGNAL, 1), item -> {
if (item != null) {
player.setHeldItem(hand, item);
item.setCount(sizeInHand);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ public void onClickAir(Player player, World world, Hand hand) {
return;

int sizeInHand = player.getHeldItem(hand).getCount();
SelectItem si = new SelectItem();
si.open(player, EntryType.BLOCKSENDER, new ItemStack(CustomItems.ITEMBLOCKSENDER, 1), item -> {
SelectItem.open(player, EntryType.BLOCKSENDER, new ItemStack(CustomItems.ITEMBLOCKSENDER, 1), item -> {
if (item != null) {
player.setHeldItem(hand, item);
item.setCount(sizeInHand);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ public void onClickAir(Player player, World world, Hand hand) {
}

int sizeInHand = player.getHeldItem(hand).getCount();
SelectItem si = new SelectItem();
si.open(player, EntryType.BLOCKSIGNAL, new ItemStack(CustomItems.ITEMBLOCKSIGNAL, 1), item -> {
SelectItem.open(player, EntryType.BLOCKSIGNAL, new ItemStack(CustomItems.ITEMBLOCKSIGNAL, 1), item -> {
if (item != null) {
player.setHeldItem(hand, item);
item.setCount(sizeInHand);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
import net.landofrails.stellwand.contentpacks.types.EntryType;
import net.landofrails.stellwand.utils.exceptions.ContentPackException;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;

public class Content {
Expand All @@ -20,7 +17,7 @@ private Content() {

}

private static List<ContentPack> contentPacks = new LinkedList<>();
private static final List<ContentPack> contentPacks = new LinkedList<>();

public static void addContentPack(ContentPack pack) {

Expand Down Expand Up @@ -109,16 +106,15 @@ public static void testOutput() {

}

public static List<ContentPack> getContentPacksFor(EntryType type) {
public static Collection<ContentPack> getContentPacksFor(EntryType type) {
// @formatter:off
return contentPacks.stream()
.filter(
cp -> cp.getEntries().stream()
.anyMatch(
cpe -> cpe.isType(type)
)
)
.distinct().collect(Collectors.toList());
).collect(Collectors.toSet());
// @formatter:on
}

Expand Down

0 comments on commit bd65f5d

Please sign in to comment.