Skip to content

Commit

Permalink
Data Linking and Networking
Browse files Browse the repository at this point in the history
Signed-off-by: SuperScary
  • Loading branch information
SuperScary committed Oct 30, 2024
1 parent 5579267 commit 8bcaf1f
Show file tree
Hide file tree
Showing 32 changed files with 400 additions and 142 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package net.superscary.fluxmachines.api.data;

public interface DataComponent {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package net.superscary.fluxmachines.api.data;

import java.util.List;

public interface DataLinkInteract {

List<DataComponent> getLinkedData();

void setLinkedData (List<DataComponent> data);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.superscary.fluxmachines.api.energy;

import net.minecraft.world.item.ItemStack;

public interface Chargeable {

void charge (ItemStack stack, int amount);

}
18 changes: 0 additions & 18 deletions src/main/java/net/superscary/fluxmachines/api/energy/Decays.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.superscary.fluxmachines.api.blockentity;
package net.superscary.fluxmachines.api.energy;

import net.minecraft.world.item.Item;

Expand All @@ -9,7 +9,7 @@ public interface EnergizedCrafter {

/**
* Has enough energy to iterate once in the crafting process.
* @param required
* @param required the amount of energy required to do a single tick process
* @return
*/
boolean hasEnoughEnergy (int required);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.superscary.fluxmachines.api.energy;

public interface PoweredBlock {

FMEnergyStorage getEnergyStorage();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.superscary.fluxmachines.api.network;

public interface NetworkComponent {

default boolean canBeAnchor () {
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@
import net.neoforged.neoforge.event.server.ServerStoppingEvent;
import net.neoforged.neoforge.registries.RegisterEvent;
import net.neoforged.neoforge.server.ServerLifecycleHooks;
import net.superscary.fluxmachines.core.hook.BlockHook;
import net.superscary.fluxmachines.core.hook.HammerHook;
import net.superscary.fluxmachines.core.hook.RedstoneHook;
import net.superscary.fluxmachines.core.hook.WrenchHook;
import net.superscary.fluxmachines.gui.screen.base.BaseScreen;
import net.superscary.fluxmachines.core.hooks.*;
import net.superscary.fluxmachines.impl.top.FMTopPlugin;
import net.superscary.fluxmachines.core.item.material.FMArmorMaterials;
import net.superscary.fluxmachines.core.registries.*;
Expand Down Expand Up @@ -53,10 +49,11 @@ public FluxMachinesBase (IEventBus modEventBus) {
NeoForge.EVENT_BUS.addListener(this::onServerAboutToStart);
NeoForge.EVENT_BUS.addListener(this::serverStopped);
NeoForge.EVENT_BUS.addListener(this::serverStopping);
NeoForge.EVENT_BUS.addListener(WrenchHook::onPlayerUseBlockEvent);
NeoForge.EVENT_BUS.addListener(HammerHook::onPlayerUseBlockEvent);
NeoForge.EVENT_BUS.addListener(RedstoneHook::onPlayerUseBlockEvent);
NeoForge.EVENT_BUS.addListener(BlockHook::place);
NeoForge.EVENT_BUS.addListener(WrenchHooks::onPlayerUseBlockEvent);
NeoForge.EVENT_BUS.addListener(HammerHooks::onPlayerUseBlockEvent);
NeoForge.EVENT_BUS.addListener(RedstoneHooks::onPlayerUseBlockEvent);
NeoForge.EVENT_BUS.addListener(DataLinkHooks::onPlayerUseBlockEvent);
NeoForge.EVENT_BUS.addListener(BlockHooks::place);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.gameevent.GameEvent;
import net.minecraft.world.phys.BlockHitResult;
import net.neoforged.neoforge.items.ItemStackHandler;
import net.superscary.fluxmachines.api.blockentity.Upgradeable;
import net.superscary.fluxmachines.api.data.BlockData;
import net.superscary.fluxmachines.api.inventory.InventoryHolder;
import net.superscary.fluxmachines.api.network.NetworkComponent;
import net.superscary.fluxmachines.core.block.base.FMBaseEntityBlock;
import net.superscary.fluxmachines.core.components.InventoryComponent;
import net.superscary.fluxmachines.core.hook.WrenchHook;
import net.superscary.fluxmachines.core.hooks.WrenchHooks;
import net.superscary.fluxmachines.core.registries.FMDataComponents;
import net.superscary.fluxmachines.core.registries.FMItems;
import net.superscary.fluxmachines.core.util.inventory.ContentDropper;
Expand All @@ -37,7 +38,7 @@
import java.util.ArrayList;
import java.util.List;

public abstract class FMBaseBlockEntity extends BlockEntity implements MenuProvider, BlockData, InventoryHolder, Upgradeable {
public abstract class FMBaseBlockEntity extends BlockEntity implements MenuProvider, BlockData, InventoryHolder, NetworkComponent {

public final ItemStackHandler INVENTORY_SINGLE = new ItemStackHandler(5) {
@Override
Expand Down Expand Up @@ -140,7 +141,7 @@ public void saveToItem (@NotNull ItemStack stack, HolderLookup.@NotNull Provider
}

/**
* Allows disassembly with wrench. Called by {@link WrenchHook#onPlayerUseBlock(Player, Level, InteractionHand, BlockHitResult)}
* Allows disassembly with wrench. Called by {@link WrenchHooks#onPlayerUseBlock(Player, Level, InteractionHand, BlockHitResult)}
* @param player {@link Player} the player
* @param level {@link Level} the level
* @param hitResult {@link BlockHitResult} hit result of the interaction
Expand Down Expand Up @@ -201,6 +202,16 @@ public void setData (ItemStack stack) {
}
}

/**
* Saves and updates the block state to sync with Server and Client.
*/
public void saveAndUpdate (Level level, BlockPos pos, BlockState state) {
assert level != null;
level.setBlockAndUpdate(pos, state);
level.gameEvent(GameEvent.BLOCK_CHANGE, pos, GameEvent.Context.of(state));
setChanged();
}

@Override
public ItemStackHandler getInventory () {
return inventory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.superscary.fluxmachines.api.blockentity.Upgradeable;
import net.superscary.fluxmachines.api.energy.Decays;
import net.superscary.fluxmachines.api.energy.EnergyDecay;
import net.superscary.fluxmachines.api.data.DataLinkInteract;
import net.superscary.fluxmachines.api.energy.FMEnergyStorage;
import net.superscary.fluxmachines.api.energy.PoweredBlock;
import net.superscary.fluxmachines.core.block.base.FMBaseEntityBlock;
import net.superscary.fluxmachines.core.registries.FMDataComponents;
import net.superscary.fluxmachines.core.util.keys.Keys;
import org.jetbrains.annotations.Nullable;

public abstract class FMBasePoweredBlockEntity extends FMBaseBlockEntity implements EnergyDecay, Upgradeable {
public abstract class FMBasePoweredBlockEntity extends FMBaseBlockEntity implements PoweredBlock, Upgradeable, DataLinkInteract {

private final FMEnergyStorage energyStorage;

Expand All @@ -46,6 +46,7 @@ public void loadClientData (CompoundTag tag, HolderLookup.Provider registries) {
energyStorage.deserializeNBT(registries, IntTag.valueOf(tag.getInt(Keys.POWER)));
}

@Override
public FMEnergyStorage getEnergyStorage () {
return energyStorage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.neoforged.neoforge.items.ItemStackHandler;
import net.superscary.fluxmachines.api.blockentity.Crafter;
import net.superscary.fluxmachines.api.blockentity.EnergizedCrafter;
import net.superscary.fluxmachines.api.data.DataComponent;
import net.superscary.fluxmachines.api.energy.EnergizedCrafter;
import net.superscary.fluxmachines.core.blockentity.base.FMBasePoweredBlockEntity;
import net.superscary.fluxmachines.core.util.block.FMBlockStates;
import net.superscary.fluxmachines.core.util.keys.Keys;
import net.superscary.fluxmachines.gui.menu.FluxFurnaceMenu;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

Expand Down Expand Up @@ -76,12 +78,12 @@ public void loadClientData (CompoundTag tag, HolderLookup.Provider registries) {
}

@Override
public void tick (Level pLevel, BlockPos pPos, BlockState pState) {
public void tick (Level level, BlockPos pos, BlockState state) {
if (TESTING) getEnergyStorage().receiveEnergy(10000, false); // TODO: not for production
if (hasRecipe()) {
isCrafting = true;
updateBlockState(pState.setValue(BlockStateProperties.POWERED, Boolean.TRUE));
updateBlockState(pState.setValue(FMBlockStates.REDSTONE_ON, Boolean.TRUE));
updateBlockState(state.setValue(BlockStateProperties.POWERED, Boolean.TRUE));
updateBlockState(state.setValue(FMBlockStates.REDSTONE_ON, Boolean.TRUE));
increaseCraftingProgress();
getEnergyStorage().extractEnergy(getEnergyAmount(), false);
setChanged();
Expand All @@ -92,12 +94,12 @@ public void tick (Level pLevel, BlockPos pPos, BlockState pState) {
isCrafting = false;
}
} else {
updateBlockState(pState.setValue(BlockStateProperties.POWERED, Boolean.FALSE));
updateBlockState(pState.setValue(FMBlockStates.REDSTONE_ON, Boolean.FALSE));
updateBlockState(state.setValue(BlockStateProperties.POWERED, Boolean.FALSE));
updateBlockState(state.setValue(FMBlockStates.REDSTONE_ON, Boolean.TRUE));
isCrafting = false;
progress = 0;
}
super.tick(pLevel, pPos, pState);
super.tick(level, pos, state);
}

@Override
Expand Down Expand Up @@ -188,4 +190,14 @@ public int getScaledProgress () {
return max != 0 && progress != 0 ? progress * arrowSize / max : 0;
}

@Override
public List<DataComponent> getLinkedData () {
return List.of();
}

@Override
public void setLinkedData (List<DataComponent> data) {

}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.superscary.fluxmachines.core.hook;
package net.superscary.fluxmachines.core.hooks;

import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.player.Player;
Expand All @@ -9,7 +9,7 @@
import net.superscary.fluxmachines.core.block.base.FMBaseEntityBlock;
import net.superscary.fluxmachines.core.blockentity.base.FMBasePoweredBlockEntity;

public class BlockHook {
public class BlockHooks {

public static void place (BlockEvent.EntityPlaceEvent event) {
if (event.getEntity() instanceof Player player) {
Expand Down
Loading

0 comments on commit 8bcaf1f

Please sign in to comment.