diff --git a/src/main/java/com/eerussianguy/beneath/client/screen/BeneathScreen.java b/src/main/java/com/eerussianguy/beneath/client/screen/BeneathScreen.java new file mode 100644 index 0000000..7e736d0 --- /dev/null +++ b/src/main/java/com/eerussianguy/beneath/client/screen/BeneathScreen.java @@ -0,0 +1,36 @@ +package com.eerussianguy.beneath.client.screen; + +import com.mojang.blaze3d.platform.InputConstants; +import net.minecraft.client.gui.GuiGraphics; +import net.minecraft.client.gui.screens.Screen; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.MutableComponent; + +public class BeneathScreen extends Screen +{ + public BeneathScreen(Component title) + { + super(title); + } + + @Override + public boolean keyPressed(int keyCode, int scanCode, int modifiers) + { + if (super.keyPressed(keyCode, scanCode, modifiers)) + return true; + assert minecraft != null; + if (minecraft.options.keyInventory.isActiveAndMatches(InputConstants.getKey(keyCode, scanCode))) + { + minecraft.setScreen(null); + return true; + } + return false; + } + + protected void drawCenteredLine(GuiGraphics graphics, MutableComponent text, int x, int y) + { + final int dx = (256 - this.font.width(text)) / 2; + graphics.drawString(this.font, text, x + dx, y, 4210752, false); + } + +} diff --git a/src/main/java/com/eerussianguy/beneath/client/screen/LostPageScreen.java b/src/main/java/com/eerussianguy/beneath/client/screen/LostPageScreen.java index 4022d21..809e521 100644 --- a/src/main/java/com/eerussianguy/beneath/client/screen/LostPageScreen.java +++ b/src/main/java/com/eerussianguy/beneath/client/screen/LostPageScreen.java @@ -2,11 +2,8 @@ import com.eerussianguy.beneath.Beneath; import com.eerussianguy.beneath.common.items.LostPageItem; -import com.mojang.blaze3d.platform.InputConstants; -import net.minecraft.client.KeyMapping; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiGraphics; -import net.minecraft.client.gui.screens.Screen; import net.minecraft.client.multiplayer.ClientLevel; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.MutableComponent; @@ -14,7 +11,7 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.crafting.Ingredient; -public class LostPageScreen extends Screen +public class LostPageScreen extends BeneathScreen { private static final ResourceLocation TEXTURE = Beneath.identifier("textures/gui/lost_page.png"); private static final Component NAME = Component.translatable("beneath.screen.lost_page"); @@ -51,34 +48,15 @@ public void render(GuiGraphics graphics, int mouseX, int mouseY, float partialTi drawCenteredLine(graphics, Component.literal(item.getCostAmount(stack) + "x ").append(item.getSpecificIngredientTranslation(stack)), x, y + 26); graphics.renderItem(cost, x + 120, y + 34); - drawCenteredLine(graphics, Component.translatable("beneath.screen.lost_page.reward"), x, y + 64); drawCenteredLine(graphics, Component.literal(item.getRewardAmount(stack) + "x ").append(reward.getHoverName()), x, y + 74); graphics.renderItem(reward, x + 120, y + 82); drawCenteredLine(graphics, Component.translatable("beneath.screen.lost_page.punishment"), x, y + 112); - drawCenteredLine(graphics, Beneath.translateEnum(item.getPunishment(stack)), x, y + 122); + final MutableComponent pun = Beneath.translateEnum(item.getPunishment(stack)); + graphics.drawWordWrap(font, pun, x + 70, y + 122, 110, 0); } } - @Override - public boolean keyPressed(int keyCode, int scanCode, int modifiers) - { - if (super.keyPressed(keyCode, scanCode, modifiers)) - return true; - assert minecraft != null; - if (minecraft.options.keyInventory.isActiveAndMatches(InputConstants.getKey(keyCode, scanCode))) - { - minecraft.setScreen(null); - return true; - } - return false; - } - - protected void drawCenteredLine(GuiGraphics graphics, MutableComponent text, int x, int y) - { - final int dx = (256 - this.font.width(text)) / 2; - graphics.drawString(this.font, text, x + dx, y, 4210752, false); - } } diff --git a/src/main/java/com/eerussianguy/beneath/common/blockentities/AncientAltarBlockEntity.java b/src/main/java/com/eerussianguy/beneath/common/blockentities/AncientAltarBlockEntity.java index c5a47dd..22a040c 100644 --- a/src/main/java/com/eerussianguy/beneath/common/blockentities/AncientAltarBlockEntity.java +++ b/src/main/java/com/eerussianguy/beneath/common/blockentities/AncientAltarBlockEntity.java @@ -1,23 +1,207 @@ package com.eerussianguy.beneath.common.blockentities; +import java.util.ArrayList; +import java.util.List; import com.eerussianguy.beneath.Beneath; +import com.eerussianguy.beneath.common.items.LostPageItem; +import com.eerussianguy.beneath.misc.LostPage; import net.minecraft.core.BlockPos; +import net.minecraft.core.particles.ParticleOptions; +import net.minecraft.core.particles.ParticleTypes; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.network.chat.Component; +import net.minecraft.sounds.SoundEvents; +import net.minecraft.util.Mth; +import net.minecraft.world.InteractionHand; +import net.minecraft.world.InteractionResult; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.crafting.Ingredient; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.state.BlockState; +import net.minecraftforge.common.Tags; import net.minecraftforge.items.IItemHandler; +import net.minecraftforge.items.ItemHandlerHelper; import net.minecraftforge.items.ItemStackHandler; import net.dries007.tfc.common.blockentities.InventoryBlockEntity; +import net.dries007.tfc.util.Helpers; public class AncientAltarBlockEntity extends InventoryBlockEntity { + public static void tickBothSides(Level level, BlockPos pos, BlockState state, AncientAltarBlockEntity altar) + { + if (altar.tick < 0) + return; + + if (level.isClientSide) + { + final double x = pos.getX() + 0.5; + final double y = pos.getY() + 1; + final double z = pos.getZ() + 0.5; + + final float partial = altar.tick / 20f * Mth.TWO_PI; + final float r = 0.5f + (0.01f * altar.tick); + + final ParticleOptions particle = switch (altar.success) + { + case 0 -> ParticleTypes.WHITE_ASH; + case 1 -> ParticleTypes.CHERRY_LEAVES; + default -> ParticleTypes.MYCELIUM; + }; + level.addParticle(particle, x + Mth.sin(partial) * r, y + ((altar.success == 2 ? 1 : 2) * altar.tick / 20f), z + Mth.cos(partial) * r, Helpers.triangle(level.random, 0.2f), -0.2f, Helpers.triangle(level.random, 0.2f)); + if (altar.tick == 0) + { + for (int i = 0; i < 10; i++) + { + level.addParticle(ParticleTypes.SMOKE, x, y + 0.125, z, level.random.nextGaussian() * 0.05, 0.005, level.random.nextGaussian() * 0.05); + } + } + } + if (altar.tick == 0) + { + Helpers.playSound(level, pos, SoundEvents.FIREWORK_ROCKET_TWINKLE); + } + + altar.tick--; + } + + private int tick = -1; + private int success = 0; public AncientAltarBlockEntity(BlockPos pos, BlockState state) { super(BeneathBlockEntities.ANCIENT_ALTAR.get(), pos, state, defaultInventory(1), Beneath.blockEntityName("ancient_altar")); } + @Override + public void loadAdditional(CompoundTag nbt) + { + super.loadAdditional(nbt); + tick = nbt.getInt("tick"); + success = nbt.getInt("success"); + } + + @Override + public void saveAdditional(CompoundTag nbt) + { + super.saveAdditional(nbt); + nbt.putInt("tick", tick); + nbt.putInt("success", success); + } + public IItemHandler getInventory() { return inventory; } + + public void playSuccess() + { + assert level != null; + tick = 40; + success = 1; + Helpers.playSound(level, worldPosition, SoundEvents.ENCHANTMENT_TABLE_USE); + markForSync(); + } + + public void playFail() + { + assert level != null; + tick = 40; + success = 0; + Helpers.playSound(level, worldPosition, SoundEvents.GLASS_BREAK); + markForSync(); + } + + public void playSecondarySuccess() + { + assert level != null; + tick = 40; + success = 2; + markForSync(); + } + + @Override + public void setAndUpdateSlots(int slot) + { + super.setAndUpdateSlots(slot); + } + + public InteractionResult use(Player player, InteractionHand hand) + { + assert level != null; + final BlockPos pos = worldPosition; + final IItemHandler inv = getInventory(); + final ItemStack held = player.getItemInHand(hand); + if (Helpers.isItem(held, Tags.Items.GEMS)) + { + final ItemStack stack = inv.getStackInSlot(0); + if (LostPage.choose(stack, level.getRandom())) + { + held.shrink(1); + playSuccess(); + return InteractionResult.sidedSuccess(level.isClientSide); + } + else if (stack.getItem() instanceof LostPageItem page && page.hasInitialized(stack)) + { + int found = 0; + final Ingredient cost = page.getCost(stack); + final int costAmount = page.getCostAmount(stack); + final List altars = new ArrayList<>(); + for (BlockPos checkPos : BlockPos.betweenClosed(pos.offset(-5, 0, -5), pos.offset(5, 0, 5))) + { + if (level.getBlockEntity(checkPos) instanceof AncientAltarBlockEntity otherAltar && this != otherAltar) + { + final ItemStack foundStack = otherAltar.getInventory().getStackInSlot(0); + if (cost.test(foundStack)) + { + found += foundStack.getCount(); + altars.add(otherAltar); + if (found >= costAmount) + break; + } + } + } + if (found >= page.getCostAmount(stack)) + { + for (AncientAltarBlockEntity otherAltar : altars) + { + otherAltar.playSecondarySuccess(); + otherAltar.getInventory().extractItem(0, 64, false); + level.sendBlockUpdated(otherAltar.getBlockPos(), otherAltar.getBlockState(), otherAltar.getBlockState(), Block.UPDATE_CLIENTS); + } + int rewardAmount = page.getRewardAmount(stack); + if (!level.dimensionType().ultraWarm()) + rewardAmount /= 2; + final ItemStack reward = page.getReward(stack); + while (rewardAmount > 0) + { + int count = Math.min(reward.getMaxStackSize(), rewardAmount); + Helpers.spawnItem(level, pos, reward.copyWithCount(count)); + rewardAmount -= count; + } + held.shrink(1); + playSuccess(); + page.getPunishment(stack).administer(player, level, pos); + } + else + { + player.hurt(level.damageSources().magic(), 1f); + playFail(); + player.displayClientMessage(Component.translatable("beneath.sacrifice.error"), true); + } + return InteractionResult.sidedSuccess(level.isClientSide); + } + } + + // regular interaction + if (!inv.getStackInSlot(0).isEmpty()) + { + ItemHandlerHelper.giveItemToPlayer(player, inv.extractItem(0, 64, false)); + } + ItemHandlerHelper.giveItemToPlayer(player, inv.insertItem(0, held.split(64), false)); + return InteractionResult.sidedSuccess(level.isClientSide); + } + } diff --git a/src/main/java/com/eerussianguy/beneath/common/blocks/AncientAltarBlock.java b/src/main/java/com/eerussianguy/beneath/common/blocks/AncientAltarBlock.java index 4fe2c0e..f314efe 100644 --- a/src/main/java/com/eerussianguy/beneath/common/blocks/AncientAltarBlock.java +++ b/src/main/java/com/eerussianguy/beneath/common/blocks/AncientAltarBlock.java @@ -1,35 +1,19 @@ package com.eerussianguy.beneath.common.blocks; -import java.util.ArrayList; -import java.util.List; import com.eerussianguy.beneath.common.blockentities.AncientAltarBlockEntity; -import com.eerussianguy.beneath.common.items.LostPageItem; -import com.eerussianguy.beneath.misc.LostPage; -import com.ibm.icu.impl.Pair; import net.minecraft.core.BlockPos; -import net.minecraft.network.chat.Component; -import net.minecraft.sounds.SoundEvents; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; -import net.minecraft.world.damagesource.DamageSources; import net.minecraft.world.entity.player.Player; -import net.minecraft.world.item.Item; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.item.crafting.Ingredient; import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.VoxelShape; -import net.minecraftforge.common.Tags; -import net.minecraftforge.items.IItemHandler; -import net.minecraftforge.items.ItemHandlerHelper; -import net.dries007.tfc.common.TFCTags; import net.dries007.tfc.common.blocks.ExtendedProperties; import net.dries007.tfc.common.blocks.devices.DeviceBlock; -import net.dries007.tfc.util.Helpers; public class AncientAltarBlock extends DeviceBlock { @@ -46,76 +30,10 @@ public InteractionResult use(BlockState state, Level level, BlockPos pos, Player { if (level.getBlockEntity(pos) instanceof AncientAltarBlockEntity altar) { - final IItemHandler inv = altar.getInventory(); - final ItemStack held = player.getItemInHand(hand); - if (Helpers.isItem(held, Tags.Items.GEMS)) - { - final ItemStack stack = inv.getStackInSlot(0); - if (LostPage.choose(stack, level.getRandom())) - { - held.shrink(1); - Helpers.playSound(level, pos, SoundEvents.ENCHANTMENT_TABLE_USE); - return InteractionResult.sidedSuccess(level.isClientSide); - } - else if (stack.getItem() instanceof LostPageItem page && page.hasInitialized(stack)) - { - int found = 0; - final Ingredient cost = page.getCost(stack); - final int costAmount = page.getCostAmount(stack); - final List altars = new ArrayList<>(); - for (BlockPos checkPos : BlockPos.betweenClosed(pos.offset(-5, 0, -5), pos.offset(5, 0, 5))) - { - if (level.getBlockEntity(checkPos) instanceof AncientAltarBlockEntity otherAltar && altar != otherAltar) - { - final ItemStack foundStack = otherAltar.getInventory().getStackInSlot(0); - if (cost.test(foundStack)) - { - found += foundStack.getCount(); - altars.add(otherAltar); - if (found >= costAmount) - break; - } - } - } - if (found >= page.getCostAmount(stack)) - { - for (AncientAltarBlockEntity otherAltar : altars) - { - otherAltar.getInventory().extractItem(0, 64, false); - } - int rewardAmount = page.getRewardAmount(stack); - if (!level.dimensionType().ultraWarm()) - rewardAmount /= 2; - final ItemStack reward = page.getReward(stack); - while (rewardAmount > 0) - { - int count = Math.min(reward.getMaxStackSize(), rewardAmount); - Helpers.spawnItem(level, pos, reward.copyWithCount(count)); - rewardAmount -= count; - } - held.shrink(1); - Helpers.playSound(level, pos, SoundEvents.ENCHANTMENT_TABLE_USE); - page.getPunishment(stack).administer(player, level, pos); - } - else - { - Helpers.playSound(level, pos, SoundEvents.GLASS_BREAK); - player.hurt(level.damageSources().magic(), 1f); - player.displayClientMessage(Component.translatable("beneath.sacrifice.error"), true); - } - return InteractionResult.sidedSuccess(level.isClientSide); - } - } - if (!inv.getStackInSlot(0).isEmpty()) - { - ItemHandlerHelper.giveItemToPlayer(player, inv.extractItem(0, 64, false)); - } - ItemHandlerHelper.giveItemToPlayer(player, inv.insertItem(0, held.split(64), false)); - return InteractionResult.sidedSuccess(level.isClientSide); + return altar.use(player, hand); } return InteractionResult.PASS; } - @Override @SuppressWarnings("deprecation") public VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) diff --git a/src/main/java/com/eerussianguy/beneath/common/blocks/BeneathBlocks.java b/src/main/java/com/eerussianguy/beneath/common/blocks/BeneathBlocks.java index b968a2a..41661e8 100644 --- a/src/main/java/com/eerussianguy/beneath/common/blocks/BeneathBlocks.java +++ b/src/main/java/com/eerussianguy/beneath/common/blocks/BeneathBlocks.java @@ -5,6 +5,7 @@ import java.util.function.Function; import java.util.function.Supplier; import com.eerussianguy.beneath.Beneath; +import com.eerussianguy.beneath.common.blockentities.AncientAltarBlockEntity; import com.eerussianguy.beneath.common.blockentities.BeneathBlockEntities; import com.eerussianguy.beneath.common.blockentities.HellforgeBlockEntity; import com.eerussianguy.beneath.common.items.BeneathItems; @@ -91,7 +92,7 @@ public class BeneathBlocks public static final RegistryObject GLEAMFLOWER = register("gleamflower", () -> new NFlowerBlock(ExtendedProperties.of().sound(SoundType.GRASS).instabreak().speedFactor(0.8f).noCollission().lightLevel(s -> 7))); public static final RegistryObject BURPFLOWER = register("burpflower", () -> new BurpingFlowerBlock(ExtendedProperties.of().sound(SoundType.GRASS).instabreak().speedFactor(0.8f).noCollission().randomTicks())); public static final RegistryObject UNPOSTER = register("unposter", () -> new UnposterBlock(ExtendedProperties.of(MapColor.COLOR_BLUE).strength(0.6F).noOcclusion().sound(SoundType.WOOD).randomTicks().blockEntity(BeneathBlockEntities.UNPOSTER))); - public static final RegistryObject ANCIENT_ALTAR = register("ancient_altar", () -> new AncientAltarBlock(ExtendedProperties.of().mapColor(MapColor.COLOR_BLACK).strength(8f).noOcclusion().sound(SoundType.GILDED_BLACKSTONE).blockEntity(BeneathBlockEntities.ANCIENT_ALTAR))); + public static final RegistryObject ANCIENT_ALTAR = register("ancient_altar", () -> new AncientAltarBlock(ExtendedProperties.of().mapColor(MapColor.COLOR_BLACK).strength(8f).noOcclusion().sound(SoundType.GILDED_BLACKSTONE).blockEntity(BeneathBlockEntities.ANCIENT_ALTAR).ticks(AncientAltarBlockEntity::tickBothSides))); public static final RegistryObject SLIMED_NETHERRACK = register("ore/slimed_netherrack", () -> new SlimeBlock(BlockBehaviour.Properties.copy(Blocks.SLIME_BLOCK).strength(1f).sound(SoundType.NETHERRACK))); public static final Map> SHROOMS = Helpers.mapOfKeys(Shroom.class, shroom -> registerNoItem("mushroom/" + shroom.getSerializedName(), () -> new NFlowerBlock(ExtendedProperties.of(Blocks.CRIMSON_FUNGUS))));