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

Add block/meta and item/meta pair classes #98

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ private static void register(ModContainer classOwner, Class<?> target, ObjectSet
for (MethodInfo method : methods) {
try {
if (method.getOptionalMod() != null) {
if (!optionalMods.computeIfAbsent(method.getOptionalMod(), Loader::isModLoaded)) {
if (!optionalMods
.computeIfAbsent(method.getOptionalMod(), (Predicate<String>) Loader::isModLoaded)) {
RecursivePineapple marked this conversation as resolved.
Show resolved Hide resolved
continue;
}
}
Expand Down
80 changes: 80 additions & 0 deletions src/main/java/com/gtnewhorizon/gtnhlib/util/data/BlockMeta.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.gtnewhorizon.gtnhlib.util.data;

import javax.annotation.Nonnull;

import net.minecraft.block.Block;

/**
* A mutable implementation of {@link ImmutableBlockMeta}. If your API should return a mutable pair, return this
* instead. Must follow the same contracts as the immutable version if this is ever upcast to a
* {@link ImmutableBlockMeta} in your API. If this type is exposed instead of the immutable interface, assume that the
* contained values can change.
*/
public class BlockMeta implements ImmutableBlockMeta {
YannickMG marked this conversation as resolved.
Show resolved Hide resolved

private Block block;
private int meta;

public BlockMeta() {}

public BlockMeta(Block block, int meta) {
this.block = block;
RecursivePineapple marked this conversation as resolved.
Show resolved Hide resolved
this.meta = meta;
}

@Override
@SuppressWarnings("null")
public Block getBlock() {
RecursivePineapple marked this conversation as resolved.
Show resolved Hide resolved
return block;
}

@Override
public int getBlockMeta() {
return meta;
}

/**
* Note: see the header comment in {@link ImmutableBlockMeta} for this method's contract.
*/
public BlockMeta setBlock(@Nonnull Block block) {
this.block = block;

return this;
}

/**
* Note: see the header comment in {@link ImmutableBlockMeta} for this method's contract.
*/
public BlockMeta setBlockMeta(int meta) {
this.meta = meta;

return this;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((block == null) ? 0 : block.hashCode());
RecursivePineapple marked this conversation as resolved.
Show resolved Hide resolved
result = prime * result + meta;
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
BlockMeta other = (BlockMeta) obj;
if (block == null) {
if (other.block != null) return false;
} else if (!block.equals(other.block)) return false;
if (meta != other.meta) return false;
return true;
}

@Override
public String toString() {
return "BlockMeta [block=" + block + ", meta=" + meta + "]";
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/gtnewhorizon/gtnhlib/util/data/IMod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.gtnewhorizon.gtnhlib.util.data;

/**
* An interface for any mod enums. Represents a mod.
*/
public interface IMod {

boolean isModLoaded();

/** Gets the mod id. */
String getID();

/** Gets the mod's resource location prefix. */
String getResourceLocation();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.gtnewhorizon.gtnhlib.util.data;

import javax.annotation.Nonnull;

import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraftforge.oredict.OreDictionary;

/**
* An immutable block-meta pair. This must not be cast down to its mutable version unless you have a very good reason.
* It can be assumed that the values of {@link #getBlock()} and {@link #getBlockMeta()} will never change for this
* object if the object is exposed through an API.
*/
public interface ImmutableBlockMeta {

/**
* The value of this must not change while this object is exposed via an API.
*
* @return The block stored in this pair.
*/
@Nonnull
public Block getBlock();

/**
* The value of this must not change while this object is exposed via an API.
*
* @return The block's metadata stored in this pair.
*/
public int getBlockMeta();

/**
* Gets the corresponding item for this block. Subclasses may provide a faster implementation.
*/
public default Item getItem() {
return Item.getItemFromBlock(getBlock());
}

/**
* Checks if this pair matches the given block & meta.
*
* @param block The block.
* @param meta The meta. If this parameter or {@link #getBlockMeta()} equals {@link OreDictionary.WILDCARD_VALUE}
* then meta checks are ignored.
* @return Whether this pair matches or not.
*/
public default boolean matches(Block block, int meta) {
return getBlock() == block
&& (meta == OreDictionary.WILDCARD_VALUE || getBlockMeta() == OreDictionary.WILDCARD_VALUE
|| getBlockMeta() == meta);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.gtnewhorizon.gtnhlib.util.data;

import javax.annotation.Nonnull;

import net.minecraft.block.Block;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.OreDictionary;

/**
* An immutable item-meta pair. This must not be cast down to its mutable version unless you have a very good reason. It
* can be assumed that the values of {@link #getItem()} and {@link #getItemMeta()} will never change for this object if
* the object is exposed through an API.
*/
public interface ImmutableItemMeta {

/**
* The value of this must not change while this object is exposed via an API.
*
* @return The item stored in this pair.
*/
@Nonnull
public Item getItem();

/**
* The value of this must not change while this object is exposed via an API.
*
* @return The item's metadata stored in this pair.
*/
public int getItemMeta();

/**
* Gets the corresponding block for this item. Subclasses may provide a faster implementation.
*/
public default Block getBlock() {
return Block.getBlockFromItem(getItem());
}

/**
* Checks if this pair matches the given ItemStack's item and metadata.
*/
public default boolean matches(ItemStack stack) {
if (stack == null) return false;

return matches(stack.getItem(), Items.feather.getDamage(stack));
}

/**
* Checks if this pair matches the given item & meta.
*
* @param Item The item.
* @param meta The meta. If this parameter or {@link #getItemMeta()} equals {@link OreDictionary.WILDCARD_VALUE}
* then meta checks are ignored.
* @return Whether this pair matches or not.
*/
public default boolean matches(Item item, int meta) {
return getItem() == item
&& (meta == OreDictionary.WILDCARD_VALUE || getItemMeta() == OreDictionary.WILDCARD_VALUE
|| getItemMeta() == meta);
}
}
79 changes: 79 additions & 0 deletions src/main/java/com/gtnewhorizon/gtnhlib/util/data/ItemMeta.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.gtnewhorizon.gtnhlib.util.data;

import javax.annotation.Nonnull;

import net.minecraft.item.Item;

/**
* A mutable implementation of {@link ImmutableItemMeta}. If your API should return a mutable pair, return this instead.
* Must follow the same contracts as the immutable version if this is ever upcast to a {@link ImmutableItemMeta} in your
* API. If this type is exposed instead of the immutable interface, assume that the contained values can change.
*/
public class ItemMeta implements ImmutableItemMeta {

private Item item;
private int meta;

public ItemMeta() {}

public ItemMeta(Item item, int meta) {
this.item = item;
this.meta = meta;
}

@Override
@SuppressWarnings("null")
public Item getItem() {
return item;
}

@Override
public int getItemMeta() {
return meta;
}

/**
* Note: see the header comment in {@link ImmutableItemMeta} for this method's contract.
*/
public ItemMeta setItem(@Nonnull Item item) {
this.item = item;

return this;
}

/**
* Note: see the header comment in {@link ImmutableItemMeta} for this method's contract.
*/
public ItemMeta setItemMeta(int meta) {
this.meta = meta;

return this;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((item == null) ? 0 : item.hashCode());
result = prime * result + meta;
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
ItemMeta other = (ItemMeta) obj;
if (item == null) {
if (other.item != null) return false;
} else if (!item.equals(other.item)) return false;
if (meta != other.meta) return false;
return true;
}

@Override
public String toString() {
return "ItemMeta [item=" + item + ", meta=" + meta + "]";
}
}
32 changes: 32 additions & 0 deletions src/main/java/com/gtnewhorizon/gtnhlib/util/data/Lazy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.gtnewhorizon.gtnhlib.util.data;

import java.util.function.Supplier;

/**
* Just a container that will lazy-load a value. Can be used in the place of a Supplier<T>
*/
public class Lazy<T> implements Supplier<T> {

private boolean hasValue = false;
private T value;

private Supplier<T> getter;

public Lazy(Supplier<T> getter) {
this.getter = getter;
}

/**
* Gets the value. Thread safe.
*/
@Override
public synchronized T get() {
if (!hasValue) {
value = getter.get();
getter = null;
hasValue = true;
}

return value;
}
}
Loading