Update 3
This commit is contained in:
parent
f2de0513f2
commit
5076757001
|
@ -4,9 +4,11 @@ import com.simibubi.create.foundation.data.CreateRegistrate;
|
|||
import com.simibubi.create.repack.registrate.util.NonNullLazyValue;
|
||||
import com.teammoeg.steampowered.create.SPBlocks;
|
||||
import com.teammoeg.steampowered.create.SPTiles;
|
||||
import com.teammoeg.steampowered.network.PacketHandler;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.item.ItemGroup;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.event.RegistryEvent;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
|
@ -25,6 +27,10 @@ public class SteamPowered {
|
|||
|
||||
public static final String MODID = "steampowered";
|
||||
|
||||
public static ResourceLocation rl(String path) {
|
||||
return new ResourceLocation(MODID, path);
|
||||
}
|
||||
|
||||
public static final ItemGroup itemGroup = new ItemGroup(MODID) {
|
||||
@Override
|
||||
@Nonnull
|
||||
|
@ -39,12 +45,8 @@ public class SteamPowered {
|
|||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
|
||||
public SteamPowered() {
|
||||
// Register the setup method for modloading
|
||||
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup);
|
||||
// Register the doClientStuff method for modloading
|
||||
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::doClientStuff);
|
||||
|
||||
// Register ourselves for server and other game events we are interested in
|
||||
MinecraftForge.EVENT_BUS.register(this);
|
||||
|
||||
FluidRegistry.FLUIDS.register(FMLJavaModLoadingContext.get().getModEventBus());
|
||||
|
@ -53,6 +55,8 @@ public class SteamPowered {
|
|||
|
||||
SPBlocks.register();
|
||||
SPTiles.register();
|
||||
|
||||
PacketHandler.register();
|
||||
}
|
||||
|
||||
private void setup(final FMLCommonSetupEvent event) {
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
package com.teammoeg.steampowered.client;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||
|
||||
@OnlyIn(Dist.CLIENT)
|
||||
public class ClientUtils {
|
||||
public static Minecraft mc() {
|
||||
return Minecraft.getInstance();
|
||||
}
|
||||
|
||||
public static World getClientWorld() {
|
||||
return mc().level;
|
||||
}
|
||||
}
|
|
@ -3,6 +3,8 @@ package com.teammoeg.steampowered.create;
|
|||
import com.simibubi.create.content.contraptions.components.flywheel.engine.EngineTileEntity;
|
||||
import com.simibubi.create.content.contraptions.goggles.IHaveGoggleInformation;
|
||||
import com.teammoeg.steampowered.FluidRegistry;
|
||||
import com.teammoeg.steampowered.network.PacketHandler;
|
||||
import com.teammoeg.steampowered.network.TileSyncPacket;
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.nbt.CompoundNBT;
|
||||
import net.minecraft.tileentity.TileEntityType;
|
||||
|
@ -13,6 +15,7 @@ import net.minecraftforge.common.util.LazyOptional;
|
|||
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
|
||||
import net.minecraftforge.fluids.capability.IFluidHandler;
|
||||
import net.minecraftforge.fluids.capability.templates.FluidTank;
|
||||
import net.minecraftforge.fml.network.PacketDistributor;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
@ -22,38 +25,66 @@ public class SteamEngineTileEntity extends EngineTileEntity implements IHaveGogg
|
|||
|
||||
private static final float GENERATING_CAPACITY = 32F;
|
||||
private static final float GENERATING_SPEED = 32F;
|
||||
private static final int CONSUMING_STEAM_MB_PER_TICK = 100;
|
||||
private static final int CONSUMING_STEAM_MB_PER_TICK = 30;
|
||||
private static final int STEAM_STORAGE_MAXIMUM = 100000;
|
||||
|
||||
protected FluidTank tank = new FluidTank(STEAM_STORAGE_MAXIMUM, fluidStack -> {
|
||||
return fluidStack.getFluid() == FluidRegistry.steam.get();
|
||||
});
|
||||
}) {
|
||||
protected void onContentsChanged() {
|
||||
syncFluidContent();
|
||||
}
|
||||
};
|
||||
|
||||
public void syncFluidContent() {
|
||||
CompoundNBT nbt = new CompoundNBT();
|
||||
nbt.put("tank", tank.writeToNBT(new CompoundNBT()));
|
||||
PacketHandler.send(PacketDistributor.TRACKING_CHUNK.with(() -> {
|
||||
return this.level.getChunkAt(this.worldPosition);
|
||||
}), new TileSyncPacket(this, nbt));
|
||||
}
|
||||
|
||||
public void receiveFromServer(CompoundNBT message) {
|
||||
if (message.contains("tank", 10)) {
|
||||
this.tank.readFromNBT(message.getCompound("tank"));
|
||||
}
|
||||
}
|
||||
|
||||
public void receiveFromClient(CompoundNBT message) {
|
||||
|
||||
}
|
||||
|
||||
private LazyOptional<IFluidHandler> holder = LazyOptional.of(() -> tank);
|
||||
|
||||
public SteamEngineTileEntity(TileEntityType<? extends SteamEngineTileEntity> type) {
|
||||
super(type);
|
||||
this.refreshCapability();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tick() {
|
||||
super.tick();
|
||||
BlockState state = this.level.getBlockState(this.worldPosition);
|
||||
if (!tank.isEmpty()) {
|
||||
if (tank.getFluidAmount() < CONSUMING_STEAM_MB_PER_TICK) {
|
||||
tank.drain(tank.getFluidAmount(), IFluidHandler.FluidAction.EXECUTE);
|
||||
if (level != null && !level.isClientSide) {
|
||||
BlockState state = this.level.getBlockState(this.worldPosition);
|
||||
if (!tank.isEmpty()) {
|
||||
if (tank.getFluidAmount() < CONSUMING_STEAM_MB_PER_TICK) {
|
||||
tank.drain(tank.getFluidAmount(), IFluidHandler.FluidAction.EXECUTE);
|
||||
} else {
|
||||
tank.drain(CONSUMING_STEAM_MB_PER_TICK, IFluidHandler.FluidAction.EXECUTE);
|
||||
this.level.setBlockAndUpdate(this.worldPosition, state.setValue(SteamEngineBlock.LIT, true));
|
||||
this.appliedCapacity = GENERATING_CAPACITY;
|
||||
this.appliedSpeed = GENERATING_SPEED;
|
||||
this.refreshWheelSpeed();
|
||||
}
|
||||
} else {
|
||||
tank.drain(CONSUMING_STEAM_MB_PER_TICK, IFluidHandler.FluidAction.EXECUTE);
|
||||
state.setValue(SteamEngineBlock.LIT, true);
|
||||
this.appliedCapacity = GENERATING_CAPACITY;
|
||||
this.appliedSpeed = GENERATING_SPEED;
|
||||
this.level.setBlockAndUpdate(this.worldPosition, state.setValue(SteamEngineBlock.LIT, false));
|
||||
this.appliedCapacity = 0;
|
||||
this.appliedSpeed = 0;
|
||||
this.refreshWheelSpeed();
|
||||
}
|
||||
System.out.println("server tank: " + tank.getFluidAmount());
|
||||
} else {
|
||||
state.setValue(SteamEngineBlock.LIT, false);
|
||||
this.appliedCapacity = 0;
|
||||
this.appliedSpeed = 0;
|
||||
this.refreshWheelSpeed();
|
||||
System.out.println("client tank: " + tank.getFluidAmount());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
package com.teammoeg.steampowered.network;
|
||||
|
||||
import com.teammoeg.steampowered.SteamPowered;
|
||||
import net.minecraftforge.fml.network.NetworkRegistry;
|
||||
import net.minecraftforge.fml.network.PacketDistributor;
|
||||
import net.minecraftforge.fml.network.simple.SimpleChannel;
|
||||
|
||||
public class PacketHandler {
|
||||
private static final String VERSION = Integer.toString(1);
|
||||
private static final SimpleChannel CHANNEL = NetworkRegistry.newSimpleChannel(SteamPowered.rl("network"), () -> VERSION, VERSION::equals, VERSION::equals);
|
||||
|
||||
public static void send(PacketDistributor.PacketTarget target, Object message) {
|
||||
CHANNEL.send(target, message);
|
||||
}
|
||||
|
||||
public static void sendToServer(Object message) {
|
||||
CHANNEL.sendToServer(message);
|
||||
}
|
||||
|
||||
public static SimpleChannel get() {
|
||||
return CHANNEL;
|
||||
}
|
||||
|
||||
@SuppressWarnings("UnusedAssignment")
|
||||
public static void register() {
|
||||
int id = 0;
|
||||
|
||||
CHANNEL.registerMessage(id++, TileSyncPacket.class, TileSyncPacket::encode, TileSyncPacket::new, TileSyncPacket::handle);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
package com.teammoeg.steampowered.network;
|
||||
|
||||
import blusunrize.immersiveengineering.ImmersiveEngineering;
|
||||
import com.teammoeg.steampowered.client.ClientUtils;
|
||||
import com.teammoeg.steampowered.create.SteamEngineTileEntity;
|
||||
import net.minecraft.entity.player.ServerPlayerEntity;
|
||||
import net.minecraft.nbt.CompoundNBT;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.server.ServerWorld;
|
||||
import net.minecraftforge.fml.LogicalSide;
|
||||
import net.minecraftforge.fml.network.NetworkEvent;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class TileSyncPacket {
|
||||
private BlockPos pos;
|
||||
private CompoundNBT nbt;
|
||||
|
||||
public TileSyncPacket(SteamEngineTileEntity tile, CompoundNBT nbt) {
|
||||
this.pos = tile.getBlockPos();
|
||||
this.nbt = nbt;
|
||||
}
|
||||
|
||||
public TileSyncPacket(PacketBuffer buf) {
|
||||
this.pos = new BlockPos(buf.readInt(), buf.readInt(), buf.readInt());
|
||||
this.nbt = buf.readNbt();
|
||||
}
|
||||
|
||||
public void encode(PacketBuffer buf) {
|
||||
buf.writeInt(this.pos.getX()).writeInt(this.pos.getY()).writeInt(this.pos.getZ());
|
||||
buf.writeNbt(this.nbt);
|
||||
}
|
||||
|
||||
public void handle(Supplier<NetworkEvent.Context> context) {
|
||||
NetworkEvent.Context ctx = (NetworkEvent.Context)context.get();
|
||||
if (ctx.getDirection().getReceptionSide() == LogicalSide.SERVER) {
|
||||
ctx.enqueueWork(() -> {
|
||||
ServerWorld world = ((ServerPlayerEntity) Objects.requireNonNull(ctx.getSender())).getLevel();
|
||||
if (world.isAreaLoaded(this.pos, 1)) {
|
||||
TileEntity tile = world.getBlockEntity(this.pos);
|
||||
if (tile instanceof SteamEngineTileEntity) {
|
||||
((SteamEngineTileEntity)tile).receiveFromClient(this.nbt);
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
} else {
|
||||
ctx.enqueueWork(() -> {
|
||||
World world = ClientUtils.getClientWorld();
|
||||
if (world != null) {
|
||||
TileEntity tile = world.getBlockEntity(this.pos);
|
||||
if (tile instanceof SteamEngineTileEntity) {
|
||||
((SteamEngineTileEntity)tile).receiveFromServer(this.nbt);
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue