Skip to content

Commit

Permalink
Network/backend rewrite (1/?) (#116)
Browse files Browse the repository at this point in the history
* network rewrite (broken)

* It kinda works now?

* Only a few bugs remain!

* This seems to work reasonably well?

* Removed placement hack so that parts don't get placed on the client when they can't get placed on the server

* Inconsistencies are bad

* Re enable sorting optimization

* That message was a bit confusing
  • Loading branch information
2xsaiko authored and amadornes committed Jan 2, 2018
1 parent b9a2dd1 commit c7c1578
Show file tree
Hide file tree
Showing 13 changed files with 424 additions and 315 deletions.
28 changes: 16 additions & 12 deletions src/main/java/mcmultipart/MCMPCommonProxy.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
package mcmultipart;

import java.util.List;

import javax.annotation.Nonnull;

import org.apache.commons.lang3.tuple.Pair;

import com.google.common.collect.Lists;

import mcmultipart.api.item.ItemBlockMultipart;
import mcmultipart.api.multipart.IMultipart;
import mcmultipart.multipart.MultipartRegistry;
import mcmultipart.multipart.MultipartRegistry.WrappedBlock;
import mcmultipart.network.MultipartNetworkHandler;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemBucket;
Expand All @@ -30,15 +24,18 @@
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.relauncher.Side;
import org.apache.commons.lang3.tuple.Pair;

import javax.annotation.Nonnull;
import java.util.List;

public class MCMPCommonProxy {

public void preInit() {
}
public void preInit() { }

public void init() {
}
public void init() { }

public EntityPlayer getPlayer() {
return null;
Expand All @@ -54,6 +51,13 @@ public void scheduleTick(Runnable runnable, Side side) {
}
}

@SubscribeEvent
public void onServerTick(TickEvent.ServerTickEvent e) {
if (e.phase == TickEvent.Phase.END) {
MultipartNetworkHandler.flushChanges();
}
}

@SubscribeEvent
public void onPlayerRightClickBlock(PlayerInteractEvent.RightClickBlock event) {
EntityPlayer player = event.getEntityPlayer();
Expand All @@ -74,7 +78,7 @@ public void onPlayerRightClickBlock(PlayerInteractEvent.RightClickBlock event) {
}

private EnumActionResult placePart(@Nonnull ItemStack itemstack, @Nonnull EntityPlayer player, @Nonnull World world, @Nonnull BlockPos pos,
@Nonnull EnumFacing side, float hitX, float hitY, float hitZ, @Nonnull EnumHand hand, @Nonnull Pair<WrappedBlock, IMultipart> info) {
@Nonnull EnumFacing side, float hitX, float hitY, float hitZ, @Nonnull EnumHand hand, @Nonnull Pair<WrappedBlock, IMultipart> info) {
int meta = itemstack.getItemDamage();
int size = itemstack.getCount();
NBTTagCompound nbt = null;
Expand Down
23 changes: 12 additions & 11 deletions src/main/java/mcmultipart/api/item/ItemBlockMultipart.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ public <T extends Block & IMultipart> ItemBlockMultipart(T block) {

@Override
public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX,
float hitY, float hitZ) {
float hitY, float hitZ) {
return place(player, world, pos, hand, facing, hitX, hitY, hitZ, this, this.block::getStateForPlacement, multipartBlock,
this::placeBlockAtTested, ItemBlockMultipart::placePartAt);
}

public static EnumActionResult place(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX,
float hitY, float hitZ, Item item, IBlockPlacementInfo stateProvider, IMultipart multipartBlock,
IBlockPlacementLogic blockLogic, IPartPlacementLogic partLogic) {
float hitY, float hitZ, Item item, IBlockPlacementInfo stateProvider, IMultipart multipartBlock,
IBlockPlacementLogic blockLogic, IPartPlacementLogic partLogic) {
ItemStack stack = player.getHeldItem(hand);

if (!stack.isEmpty()) {
Expand All @@ -65,14 +65,15 @@ public static EnumActionResult place(EntityPlayer player, World world, BlockPos
if (!player.capabilities.isCreativeMode) {
stack.shrink(1);
}

return EnumActionResult.SUCCESS;
}
return EnumActionResult.FAIL;
}

public static boolean placeAt(ItemStack stack, EntityPlayer player, EnumHand hand, World world, BlockPos pos, EnumFacing facing,
float hitX, float hitY, float hitZ, IBlockPlacementInfo stateProvider, int meta, IMultipart multipartBlock,
IBlockPlacementLogic blockLogic, IPartPlacementLogic partLogic) {
float hitX, float hitY, float hitZ, IBlockPlacementInfo stateProvider, int meta, IMultipart multipartBlock,
IBlockPlacementLogic blockLogic, IPartPlacementLogic partLogic) {
IBlockState state = stateProvider.getStateForPlacement(world, pos, facing, hitX, hitY, hitZ, meta, player, hand);
AxisAlignedBB bb = state.getCollisionBoundingBox(world, pos);
if ((bb == null || world.checkNoEntityCollision(bb.offset(pos)))
Expand All @@ -85,14 +86,14 @@ public static boolean placeAt(ItemStack stack, EntityPlayer player, EnumHand han
}

public boolean placeBlockAtTested(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumFacing facing, float hitX,
float hitY, float hitZ, IBlockState newState) {
float hitY, float hitZ, IBlockState newState) {
return player.canPlayerEdit(pos, facing, stack) && world.getBlockState(pos).getBlock().isReplaceable(world, pos)
&& block.canPlaceBlockAt(world, pos) && block.canPlaceBlockOnSide(world, pos, facing)
&& super.placeBlockAt(stack, player, world, pos, facing, hitX, hitY, hitZ, newState);
}

public static boolean placePartAt(ItemStack stack, EntityPlayer player, EnumHand hand, World world, BlockPos pos, EnumFacing facing,
float hitX, float hitY, float hitZ, IMultipart multipartBlock, IBlockState state) {
float hitX, float hitY, float hitZ, IMultipart multipartBlock, IBlockState state) {
IPartSlot slot = multipartBlock.getSlotForPlacement(world, pos, state, facing, hitX, hitY, hitZ, player);
if (!multipartBlock.canPlacePartAt(world, pos) || !multipartBlock.canPlacePartOnSide(world, pos, facing, slot))
return false;
Expand Down Expand Up @@ -156,28 +157,28 @@ public static boolean setMultipartTileNBT(EntityPlayer player, ItemStack stack,
public static interface IPartPlacementLogic {

public boolean placePart(ItemStack stack, EntityPlayer player, EnumHand hand, World world, BlockPos pos, EnumFacing facing,
float hitX, float hitY, float hitZ, IMultipart multipartBlock, IBlockState state);
float hitX, float hitY, float hitZ, IMultipart multipartBlock, IBlockState state);

}

public static interface IBlockPlacementLogic {

public boolean place(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumFacing facing, float hitX, float hitY,
float hitZ, IBlockState newState);
float hitZ, IBlockState newState);

}

public static interface IBlockPlacementInfo {

public IBlockState getStateForPlacement(World world, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta,
EntityLivingBase placer, EnumHand hand);
EntityLivingBase placer, EnumHand hand);

}

public static interface IExtendedBlockPlacementInfo {

public IBlockState getStateForPlacement(World world, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta,
EntityLivingBase placer, EnumHand hand, IBlockState state);
EntityLivingBase placer, EnumHand hand, IBlockState state);

}

Expand Down
15 changes: 8 additions & 7 deletions src/main/java/mcmultipart/api/multipart/MultipartHelper.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
package mcmultipart.api.multipart;

import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Function;

import com.google.common.base.Preconditions;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;

import mcmultipart.api.container.IMultipartContainer;
import mcmultipart.api.container.IMultipartContainerBlock;
import mcmultipart.api.container.IPartInfo;
import mcmultipart.api.ref.MCMPCapabilities;
import mcmultipart.api.slot.IPartSlot;
import mcmultipart.network.MultipartNetworkHandler;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;

import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Function;

public final class MultipartHelper {

private MultipartHelper() {
Expand All @@ -45,6 +45,7 @@ public static boolean addPart(World world, BlockPos pos, IPartSlot slot, IBlockS
if (container.canAddPart(slot, state, tile)) {
if (!simulated && !world.isRemote) {
container.addPart(slot, state, tile);
MultipartNetworkHandler.flushChanges(world, pos);
}
return true;
}
Expand Down
Loading

0 comments on commit c7c1578

Please sign in to comment.