Skip to content

Commit

Permalink
fix: Fix inventory drops for waystones, make landing stones initializ…
Browse files Browse the repository at this point in the history
…e a shard as well
  • Loading branch information
BlayTheNinth committed Dec 17, 2023
1 parent e4defe2 commit 8304556
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,32 +91,6 @@ public void setPlacedBy(Level world, BlockPos pos, BlockState state, @Nullable L
}
}

@Override
public void playerWillDestroy(Level world, BlockPos pos, BlockState state, Player player) {
BlockEntity blockEntity = world.getBlockEntity(pos);
if (blockEntity instanceof WarpPlateBlockEntity && !player.getAbilities().instabuild) {
boolean isSilkTouch = EnchantmentHelper.getEnchantmentLevel(Enchantments.SILK_TOUCH, player) > 0;
WarpPlateBlockEntity warpPlate = (WarpPlateBlockEntity) blockEntity;
if (warpPlate.isCompletedFirstAttunement()) {
for (int i = 0; i < warpPlate.getContainerSize(); i++) {
ItemStack itemStack = warpPlate.getItem(i);

// If not silk touching, don't bother dropping shards attuned to this waystone, since the waystone is gonna die anyways
if (!isSilkTouch && itemStack.getItem() == ModItems.attunedShard) {
IWaystone waystoneAttunedTo = ((IAttunementItem) ModItems.attunedShard).getWaystoneAttunedTo(world.getServer(), itemStack);
if (waystoneAttunedTo != null && waystoneAttunedTo.getWaystoneUid().equals(warpPlate.getWaystone().getWaystoneUid())) {
continue;
}
}

popResource(world, pos, itemStack);
}
}
}

super.playerWillDestroy(world, pos, state, player);
}

@Override
public VoxelShape getShape(BlockState blockState, BlockGetter blockGetter, BlockPos blockPos, CollisionContext collisionContext) {
return SHAPE;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package net.blay09.mods.waystones.block;

import net.blay09.mods.balm.api.Balm;
import net.blay09.mods.waystones.api.IAttunementItem;
import net.blay09.mods.waystones.api.IWaystone;
import net.blay09.mods.waystones.api.WaystoneOrigin;
import net.blay09.mods.waystones.block.entity.WaystoneBlockEntityBase;
import net.blay09.mods.waystones.core.*;
import net.blay09.mods.waystones.item.ModItems;
import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
Expand Down Expand Up @@ -120,6 +122,24 @@ public void playerWillDestroy(Level world, BlockPos pos, BlockState state, Playe
}
}

if (blockEntity instanceof WaystoneBlockEntityBase waystoneBlockEntity && !player.getAbilities().instabuild) {
if (waystoneBlockEntity.isCompletedFirstAttunement()) {
for (int i = 0; i < waystoneBlockEntity.getContainerSize(); i++) {
ItemStack itemStack = waystoneBlockEntity.getItem(i);

// If not silk touching, don't bother dropping shards attuned to this waystone, since the waystone is gonna die anyways
if (!hasSilkTouch && itemStack.getItem() == ModItems.attunedShard) {
IWaystone waystoneAttunedTo = ((IAttunementItem) ModItems.attunedShard).getWaystoneAttunedTo(world.getServer(), itemStack);
if (waystoneAttunedTo != null && waystoneAttunedTo.getWaystoneUid().equals(waystoneBlockEntity.getWaystone().getWaystoneUid())) {
continue;
}
}

popResource(world, pos, itemStack);
}
}
}

super.playerWillDestroy(world, pos, state, player);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,8 @@ public void writeScreenOpeningData(ServerPlayer player, FriendlyByteBuf buf) {
return getMenuProvider();
}

@Override
public boolean shouldPerformInitialAttunement() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

import net.blay09.mods.balm.api.Balm;
import net.blay09.mods.balm.api.menu.BalmMenuProvider;
import net.blay09.mods.waystones.Waystones;
import net.blay09.mods.waystones.api.*;
import net.blay09.mods.waystones.api.WaystoneTypes;
import net.blay09.mods.waystones.block.WarpPlateBlock;
import net.blay09.mods.waystones.config.WaystonesConfig;
import net.blay09.mods.waystones.core.*;
import net.blay09.mods.waystones.menu.WarpPlateMenu;
import net.blay09.mods.waystones.recipe.ModRecipes;
import net.blay09.mods.waystones.recipe.WarpPlateRecipe;
import net.blay09.mods.waystones.tag.ModItemTags;
import net.blay09.mods.waystones.worldgen.namegen.NameGenerationMode;
import net.blay09.mods.waystones.worldgen.namegen.NameGenerator;
Expand All @@ -34,28 +31,21 @@
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.crafting.RecipeHolder;
import net.minecraft.world.level.ServerLevelAccessor;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.function.Consumer;


public class WarpPlateBlockEntity extends WaystoneBlockEntityBase implements Nameable {

private static final Logger logger = LoggerFactory.getLogger(WarpPlateBlockEntity.class);

private final WeakHashMap<Entity, Integer> ticksPassedPerEntity = new WeakHashMap<>();

private final Random random = new Random();

private boolean readyForAttunement;
private boolean completedFirstAttunement;
private int lastAttunementSlot;

private Component customName;
Expand All @@ -64,36 +54,6 @@ public WarpPlateBlockEntity(BlockPos blockPos, BlockState blockState) {
super(ModBlockEntities.warpPlate.get(), blockPos, blockState);
}


@Override
public ItemStack removeItem(int slot, int count) {
if (!completedFirstAttunement) {
return ItemStack.EMPTY;
}
return WarpPlateBlockEntity.super.removeItem(slot, count);
}

@Override
public ItemStack removeItemNoUpdate(int slot) {
if (!completedFirstAttunement) {
return ItemStack.EMPTY;
}

return WarpPlateBlockEntity.super.removeItemNoUpdate(slot);
}

@Override
public void initializeFromExisting(ServerLevelAccessor world, Waystone existingWaystone, ItemStack itemStack) {
super.initializeFromExisting(world, existingWaystone, itemStack);

CompoundTag tag = itemStack.getTag();
completedFirstAttunement = tag != null && tag.getBoolean("CompletedFirstAttunement");

if (!completedFirstAttunement) {
initializeInventory(world);
}
}

@Override
public void initializeWaystone(ServerLevelAccessor world, @Nullable LivingEntity player, WaystoneOrigin origin) {
super.initializeWaystone(world, player, origin);
Expand All @@ -106,29 +66,6 @@ public void initializeWaystone(ServerLevelAccessor world, @Nullable LivingEntity
}

WaystoneSyncManager.sendWaystoneUpdateToAll(world.getServer(), waystone);

initializeInventory(world);
}

private void initializeInventory(ServerLevelAccessor levelAccessor) {
WarpPlateRecipe initializingRecipe = levelAccessor.getLevel().getRecipeManager().getAllRecipesFor(ModRecipes.warpPlateRecipeType)
.stream()
.filter(holder -> holder.id().getNamespace().equals(Waystones.MOD_ID) && holder.id().getPath().equals("attuned_shard"))
.map(RecipeHolder::value)
.findFirst()
.orElse(null);
if (initializingRecipe == null) {
logger.error("Failed to find Warp Plate recipe for initial attunement");
completedFirstAttunement = true;
return;
}

for (int i = 0; i < 5; i++) {
final var ingredient = initializingRecipe.getIngredients().get(i);
final var ingredientItems = ingredient.getItems();
final var ingredientItem = ingredientItems.length > 0 ? ingredientItems[0] : ItemStack.EMPTY;
setItem(i, ingredientItem.copy());
}
}

@Override
Expand All @@ -144,8 +81,6 @@ public void saveAdditional(CompoundTag tag) {
tag.putString("CustomName", Component.Serializer.toJson(customName));
}

tag.putBoolean("ReadyForAttunement", readyForAttunement);
tag.putBoolean("CompletedFirstAttunement", completedFirstAttunement);
tag.putInt("LastAttunementSlot", lastAttunementSlot);
}

Expand All @@ -157,8 +92,6 @@ public void load(CompoundTag compound) {
customName = Component.Serializer.fromJson(compound.getString("CustomName"));
}

readyForAttunement = compound.getBoolean("ReadyForAttunement");
completedFirstAttunement = compound.getBoolean("CompletedFirstAttunement");
lastAttunementSlot = compound.getInt("LastAttunementSlot");
}

Expand Down Expand Up @@ -209,12 +142,6 @@ private boolean isEntityOnWarpPlate(Entity entity) {
&& entity.getZ() >= worldPosition.getZ() && entity.getZ() < worldPosition.getZ() + 1;
}

@Override
protected void craft(WarpPlateRecipe recipe) {
super.craft(recipe);
this.completedFirstAttunement = true;
}

@Override
public void serverTick() {
super.serverTick();
Expand Down Expand Up @@ -394,37 +321,10 @@ public Optional<IWaystone> getTargetWaystone() {
return WaystonesAPI.getBoundWaystone(getTargetAttunementStack());
}

@Nullable
protected WarpPlateRecipe trySelectRecipe() {
if (!readyForAttunement) {
return null;
}
return super.trySelectRecipe();
}

/**
* We delay attunement until the menu is opened to show the player what's happening inside the slots before converting the items to an attuned shard.
*/
public void markReadyForAttunement() {
readyForAttunement = true;
}

public void markEntityForCooldown(Entity entity) {
ticksPassedPerEntity.put(entity, -1);
}

public boolean isCompletedFirstAttunement() {
return completedFirstAttunement;
}

@Override
public boolean canPlaceItem(int index, ItemStack stack) {
if (index == 0 && !getItem(0).isEmpty()) {
return false; //prevents hoppers to add items in an occupied center slot
}
return WarpPlateBlockEntity.super.canPlaceItem(index, stack);
}

@Override
public Component getDisplayName() {
return hasCustomName() ? getCustomName() : getName();
Expand All @@ -444,4 +344,9 @@ public void setCustomName(@Nullable Component customName) {
public Component getName() {
return Component.translatable("container.waystones.warp_plate");
}

@Override
public boolean shouldPerformInitialAttunement() {
return true;
}
}
Loading

0 comments on commit 8304556

Please sign in to comment.