Skip to content

Instantly share code, notes, and snippets.

@Lanse505
Last active May 26, 2020 15:19
Show Gist options
  • Save Lanse505/427f03d7368595d88cc624353d43fa4d to your computer and use it in GitHub Desktop.
Save Lanse505/427f03d7368595d88cc624353d43fa4d to your computer and use it in GitHub Desktop.
package com.teamacronymcoders.quantumquarry.recipe;
import com.google.gson.JsonObject;
import com.teamacronymcoders.quantumquarry.json.JsonHelper;
import com.teamacronymcoders.quantumquarry.misc.ItemStackKey;
import net.minecraft.block.BlockState;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.util.JsonUtils;
public class MinerEntry {
private String modid;
private double weight;
private ResourceLocation id;
private ItemStackKey lens;
private BlockState state;
private CompoundNBT data;
public MinerEntry() {}
public MinerEntry(String modid, double weight, ItemStackKey lens, BlockState state, CompoundNBT data) {
this.modid = modid;
this.weight = weight;
this.lens = lens;
this.state = state;
this.data = data;
}
public MinerEntry(String modid, double weight, ResourceLocation id, ItemStackKey lens, BlockState state, CompoundNBT data) {
this.modid = modid;
this.weight = weight;
this.id = id;
this.lens = lens;
this.state = state;
this.data = data;
}
public void setModid(String modid) {
this.modid = modid;
}
public String getModid() {
return modid;
}
public void setId(ResourceLocation id) {
this.id = id;
}
public ResourceLocation getId() {
return id;
}
public void setWeight(double weight) {
this.weight = weight;
}
public double getWeight() {
return weight;
}
public MinerEntry setLens(ItemStackKey lens) {
this.lens = lens;
return this;
}
public ItemStackKey getLens() {
return lens;
}
public MinerEntry setState(BlockState state) {
this.state = state;
return this;
}
public BlockState getState() {
return state;
}
public MinerEntry setData(CompoundNBT data) {
this.data = data;
return this;
}
public CompoundNBT getData() {
return data;
}
public JsonObject serialize() {
JsonObject object = new JsonObject();
object.addProperty("modid", modid);
object.addProperty("weight", weight);
object.addProperty("id", id.toString());
object.add("lens", lens.serialize());
object.add("state", JsonHelper.serializeBlockState(state));
if (data != null) {
object.addProperty("data", data.toString());
}
return object;
}
public void deserialize(JsonObject object) {
setModid(object.get("modid").getAsString());
setWeight(object.get("weight").getAsDouble());
ResourceLocation id = new ResourceLocation(object.get("id").getAsString());
ItemStackKey lens = ItemStackKey.deserialize(object.getAsJsonObject("lens"));
BlockState state = JsonHelper.deserializeBlockState(object.getAsJsonObject("state"));
setId(id);
setLens(lens);
setState(state);
if (object.has("data")) {
CompoundNBT data = JsonUtils.readNBT(object, "data");
setData(data);
}
}
}
package com.teamacronymcoders.quantumquarry.misc;
import com.google.gson.JsonObject;
import com.hrznstudio.titanium.recipe.serializer.JSONSerializableDataHandler;
import net.minecraft.item.ItemStack;
import java.util.Objects;
public class ItemStackKey {
private final ItemStack itemStack;
public ItemStackKey(ItemStack itemStack) {
this.itemStack = itemStack;
}
public ItemStack getItemStack() {
return this.itemStack;
}
@Override
public int hashCode() {
return Objects.hash(this.itemStack.getItem(), this.itemStack.getTag());
}
public boolean match(ItemStack stack) {
return getItemStack().isItemEqualIgnoreDurability(stack);
}
public JsonObject serialize() {
return JSONSerializableDataHandler.writeItemStack(getItemStack());
}
public static ItemStackKey deserialize(JsonObject object) {
return new ItemStackKey(JSONSerializableDataHandler.readItemStack(object));
}
}
package com.teamacronymcoders.quantumquarry.quarry;
import com.hrznstudio.titanium.component.inventory.SidedInventoryComponent;
import com.teamacronymcoders.quantumquarry.QuantumConfig;
import com.teamacronymcoders.quantumquarry.QuantumQuarry;
import com.teamacronymcoders.quantumquarry.misc.ItemStackKey;
import com.teamacronymcoders.quantumquarry.misc.RandomCollection;
import com.teamacronymcoders.quantumquarry.recipe.MinerEntry;
import net.minecraft.block.BlockState;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.enchantment.Enchantments;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.server.ServerWorld;
import net.minecraft.world.storage.loot.LootContext.Builder;
import net.minecraft.world.storage.loot.LootParameters;
import net.minecraftforge.common.ToolType;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class QuarryHelper {
private static final List<MinerEntry> ENTRIES = new ArrayList<>();
private static Map<ItemStackKey, RandomCollection<MinerEntry>> cachedMaps = new HashMap<>();
/**
* @param powerIn Power Stored in the Quarry
* @param lens Stored Lens
* @param tool Stored Appropriate Tool for breaking the blockstate.
* @return returns the amount of operations that can be completed with the current power amount.
*/
public static int getAmountOfOperationsForPower(int powerIn, ItemStack lens, ItemStack tool) {
int powerPerOperation = getPowerPerOperationWithEfficiency(lens, tool);
return doesNotHaveClashingEnchantments(lens, tool) ? Math.round((float) powerIn / powerPerOperation) : 0;
}
/**
* @param lens Stored Lens
* @param tool Stored Appropriate Tool
* @return Returns the default cost minus the efficiency modifier
*/
public static int getPowerPerOperationWithEfficiency(ItemStack lens, ItemStack tool) {
int efficiency = EnchantmentHelper.getEnchantmentLevel(Enchantments.EFFICIENCY, lens) + EnchantmentHelper.getEnchantmentLevel(Enchantments.EFFICIENCY, tool);
return Math.min(QuantumConfig.getBaseCost() - (QuantumConfig.getEfficiencyReduction() * efficiency), QuantumConfig.getMinimumPowerDrain());
}
private static boolean doesNotHaveClashingEnchantments(ItemStack lens, ItemStack tool) {
boolean hasFortuneLens = EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, lens) > 0;
boolean hasSilkTouchLens = EnchantmentHelper.getEnchantmentLevel(Enchantments.SILK_TOUCH, lens) > 0;
boolean hasFortuneTool = EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, tool) > 0;
boolean hasSilkTouchTool = EnchantmentHelper.getEnchantmentLevel(Enchantments.SILK_TOUCH, tool) > 0;
return !(hasFortuneLens && hasSilkTouchTool) || !(hasSilkTouchLens && hasFortuneTool);
}
/**
* @param world Server World
* @param state BlockState to generate drops for
* @param lens Stored Lens
* @param tool Stored Appropriate Tool for "breaking" the block
* @param playerEntity Nullable Player
* @param compoundNBT Nullable NBT for Tile-Entity Ores
* @return List of ItemStack Drops generated from LootTables
*/
public static List<ItemStack> getDrops(@Nonnull ServerWorld world, @Nonnull BlockState state, @Nullable ItemStack lens, @Nonnull ItemStack tool, @Nullable PlayerEntity playerEntity, @Nullable CompoundNBT compoundNBT) {
Builder context = new Builder(world)
.withParameter(LootParameters.BLOCK_STATE, state)
.withParameter(LootParameters.TOOL, tool);
if (lens != null) {
context.withLuck(3.0F);
}
if (playerEntity != null) {
context.withParameter(LootParameters.THIS_ENTITY, playerEntity);
}
if (compoundNBT != null) {
TileEntity tileEntity = createTileEntityWithData(world, state, compoundNBT);
if (tileEntity != null) {
context.withParameter(LootParameters.BLOCK_ENTITY, tileEntity);
return state.getDrops(context);
}
}
return state.getDrops(context);
}
private static TileEntity createTileEntityWithData(IBlockReader reader, BlockState state, CompoundNBT nbt) {
TileEntity tileEntity = state.createTileEntity(reader);
if (tileEntity != null) {
tileEntity.read(nbt);
return tileEntity;
}
return null;
}
/**
* @param state Blockstate being broken.
* @param toolInventory Tool-Inventory
* @return Returns the appropriate tool to use to break the tool.
*/
public static ItemStack getAppropriateTool(BlockState state, SidedInventoryComponent toolInventory) {
switch (state.getHarvestTool().getName()) {
case "axe": return toolInventory.getStackInSlot(0);
case "shovel": return toolInventory.getStackInSlot(1);
case "pickaxe": return toolInventory.getStackInSlot(2);
default: return ItemStack.EMPTY;
}
}
/**
* @param state Blockstate being broken
* @param tool Appropriate Tool for the Blockstate
* @return Returns if the blockstate can be broken by the tool.
*/
public static boolean canToolBreakBlock(BlockState state, ItemStack tool) {
for (ToolType type : tool.getToolTypes()) {
if (state.getHarvestLevel() < tool.getHarvestLevel(type, null, state)) {
return true;
}
}
return false;
}
public static RandomCollection<MinerEntry> getMinerEntriesByLens(ItemStack lens) {
ItemStackKey key = new ItemStackKey(lens);
Predicate<MinerEntry> filter = (lens.isEmpty()) ? entry -> true : entry -> entry.getLens().match(lens);
if (cachedMaps.containsKey(key)) {
return cachedMaps.get(key);
}
List<MinerEntry> entries = ENTRIES.stream().filter(filter).collect(Collectors.toList());
RandomCollection<MinerEntry> collection = new RandomCollection<>(QuantumQuarry.RANDOM);
for (MinerEntry entry : entries) {
collection.add(entry.getWeight(), entry);
}
cachedMaps.put(key, collection);
return collection;
}
public static List<MinerEntry> getEntries() {
return ENTRIES;
}
}
package com.teamacronymcoders.quantumquarry.compat;
import com.teamacronymcoders.quantumquarry.QuantumQuarry;
import com.teamacronymcoders.quantumquarry.misc.ItemStackKey;
import com.teamacronymcoders.quantumquarry.recipe.MinerEntry;
import com.teamacronymcoders.quantumquarry.registry.QuantumQuarryRegistryHandler;
import net.minecraft.block.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import java.util.function.Consumer;
public class MiningEntryMinecraft {
public static void addMinecraftOres(Consumer<MinerEntry> consumer) {
consumer.accept(
new MinerEntry(
"minecraft",
30D,
new ResourceLocation(QuantumQuarry.MODID, "coal_ore"),
new ItemStackKey(new ItemStack(QuantumQuarryRegistryHandler.BLACK_LENS.get())),
Blocks.COAL_ORE.getDefaultState(),
null
)
);
consumer.accept(
new MinerEntry(
"minecraft",
20D,
new ResourceLocation(QuantumQuarry.MODID, "iron_ore"),
new ItemStackKey(new ItemStack(QuantumQuarryRegistryHandler.BROWN_LENS.get())),
Blocks.IRON_ORE.getDefaultState(),
null
)
);
consumer.accept(
new MinerEntry(
"minecraft",
13D,
new ResourceLocation(QuantumQuarry.MODID, "gold_ore"),
new ItemStackKey(new ItemStack(QuantumQuarryRegistryHandler.YELLOW_LENS.get())),
Blocks.GOLD_ORE.getDefaultState(),
null
)
);
consumer.accept(
new MinerEntry(
"minecraft",
15D,
new ResourceLocation(QuantumQuarry.MODID, "redstone_ore"),
new ItemStackKey(new ItemStack(QuantumQuarryRegistryHandler.RED_LENS.get())),
Blocks.REDSTONE_ORE.getDefaultState(),
null
)
);
consumer.accept(
new MinerEntry(
"minecraft",
10D,
new ResourceLocation(QuantumQuarry.MODID, "lapis_ore"),
new ItemStackKey(new ItemStack(QuantumQuarryRegistryHandler.BLUE_LENS.get())),
Blocks.LAPIS_ORE.getDefaultState(),
null
)
);
consumer.accept(
new MinerEntry(
"minecraft",
2D,
new ResourceLocation(QuantumQuarry.MODID, "diamond_ore"),
new ItemStackKey(new ItemStack(QuantumQuarryRegistryHandler.CYAN_LENS.get())),
Blocks.DIAMOND_ORE.getDefaultState(),
null
)
);
consumer.accept(
new MinerEntry(
"minecraft",
10D,
new ResourceLocation(QuantumQuarry.MODID, "quartz_ore"),
new ItemStackKey(new ItemStack(QuantumQuarryRegistryHandler.WHITE_LENS.get())),
Blocks.NETHER_QUARTZ_ORE.getDefaultState(),
null
)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment