-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from UselessBullets/CreativeInventory
Creative inventory Helper
- Loading branch information
Showing
5 changed files
with
206 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/turniplabs/halplibe/helper/CreativeHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
src/main/java/turniplabs/halplibe/mixin/mixins/ContainerPlayerCreativeMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters