address inspections

This commit is contained in:
Glease committed 2022-09-06 16:34:46 +08:00
1 parent e71edd50b0
commit df4c54ea8d
13 files changed
+61 -81

No files matched your search

@@ -11,19 +11,32 @@ import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
/**
* Represent an item source. Take only, cannot be placed back.
*/
public interface IItemSource {
/**
* We are probably not going to need to get a lot of items from this method
* We are probably not going to need to get a lot of items from this method,
* so I don't care about the overhead of storing all fetched items even if downstream code doesn't need it
*/
@Nonnull
Map<ItemStack, Integer> take(Predicate<ItemStack> predicate, boolean simulate, int count);
/**
* Take exactly one item that matches the predicate.
*
* @return item stack, or null if none matches
*/
default ItemStack takeOne(Predicate<ItemStack> predicate, boolean simulate) {
Map<ItemStack, Integer> take = take(predicate, simulate, 1);
return take.isEmpty() ? null : take.keySet().iterator().next();
}
/**
* Take exactly count amount of items matching predicate.
*
* @return true if at least count amount of items can be/is taken.
*/
default boolean takeAll(Predicate<ItemStack> predicate, boolean simulate, int count) {
// fast path for 1 item take requests
if (count == 1) return takeOne(predicate, simulate) != null;
@@ -33,22 +46,38 @@ public interface IItemSource {
return true;
}
/**
* Take exactly one item. ItemStack given must have a stack size of 1.
*
* @return true if found. false otherwise
*/
default boolean takeOne(ItemStack stack, boolean simulate) {
if (stack == null || stack.getItem() == null || stack.stackSize != 1) throw new IllegalArgumentException();
ItemStack took = takeOne(ItemStackPredicate.from(stack, NBTMode.EXACT), simulate);
return took == null || took.stackSize <= 0;
}
/**
* Take some item. Will not take item if it cannot find enough items to take.
*
* @return true if enough item can be/has been extracted. false otherwise
*/
default boolean takeAll(ItemStack stack, boolean simulate) {
// fast path for 1 item take requests
if (stack.stackSize == 1) return takeOne(stack, simulate);
ItemStackPredicate predicate = ItemStackPredicate.from(stack, NBTMode.EXACT);
Map<ItemStack, Integer> have = take(predicate, true, stack.stackSize);
if (have.values().stream().mapToInt(Integer::intValue).sum() < stack.stackSize) return false;
take(predicate, simulate, stack.stackSize);
if (simulate) return true;
take(predicate, false, stack.stackSize);
return true;
}
/**
* Construct an IItemSource from inventories associated with given player.
* <p>
* This will be backed by {@link InventoryUtility}
*/
static IItemSource fromPlayer(EntityPlayerMP player) {
return new IItemSource() {
@Nonnull
@@ -71,6 +100,11 @@ public interface IItemSource {
};
}
/**
* Construct an IItemSource from given inventory
* <p>
* This will be backed by {@link InventoryUtility}
*/
static IItemSource fromInventory(IInventory inv) {
return (p, s, c) -> InventoryUtility.takeFromInventory(new InventoryIterable<>(inv), p, s, c, true);
}
@@ -141,7 +141,7 @@ public interface IStructureDefinition<T> {
}
/**
* Build the multi in creative mode on a best effort basis. Does not guarantee the structure is correct after a
* Build the multi in creative mode on a best-effort basis. Does not guarantee the structure is correct after a
* successful call.
*
* @param object context object. usually multiblock controller.
@@ -402,7 +402,6 @@ public interface IStructureDefinition<T> {
/**
* Create a new instance of builder.
* @return
* @param <T> type of context object
*/
static <T> StructureDefinition.Builder<T> builder() {
@@ -3,7 +3,7 @@ package com.gtnewhorizon.structurelib.structure;
/**
* Implement this and use {@link StructureUtility#withContext(IStructureElement)} in case you need to record some
* temporary states that don't need to live in your main context, which would usually live for the whole lifetime of the
* structure than just during structure check. Usually just save some few bytes, but becomes non trivial if you need to
* structure than just during structure check. Usually just save some few bytes, but becomes non-trivial if you need to
* record too much stuff.
* <p>
* You will have to reset the context yourself though. The API can't guess when the old context becomes invalid.
@@ -53,7 +53,7 @@ public class StructureDefinition<T> implements IStructureDefinition<T> {
* + is anything but air
* - is air checks
* space bar is skip
* ~ is also skip (but marks controller position, optional and logically it is a space...)
* ~ is also skip (but marks controller position, optional. implementation wise it is a space...)
* rest needs to be defined
* <p>
* next char is next block(a)
@@ -1947,6 +1947,7 @@ public class StructureUtility {
* @deprecated renamed to partitionBy
*/
@SafeVarargs
@Deprecated
public static <T> IStructureElementDeferred<T> defer(
Function<T, Integer> keyExtractor, IStructureElement<T>... array) {
return partitionBy(keyExtractor, array);
@@ -2015,7 +2016,7 @@ public class StructureUtility {
* @param array all possible structure element
* @deprecated renamed to partitionBy
*/
@SuppressWarnings("unchecked")
@Deprecated
public static <T> IStructureElementDeferred<T> defer(
Function<T, Integer> keyExtractor, List<IStructureElement<T>> array) {
return partitionBy(keyExtractor, array);
@@ -2036,7 +2037,7 @@ public class StructureUtility {
@SuppressWarnings("unchecked")
public static <T> IStructureElementDeferred<T> partitionBy(
Function<T, Integer> keyExtractor, List<IStructureElement<T>> array) {
return defer(keyExtractor, array.toArray(new IStructureElement[0]));
return partitionBy(keyExtractor, array.toArray(new IStructureElement[0]));
}
/**
@@ -2568,6 +2569,7 @@ public class StructureUtility {
* @deprecated renamed to partitionBy
*/
@SafeVarargs
@Deprecated
public static <T> IStructureElementDeferred<T> defer(
Function<T, Integer> keyExtractorCheck,
BiFunction<T, ItemStack, Integer> keyExtractor,
@@ -2651,7 +2653,7 @@ public class StructureUtility {
* @param array all possible structure element
* @deprecated renamed to partitionBy
*/
@SuppressWarnings("unchecked")
@Deprecated
public static <T> IStructureElementDeferred<T> defer(
Function<T, Integer> keyExtractorCheck,
BiFunction<T, ItemStack, Integer> keyExtractor,
@@ -2680,7 +2682,7 @@ public class StructureUtility {
Function<T, Integer> keyExtractorCheck,
BiFunction<T, ItemStack, Integer> keyExtractor,
List<IStructureElement<T>> array) {
return defer(keyExtractorCheck, keyExtractor, array.toArray(new IStructureElement[0]));
return partitionBy(keyExtractorCheck, keyExtractor, array.toArray(new IStructureElement[0]));
}
// endregion