Skip to content

Commit

Permalink
Merge pull request #80 from klikli-dev/fix/dimensional-miner-spirit-c…
Browse files Browse the repository at this point in the history
…onfig

Fix/dimensional miner spirit config
  • Loading branch information
klikli-dev authored Jan 6, 2021
2 parents daf8263 + 504e2ef commit d32718d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public void render(MatrixStack stack, int mouseX, int mouseY, float partialTicks
this.renderHoveredTooltip(stack, mouseX, mouseY);
}

@Override
protected void drawGuiContainerForegroundLayer(MatrixStack matrixStack, int x, int y) {
//do not call super as it renders the inventory name which we do not want
}

@Override
protected void drawGuiContainerBackgroundLayer(MatrixStack stack, float partialTicks, int mouseX, int mouseY) {
RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,45 @@
package com.github.klikli_dev.occultism.common.item.spirit;

import com.github.klikli_dev.occultism.common.tile.DimensionalMineshaftTileEntity;
import com.github.klikli_dev.occultism.config.value.CachedInt;
import com.github.klikli_dev.occultism.util.ItemNBTUtil;
import com.github.klikli_dev.occultism.util.TextUtil;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipeType;
import net.minecraft.item.crafting.RecipeManager;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.ObfuscationReflectionHelper;

import javax.annotation.Nullable;
import java.lang.reflect.Field;
import java.util.List;
import java.util.function.Supplier;

public class MinerSpiritItem extends Item {

protected static Field maxDamageField =
ObfuscationReflectionHelper.findField(Item.class, "field_77699_b");

//region Fields
private final int maxMiningTime;
private final int rollsPerOperation;
private final Supplier<Integer> maxMiningTime;
private final Supplier<Integer> rollsPerOperation;
private final Supplier<Integer> maxDamage;
private boolean hasInitializedMaxDamage;
//endregion Fields

//region Initialization
public MinerSpiritItem(Properties properties, int maxMiningTime, int rollsPerOperation) {
public MinerSpiritItem(Properties properties, Supplier<Integer> maxMiningTime, Supplier<Integer> rollsPerOperation, Supplier<Integer> maxDamage) {
super(properties);
this.maxMiningTime = maxMiningTime;
this.rollsPerOperation = rollsPerOperation;
this.maxDamage = maxDamage;
this.hasInitializedMaxDamage = false;
}
//endregion Initialization

Expand All @@ -60,12 +74,32 @@ public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextC
TextUtil.formatDemonName(ItemNBTUtil.getBoundSpiritName(stack))));
}

@Override
public double getDurabilityForDisplay(ItemStack stack) {
if(!this.hasInitializedMaxDamage){
this.hasInitializedMaxDamage = true;
try {
maxDamageField.setInt(this, this.maxDamage.get());
} catch (IllegalAccessException ignored) {

}
}
return super.getDurabilityForDisplay(stack);
}

@Override
public void onCreated(ItemStack stack, World worldIn, PlayerEntity playerIn) {
super.onCreated(stack, worldIn, playerIn);
if(!this.hasInitializedMaxDamage){
this.hasInitializedMaxDamage = true;
try {
maxDamageField.setInt(this, this.maxDamage.get());
} catch (IllegalAccessException ignored) {

stack.getOrCreateTag().putInt(DimensionalMineshaftTileEntity.MAX_MINING_TIME_TAG, this.maxMiningTime);
stack.getOrCreateTag().putInt(DimensionalMineshaftTileEntity.ROLLS_PER_OPERATION_TAG, this.rollsPerOperation);
}
}
stack.getOrCreateTag().putInt(DimensionalMineshaftTileEntity.MAX_MINING_TIME_TAG, this.maxMiningTime.get());
stack.getOrCreateTag().putInt(DimensionalMineshaftTileEntity.ROLLS_PER_OPERATION_TAG, this.rollsPerOperation.get());
}
//endregion Overrides
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@
import net.minecraft.util.WeightedRandom;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.FakePlayerFactory;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
Expand Down Expand Up @@ -183,6 +186,8 @@ public void tick() {
else if (!input.isEmpty()) {
//if we're done with the last mining job, and we have valid input, start the next one.
this.currentInputType = input.getItem();
//ensure nbt is initialized, fixes issues with spawned miner spirits
forceInitStackNBT(input, (ServerWorld) this.world);
this.maxMiningTime = getMaxMiningTime(input);
this.rollsPerOperation = getRollsPerOperation(input);
this.miningTime = this.maxMiningTime;
Expand All @@ -209,6 +214,10 @@ public Container createMenu(int id, PlayerInventory playerInventory, PlayerEntit
//endregion Overrides

//region Static Methods
public static void forceInitStackNBT(ItemStack stack, ServerWorld world){
stack.getItem().onCreated(stack, world, FakePlayerFactory.getMinecraft(world));
}

public static int getMaxMiningTime(ItemStack stack) {
int time = stack.getOrCreateTag().getInt(MAX_MINING_TIME_TAG);
return time <= 0 ? DEFAULT_MAX_MINING_TIME : time;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,20 +237,22 @@ public class OccultismItems {
//Miner Spirits
public static final RegistryObject<MinerSpiritItem> MINER_DEBUG_UNSPECIALIZED =
ITEMS.register("miner_debug_unspecialized",
() -> new MinerSpiritItem(defaultProperties().maxDamage(10000), 100, 10));
() -> new MinerSpiritItem(defaultProperties().maxDamage(10000), () -> 100, () -> 10, () -> 10000));
public static final RegistryObject<MinerSpiritItem> MINER_FOLIOT_UNSPECIALIZED =
ITEMS.register("miner_foliot_unspecialized",
() -> new MinerSpiritItem(defaultProperties()
.maxDamage(
Occultism.CONFIG.dimensionalMineshaft.minerFoliotUnspecialized.durability
.get()),
Occultism.CONFIG.dimensionalMineshaft.minerFoliotUnspecialized.maxMiningTime.get(),
Occultism.CONFIG.dimensionalMineshaft.minerFoliotUnspecialized.rollsPerOperation.get()));
Occultism.CONFIG.dimensionalMineshaft.minerFoliotUnspecialized.maxMiningTime::get,
Occultism.CONFIG.dimensionalMineshaft.minerFoliotUnspecialized.rollsPerOperation::get,
Occultism.CONFIG.dimensionalMineshaft.minerFoliotUnspecialized.durability::get));
public static final RegistryObject<MinerSpiritItem> MINER_DJINNI_ORES = ITEMS.register("miner_djinni_ores",
() -> new MinerSpiritItem(defaultProperties().maxDamage(
Occultism.CONFIG.dimensionalMineshaft.minerDjinniOres.durability.get())
, Occultism.CONFIG.dimensionalMineshaft.minerDjinniOres.maxMiningTime.get(),
Occultism.CONFIG.dimensionalMineshaft.minerDjinniOres.rollsPerOperation.get()));
, Occultism.CONFIG.dimensionalMineshaft.minerDjinniOres.maxMiningTime::get,
Occultism.CONFIG.dimensionalMineshaft.minerDjinniOres.rollsPerOperation::get,
Occultism.CONFIG.dimensionalMineshaft.minerDjinniOres.durability::get));
//endregion Fields

//region Static Methods
Expand Down

0 comments on commit d32718d

Please sign in to comment.