Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creative inventory Helper #53

Merged
merged 4 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/turniplabs/halplibe/HalpLibe.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import turniplabs.halplibe.helper.AchievementHelper;
import turniplabs.halplibe.helper.CreativeHelper;
import turniplabs.halplibe.helper.ModVersionHelper;
import turniplabs.halplibe.helper.NetworkHelper;
import turniplabs.halplibe.util.TomlConfigHandler;
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/turniplabs/halplibe/helper/CreativeHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package turniplabs.halplibe.helper;

import net.minecraft.core.item.IItemConvertible;
import net.minecraft.core.item.ItemStack;
import turniplabs.halplibe.util.CreativeEntry;

public class CreativeHelper {
/**
* @param itemToAdd The itemstack to be added to the creative inventory list
* @param priority the priority for the item to be added, lower numbers appear higher in the list. Default is 1000
*/
@SuppressWarnings("unused")
public static void setPriority(IItemConvertible itemToAdd, int priority){
setPriority(itemToAdd.getDefaultStack(), priority);
}

/**
* @param itemToAdd The itemstack to be added to the creative inventory list
* @param meta the meta value for the itemToAdd
* @param priority the priority for the item to be added, lower numbers appear higher in the list. Default is 1000
*/
@SuppressWarnings("unused")
public static void setPriority(IItemConvertible itemToAdd, int meta, int priority){
setPriority(new ItemStack(itemToAdd, 1, meta), priority);
}
/**
* @param itemToAdd The itemstack to be added to the creative inventory list
* @param priority the priority for the item to be added, lower numbers appear higher in the list. Default is 1000
*/
@SuppressWarnings("unused")
public static void setPriority(ItemStack itemToAdd, int priority){
CreativeEntry.addEntry(new CreativeEntry(itemToAdd, priority));
}

/**
* @param itemToAdd The itemstack to be added to the creative inventory list
* @param itemParent The itemstack that the itemToAdd will be placed after
*/
@SuppressWarnings("unused")
public static void setParent(IItemConvertible itemToAdd, IItemConvertible itemParent){
setParent(itemToAdd.getDefaultStack(), itemParent.getDefaultStack());
}
/**
* @param itemToAdd The itemstack to be added to the creative inventory list
* @param metaToAdd the meta value for the itemToAdd
* @param itemParent The itemstack that the itemToAdd will be placed after
* @param metaParent the meta value for the itemParent
*/
@SuppressWarnings("unused")
public static void setParent(IItemConvertible itemToAdd, int metaToAdd, IItemConvertible itemParent, int metaParent){
setParent(new ItemStack(itemToAdd, 1, metaToAdd), new ItemStack(itemParent, metaParent));
}
/**
* @param itemToAdd The itemstack to be added to the creative inventory list
* @param itemParent The itemstack that the itemToAdd will be placed after
*/
@SuppressWarnings("unused")
public static void setParent(ItemStack itemToAdd, ItemStack itemParent){
CreativeEntry.addEntry(new CreativeEntry(itemToAdd, itemParent));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package turniplabs.halplibe.mixin.mixins;

import com.google.common.collect.Lists;
import net.minecraft.core.block.Block;
import net.minecraft.core.item.ItemStack;
import net.minecraft.core.player.inventory.ContainerPlayerCreative;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import turniplabs.halplibe.HalpLibe;
import turniplabs.halplibe.util.CreativeEntry;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Mixin(value = ContainerPlayerCreative.class, remap = false, priority = 900)
public class ContainerPlayerCreativeMixin {
@Shadow public static int creativeItemsCount;

@Shadow public static List<ItemStack> creativeItems;

@Inject(method = "<clinit>", at = @At("TAIL"))
private static void test(CallbackInfo ci){
List<ItemStack> trimmedList = new ArrayList<>(creativeItems);
List<ItemStack> newList = new ArrayList<>();
for (ItemStack stack : creativeItems){
if (CreativeEntry.priorityEntryMap.containsKey(stack.toString()) || CreativeEntry.childEntryMap.containsKey(stack.toString())){
trimmedList.remove(stack);
}
}

List<ItemStack> trimmedBlocks = new ArrayList<>();
List<ItemStack> trimmedItems = new ArrayList<>();

for (ItemStack stack : trimmedList){
if (stack.itemID < Block.blocksList.length){
trimmedBlocks.add(stack);
} else {
trimmedItems.add(stack);
}
}

List<CreativeEntry> entries = CreativeEntry.priorityEntryMap.values().stream().sorted().collect(Collectors.toList());
boolean hasAddedVanillaBlocks = false;
for (CreativeEntry entry : entries){
if (entry.stackToAdd.itemID < Block.blocksList.length){
if (entry.priority > 1000 && !hasAddedVanillaBlocks){
hasAddedVanillaBlocks = true;
newList.addAll(trimmedBlocks);
}
newList.add(entry.stackToAdd);
}
}
if (!hasAddedVanillaBlocks){
newList.addAll(trimmedBlocks);
}

boolean hasAddedVanillaItems = false;
for (CreativeEntry entry : entries){
if (entry.stackToAdd.itemID >= Block.blocksList.length){
if (entry.priority > 1000 && !hasAddedVanillaItems){
hasAddedVanillaItems = true;
newList.addAll(trimmedItems);
}
newList.add(entry.stackToAdd);
}
}
if (!hasAddedVanillaItems){
newList.addAll(trimmedItems);
}

List<CreativeEntry> entriesChild = Lists.reverse(new ArrayList<>(CreativeEntry.childEntryMap.values()));
for (CreativeEntry entry : entriesChild){
int index = -1;
for (ItemStack stack : newList){
if (stack.toString().equals(entry.parentStack.toString())){
index = newList.indexOf(stack);
break;
}
}
if (index != -1){
newList.add(index + 1, entry.stackToAdd);
} else {
newList.add(entry.stackToAdd);
HalpLibe.LOGGER.warn("Could not find parent stack of '" + entry.parentStack + "' in the list! does it exist? adding stack to end of list!");
}
}

creativeItems = newList;
creativeItemsCount = creativeItems.size();
}
}
43 changes: 43 additions & 0 deletions src/main/java/turniplabs/halplibe/util/CreativeEntry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package turniplabs.halplibe.util;

import net.minecraft.core.item.ItemStack;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;

public class CreativeEntry implements Comparable<CreativeEntry> {
public static final HashMap<String, CreativeEntry> priorityEntryMap = new HashMap<>();
public static final HashMap<String, CreativeEntry> childEntryMap = new HashMap<>();
public static void addEntry(CreativeEntry entry){
String key = entry.stackToAdd.toString();
if (entry.parentStack != null){
childEntryMap.put(key, entry);
priorityEntryMap.remove(key);
} else {
priorityEntryMap.put(key, entry);
childEntryMap.remove(key);
}
}
public int priority;
public final ItemStack stackToAdd;
public ItemStack parentStack;
public CreativeEntry(ItemStack stack){
this(stack, 1000);
}
public CreativeEntry(ItemStack stack, int priority){
this.stackToAdd = stack.copy();
this.stackToAdd.stackSize = 1;
this.priority = priority;
}
public CreativeEntry(ItemStack stackToAdd, ItemStack parentStack){
this.stackToAdd = stackToAdd.copy();
this.stackToAdd.stackSize = 1;
this.parentStack = parentStack.copy();
this.parentStack.stackSize = 1;
}

@Override
public int compareTo(@NotNull CreativeEntry o) {
return priority - o.priority;
}
}
9 changes: 5 additions & 4 deletions src/main/resources/halplibe.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"mixins.BlockFireMixin",
"mixins.BlockMixin",
"mixins.BlockSoundDispatcherMixin",
"mixins.ContainerPlayerCreativeMixin",
"mixins.I18nMixin",
"mixins.ItemStackJsonAdapterMixin",
"mixins.RegistryMixin",
Expand All @@ -34,17 +35,17 @@
"mixins.PlayerRendererMixin",
"mixins.RenderEngineMixin",
"mixins.RenderGlobalMixin",
"mixins.commands.CommandsClientMixin",
"mixins.network.MinecraftMixin",
"mixins.registry.MinecraftMixin",
"mixins.version.NetClientHandlerMixin",
"mixins.commands.CommandsClientMixin"
"mixins.version.NetClientHandlerMixin"
],
"server": [
"mixins.MinecraftServerMixin",
"mixins.commands.CommandsServerMixin",
"mixins.network.MinecraftServerMixin",
"mixins.registry.MinecraftServerMixin",
"mixins.version.NetLoginHandlerMixin",
"mixins.commands.CommandsServerMixin"
"mixins.version.NetLoginHandlerMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down
Loading