Open/close EC block while opening/closing SEC + Play open & close sounds

This commit is contained in:
2025-04-11 14:06:21 +02:00
parent c3a13c8063
commit f28159b442
3 changed files with 41 additions and 5 deletions

View File

@ -38,7 +38,10 @@ As a challenge I'm trying to make it as user-friendly as possible. (It ain't the
## Server Side
- [x] Chat Calculator
## Client Side
### New Features
- [x] Teleportation Angle Viewer for [this machine](https://www.youtube.com/watch?v=FnUE-ZaALLw)
![Teleportation Keybindings](https://i.imgur.com/gjO1H3d.png)
- [x] Removed game fog (lava, water, etc.)
@ -48,6 +51,11 @@ As a challenge I'm trying to make it as user-friendly as possible. (It ain't the
- [x] Removed darkness effect
- [x] Toggleable
### Changes
- Shared Ender Chest
- [x] Open/close EC block while opening/closing SEC
- [x] Play open & close sounds
# Features to come
## Server Side

View File

@ -10,6 +10,7 @@ import net.fabricmc.fabric.api.event.player.UseBlockCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.minecraft.block.Blocks;
import net.minecraft.block.EnderChestBlock;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventories;
@ -20,6 +21,7 @@ import net.minecraft.nbt.NbtSizeTracker;
import net.minecraft.screen.GenericContainerScreenHandler;
import net.minecraft.screen.SimpleNamedScreenHandlerFactory;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text;
@ -88,15 +90,13 @@ public class SharedEnderChest implements ServerLifecycleEvents.ServerStopping, S
if (!player.isSpectator()) {
if(!getConfig().sharedEnderChestLimitedAccess) {
if (world.isClient()) return ActionResult.SUCCESS;
playEnderChestOpenSound(world, hitResult.getBlockPos());
openSharedEnderChest(player);
openSharedEnderChest(player, world, hitResult.getBlockPos());
return ActionResult.SUCCESS;
} else {
for(String name : getConfig().sharedEnderChestNames) {
if(name.toLowerCase().strip().equalsIgnoreCase(player.getNameForScoreboard().toLowerCase())) {
if (world.isClient()) return ActionResult.SUCCESS;
playEnderChestOpenSound(world, hitResult.getBlockPos());
openSharedEnderChest(player);
openSharedEnderChest(player, world, hitResult.getBlockPos());
return ActionResult.SUCCESS;
}
}
@ -113,7 +113,10 @@ public class SharedEnderChest implements ServerLifecycleEvents.ServerStopping, S
}
public static void openSharedEnderChest(PlayerEntity player) {
public static void openSharedEnderChest(PlayerEntity player, World world, BlockPos pos) {
playEnderChestOpenSound(world, pos);
fakeEnderChestOpen(world, pos, true);
sharedInventory.openedEnderChests.put(player, pos);
player.openHandledScreen(new SimpleNamedScreenHandlerFactory((int_1, playerInventory, playerEntity) ->
new GenericContainerScreenHandler(getConfig().screenHandlerType(), int_1, playerInventory, sharedInventory, getConfig().sharedEnderChestRows), Text.of(getConfig().sharedEnderChestName)));
}
@ -122,6 +125,19 @@ public class SharedEnderChest implements ServerLifecycleEvents.ServerStopping, S
world.playSound(null, pos, SoundEvents.BLOCK_ENDER_CHEST_OPEN, SoundCategory.BLOCKS, 0.5F, world.random.nextFloat() * 0.1F + 0.9F);
}
public static void playEnderChestCloseSound(World world, BlockPos pos) {
world.playSound(null, pos, SoundEvents.BLOCK_ENDER_CHEST_CLOSE, SoundCategory.BLOCKS, 0.5F, world.random.nextFloat() * 0.1F + 0.9F);
}
public static void fakeEnderChestOpen(World world, BlockPos pos, boolean open) {
if (!(world.getBlockState(pos).getBlock() instanceof EnderChestBlock)) {
return;
}
world.addSyncedBlockEvent(pos, Blocks.ENDER_CHEST, 1, open ? 1 : 0);
}
private static File getFile(MinecraftServer server) {
return server.getSavePath(WorldSavePath.ROOT).resolve("sharedenderchest.sav").toFile();
}

View File

@ -5,10 +5,15 @@ import net.minecraft.inventory.Inventories;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.math.BlockPos;
import java.util.HashMap;
import java.util.Map;
public class SharedInventory implements Inventory {
private final DefaultedList<ItemStack> stacks;
public final Map<PlayerEntity, BlockPos> openedEnderChests = new HashMap<>();
public SharedInventory(int inventoryRows) {
this.stacks = DefaultedList.ofSize(inventoryRows * 9, ItemStack.EMPTY);
@ -80,4 +85,11 @@ public class SharedInventory implements Inventory {
public void clear() {
stacks.clear();
}
@Override
public void onClose(PlayerEntity player) {
BlockPos pos = openedEnderChests.remove(player);
SharedEnderChest.fakeEnderChestOpen(player.getWorld(), pos, false);
SharedEnderChest.playEnderChestCloseSound(player.getWorld(), pos);
}
}