Skip to content

Commit

Permalink
chore: improve crafting matrix sync #3
Browse files Browse the repository at this point in the history
  • Loading branch information
klikli-dev committed Jun 13, 2023
1 parent 8e2522f commit 34c7b40
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 16 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ minecraft {
clientalt {
parent runs.client
//somehow doesnt replace the name but adds a new one, so we manually need remove
args '--username', 'Dev####' //#s get replaced with random numbers when starting
args '--username', 'Occultism2' //#s get replaced with random numbers when starting
}

server {
Expand Down
1 change: 1 addition & 0 deletions src/generated/resources/assets/occultism/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,7 @@
"message.occultism.familiar.otherworld_bird.enabled": "Ring Effect - Drikwing: Enabled",
"message.occultism.familiar.shub_niggurath_familiar.disabled": "Ring Effect - Shub Niggurath: Disabled",
"message.occultism.familiar.shub_niggurath_familiar.enabled": "Ring Effect - Shub Niggurath: Enabled",
"messages.occultism.container_already_open": "This container is already opened by another player, wait until they close it.",
"multiblock.occultism.craft_afrit": "Sevira's Permanent Confinement",
"multiblock.occultism.craft_djinni": "Strigeor's Higher Binding",
"multiblock.occultism.craft_foliot": "Eziveus' Spectral Compulsion",
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/klikli_dev/occultism/TranslationKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ public class TranslationKeys {
protected static final String JEI = "jei." + Occultism.MODID;

public static final String JEI_CRUSHING_RECIPE_TIER = JEI + ".crushing.tier";

public static final String MESSAGE_CONTAINER_ALREADY_OPEN = "messages." + Occultism.MODID + ".container_already_open";
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.klikli_dev.occultism.common.blockentity.StableWormholeBlockEntity;
import com.klikli_dev.occultism.common.container.storage.StorageControllerContainerBase;
import com.klikli_dev.occultism.registry.OccultismTiles;
import com.klikli_dev.occultism.util.BlockEntityUtil;
import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -219,10 +220,11 @@ public InteractionResult use(BlockState state, Level level, BlockPos pos, Player
InteractionHand handIn, BlockHitResult rayTraceResult) {
if (!level.isClientSide) {
BlockEntity blockEntity = level.getBlockEntity(pos);
if (blockEntity instanceof StableWormholeBlockEntity wormhole) {
if (wormhole.getLinkedStorageController() != null)
if (blockEntity instanceof StableWormholeBlockEntity wormhole && StorageControllerContainerBase.canOpen(player, pos)) {
if (wormhole.getLinkedStorageController() != null) {
NetworkHooks.openScreen((ServerPlayer) player, wormhole, pos);
else {
StorageControllerContainerBase.reserve(player, pos);
} else {
level.setBlock(pos, state.setValue(LINKED, false), 2);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package com.klikli_dev.occultism.common.block.storage;

import com.klikli_dev.occultism.common.blockentity.StorageControllerBlockEntity;
import com.klikli_dev.occultism.common.container.storage.StorageControllerContainerBase;
import com.klikli_dev.occultism.registry.OccultismTiles;
import com.klikli_dev.occultism.util.BlockEntityUtil;
import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -84,8 +85,9 @@ public InteractionResult use(BlockState state, Level level, BlockPos pos, Player
InteractionHand handIn, BlockHitResult rayTraceResult) {
if (!level.isClientSide) {
BlockEntity blockEntity = level.getBlockEntity(pos);
if (blockEntity instanceof MenuProvider provider) {
if (blockEntity instanceof MenuProvider provider && StorageControllerContainerBase.canOpen(player, pos)) {
NetworkHooks.openScreen((ServerPlayer) player, provider, pos);
StorageControllerContainerBase.reserve(player, pos);
}
}
return InteractionResult.SUCCESS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public CompoundTag saveNetwork(CompoundTag compound) {
compound.putInt("sortType", this.getSortType().getValue());

ListTag matrixNbt = new ListTag();
for (int i = 0; i < 9; i++) {
for (int i = 0; i < this.matrix.size(); i++) {
if (this.matrix.get(i) != null && !this.matrix.get(i).isEmpty()) {
CompoundTag stackTag = new CompoundTag();
stackTag.putByte("slot", (byte) i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

package com.klikli_dev.occultism.common.container.storage;

import com.klikli_dev.occultism.TranslationKeys;
import com.klikli_dev.occultism.api.common.blockentity.IStorageController;
import com.klikli_dev.occultism.api.common.container.IStorageControllerContainer;
import com.klikli_dev.occultism.api.common.data.GlobalBlockPos;
Expand All @@ -30,7 +31,10 @@
import com.klikli_dev.occultism.common.misc.StorageControllerCraftingInventory;
import com.klikli_dev.occultism.common.misc.StorageControllerSlot;
import com.klikli_dev.occultism.network.OccultismPackets;
import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.core.NonNullList;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.Container;
Expand All @@ -46,19 +50,20 @@
import net.minecraftforge.items.wrapper.PlayerMainInvWrapper;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.*;

public abstract class StorageControllerContainerBase extends AbstractContainerMenu implements IStorageControllerContainer {

/**
* Hack to only allow one player to open a container at a time.
*/
public static Map<BlockPos, UUID> openContainers = new HashMap<>();
public Inventory playerInventory;
public Player player;
protected ResultContainer result;
protected StorageControllerCraftingInventory matrix;
protected SimpleContainer orderInventory;
protected CraftingRecipe currentRecipe;

/**
* used to lock recipe while crafting
*/
Expand All @@ -73,6 +78,19 @@ protected StorageControllerContainerBase(@Nullable MenuType<?> type, int id, Inv
this.orderInventory = new SimpleContainer(1);
}

public static boolean canOpen(Player player, BlockPos pos) {
if (!openContainers.containsKey(pos)) {
return true;
}

player.sendSystemMessage(Component.translatable(TranslationKeys.MESSAGE_CONTAINER_ALREADY_OPEN).withStyle(ChatFormatting.RED));
return false;
}

public static void reserve(Player player, BlockPos pos) {
openContainers.put(pos, player.getUUID());
}

@Override
public GlobalBlockPos getStorageControllerGlobalBlockPos() {
return GlobalBlockPos.from(
Expand Down Expand Up @@ -148,6 +166,7 @@ public void removed(Player playerIn) {
this.updateCraftingSlots(false);
this.updateOrderSlot(true); //only send network update on second call
super.removed(playerIn);
openContainers.values().removeIf(uuid -> uuid.equals(playerIn.getUUID()));
}

protected void setupPlayerInventorySlots() {
Expand Down Expand Up @@ -193,10 +212,7 @@ protected void setupOrderInventorySlot() {
protected void findRecipeForMatrixClient() {
Optional<CraftingRecipe> optional =
this.player.level().getRecipeManager().getRecipeFor(RecipeType.CRAFTING, this.matrix, this.player.level());
optional.ifPresentOrElse(iCraftingRecipe -> this.currentRecipe = iCraftingRecipe, () -> {
this.currentRecipe = null;
this.result.setItem(0, ItemStack.EMPTY);
});
optional.ifPresentOrElse(iCraftingRecipe -> this.currentRecipe = iCraftingRecipe, () -> this.currentRecipe = null);
}

protected void findRecipeForMatrix() {
Expand All @@ -219,8 +235,6 @@ protected void findRecipeForMatrix() {

this.result.setItem(0, itemstack);
serverplayerentity.connection.send(new ClientboundContainerSetSlotPacket(this.containerId, 0, 0, itemstack));
} else {
this.findRecipeForMatrixClient();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@ private void addMiscTranslations() {
//"(.*?)": "(.*)",
//this.add\("\1", "\2"\);

this.add(TranslationKeys.MESSAGE_CONTAINER_ALREADY_OPEN, "This container is already opened by another player, wait until they close it.");

//Jobs
this.add("job.occultism.lumberjack", "Lumberjack");
this.add("job.occultism.crush_tier1", "Slow Crusher");
Expand Down

0 comments on commit 34c7b40

Please sign in to comment.