Skip to content

Commit

Permalink
energy items done
Browse files Browse the repository at this point in the history
  • Loading branch information
Thepigcat76 committed Dec 6, 2024
1 parent a85f71f commit 3dcb026
Show file tree
Hide file tree
Showing 22 changed files with 365 additions and 79 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/leclowndu93150/carbort/Carbort.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public Carbort(IEventBus modEventBus, ModContainer modContainer) {
CBItems.ITEMS.register(modEventBus);
CBBlocks.BLOCKS.register(modEventBus);
CBBlockEntities.REGISTER.register(modEventBus);
CBEntityTypes.ENTITY_TYPES.register(modEventBus);
CBMobEffects.EFFECTS.register(modEventBus);
CBMobEffects.POTIONS.register(modEventBus);
CBDataComponents.DATA_COMPONENTS.register(modEventBus);
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/com/leclowndu93150/carbort/CarbortClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import com.leclowndu93150.carbort.content.screen.ChunkAnalyzerScreen;
import com.leclowndu93150.carbort.registries.CBBlockEntities;
import com.leclowndu93150.carbort.data.CBDataComponents;
import com.leclowndu93150.carbort.registries.CBEntityTypes;
import com.leclowndu93150.carbort.registries.CBItems;
import com.leclowndu93150.carbort.registries.CBMenus;
import net.minecraft.client.model.HumanoidModel;
import net.minecraft.client.renderer.entity.EntityRenderers;
import net.minecraft.client.renderer.entity.ThrownItemRenderer;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.FastColor;
Expand All @@ -17,6 +20,7 @@
import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent;
import net.neoforged.neoforge.client.event.EntityRenderersEvent;
import net.neoforged.neoforge.client.event.ModelEvent;
import net.neoforged.neoforge.client.event.RegisterColorHandlersEvent;
Expand All @@ -36,12 +40,17 @@ public CarbortClient(IEventBus modEventBus) {
modEventBus.addListener(this::registerColorHandlers);
modEventBus.addListener(this::registerClientExtensions);
modEventBus.addListener(this::registerModels);
modEventBus.addListener(this::onClientSetup);
}

private void registerBERs(EntityRenderersEvent.RegisterRenderers event) {
event.registerBlockEntityRenderer(CBBlockEntities.BEDROCK_DRILL.get(), BedrockDrillBER::new);
}

private void onClientSetup(FMLClientSetupEvent event) {
event.enqueueWork(() -> EntityRenderers.register(CBEntityTypes.DYNAMITE.get(), pContext -> new ThrownItemRenderer<>(pContext, 2, false)));
}

private void registerMenuScreens(RegisterMenuScreensEvent event) {
event.register(CBMenus.CHUNK_ANALYZER_MENU.get(), ChunkAnalyzerScreen::new);
}
Expand Down Expand Up @@ -70,7 +79,7 @@ private void registerClientExtensions(RegisterClientExtensionsEvent event) {
public HumanoidModel.@NotNull ArmPose getArmPose(LivingEntity entityLiving, InteractionHand hand, ItemStack itemStack) {
return HumanoidModel.ArmPose.CROSSBOW_CHARGE;
}
}, CBItems.IRON_GREAT_SWORD);
}, CBItems.BEDROCKIUM_BLADE);
}

private void registerBakedModels(ModelEvent.RegisterAdditional event) {
Expand Down
33 changes: 30 additions & 3 deletions src/main/java/com/leclowndu93150/carbort/CarbortConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
public final class CarbortConfig {
private static final Map<ResourceLocation, Pair<ModConfigSpec.IntValue, ModConfigSpec.IntValue>> ITEM_ENERGY_CONFIGS = new HashMap<>();
private static final Map<Item, IntIntPair> ITEM_ENERGY_VALUES = new HashMap<>();

private static final Map<ResourceLocation, Pair<ModConfigSpec.IntValue, ModConfigSpec.IntValue>> ITEM_FLUID_CONFIGS = new HashMap<>();
private static final Map<Item, IntIntPair> ITEM_FLUID_VALUES = new HashMap<>();

private static final ModConfigSpec.Builder BUILDER = new ModConfigSpec.Builder();

public static ModConfigSpec SPEC;
Expand All @@ -30,6 +34,10 @@ public static void onLoad(final ModConfigEvent event) {
Pair<ModConfigSpec.IntValue, ModConfigSpec.IntValue> value = config.getValue();
ITEM_ENERGY_VALUES.put(BuiltInRegistries.ITEM.get(config.getKey()), IntIntPair.of(value.first().getAsInt(), value.second().getAsInt()));
}
for (Map.Entry<ResourceLocation, Pair<ModConfigSpec.IntValue, ModConfigSpec.IntValue>> config : ITEM_FLUID_CONFIGS.entrySet()) {
Pair<ModConfigSpec.IntValue, ModConfigSpec.IntValue> value = config.getValue();
ITEM_FLUID_VALUES.put(BuiltInRegistries.ITEM.get(config.getKey()), IntIntPair.of(value.first().getAsInt(), value.second().getAsInt()));
}
}

public static int itemEnergyUsage(ItemLike itemLike) {
Expand All @@ -40,19 +48,38 @@ public static int itemEnergyCapacity(ItemLike itemLike) {
return ITEM_ENERGY_VALUES.get(itemLike.asItem()).secondInt();
}

public static int itemFluidUsage(ItemLike itemLike) {
return ITEM_FLUID_VALUES.get(itemLike.asItem()).firstInt();
}

public static int itemFluidCapacity(ItemLike itemLike) {
return ITEM_FLUID_VALUES.get(itemLike.asItem()).secondInt();
}

private static void itemEnergyConfigs(String name, ResourceLocation itemLoc, int usageAmount, int energyCapacity) {
ModConfigSpec.IntValue energyUsage = BUILDER.comment("Configure how much energy the " + name + " uses.")
.defineInRange(name + "_energy_usage", usageAmount, 0, Integer.MAX_VALUE);
ModConfigSpec.IntValue energyUsage = BUILDER.comment("Configure how much energy the " + itemLoc.getPath() + " uses.")
.defineInRange(itemLoc.getPath() + "_energy_usage", usageAmount, 0, Integer.MAX_VALUE);
ModConfigSpec.IntValue energyMax = BUILDER.comment("Configure the maximum amount of energy the " + name + " can hold. Setting this to 0 will disable energy on the " + name)
.defineInRange(name + "_max_energy", energyCapacity, 0, Integer.MAX_VALUE);
.defineInRange(itemLoc.getPath() + "_max_energy", energyCapacity, 0, Integer.MAX_VALUE);
ITEM_ENERGY_CONFIGS.put(itemLoc, Pair.of(energyUsage, energyMax));
}

private static void itemFluidConfigs(String name, ResourceLocation itemLoc, int usageAmount, int fluidCapacity) {
ModConfigSpec.IntValue fluidUsage = BUILDER.comment("Configure how much Fluid the " + name + " uses.")
.defineInRange(itemLoc.getPath() + "_energy_usage", usageAmount, 0, Integer.MAX_VALUE);
ModConfigSpec.IntValue fluidMax = BUILDER.comment("Configure the maximum amount of Fluid the " + name + " can hold. Setting this to 0 will disable Fluid on the " + name)
.defineInRange(itemLoc.getPath() + "_max_energy", fluidCapacity, 0, Integer.MAX_VALUE);
ITEM_FLUID_CONFIGS.put(itemLoc, Pair.of(fluidUsage, fluidMax));
}

static {
itemEnergyConfigs("Chunk Analyzer", Carbort.rl("chunk_analyzer"), 250, 10_000);
itemEnergyConfigs("Shrinkinator", Carbort.rl("shrinkinator"), 250, 10_000);
itemEnergyConfigs("Chunk Vacuum", Carbort.rl("chunk_vacuum"), 25_000, 500_000);

itemFluidConfigs("Funeral Pickaxe", Carbort.rl("funeral_pickaxe"), 25, 10_000);
itemFluidConfigs("Bedrockium Blade", Carbort.rl("bedrockium_blade"), 25, 10_000);

SPEC = BUILDER.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package com.leclowndu93150.carbort.api.items;

import com.leclowndu93150.carbort.utils.CapabilityUtils;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import net.minecraft.util.FastColor;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.neoforged.neoforge.energy.IEnergyStorage;

import java.util.List;

public abstract class SimpleEnergyItem extends Item implements IEnergyItem {
public SimpleEnergyItem(Properties properties) {
super(properties);
Expand All @@ -29,5 +35,25 @@ public int getBarColor(ItemStack stack) {
return FastColor.ARGB32.color(235, 7, 7);
}

// TODO: Fancy tooltip
@Override
public void appendHoverText(ItemStack stack, TooltipContext context, List<Component> tooltipComponents, TooltipFlag tooltipFlag) {
IEnergyStorage energyStorage = CapabilityUtils.itemEnergyStorage(stack);
tooltipComponents.add(Component.literal("Stored: ")
.append(energyStorage.getEnergyStored() + "/" + energyStorage.getMaxEnergyStored())
.withStyle(ChatFormatting.GRAY));
tooltipComponents.add(Component.literal("Usage: ")
.append(String.valueOf(getEnergyUsage()))
.withStyle(ChatFormatting.GRAY));
super.appendHoverText(stack, context, tooltipComponents, tooltipFlag);
}

public boolean useEnergy(Player player, ItemStack itemStack) {
IEnergyStorage energyStorage = CapabilityUtils.itemEnergyStorage(itemStack);
if (player.hasInfiniteMaterials() || energyStorage.extractEnergy(getEnergyUsage(), true) == getEnergyUsage()) {
energyStorage.extractEnergy(getEnergyUsage(), false);
return true;
}
return false;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.leclowndu93150.carbort.content.entities;

import com.leclowndu93150.carbort.registries.CBEntityTypes;
import com.leclowndu93150.carbort.registries.CBItems;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.projectile.Snowball;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.Explosion;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.EntityHitResult;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;

public class DynamiteEntity extends Snowball {
public DynamiteEntity(EntityType<? extends Snowball> entityType, Level level) {
super(entityType, level);
}

public DynamiteEntity(Level level, LivingEntity livingEntity) {
super(level, livingEntity);
}

@Override
protected @NotNull Item getDefaultItem() {
return CBItems.DYNAMITE.asItem();
}

@Override
protected void onHitBlock(BlockHitResult result) {
this.level().explode(
this,
Explosion.getDefaultDamageSource(this.level(), this),
null,
this.getX(),
this.getY(0.0625),
this.getZ(),
3.0F,
false,
Level.ExplosionInteraction.TNT
);
}

@Override
protected void onHitEntity(EntityHitResult result) {
super.onHitEntity(result);
this.level().explode(
this,
Explosion.getDefaultDamageSource(this.level(), this),
null,
this.getX(),
this.getY(0.0625),
this.getZ(),
4.0F,
false,
Level.ExplosionInteraction.TNT
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.leclowndu93150.carbort.content.items;

import com.leclowndu93150.carbort.CarbortConfig;
import com.leclowndu93150.carbort.api.items.IFluidItem;
import com.leclowndu93150.carbort.data.CBDataComponents;
import com.leclowndu93150.carbort.utils.CapabilityUtils;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.util.FastColor;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.SwordItem;
import net.minecraft.world.level.Level;
import net.neoforged.neoforge.common.Tags;
import net.neoforged.neoforge.fluids.FluidStack;
import net.neoforged.neoforge.fluids.capability.IFluidHandler;

public class BedrockiumBladeItem extends SwordItem implements IFluidItem {
public BedrockiumBladeItem(Properties properties) {
super(ToolTiers.BEDROCKIUM, properties);
}

@Override
public int getCapacity() {
return CarbortConfig.itemFluidCapacity(this);
}

@Override
public boolean isBarVisible(ItemStack stack) {
return true;
}

@Override
public int getBarColor(ItemStack stack) {
return FastColor.ARGB32.color(82, 82, 82);
}

@Override
public int getBarWidth(ItemStack stack) {
IFluidHandler energyStorage = CapabilityUtils.itemFluidHandler(stack);
return Math.round(13.0F - ((1 - ((float) energyStorage.getFluidInTank(0).getAmount() / energyStorage.getTankCapacity(0))) * 13.0F));
}

@Override
public boolean isFluidValid(int tank, FluidStack stack) {
// TODO: Void fluid
return stack.is(Tags.Fluids.LAVA);
}

@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand usedHand) {
if (player.isShiftKeyDown()) {
ItemStack itemInHand = player.getItemInHand(usedHand);
itemInHand.set(CBDataComponents.ACTIVE, !itemInHand.getOrDefault(CBDataComponents.ACTIVE, false));
player.playSound(SoundEvents.EXPERIENCE_ORB_PICKUP);
return InteractionResultHolder.success(itemInHand);
}
return super.use(level, player, usedHand);
}

@Override
public boolean isFoil(ItemStack stack) {
return super.isFoil(stack) || stack.getOrDefault(CBDataComponents.ACTIVE, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,27 +60,21 @@ public int getUseDuration(ItemStack stack, LivingEntity entity) {
@Override
public ItemStack finishUsingItem(ItemStack stack, Level level, LivingEntity livingEntity) {
if (level instanceof ServerLevel serverLevel && livingEntity instanceof ServerPlayer serverPlayer) {
IEnergyStorage energyStorage = serverPlayer.getMainHandItem().getCapability(Capabilities.EnergyStorage.ITEM);

BlockPos pos = getPlayerPOVHitResult(level, serverPlayer, ClipContext.Fluid.NONE).getBlockPos();
if (pos.getY() < level.getMinBuildHeight()
&& level.getBlockState(pos).is(Blocks.VOID_AIR)
&& level.dimensionTypeRegistration().is(BuiltinDimensionTypes.OVERWORLD)) {
ItemStack offhandItem = serverPlayer.getOffhandItem();
IFluidHandler fluidHandler = offhandItem.getCapability(Capabilities.FluidHandler.ITEM);
level.playSound(null, pos, SoundEvents.WITHER_HURT, SoundSource.BLOCKS);
if (fluidHandler != null) {
fluidHandler.fill(new FluidStack(Fluids.LAVA, 50), IFluidHandler.FluidAction.EXECUTE);
serverPlayer.startUsingItem(InteractionHand.MAIN_HAND);
}
if (!serverPlayer.hasInfiniteMaterials()) {
energyStorage.extractEnergy(getEnergyUsage() / 10, false);
}
} else {
ChunkVacuumHelper helper = new ChunkVacuumHelper(serverLevel, serverPlayer, pos);
serverLevel.getServer().doRunTask(new TickTask(0, helper::removeArea));
if (!serverPlayer.hasInfiniteMaterials()) {
energyStorage.extractEnergy(getEnergyUsage(), false);
if (useEnergy(serverPlayer, stack)) {
if (pos.getY() < level.getMinBuildHeight()
&& level.getBlockState(pos).is(Blocks.VOID_AIR)
&& level.dimensionTypeRegistration().is(BuiltinDimensionTypes.OVERWORLD)) {
ItemStack offhandItem = serverPlayer.getOffhandItem();
IFluidHandler fluidHandler = offhandItem.getCapability(Capabilities.FluidHandler.ITEM);
level.playSound(null, pos, SoundEvents.WITHER_HURT, SoundSource.BLOCKS);
if (fluidHandler != null) {
fluidHandler.fill(new FluidStack(Fluids.LAVA, 50), IFluidHandler.FluidAction.EXECUTE);
serverPlayer.startUsingItem(InteractionHand.MAIN_HAND);
}
} else {
ChunkVacuumHelper helper = new ChunkVacuumHelper(serverLevel, serverPlayer, pos);
serverLevel.getServer().doRunTask(new TickTask(0, helper::removeArea));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.leclowndu93150.carbort.content.items;

import com.leclowndu93150.carbort.content.entities.DynamiteEntity;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.stats.Stats;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;

public class DynamiteItem extends Item {
public DynamiteItem(Properties properties) {
super(properties);
}

@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand usedHand) {
ItemStack itemstack = player.getItemInHand(usedHand);
level.playSound(
null,
player.getX(),
player.getY(),
player.getZ(),
SoundEvents.SNOWBALL_THROW,
SoundSource.NEUTRAL,
0.5F,
0.4F / (level.getRandom().nextFloat() * 0.4F + 0.8F)
);
if (!level.isClientSide) {
DynamiteEntity dynamite = new DynamiteEntity(level, player);
dynamite.setItem(itemstack);
dynamite.shootFromRotation(player, player.getXRot(), player.getYRot(), 0.0F, 0.5F, 1.0F);
level.addFreshEntity(dynamite);
}

player.awardStat(Stats.ITEM_USED.get(this));
itemstack.consume(1, player);
return InteractionResultHolder.sidedSuccess(itemstack, level.isClientSide());
}
}
Loading

0 comments on commit 3dcb026

Please sign in to comment.