Skip to content

Commit

Permalink
Major refactor (#25)
Browse files Browse the repository at this point in the history
* Fixed networking synchronisation

* Ported to 1.16.2
Refactored everything from CCA to custom elements.
  • Loading branch information
Redblueflame committed Aug 21, 2020
1 parent b889fb3 commit 577f634
Show file tree
Hide file tree
Showing 53 changed files with 816 additions and 530 deletions.
28 changes: 21 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,34 @@ dependencies {
modCompile "net.fabricmc:fabric-loader:${project.loader_version}"
testImplementation 'com.google.code.findbugs:jsr305:3.0.2'
modCompile "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
modCompile "io.github.onyxstudios.Cardinal-Components-API:cardinal-components-base:2.4.0"
include "io.github.onyxstudios.Cardinal-Components-API:cardinal-components-base:2.4.0"

modCompile "io.github.onyxstudios.Cardinal-Components-API:cardinal-components-base:2.5.1"
include "io.github.onyxstudios.Cardinal-Components-API:cardinal-components-base:2.5.1"
// Adds an implementation dependency on the entity module

/*
modCompile "io.github.onyxstudios.Cardinal-Components-API:cardinal-components-entity:2.4.0"
include "io.github.onyxstudios.Cardinal-Components-API:cardinal-components-entity:2.4.0"
modCompile "io.github.onyxstudios.Cardinal-Components-API:cardinal-components-item:2.4.0"
include "io.github.onyxstudios.Cardinal-Components-API:cardinal-components-item:2.4.0"
modCompile "io.github.onyxstudios.Cardinal-Components-API:cardinal-components-block:2.4.0"
include "io.github.onyxstudios.Cardinal-Components-API:cardinal-components-block:2.4.0"
modCompile "com.github.vini2003:spinnery:${project.spinnery_version}"
include "com.github.vini2003:spinnery:${project.spinnery_version}"
modImplementation "io.github.prospector:modmenu:${project.modmenu_version}"
include "io.github.prospector:modmenu:${project.modmenu_version}"
modImplementation "vazkii.patchouli:Patchouli_1.16-fabric:${project.patchouli_version}"
*/
modCompile("com.github.vini2003:spinnery:${project.spinnery_version}"){
exclude module:"fabric-api"
}
include("com.github.vini2003:spinnery:${project.spinnery_version}"){
exclude module:"fabric-api"
}
modImplementation("io.github.prospector:modmenu:${project.modmenu_version}"){
exclude module:"fabric-api"
}
include("io.github.prospector:modmenu:${project.modmenu_version}"){
exclude module:"fabric-api"
}
modImplementation("vazkii.patchouli:Patchouli:${project.patchouli_version}"){
exclude module:"fabric-api"
}
//modRuntime ("com.github.Chocohead:Data-Breaker-Lower:24be1a2")

/* this will expose the dependency to anyone building against your mod. You might
Expand Down
14 changes: 7 additions & 7 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
org.gradle.jvmargs=-Xmx1G
# Fabric Properties
# check these on https://fabricmc.net/use
minecraft_version=1.16.1
yarn_mappings=1.16.1+build.5
loader_version=0.8.8+build.202
minecraft_version=1.16.2
yarn_mappings=1.16.2+build.21
loader_version=0.9.2+build.206
# Mod Properties
mod_version=1.0-SNAPSHOT
maven_group=com.redblueflame
archives_base_name=herbocraft
# Dependencies
# currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api
fabric_version=0.13.1+build.370-1.16
spinnery_version=3.0.39+fabric-1.16.x
modmenu_version = 1.12.1+build.15
patchouli_version = 1.16-37.15-FABRIC
fabric_version=0.19.0+build.398-1.16
spinnery_version=3.1.15+fabric-1.16.x
modmenu_version = 1.14.6+build.31
patchouli_version = 1.16-40-FABRIC
32 changes: 11 additions & 21 deletions src/main/java/com/redblueflame/herbocraft/HerboCraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.google.gson.JsonSyntaxException;
import com.redblueflame.herbocraft.blocks.*;
import com.redblueflame.herbocraft.components.LevelComponent;
import com.redblueflame.herbocraft.components.TurretLevelComponent;
import com.redblueflame.herbocraft.entities.BulletEntity;
import com.redblueflame.herbocraft.entities.SnowTurretEntity;
import com.redblueflame.herbocraft.entities.TurretBaseEntity;
Expand All @@ -18,17 +17,15 @@
import io.netty.buffer.Unpooled;
import nerdhub.cardinal.components.api.ComponentRegistry;
import nerdhub.cardinal.components.api.ComponentType;
import nerdhub.cardinal.components.api.event.EntityComponentCallback;
import nerdhub.cardinal.components.api.event.ItemComponentCallback;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder;
import net.fabricmc.fabric.api.container.ContainerProviderRegistry;
import net.fabricmc.fabric.api.network.ServerSidePacketRegistry;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.fabricmc.fabric.api.object.builder.v1.entity.FabricDefaultAttributeRegistry;
import net.fabricmc.fabric.api.object.builder.v1.entity.FabricEntityTypeBuilder;
import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.fabricmc.fabric.api.screenhandler.v1.ScreenHandlerRegistry;
import net.fabricmc.fabric.api.server.PlayerStream;
import net.fabricmc.fabric.api.tag.TagRegistry;
import net.minecraft.block.*;
Expand All @@ -39,8 +36,6 @@
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.*;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.recipe.Recipe;
import net.minecraft.recipe.RecipeType;
import net.minecraft.resource.ResourceManager;
import net.minecraft.resource.ResourceType;
import net.minecraft.server.network.ServerPlayerEntity;
Expand Down Expand Up @@ -71,7 +66,7 @@ public class HerboCraft implements ModInitializer {
// Utils
public static Gson GSON;
public static TurretLooter LOOTS;
private static Logger LOGGER = LogManager.getLogger();
public static Logger LOGGER = LogManager.getLogger();

public static Tag<Item> SEEDS;
public static Tag<Item> BASE_SEEDS;
Expand Down Expand Up @@ -137,6 +132,7 @@ public class HerboCraft implements ModInitializer {
//endregion

//region Components
@Deprecated
public static final ComponentType<LevelComponent> LEVELLING =
ComponentRegistry.INSTANCE.registerIfAbsent(new Identifier(name, "levelling"), LevelComponent.class);
//endregion
Expand Down Expand Up @@ -217,14 +213,14 @@ public void onInitialize() {
UPGRADER_BLOCK_ENTITY = Registry.register(Registry.BLOCK_ENTITY_TYPE, new Identifier(name, "upgrader"), BlockEntityType.Builder.create(UpgraderBlockEntity::new, UPGRADER).build(null));

// Register containers
ContainerProviderRegistry.INSTANCE.registerFactory(STERILIZER_CONTAINER,
(syncId, id, player, buf) -> new SterilizerBlockContainer(syncId, buf.readText(), player.inventory, buf.readBlockPos(), player.world));
ContainerProviderRegistry.INSTANCE.registerFactory(GROWTH_CONTROLLER_CONTAINER,
(syncId, id, player, buf) -> new GrowthControllerContainer(syncId, buf.readText(), player.inventory, buf.readBlockPos(), player.world));
ContainerProviderRegistry.INSTANCE.registerFactory(REPRODUCER_CONTAINER,
(syncId, id, player, buf) -> new ReproducerBlockContainer(syncId, buf.readText(), player.inventory, buf.readBlockPos(), player.world));
ContainerProviderRegistry.INSTANCE.registerFactory(UPGRADER_CONTAINER,
(syncId, id, player, buf) -> new UpgraderBlockContainer(syncId, buf.readText(), player.inventory, buf.readBlockPos(), player.world));
ScreenHandlerRegistry.registerExtended(STERILIZER_CONTAINER,
(id, inv, buf) -> new SterilizerBlockContainer(id, buf.readText(), inv, buf.readBlockPos(), inv.player.world));
ScreenHandlerRegistry.registerExtended(GROWTH_CONTROLLER_CONTAINER,
(syncId, inv, buf) -> new GrowthControllerContainer(syncId, buf.readText(), inv, buf.readBlockPos(), inv.player.world));
ScreenHandlerRegistry.registerExtended(REPRODUCER_CONTAINER,
(syncId, inv, buf) -> new ReproducerBlockContainer(syncId, buf.readText(), inv, buf.readBlockPos(), inv.player.world));
ScreenHandlerRegistry.registerExtended(UPGRADER_CONTAINER,
(syncId, inv, buf) -> new UpgraderBlockContainer(syncId, buf.readText(), inv, buf.readBlockPos(), inv.player.world));
// Register recipes
Registry.register(Registry.RECIPE_SERIALIZER, new Identifier(name, "patchouli_book"), new PatchouliBookRecipe.Serializer());

Expand Down Expand Up @@ -258,12 +254,6 @@ public void onInitialize() {
});
});
//endregion
// Register turret Levelling system
EntityComponentCallback.event(TurretBaseEntity.class).register(TurretBaseEntity::initComponents);

ItemComponentCallback.event(TURRET_SEED).register((stack, components) -> components.put(LEVELLING, TurretLevelComponent.getRandomStats((short) 5)));
ItemComponentCallback.event(WITHER_SEED).register((stack, components) -> components.put(LEVELLING, TurretLevelComponent.getRandomStats((short) 5)));
ItemComponentCallback.event(SNOW_SEED).register((stack, components) -> components.put(LEVELLING, TurretLevelComponent.getRandomStats((short) 5)));
// Load JSON config files
ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(new IdentifiableResourceReloadListener() {
@Override
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/com/redblueflame/herbocraft/HerboCraftClient.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.redblueflame.herbocraft;

import com.redblueflame.herbocraft.blocks.AbstractProgressBlockEntity;
import com.redblueflame.herbocraft.entities.renderer.SnowTurretRenderer;
import com.redblueflame.herbocraft.entities.renderer.TurretBaseRenderer;
import com.redblueflame.herbocraft.entities.renderer.WitherTurretRenderer;
Expand Down Expand Up @@ -59,5 +60,43 @@ public void onInitializeClient() {
}
});
});
// Register the initial load
ClientSidePacketRegistry.INSTANCE.register(HerboCraftPackets.STATE_CHANGE, ((packetContext, packetByteBuf) -> {
BlockPos pos = packetByteBuf.readBlockPos();
boolean isWorking = packetByteBuf.readBoolean();
int targetWork = packetByteBuf.readInt();
packetContext.getTaskQueue().execute(() -> {
assert MinecraftClient.getInstance().world != null;
AbstractProgressBlockEntity blockEntity = (AbstractProgressBlockEntity) MinecraftClient.getInstance().world.getBlockEntity(pos);
if (blockEntity == null) {
HerboCraft.LOGGER.error("The block entity you're trying to change does not exist on the client !");
return;
}
if (isWorking) {
blockEntity.isWorking = true;
blockEntity.targetWork = targetWork;
} else {
blockEntity.resetWork();
}
});
}));
ClientSidePacketRegistry.INSTANCE.register(HerboCraftPackets.SEND_STATUS, ((packetContext, packetByteBuf) -> {
BlockPos pos = packetByteBuf.readBlockPos();
int targetWork = packetByteBuf.readInt();
int currentWork = packetByteBuf.readInt();
packetContext.getTaskQueue().execute(() -> {
assert MinecraftClient.getInstance().world != null;
AbstractProgressBlockEntity blockEntity = (AbstractProgressBlockEntity) MinecraftClient.getInstance().world.getBlockEntity(pos);
if (blockEntity == null) {
HerboCraft.LOGGER.error("The block entity you're trying to change does nos exist on the client !");
return;
}
blockEntity.targetWork = targetWork;
blockEntity.currentWork = currentWork;
if (targetWork != 0) {
blockEntity.state = (int) ((targetWork/(float)currentWork)*255F);
}
});
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
public class HerboCraftPackets {
public static final Identifier WATERING_CAN_USAGE_PACKET = new Identifier("herbocraft", "watering_can_usage");
public static final Identifier WATERING_PARTICLE_PACKET = new Identifier("herbocraft", "watering_particle");
public static final Identifier STATE_CHANGE = new Identifier("herbocraft", "state_change");
public static final Identifier SEND_STATUS = new Identifier("herbocraft", "send_state");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.redblueflame.herbocraft.blocks;

import com.redblueflame.herbocraft.HerboCraft;
import net.minecraft.block.BlockEntityProvider;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

@SuppressWarnings("deprecation")
public abstract class AbstractProgressBlock extends AbstractUpgradableBlock implements BlockEntityProvider {
protected AbstractProgressBlock(Settings settings) {
super(settings);
}

@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
if (!world.isClient) {
// Sync the server with client for the state of the block.
AbstractProgressBlockEntity blockEntity = (AbstractProgressBlockEntity) world.getBlockEntity(pos);
if (blockEntity == null) {
HerboCraft.LOGGER.error("There were an error while getting the blockentity. Please check your input.");
return ActionResult.PASS;
}
blockEntity.sendBlockProgressPacket(player);
}
return ActionResult.PASS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package com.redblueflame.herbocraft.blocks;

import com.redblueflame.herbocraft.HerboCraftPackets;
import com.redblueflame.herbocraft.utils.PlayerContainerStream;
import io.netty.buffer.Unpooled;
import net.fabricmc.fabric.api.network.ServerSidePacketRegistry;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventory;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Tickable;

public abstract class AbstractProgressBlockEntity extends BlockEntity implements Tickable, Progress {
public boolean isWorking;
public int targetWork;
public int currentWork;
public int state;

public AbstractProgressBlockEntity(BlockEntityType<?> type) {
super(type);
isWorking = false;
targetWork = 0;
currentWork = 0;
state = 0;
}

@Override
public void tick() {
if (world != null && !world.isClient) {
// We have to check for the server, is it's on the client, we will have problems.
if (isWorking && targetWork != 0) {
currentWork += 1;
state = (int) (((float)currentWork / targetWork) * 255F);
if (currentWork >= targetWork) {
resetWork();
finishWork();
}
} else {
if (getInventory() != null && this.checkWork()) {
targetWork = estimateWork();
setWorking();
}
}
} else if (isWorking) {
// On the client, we can add the currentWork for display
currentWork += 1;
state = (int) (((float)currentWork / targetWork) * 255F);
}
}

@Override
public void fromTag(BlockState state, CompoundTag tag) {
super.fromTag(state, tag);
isWorking = tag.getBoolean("isWorking");
targetWork = tag.getInt("targetWork");
currentWork = tag.getInt("currentWork");
if (targetWork != 0) {
this.state = currentWork/targetWork;
}
}

@Override
public CompoundTag toTag(CompoundTag tag) {
super.toTag(tag);
tag.putBoolean("isWorking", isWorking);
tag.putInt("targetWork", targetWork);
tag.putInt("currentWork", currentWork);
return tag;
}

@Override
public void resetWork() {
isWorking = false;
// Send the update to clients
if (world != null && !world.isClient) {
sendStateChangePacket();
}
targetWork = 0;
currentWork = 0;
state = 0;
}

private void setWorking() {
isWorking = true;
if (world != null && !world.isClient) {
sendStateChangePacket();
}
currentWork = 0;
}

protected void registerEvents() {
if (getInventory() != null) {
getInventory().addListener(this::onInventoryUpdate);
}
}

private void onInventoryUpdate(Inventory inventory) {
if (inventoryUpdate(inventory)) {
resetWork();
}
}
// region Packets
private void sendStateChangePacket() {
PacketByteBuf passedData = new PacketByteBuf(Unpooled.buffer());
passedData.writeBlockPos(pos);
passedData.writeBoolean(isWorking);
passedData.writeInt(targetWork);
PlayerContainerStream.GetPlayerWatchingContainer(world, pos, this.getContainerClass()).forEach(playerEntity ->
ServerSidePacketRegistry.INSTANCE.sendToPlayer(playerEntity, HerboCraftPackets.STATE_CHANGE, passedData));
}

protected void sendBlockProgressPacket() {
PacketByteBuf passedData = new PacketByteBuf(Unpooled.buffer());
passedData.writeBlockPos(pos);
passedData.writeInt(targetWork);
passedData.writeInt(currentWork);
PlayerContainerStream.GetPlayerWatchingContainer(world, pos, this.getContainerClass()).forEach(playerEntity ->
ServerSidePacketRegistry.INSTANCE.sendToPlayer(playerEntity, HerboCraftPackets.SEND_STATUS, passedData));
}

protected void sendBlockProgressPacket(PlayerEntity e) {
PacketByteBuf passedData = new PacketByteBuf(Unpooled.buffer());
passedData.writeBlockPos(pos);
passedData.writeInt(targetWork);
passedData.writeInt(currentWork);
ServerSidePacketRegistry.INSTANCE.sendToPlayer(e, HerboCraftPackets.SEND_STATUS, passedData);
}
// endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import net.minecraft.world.BlockView;
import net.minecraft.world.World;

public class GrowthController extends AbstractUpgradableBlock implements BlockEntityProvider {
public class GrowthController extends AbstractProgressBlock {
public GrowthController(Settings settings) {
super(settings);
setDefaultState(this.stateManager.getDefaultState().with(Properties.HORIZONTAL_FACING, Direction.NORTH));
Expand All @@ -34,6 +34,7 @@ public BlockEntity createBlockEntity(BlockView world) {
@SuppressWarnings("deprecation")
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
super.onUse(state, world, pos, player, hand, hit);
if (!world.isClient) {
ContainerProviderRegistry.INSTANCE.openContainer(HerboCraft.GROWTH_CONTROLLER_CONTAINER, player, (buffer) -> {
buffer.writeText(new TranslatableText(this.getTranslationKey()));
Expand Down
Loading

0 comments on commit 577f634

Please sign in to comment.