Skip to content

Commit

Permalink
Merge pull request #547 from refinedmods/fix/GH-541/auto-register
Browse files Browse the repository at this point in the history
Auto register noop synchronizer
  • Loading branch information
raoulvdberge authored Jun 9, 2024
2 parents 5e13073 + 7f7505e commit 51af76e
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,6 @@ public List<NetworkNodeContainer> sortDeterministically(final Set<NetworkNodeCon
return containers
.stream()
.sorted(Comparator.comparingInt(allowed::indexOf))
.collect(Collectors.toList());
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.refinedmods.refinedstorage2.platform.common.detector.DetectorScreen;
import com.refinedmods.refinedstorage2.platform.common.exporter.ExporterScreen;
import com.refinedmods.refinedstorage2.platform.common.grid.GridContainerMenu;
import com.refinedmods.refinedstorage2.platform.common.grid.NoopGridSynchronizer;
import com.refinedmods.refinedstorage2.platform.common.grid.WirelessGridContainerMenu;
import com.refinedmods.refinedstorage2.platform.common.grid.screen.CraftingGridScreen;
import com.refinedmods.refinedstorage2.platform.common.grid.screen.GridScreen;
Expand Down Expand Up @@ -51,13 +50,6 @@
import static com.refinedmods.refinedstorage2.platform.common.util.IdentifierUtil.createIdentifier;

public abstract class AbstractClientModInitializer {
protected static void registerBaseGridSynchronizer() {
PlatformApi.INSTANCE.getGridSynchronizerRegistry().register(
createIdentifier("off"),
NoopGridSynchronizer.INSTANCE
);
}

protected static void registerScreens(final ScreenRegistration registration) {
registration.register(Menus.INSTANCE.getDiskDrive(), DiskDriveScreen::new);
registration.register(Menus.INSTANCE.getGrid(), GridScreen<GridContainerMenu>::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import com.refinedmods.refinedstorage2.platform.api.upgrade.UpgradeRegistry;
import com.refinedmods.refinedstorage2.platform.api.wirelesstransmitter.WirelessTransmitterRangeModifier;
import com.refinedmods.refinedstorage2.platform.common.grid.AbstractGridContainerMenu;
import com.refinedmods.refinedstorage2.platform.common.grid.NoopGridSynchronizer;
import com.refinedmods.refinedstorage2.platform.common.grid.screen.hint.GridInsertionHintsImpl;
import com.refinedmods.refinedstorage2.platform.common.grid.screen.hint.ItemGridInsertionHint;
import com.refinedmods.refinedstorage2.platform.common.grid.screen.hint.SingleItemGridInsertionHint;
Expand Down Expand Up @@ -88,7 +89,6 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.PriorityQueue;
import java.util.Queue;
Expand Down Expand Up @@ -116,7 +116,9 @@
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.saveddata.SavedData;

import static com.refinedmods.refinedstorage2.platform.common.util.IdentifierUtil.createIdentifier;
import static com.refinedmods.refinedstorage2.platform.common.util.IdentifierUtil.createTranslation;
import static java.util.Objects.requireNonNull;

public class PlatformApiImpl implements PlatformApi {
private final StorageRepository clientStorageRepository =
Expand Down Expand Up @@ -169,6 +171,10 @@ public class PlatformApiImpl implements PlatformApi {
private final CompositeSlotReferenceProvider slotReferenceProvider = new CompositeSlotReferenceProvider();
private final PlatformRegistry<PlatformPermission> permissionRegistry = new PlatformRegistryImpl<>();

public PlatformApiImpl() {
gridSynchronizerRegistry.register(createIdentifier("off"), NoopGridSynchronizer.INSTANCE);
}

@Override
public PlatformRegistry<StorageType> getStorageTypeRegistry() {
return storageTypeRegistry;
Expand All @@ -180,7 +186,7 @@ public StorageRepository getStorageRepository(final Level level) {
if (level.getServer() == null) {
return clientStorageRepository;
}
final ServerLevel serverLevel = Objects.requireNonNull(level.getServer().getLevel(Level.OVERWORLD));
final ServerLevel serverLevel = requireNonNull(level.getServer().getLevel(Level.OVERWORLD));
return serverLevel.getDataStorage().computeIfAbsent(new SavedData.Factory<>(
this::createStorageRepository,
this::createStorageRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.IOException;
import java.nio.file.AtomicMoveNotSupportedException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

import com.mojang.logging.LogUtils;
Expand All @@ -27,17 +28,23 @@ public void save(final File file) {
compoundTag.put("data", this.save(new CompoundTag()));
NbtUtils.addCurrentDataVersion(compoundTag);
try {
// Write to temp file first.
NbtIo.writeCompressed(compoundTag, tempFile);
// Try atomic move
try {
Files.move(tempFile, targetPath, StandardCopyOption.ATOMIC_MOVE);
} catch (final AtomicMoveNotSupportedException ignored) {
Files.move(tempFile, targetPath, StandardCopyOption.REPLACE_EXISTING);
}
doSave(compoundTag, tempFile, targetPath);
} catch (final IOException e) {
LOGGER.error("Could not save data {}", this, e);
}
setDirty(false);
}

private void doSave(final CompoundTag compoundTag,
final Path tempFile,
final Path targetPath) throws IOException {
// Write to temp file first.
NbtIo.writeCompressed(compoundTag, tempFile);
// Try atomic move
try {
Files.move(tempFile, targetPath, StandardCopyOption.ATOMIC_MOVE);
} catch (final AtomicMoveNotSupportedException ignored) {
Files.move(tempFile, targetPath, StandardCopyOption.REPLACE_EXISTING);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"block.refinedstorage2.grid": "Grid",
"block.refinedstorage2.crafting_grid": "Crafting Grid",
"block.refinedstorage2.controller": "Controller",
"block.refinedstorage2.controller.rei_fully_charged": "Fully charged Controller",
"block.refinedstorage2.creative_controller": "Creative Controller",
"block.refinedstorage2.1k_storage_block": "1K Storage Block",
"block.refinedstorage2.4k_storage_block": "4K Storage Block",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public Optional<String> getModName(final String modId) {
void setUp() {
viewBuilder = new GridViewBuilderImpl(
FACTORY,
(view) -> Comparator.comparing(GridResource::getName),
(view) -> Comparator.comparing(GridResource::getAmount)
view -> Comparator.comparing(GridResource::getName),
view -> Comparator.comparing(GridResource::getAmount)
);
dirt = new ItemResource(Items.DIRT, null);
stone = new ItemResource(Items.STONE, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,11 @@
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.level.block.Block;
import org.lwjgl.glfw.GLFW;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.refinedmods.refinedstorage2.platform.common.util.IdentifierUtil.createIdentifier;
import static com.refinedmods.refinedstorage2.platform.common.util.IdentifierUtil.createTranslationKey;

public class ClientModInitializerImpl extends AbstractClientModInitializer implements ClientModInitializer {
private static final Logger LOGGER = LoggerFactory.getLogger(ClientModInitializerImpl.class);

@Override
public void onInitializeClient() {
setRenderLayers();
Expand All @@ -91,7 +87,6 @@ public <M extends AbstractContainerMenu, U extends Screen & MenuAccess<M>> void
});
registerKeyBindings();
registerModelPredicates();
registerBaseGridSynchronizer();
registerResourceRendering();
registerAlternativeGridHints();
registerItemProperties();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,13 @@
import net.neoforged.neoforge.client.settings.KeyModifier;
import net.neoforged.neoforge.common.NeoForge;
import org.lwjgl.glfw.GLFW;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.refinedmods.refinedstorage2.platform.common.content.ContentIds.DISK_DRIVE;
import static com.refinedmods.refinedstorage2.platform.common.content.ContentIds.PORTABLE_GRID;
import static com.refinedmods.refinedstorage2.platform.common.util.IdentifierUtil.createIdentifier;
import static com.refinedmods.refinedstorage2.platform.common.util.IdentifierUtil.createTranslationKey;

public final class ClientModInitializer extends AbstractClientModInitializer {
private static final Logger LOGGER = LoggerFactory.getLogger(ClientModInitializer.class);

private ClientModInitializer() {
}

Expand All @@ -66,7 +62,6 @@ public static void onClientSetup(final FMLClientSetupEvent e) {
e.enqueueWork(ClientModInitializer::registerModelPredicates);
e.enqueueWork(ClientModInitializer::registerItemProperties);
registerBlockEntityRenderer();
registerBaseGridSynchronizer();
registerResourceRendering();
registerAlternativeGridHints();
registerDiskModels();
Expand Down

0 comments on commit 51af76e

Please sign in to comment.