Browse Source

huge improvement on performance

master
Martmists 2 years ago
parent
commit
cff5cda03a
  1. 46
      src/main/java/com/martmists/ynet/YNetMod.java
  2. 16
      src/main/java/com/martmists/ynet/YNetModClient.java
  3. 5
      src/main/java/com/martmists/ynet/api/BaseProvider.java
  4. 13
      src/main/java/com/martmists/ynet/api/EnergyProvider.java
  5. 4
      src/main/java/com/martmists/ynet/api/FluidProvider.java
  6. 15
      src/main/java/com/martmists/ynet/api/ItemProvider.java
  7. 55
      src/main/java/com/martmists/ynet/blockentities/ControllerBlockEntity.java
  8. 7
      src/main/java/com/martmists/ynet/blocks/CableBlock.java
  9. 46
      src/main/java/com/martmists/ynet/blocks/ConnectorBlock.java
  10. 15
      src/main/java/com/martmists/ynet/blocks/ControllerBlock.java
  11. 7
      src/main/java/com/martmists/ynet/containers/ControllerContainer.java
  12. 7
      src/main/java/com/martmists/ynet/event/ProviderTickCallback.java
  13. 10
      src/main/java/com/martmists/ynet/event/impl/FluidTickCallback.java
  14. 11
      src/main/java/com/martmists/ynet/event/impl/ItemTickCallback.java
  15. 16
      src/main/java/com/martmists/ynet/mixin/providers/energy/PowerAcceptorMixin.java
  16. 22
      src/main/java/com/martmists/ynet/mixin/providers/item/AbstractFurnaceBlockMixin.java
  17. 22
      src/main/java/com/martmists/ynet/mixin/providers/item/GenericMachineBlockMixin.java
  18. 24
      src/main/java/com/martmists/ynet/mixin/providers/item/LootableContainerBlockMixin.java
  19. 34
      src/main/java/com/martmists/ynet/network/ConnectorConfiguration.java
  20. 88
      src/main/java/com/martmists/ynet/network/Network.java
  21. 34
      src/main/java/com/martmists/ynet/screens/ControllerScreen.java
  22. 100
      src/main/resources/assets/ynet/models/block/cable.json
  23. 98
      src/main/resources/assets/ynet/models/block/cable_side.json
  24. 144
      src/main/resources/assets/ynet/models/block/connector.json
  25. 36
      src/main/resources/assets/ynet/models/item/cable.json
  26. 96
      src/main/resources/assets/ynet/models/item/connector.json
  27. 8
      versions/1.16/gradle.properties

46
src/main/java/com/martmists/ynet/YNetMod.java

@ -10,13 +10,14 @@ import com.martmists.ynet.blocks.CableBlock; @@ -10,13 +10,14 @@ import com.martmists.ynet.blocks.CableBlock;
import com.martmists.ynet.blocks.ConnectorBlock;
import com.martmists.ynet.blocks.ControllerBlock;
import com.martmists.ynet.containers.ControllerContainer;
import com.martmists.ynet.event.ProviderTickCallback;
import com.martmists.ynet.event.impl.EnergyTickCallback;
import com.martmists.ynet.event.impl.FluidTickCallback;
import com.martmists.ynet.event.impl.ItemTickCallback;
import com.martmists.ynet.event.ProviderTickCallback;
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.minecraft.block.Block;
import net.minecraft.block.Material;
import net.minecraft.block.entity.BlockEntityType;
@ -24,13 +25,18 @@ import net.minecraft.item.BlockItem; @@ -24,13 +25,18 @@ import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.registry.Registry;
import java.util.HashMap;
import java.util.Map;
public class YNetMod implements ModInitializer {
public static Identifier CONTROLLER_UPDATE_S2C = new Identifier("ynet", "cus2c");
public static Identifier CONTROLLER_UPDATE_C2S = new Identifier("ynet", "cuc2s");
public static ItemGroup YNET_GROUP = FabricItemGroupBuilder.build(
new Identifier("ynet", "items"),
() -> new ItemStack(
@ -60,20 +66,6 @@ public class YNetMod implements ModInitializer { @@ -60,20 +66,6 @@ public class YNetMod implements ModInitializer {
public static Map<Class<? extends BaseProvider>, String> PROVIDER_NAMES = new HashMap<>();
public static Map<Class<? extends BaseProvider>, Integer> COLOR_MAP = new HashMap<>();
@Override
public void onInitialize() {
System.out.println("YNet loading up!");
register("ynet:item", 0xff489030, ItemProvider.class, new ItemTickCallback());
register("ynet:fluid", 0xff0077be, FluidProvider.class, new FluidTickCallback());
register("ynet:energy", 0xfffffe00, EnergyProvider.class, new EnergyTickCallback());
// TODO:
// - Add support for configuring redstone signals on connectorss
ContainerProviderRegistry.INSTANCE.registerFactory(new Identifier("ynet:controller"), (syncId, id, player, buf) -> {
return new ControllerContainer(syncId, player.inventory, buf);
});
}
static <T extends Block> T register(String name, T block) {
return register(name, block, new BlockItem(block, new Item.Settings().group(YNET_GROUP)));
}
@ -94,4 +86,28 @@ public class YNetMod implements ModInitializer { @@ -94,4 +86,28 @@ public class YNetMod implements ModInitializer {
COLOR_MAP.put(clazz, color);
return callback;
}
@Override
public void onInitialize() {
System.out.println("YNet loading up!");
register("ynet:item", 0xff489030, ItemProvider.class, new ItemTickCallback());
register("ynet:fluid", 0xff0077be, FluidProvider.class, new FluidTickCallback());
register("ynet:energy", 0xfffffe00, EnergyProvider.class, new EnergyTickCallback());
// TODO:
// - Add support for configuring redstone signals on connectorss
ContainerProviderRegistry.INSTANCE.registerFactory(new Identifier("ynet:controller"), (syncId, id, player, buf) -> {
return new ControllerContainer(syncId, player.inventory, buf);
});
ServerSidePacketRegistry.INSTANCE.register(CONTROLLER_UPDATE_C2S, (packetContext, attachedData) -> {
BlockPos pos = attachedData.readBlockPos();
CompoundTag tag = attachedData.readCompoundTag();
packetContext.getTaskQueue().execute(() -> {
ControllerBlockEntity be = (ControllerBlockEntity) packetContext.getPlayer().world.getBlockEntity(pos);
be.fromTag(tag);
be.markDirty();
});
});
}
}

16
src/main/java/com/martmists/ynet/YNetModClient.java

@ -1,10 +1,15 @@ @@ -1,10 +1,15 @@
package com.martmists.ynet;
import com.martmists.ynet.screens.ControllerScreen;
import com.martmists.ynet.blockentities.ControllerBlockEntity;
import com.martmists.ynet.containers.ControllerContainer;
import com.martmists.ynet.screens.ControllerScreen;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.screen.ScreenProviderRegistry;
import net.fabricmc.fabric.api.network.ClientSidePacketRegistry;
import net.minecraft.client.MinecraftClient;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
public class YNetModClient implements ClientModInitializer {
@Override
@ -12,5 +17,14 @@ public class YNetModClient implements ClientModInitializer { @@ -12,5 +17,14 @@ public class YNetModClient implements ClientModInitializer {
ScreenProviderRegistry.INSTANCE.registerFactory(new Identifier("ynet:controller"), (c) -> {
return new ControllerScreen((ControllerContainer) c);
});
ClientSidePacketRegistry.INSTANCE.register(YNetMod.CONTROLLER_UPDATE_S2C, (packetContext, attachedData) -> {
BlockPos pos = attachedData.readBlockPos();
CompoundTag tag = attachedData.readCompoundTag();
packetContext.getTaskQueue().execute(() -> {
ControllerBlockEntity be = (ControllerBlockEntity) MinecraftClient.getInstance().world.getBlockEntity(pos);
be.fromTag(tag);
be.sMarkDirty();
});
});
}
}

5
src/main/java/com/martmists/ynet/api/BaseProvider.java

@ -1,6 +1,5 @@ @@ -1,6 +1,5 @@
package com.martmists.ynet.api;
import java.util.Arrays;
// Base interface for implementation checks
public interface BaseProvider { }
public interface BaseProvider {
}

13
src/main/java/com/martmists/ynet/api/EnergyProvider.java

@ -1,34 +1,33 @@ @@ -1,34 +1,33 @@
package com.martmists.ynet.api;
import net.minecraft.block.BlockState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.BlockView;
public interface EnergyProvider extends BaseProvider {
/**
* @param world The world the block resides in
* @param pos The BlockPos of the block
* @param pos The BlockPos of the block
* @return The maximum energy this block can receive
*/
double getEnergyInputLimit(BlockView world, BlockPos pos);
/**
* @param world The world the block resides in
* @param pos The BlockPos of the block
* @param world The world the block resides in
* @param pos The BlockPos of the block
* @param energy The amount of energy to be added to the machine
*/
void inputEnergy(BlockView world, BlockPos pos, double energy);
/**
* @param world The world the block resides in
* @param pos The BlockPos of the block
* @param pos The BlockPos of the block
* @return The maximum energy this block can output
*/
double getEnergyOutputLimit(BlockView world, BlockPos pos);
/**
* @param world The world the block resides in
* @param pos The BlockPos of the block
* @param world The world the block resides in
* @param pos The BlockPos of the block
* @param energy The amount of energy to be added to the machine
*/
void outputEnergy(BlockView world, BlockPos pos, double energy);

4
src/main/java/com/martmists/ynet/api/FluidProvider.java

@ -6,8 +6,12 @@ import net.minecraft.world.BlockView; @@ -6,8 +6,12 @@ import net.minecraft.world.BlockView;
public interface FluidProvider extends BaseProvider {
int getFluidOutputCount(BlockView world, BlockPos pos);
Fluid getFluidOutput(BlockView world, BlockPos pos);
int getFluidInputCount(BlockView world, BlockPos pos, Fluid fluid);
void outputFluid(BlockView world, BlockPos pos, Fluid fluid, int amount);
void inputFluid(BlockView world, BlockPos pos, Fluid fluid, int amount);
}

15
src/main/java/com/martmists/ynet/api/ItemProvider.java

@ -1,36 +1,35 @@ @@ -1,36 +1,35 @@
package com.martmists.ynet.api;
import net.minecraft.block.BlockState;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.BlockView;
public interface ItemProvider extends BaseProvider {
/**
* @param world The world the block resides in
* @param pos The BlockPos of the block
* @param world The world the block resides in
* @param pos The BlockPos of the block
* @param itemStack The ItemStack to attempt to input
* @return The amount of items from the stack that can be input
*/
int getItemInputCount(BlockView world, BlockPos pos, ItemStack itemStack);
/**
* @param world The world the block resides in
* @param pos The BlockPos of the block
* @param world The world the block resides in
* @param pos The BlockPos of the block
* @param itemStack The ItemStack to add to the block
*/
void inputItem(BlockView world, BlockPos pos, ItemStack itemStack);
/**
* @param world The world the block resides in
* @param pos The BlockPos of the block
* @param pos The BlockPos of the block
* @return The ItemStacks that can be output
*/
ItemStack[] getItemOutputStacks(BlockView world, BlockPos pos);
/**
* @param world The world the block resides in
* @param pos The BlockPos of the block
* @param world The world the block resides in
* @param pos The BlockPos of the block
* @param itemStack the ItemStack to remove from the container
* May not be an existing stack, make sure to check the count
*/

55
src/main/java/com/martmists/ynet/blockentities/ControllerBlockEntity.java

@ -6,14 +6,22 @@ import com.martmists.ynet.event.ProviderTickCallback; @@ -6,14 +6,22 @@ import com.martmists.ynet.event.ProviderTickCallback;
import com.martmists.ynet.network.Channel;
import com.martmists.ynet.network.ConnectorConfiguration;
import com.martmists.ynet.network.Network;
import io.netty.buffer.Unpooled;
import net.fabricmc.fabric.api.network.ClientSidePacketRegistry;
import net.fabricmc.fabric.api.network.ServerSidePacketRegistry;
import net.fabricmc.fabric.api.util.NbtType;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.util.PacketByteBuf;
import net.minecraft.util.Tickable;
import net.minecraft.util.math.BlockPos;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
public class ControllerBlockEntity extends BlockEntity implements Tickable {
@ -23,7 +31,6 @@ public class ControllerBlockEntity extends BlockEntity implements Tickable { @@ -23,7 +31,6 @@ public class ControllerBlockEntity extends BlockEntity implements Tickable {
public ControllerBlockEntity() {
super(YNetMod.CONTROLLER_BE);
this.network = new Network();
this.network.setController(pos);
}
public void updateNetwork() {
@ -32,8 +39,8 @@ public class ControllerBlockEntity extends BlockEntity implements Tickable { @@ -32,8 +39,8 @@ public class ControllerBlockEntity extends BlockEntity implements Tickable {
private List<BlockPos> getConnectedProviders(Set<BlockPos> connectors) {
List<BlockPos> providers = new ArrayList<>();
for (BlockPos c : connectors){
for (BlockPos offset : new BlockPos[]{ c.up(), c.down(), c.north(), c.east(), c.south(), c.west() }){
for (BlockPos c : connectors) {
for (BlockPos offset : new BlockPos[]{c.up(), c.down(), c.north(), c.east(), c.south(), c.west()}) {
if (world.getBlockState(offset).getBlock() instanceof BaseProvider) {
providers.add(offset);
}
@ -44,9 +51,8 @@ public class ControllerBlockEntity extends BlockEntity implements Tickable { @@ -44,9 +51,8 @@ public class ControllerBlockEntity extends BlockEntity implements Tickable {
@Override
public void tick() {
if (network.cables == null) {
network.setController(pos);
updateNetwork();
if (network.connectors == null) {
register();
}
for (Channel ch : channels) {
if (ch != null && ch.providerType != null) {
@ -56,10 +62,6 @@ public class ControllerBlockEntity extends BlockEntity implements Tickable { @@ -56,10 +62,6 @@ public class ControllerBlockEntity extends BlockEntity implements Tickable {
}
}
// TODO:
// - fromTag and toTag
@Override
public void fromTag(CompoundTag tag) {
super.fromTag(tag);
@ -99,10 +101,39 @@ public class ControllerBlockEntity extends BlockEntity implements Tickable { @@ -99,10 +101,39 @@ public class ControllerBlockEntity extends BlockEntity implements Tickable {
c.connectorSettings.forEach((s) -> s.toTag(connectors));
cData.put("connectors", connectors);
cData.putString("type", YNetMod.PROVIDER_NAMES.getOrDefault(c.providerType, "null"));
customData.put("channel_"+i, cData);
customData.put("channel_" + i, cData);
}
}
tag.put("controllerData", customData);
return super.toTag(tag);
}
@Override
public void markDirty() {
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
buf.writeBlockPos(pos);
CompoundTag tag = new CompoundTag();
this.toTag(tag);
buf.writeCompoundTag(tag);
if (world.isClient()) {
// Send C2S
ClientSidePacketRegistry.INSTANCE.sendToServer(YNetMod.CONTROLLER_UPDATE_C2S, buf);
} else {
// Send S2C
for (PlayerEntity player : world.getPlayers()) {
ServerSidePacketRegistry.INSTANCE.sendToPlayer(player, YNetMod.CONTROLLER_UPDATE_S2C, buf);
}
}
super.markDirty();
}
public void sMarkDirty() {
super.markDirty();
}
public void register() {
network.setController(pos);
updateNetwork();
}
}

7
src/main/java/com/martmists/ynet/blocks/CableBlock.java

@ -2,27 +2,20 @@ package com.martmists.ynet.blocks; @@ -2,27 +2,20 @@ package com.martmists.ynet.blocks;
import com.martmists.ynet.YNetMod;
import com.martmists.ynet.api.BaseProvider;
import com.martmists.ynet.blockentities.ControllerBlockEntity;
import com.martmists.ynet.network.Network;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.ConnectingBlock;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.Property;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.BlockView;
import net.minecraft.world.IWorld;
import net.minecraft.world.World;
import java.util.HashSet;
import java.util.Set;
public class CableBlock extends ConnectingBlock {
public CableBlock(Settings settings) {
super(0.1875F, settings);

46
src/main/java/com/martmists/ynet/blocks/ConnectorBlock.java

@ -3,25 +3,25 @@ package com.martmists.ynet.blocks; @@ -3,25 +3,25 @@ package com.martmists.ynet.blocks;
import com.martmists.ynet.YNetMod;
import com.martmists.ynet.api.BaseProvider;
import com.martmists.ynet.blockentities.ConnectorBlockEntity;
import com.martmists.ynet.blockentities.ControllerBlockEntity;
import com.martmists.ynet.network.Network;
import net.minecraft.block.*;
import net.minecraft.block.Block;
import net.minecraft.block.BlockEntityProvider;
import net.minecraft.block.BlockState;
import net.minecraft.block.ConnectingBlock;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.BooleanProperty;
import net.minecraft.state.property.IntProperty;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.BlockView;
import net.minecraft.world.IWorld;
import net.minecraft.world.ModifiableWorld;
import net.minecraft.world.World;
import java.util.*;
import java.util.HashMap;
import java.util.Map;
public class ConnectorBlock extends ConnectingBlock implements BlockEntityProvider {
private static BooleanProperty NORTH_CABLE = BooleanProperty.of("north_cable");
@ -76,8 +76,8 @@ public class ConnectorBlock extends ConnectingBlock implements BlockEntityProvid @@ -76,8 +76,8 @@ public class ConnectorBlock extends ConnectingBlock implements BlockEntityProvid
return super.getStateForNeighborUpdate(state, facing, neighborState, world, pos, neighborPos);
} else {
Block block = neighborState.getBlock();
return state.with(FACING_PROPERTIES.get(facing), block == this || block == YNetMod.CONTROLLER || block instanceof BaseProvider)
.with(CABLE_FACING_PROPERTIES.get(facing), block == YNetMod.CABLE);
return state.with(FACING_PROPERTIES.get(facing), block == YNetMod.CONTROLLER || block instanceof BaseProvider)
.with(CABLE_FACING_PROPERTIES.get(facing), block == this || block == YNetMod.CABLE);
}
}
@ -89,18 +89,18 @@ public class ConnectorBlock extends ConnectingBlock implements BlockEntityProvid @@ -89,18 +89,18 @@ public class ConnectorBlock extends ConnectingBlock implements BlockEntityProvid
Block block5 = world.getBlockState(pos.south()).getBlock();
Block block6 = world.getBlockState(pos.west()).getBlock();
return this.getDefaultState()
.with(DOWN, block == this || block == YNetMod.CONTROLLER || block instanceof BaseProvider)
.with(UP, block2 == this || block2 == YNetMod.CONTROLLER || block2 instanceof BaseProvider)
.with(NORTH, block3 == this || block3 == YNetMod.CONTROLLER || block3 instanceof BaseProvider)
.with(EAST, block4 == this || block4 == YNetMod.CONTROLLER || block4 instanceof BaseProvider)
.with(SOUTH, block5 == this || block5 == YNetMod.CONTROLLER || block5 instanceof BaseProvider)
.with(WEST, block6 == this || block6 == YNetMod.CONTROLLER || block6 instanceof BaseProvider)
.with(DOWN_CABLE, block == YNetMod.CABLE)
.with(UP_CABLE, block2 == YNetMod.CABLE)
.with(NORTH_CABLE, block3 == YNetMod.CABLE)
.with(EAST_CABLE, block4 == YNetMod.CABLE)
.with(SOUTH_CABLE, block5 == YNetMod.CABLE)
.with(WEST_CABLE, block6 == YNetMod.CABLE);
.with(DOWN, block == YNetMod.CONTROLLER || block instanceof BaseProvider)
.with(UP, block2 == YNetMod.CONTROLLER || block2 instanceof BaseProvider)
.with(NORTH, block3 == YNetMod.CONTROLLER || block3 instanceof BaseProvider)
.with(EAST, block4 == YNetMod.CONTROLLER || block4 instanceof BaseProvider)
.with(SOUTH, block5 == YNetMod.CONTROLLER || block5 instanceof BaseProvider)
.with(WEST, block6 == YNetMod.CONTROLLER || block6 instanceof BaseProvider)
.with(DOWN_CABLE, block == this || block == YNetMod.CABLE)
.with(UP_CABLE, block2 == this || block2 == YNetMod.CABLE)
.with(NORTH_CABLE, block3 == this || block3 == YNetMod.CABLE)
.with(EAST_CABLE, block4 == this || block4 == YNetMod.CABLE)
.with(SOUTH_CABLE, block5 == this || block5 == YNetMod.CABLE)
.with(WEST_CABLE, block6 == this || block6 == YNetMod.CABLE);
}
@Override
@ -110,11 +110,11 @@ public class ConnectorBlock extends ConnectingBlock implements BlockEntityProvid @@ -110,11 +110,11 @@ public class ConnectorBlock extends ConnectingBlock implements BlockEntityProvid
@Override
public int getWeakRedstonePower(BlockState state, BlockView world, BlockPos pos, Direction facing) {
return ((ConnectorBlockEntity)world.getBlockEntity(pos)).getRedstonePower(facing);
return ((ConnectorBlockEntity) world.getBlockEntity(pos)).getRedstonePower(facing);
}
public void setRedstoneOutput(Direction facing, BlockView world, BlockPos pos, int strength) {
((ConnectorBlockEntity)world.getBlockEntity(pos)).setRedstonePower(facing, strength);
((ConnectorBlockEntity) world.getBlockEntity(pos)).setRedstonePower(facing, strength);
}
@Override
@ -127,7 +127,7 @@ public class ConnectorBlock extends ConnectingBlock implements BlockEntityProvid @@ -127,7 +127,7 @@ public class ConnectorBlock extends ConnectingBlock implements BlockEntityProvid
Network.addConnector(world, pos);
}
public int getRedstoneOutput(Direction facing, BlockView world, BlockPos pos){
public int getRedstoneOutput(Direction facing, BlockView world, BlockPos pos) {
BlockState state = world.getBlockState(pos.offset(facing));
return state.getBlock().getWeakRedstonePower(state, world, pos, facing);
}

15
src/main/java/com/martmists/ynet/blocks/ControllerBlock.java

@ -1,26 +1,21 @@ @@ -1,26 +1,21 @@
package com.martmists.ynet.blocks;
import com.martmists.ynet.YNetModClient;
import com.martmists.ynet.api.BaseProvider;
import com.martmists.ynet.blockentities.ControllerBlockEntity;
import com.martmists.ynet.network.Network;
import net.fabricmc.fabric.api.container.ContainerProviderRegistry;
import net.minecraft.block.BlockState;
import net.minecraft.block.BlockWithEntity;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.BlockView;
import net.minecraft.world.IWorld;
import net.minecraft.world.World;
import java.util.List;
import java.util.Set;
public class ControllerBlock extends BlockWithEntity {
public ControllerBlock(Settings settings) {
super(settings);
@ -41,4 +36,10 @@ public class ControllerBlock extends BlockWithEntity { @@ -41,4 +36,10 @@ public class ControllerBlock extends BlockWithEntity {
return ActionResult.SUCCESS;
}
@Override
public void onBreak(World world, BlockPos pos, BlockState state, PlayerEntity player) {
super.onBreak(world, pos, state, player);
Network.networks.remove(pos);
}
}

7
src/main/java/com/martmists/ynet/containers/ControllerContainer.java

@ -1,10 +1,12 @@ @@ -1,10 +1,12 @@
package com.martmists.ynet.containers;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.util.PacketByteBuf;
import spinnery.common.BaseContainer;
import spinnery.widget.WInterface;
import spinnery.widget.WSlot;
import spinnery.widget.api.Action;
public class ControllerContainer extends BaseContainer {
public PlayerInventory playerInv;
@ -18,4 +20,9 @@ public class ControllerContainer extends BaseContainer { @@ -18,4 +20,9 @@ public class ControllerContainer extends BaseContainer {
WInterface mainInterface = getInterface();
WSlot.addHeadlessPlayerInventory(mainInterface);
}
@Override
public void onSlotAction(int slotNumber, int inventoryNumber, int button, Action action, PlayerEntity player) {
super.onSlotAction(slotNumber, inventoryNumber, button, action, player);
}
}

7
src/main/java/com/martmists/ynet/event/ProviderTickCallback.java

@ -3,14 +3,7 @@ package com.martmists.ynet.event; @@ -3,14 +3,7 @@ package com.martmists.ynet.event;
import com.martmists.ynet.api.BaseProvider;
import com.martmists.ynet.blockentities.ControllerBlockEntity;
import com.martmists.ynet.network.ConnectorConfiguration;
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.util.ActionResult;
import net.minecraft.util.math.BlockPos;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
public interface ProviderTickCallback<T extends BaseProvider> {

10
src/main/java/com/martmists/ynet/event/impl/FluidTickCallback.java

@ -1,13 +1,11 @@ @@ -1,13 +1,11 @@
package com.martmists.ynet.event.impl;
import com.martmists.ynet.api.FluidProvider;
import com.martmists.ynet.api.ItemProvider;
import com.martmists.ynet.blockentities.ControllerBlockEntity;
import com.martmists.ynet.event.ProviderTickCallback;
import com.martmists.ynet.network.ConnectorConfiguration;
import net.minecraft.fluid.Fluid;
import net.minecraft.fluid.Fluids;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
@ -59,7 +57,7 @@ public class FluidTickCallback implements ProviderTickCallback<FluidProvider> { @@ -59,7 +57,7 @@ public class FluidTickCallback implements ProviderTickCallback<FluidProvider> {
Map<FluidProvider, Integer> fluidsRemoved = new HashMap<>();
while (!entries.isEmpty()) {
if (e.amount <= 0){
if (e.amount <= 0) {
try {
e = entries.get(1);
} catch (IndexOutOfBoundsException exc) {
@ -76,7 +74,7 @@ public class FluidTickCallback implements ProviderTickCallback<FluidProvider> { @@ -76,7 +74,7 @@ public class FluidTickCallback implements ProviderTickCallback<FluidProvider> {
}
boolean found = false;
for (ConnectorConfiguration receiverConfig : takeItems){
for (ConnectorConfiguration receiverConfig : takeItems) {
FluidProvider receiver = (FluidProvider) world.getBlockState(receiverConfig.providerPos).getBlock();
fluidsStored.putIfAbsent(receiver, 0);
int count = Math.min(
@ -88,12 +86,12 @@ public class FluidTickCallback implements ProviderTickCallback<FluidProvider> { @@ -88,12 +86,12 @@ public class FluidTickCallback implements ProviderTickCallback<FluidProvider> {
if (receiverConfig.filter != null) {
Entry fe = e;
// TODO
if (Arrays.stream(receiverConfig.filter).noneMatch((obj) -> obj.equals(fe.fluid))){
if (Arrays.stream(receiverConfig.filter).noneMatch((obj) -> obj.equals(fe.fluid))) {
continue;
}
}
if (count > 0){
if (count > 0) {
fluidsStored.put(receiver, fluidsStored.get(receiver) + count);
fluidsRemoved.put(e.provider, fluidsRemoved.get(e.provider) + count);
int finalCount = e.amount - count;

11
src/main/java/com/martmists/ynet/event/impl/ItemTickCallback.java

@ -1,6 +1,5 @@ @@ -1,6 +1,5 @@
package com.martmists.ynet.event.impl;
import com.martmists.ynet.api.EnergyProvider;
import com.martmists.ynet.api.ItemProvider;
import com.martmists.ynet.blockentities.ControllerBlockEntity;
import com.martmists.ynet.event.ProviderTickCallback;
@ -39,7 +38,7 @@ public class ItemTickCallback implements ProviderTickCallback<ItemProvider> { @@ -39,7 +38,7 @@ public class ItemTickCallback implements ProviderTickCallback<ItemProvider> {
.sorted((a, b) -> a.priority >= b.priority ? 0 : 1)
.forEach(config -> {
ItemProvider p = (ItemProvider) world.getBlockState(config.providerPos).getBlock();
for (ItemStack s : p.getItemOutputStacks(world, config.providerPos)){
for (ItemStack s : p.getItemOutputStacks(world, config.providerPos)) {
entries.add(new Entry(config.providerPos, p, s));
}
});
@ -54,7 +53,7 @@ public class ItemTickCallback implements ProviderTickCallback<ItemProvider> { @@ -54,7 +53,7 @@ public class ItemTickCallback implements ProviderTickCallback<ItemProvider> {
Map<ItemProvider, Integer> itemsRemoved = new HashMap<>();
while (!entries.isEmpty()) {
if (e.items.getCount() <= 0){
if (e.items.getCount() <= 0) {
try {
e = entries.get(1);
} catch (IndexOutOfBoundsException exc) {
@ -71,7 +70,7 @@ public class ItemTickCallback implements ProviderTickCallback<ItemProvider> { @@ -71,7 +70,7 @@ public class ItemTickCallback implements ProviderTickCallback<ItemProvider> {
}
boolean found = false;
for (ConnectorConfiguration receiverConfig : takeItems){
for (ConnectorConfiguration receiverConfig : takeItems) {
ItemProvider receiver = (ItemProvider) world.getBlockState(receiverConfig.providerPos).getBlock();
itemsStored.putIfAbsent(receiver, 0);
int count = Math.min(
@ -82,12 +81,12 @@ public class ItemTickCallback implements ProviderTickCallback<ItemProvider> { @@ -82,12 +81,12 @@ public class ItemTickCallback implements ProviderTickCallback<ItemProvider> {
if (receiverConfig.filter != null) {
Entry fe = e;
if (Arrays.stream(receiverConfig.filter).noneMatch((obj) -> obj == fe.items.getItem())){
if (Arrays.stream(receiverConfig.filter).noneMatch((obj) -> obj == fe.items.getItem())) {
continue;
}
}
if (count > 0){
if (count > 0) {
itemsStored.put(receiver, itemsStored.get(receiver) + count);
itemsRemoved.put(e.provider, itemsRemoved.get(e.provider) + count);
int finalCount = e.items.getCount() - count;

16
src/main/java/com/martmists/ynet/mixin/providers/energy/PowerAcceptorMixin.java

@ -23,15 +23,15 @@ public class PowerAcceptorMixin implements EnergyProvider { @@ -23,15 +23,15 @@ public class PowerAcceptorMixin implements EnergyProvider {
}
return be.canAcceptEnergy(null) ?
Math.min(
be.getMaxStoredPower()-be.getStored(EnergySide.UNKNOWN),
be.getMaxInput(EnergySide.UNKNOWN)) :
be.getMaxStoredPower() - be.getStored(EnergySide.UNKNOWN),
be.getMaxInput(EnergySide.UNKNOWN)) :
0;
}
@Override
public void inputEnergy(BlockView world, BlockPos pos, double energy) {
PowerAcceptorBlockEntity be = getBlockEntity(world, pos);
be.setEnergy(be.getEnergy()+energy);
be.setEnergy(be.getEnergy() + energy);
}
@Override
@ -42,20 +42,20 @@ public class PowerAcceptorMixin implements EnergyProvider { @@ -42,20 +42,20 @@ public class PowerAcceptorMixin implements EnergyProvider {
}
return be.canProvideEnergy(null) ?
Math.min(
be.getStored(EnergySide.UNKNOWN),
be.getMaxOutput(EnergySide.UNKNOWN)) :
be.getStored(EnergySide.UNKNOWN),
be.getMaxOutput(EnergySide.UNKNOWN)) :
0;
}
@Override
public void outputEnergy(BlockView world, BlockPos pos, double energy) {
PowerAcceptorBlockEntity be = getBlockEntity(world, pos);
be.setEnergy(be.getEnergy()-energy);
be.setEnergy(be.getEnergy() - energy);
}
private PowerAcceptorBlockEntity getBlockEntity(BlockView world, BlockPos pos){
private PowerAcceptorBlockEntity getBlockEntity(BlockView world, BlockPos pos) {
BlockEntity be = world.getBlockEntity(pos);
if (be instanceof PowerAcceptorBlockEntity){
if (be instanceof PowerAcceptorBlockEntity) {
return (PowerAcceptorBlockEntity) be;
}
return null;

22
src/main/java/com/martmists/ynet/mixin/providers/item/AbstractFurnaceBlockMixin.java

@ -2,32 +2,30 @@ package com.martmists.ynet.mixin.providers.item; @@ -2,32 +2,30 @@ package com.martmists.ynet.mixin.providers.item;
import com.martmists.ynet.api.ItemProvider;
import net.minecraft.block.AbstractFurnaceBlock;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.AbstractFurnaceBlockEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.BlockView;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
@Mixin(AbstractFurnaceBlock.class)
public abstract class AbstractFurnaceBlockMixin implements ItemProvider {
@Override
public int getItemInputCount(BlockView world, BlockPos pos, ItemStack itemStack) {
AbstractFurnaceBlockEntity be = getBlockEntity(world, pos);
if (AbstractFurnaceBlockEntity.canUseAsFuel(itemStack)){
if (AbstractFurnaceBlockEntity.canUseAsFuel(itemStack)) {
// put in fuel slot
ItemStack stack = be.getInvStack(1);
if (stack.isEmpty()){
if (stack.isEmpty()) {
return itemStack.getCount();
}
return stack.getMaxCount() - stack.getCount();
} else {
ItemStack stack = be.getInvStack(0);
if (stack.isEmpty()){
if (stack.isEmpty()) {
return itemStack.getCount();
} else {
if (stack.getItem() == itemStack.getItem()){
if (stack.getItem() == itemStack.getItem()) {
return stack.getMaxCount() - stack.getCount();
} else {
return 0;
@ -39,16 +37,16 @@ public abstract class AbstractFurnaceBlockMixin implements ItemProvider { @@ -39,16 +37,16 @@ public abstract class AbstractFurnaceBlockMixin implements ItemProvider {
@Override
public void inputItem(BlockView world, BlockPos pos, ItemStack itemStack) {
AbstractFurnaceBlockEntity be = getBlockEntity(world, pos);
if (AbstractFurnaceBlockEntity.canUseAsFuel(itemStack)){
if (AbstractFurnaceBlockEntity.canUseAsFuel(itemStack)) {
ItemStack stack = be.getInvStack(1);
if (stack.isEmpty()){
if (stack.isEmpty()) {
be.setInvStack(1, itemStack);
} else {
stack.setCount(itemStack.getCount() + stack.getCount());
}
} else {
ItemStack stack = be.getInvStack(0);
if (stack.isEmpty()){
if (stack.isEmpty()) {
be.setInvStack(0, itemStack);
} else {
stack.setCount(itemStack.getCount() + stack.getCount());
@ -58,21 +56,21 @@ public abstract class AbstractFurnaceBlockMixin implements ItemProvider { @@ -58,21 +56,21 @@ public abstract class AbstractFurnaceBlockMixin implements ItemProvider {
@Override
public ItemStack[] getItemOutputStacks(BlockView world, BlockPos pos) {
return new ItemStack[] { getBlockEntity(world, pos).getInvStack(2) };
return new ItemStack[]{getBlockEntity(world, pos).getInvStack(2)};
}
@Override
public void outputItem(BlockView world, BlockPos pos, ItemStack itemStack) {
AbstractFurnaceBlockEntity be = getBlockEntity(world, pos);
ItemStack stack = be.getInvStack(2);
if (itemStack.getCount() == stack.getCount()){
if (itemStack.getCount() == stack.getCount()) {
be.setInvStack(2, ItemStack.EMPTY);
} else {
stack.setCount(stack.getCount() - itemStack.getCount());
}
}
private AbstractFurnaceBlockEntity getBlockEntity(BlockView world, BlockPos pos){
private AbstractFurnaceBlockEntity getBlockEntity(BlockView world, BlockPos pos) {
return (AbstractFurnaceBlockEntity) world.getBlockEntity(pos);
}
}

22
src/main/java/com/martmists/ynet/mixin/providers/item/GenericMachineBlockMixin.java

@ -1,12 +1,10 @@ @@ -1,12 +1,10 @@
package com.martmists.ynet.mixin.providers.item;
import com.martmists.ynet.api.ItemProvider;
import com.martmists.ynet.mixin.accessors.InventoryStacksAccessor;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.BlockView;
import org.spongepowered.asm.mixin.Mixin;
import reborncore.common.blockentity.MachineBaseBlockEntity;
import reborncore.common.util.DefaultedListCollector;
import techreborn.blockentity.machine.GenericMachineBlockEntity;
import techreborn.blocks.GenericMachineBlock;
@ -20,14 +18,14 @@ public class GenericMachineBlockMixin implements ItemProvider { @@ -20,14 +18,14 @@ public class GenericMachineBlockMixin implements ItemProvider {
@Override
public int getItemInputCount(BlockView world, BlockPos pos, ItemStack itemStack) {
GenericMachineBlockEntity be = getBlockEntity(world, pos);
if (hasInventory(be)){
if (hasInventory(be)) {
List<ItemStack> stacks = getInputStacks(be);
if (stacks.stream().anyMatch(ItemStack::isEmpty)) {
return itemStack.getMaxCount();
}
int available = 0;
for (ItemStack stack : stacks) {
if (stack.getItem() == itemStack.getItem()){
if (stack.getItem() == itemStack.getItem()) {
available += stack.getMaxCount() - stack.getCount();
}
}
@ -41,7 +39,7 @@ public class GenericMachineBlockMixin implements ItemProvider { @@ -41,7 +39,7 @@ public class GenericMachineBlockMixin implements ItemProvider {
List<ItemStack> stacks = getInputStacks(getBlockEntity(world, pos));
int inputCount = itemStack.getCount();
for (ItemStack stack : stacks) {
if (stack.getItem() == itemStack.getItem()){
if (stack.getItem() == itemStack.getItem()) {
int available = stack.getMaxCount() - stack.getCount();
int inputting = Math.min(available, inputCount);
inputCount -= inputting;
@ -51,10 +49,10 @@ public class GenericMachineBlockMixin implements ItemProvider { @@ -51,10 +49,10 @@ public class GenericMachineBlockMixin implements ItemProvider {
return;
}
}
if (inputCount > 0){
if (inputCount > 0) {
int i = 0;
for (ItemStack stack : stacks) {
if (stack.isEmpty()){
if (stack.isEmpty()) {
stacks.set(i, new ItemStack(itemStack.getItem(), inputCount));
return;
}
@ -73,12 +71,12 @@ public class GenericMachineBlockMixin implements ItemProvider { @@ -73,12 +71,12 @@ public class GenericMachineBlockMixin implements ItemProvider {
List<ItemStack> stacks = getOutputStacks(getBlockEntity(world, pos));
int outputCount = itemStack.getCount();
int i = 0;
for (ItemStack stack : stacks){
if (stack.getItem() == itemStack.getItem()){
for (ItemStack stack : stacks) {
if (stack.getItem() == itemStack.getItem()) {
int outputting = Math.min(outputCount, stack.getCount());
stack.setCount(outputting);
outputCount -= outputting;
if (stack.getCount() == 0){
if (stack.getCount() == 0) {
stacks.set(i, ItemStack.EMPTY);
}
if (outputCount <= 0) {
@ -93,8 +91,8 @@ public class GenericMachineBlockMixin implements ItemProvider { @@ -93,8 +91,8 @@ public class GenericMachineBlockMixin implements ItemProvider {
}
}
private GenericMachineBlockEntity getBlockEntity(BlockView world, BlockPos pos){
return (GenericMachineBlockEntity)world.getBlockEntity(pos);
private GenericMachineBlockEntity getBlockEntity(BlockView world, BlockPos pos) {
return (GenericMachineBlockEntity) world.getBlockEntity(pos);
}
private List<ItemStack> getInputStacks(GenericMachineBlockEntity be) {

24
src/main/java/com/martmists/ynet/mixin/providers/item/LootableContainerBlockMixin.java

@ -11,7 +11,7 @@ import org.spongepowered.asm.mixin.Mixin; @@ -11,7 +11,7 @@ import org.spongepowered.asm.mixin.Mixin;
import java.util.List;
@Mixin(value={
@Mixin(value = {
ChestBlock.class, TrappedChestBlock.class,
ShulkerBoxBlock.class, DispenserBlock.class,
HopperBlock.class, BarrelBlock.class})
@ -19,13 +19,13 @@ public abstract class LootableContainerBlockMixin implements ItemProvider { @@ -19,13 +19,13 @@ public abstract class LootableContainerBlockMixin implements ItemProvider {
@Override
public int getItemInputCount(BlockView world, BlockPos pos, ItemStack itemStack) {
List<ItemStack> stacks = ((InventoryStacksAccessor)getBlockEntity(world, pos)).callGetInvStackList();
List<ItemStack> stacks = ((InventoryStacksAccessor) getBlockEntity(world, pos)).callGetInvStackList();
if (stacks.stream().anyMatch(ItemStack::isEmpty)) {
return itemStack.getMaxCount();
}
int available = 0;
for (ItemStack stack : stacks) {
if (stack.getItem() == itemStack.getItem()){
if (stack.getItem() == itemStack.getItem()) {
available += stack.getMaxCount() - stack.getCount();
}
}
@ -34,10 +34,10 @@ public abstract class LootableContainerBlockMixin implements ItemProvider { @@ -34,10 +34,10 @@ public abstract class LootableContainerBlockMixin implements ItemProvider {
@Override
public void inputItem(BlockView world, BlockPos pos, ItemStack itemStack) {
List<ItemStack> stacks = ((InventoryStacksAccessor)getBlockEntity(world, pos)).callGetInvStackList();
List<ItemStack> stacks = ((InventoryStacksAccessor) getBlockEntity(world, pos)).callGetInvStackList();
int inputCount = itemStack.getCount();
for (ItemStack stack : stacks) {
if (stack.getItem() == itemStack.getItem()){
if (stack.getItem() == itemStack.getItem()) {
int available = stack.getMaxCount() - stack.getCount();
int inputting = Math.min(available, inputCount);
inputCount -= inputting;
@ -47,10 +47,10 @@ public abstract class LootableContainerBlockMixin implements ItemProvider { @@ -47,10 +47,10 @@ public abstract class LootableContainerBlockMixin implements ItemProvider {
return;
}
}
if (inputCount > 0){
if (inputCount > 0) {
int i = 0;
for (ItemStack stack : stacks) {
if (stack.isEmpty()){
if (stack.isEmpty()) {
stacks.set(i, new ItemStack(itemStack.getItem(), inputCount));
return;
}
@ -61,20 +61,20 @@ public abstract class LootableContainerBlockMixin implements ItemProvider { @@ -61,20 +61,20 @@ public abstract class LootableContainerBlockMixin implements ItemProvider {
@Override
public ItemStack[] getItemOutputStacks(BlockView world, BlockPos pos) {
return ((InventoryStacksAccessor)getBlockEntity(world, pos)).callGetInvStackList().stream().filter(stack -> !stack.isEmpty()).toArray(ItemStack[]::new);
return ((InventoryStacksAccessor) getBlockEntity(world, pos)).callGetInvStackList().stream().filter(stack -> !stack.isEmpty()).toArray(ItemStack[]::new);
}
@Override
public void outputItem(BlockView world, BlockPos pos, ItemStack itemStack) {
List<ItemStack> stacks = ((InventoryStacksAccessor)getBlockEntity(world, pos)).callGetInvStackList();
List<ItemStack> stacks = ((InventoryStacksAccessor) getBlockEntity(world, pos)).callGetInvStackList();
int outputCount = itemStack.getCount();
int i = 0;
for (ItemStack stack : stacks){
if (stack.getItem() == itemStack.getItem()){
for (ItemStack stack : stacks) {
if (stack.getItem() == itemStack.getItem()) {
int outputting = Math.min(outputCount, stack.getCount());
stack.setCount(outputting);
outputCount -= outputting;
if (stack.getCount() == 0){
if (stack.getCount() == 0) {
stacks.set(i, ItemStack.EMPTY);
}
if (outputCount <= 0) {

34
src/main/java/com/martmists/ynet/network/ConnectorConfiguration.java

@ -5,28 +5,20 @@ import net.minecraft.item.Item; @@ -5,28 +5,20 @@ import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.NbtHelper;
import net.minecraft.util.math.BlockPos;
import java.util.Arrays;
import java.util.stream.Collectors;
public class ConnectorConfiguration {
public ConnectorConfiguration() {
state = State.DISABLED;
priority = 0;
}
public State state;
public BlockPos providerPos;
public int priority;
public Item[] filter;
public enum State {
DISABLED,
INPUT,
OUTPUT
public ConnectorConfiguration() {
state = State.DISABLED;
priority = 0;
filter = new Item[0];
}
public void toTag(ListTag tag) {
@ -34,15 +26,17 @@ public class ConnectorConfiguration { @@ -34,15 +26,17 @@ public class ConnectorConfiguration {
ListTag l = new ListTag();
Arrays.stream(filter).forEach(i -> {
CompoundTag t = new CompoundTag();
new ItemStack(i).toTag(t);
l.add(t);
if (i != null) {
CompoundTag t = new CompoundTag();
new ItemStack(i).toTag(t);
l.add(t);
}
});
data.putInt("priority", priority);
data.putInt("state", state.ordinal());
data.put("filter", l);
data.putIntArray("pos", new int[] { providerPos.getX(), providerPos.getY(), providerPos.getZ() });
data.putIntArray("pos", new int[]{providerPos.getX(), providerPos.getY(), providerPos.getZ()});
}
public void fromTag(CompoundTag tag) {
@ -52,7 +46,13 @@ public class ConnectorConfiguration { @@ -52,7 +46,13 @@ public class ConnectorConfiguration {
providerPos = new BlockPos(pos[0], pos[1], pos[2]);
state = State.values()[tag.getInt("state")];
filter = tag.getList("filter", NbtType.COMPOUND).stream()
.map(t -> ItemStack.fromTag((CompoundTag)t).getItem())
.map(t -> ItemStack.fromTag((CompoundTag) t).getItem())
.toArray(Item[]::new);
}
public enum State {
DISABLED,
INPUT,
OUTPUT
}
}

88
src/main/java/com/martmists/ynet/network/Network.java

@ -3,6 +3,7 @@ package com.martmists.ynet.network; @@ -3,6 +3,7 @@ package com.martmists.ynet.network;
import com.martmists.ynet.YNetMod;
import com.martmists.ynet.api.BaseProvider;
import com.martmists.ynet.blockentities.ControllerBlockEntity;
import com.martmists.ynet.blocks.ControllerBlock;
import net.minecraft.block.Block;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.BlockView;
@ -10,40 +11,36 @@ import net.minecraft.world.BlockView; @@ -10,40 +11,36 @@ import net.minecraft.world.BlockView;
import java.util.*;
public class Network {
private static Map<BlockPos, Network> networks = new HashMap<>();
private BlockPos controller;
public static Map<BlockPos, Network> networks = new HashMap<>();
private static Map<Class<?>, Set<Class<? extends BaseProvider>>> tMap = new HashMap<>();
public Set<BlockPos> cables;
public Set<BlockPos> connectors;
private BlockPos controller;
public synchronized static void removeCable(BlockView world, BlockPos p) {
public static void removeCable(BlockView world, BlockPos p) {
for (Map.Entry<BlockPos, Network> e : networks.entrySet()) {
Network n = e.getValue();
if (n.cables.contains(p)) {
System.out.println("Reloading nodes for network " + n);
n.reloadAllNodes(world);
System.out.println("Done reloading nodes");
}
}
}
public synchronized static void removeConnector(BlockView world, BlockPos p) {
public static void removeConnector(BlockView world, BlockPos p) {
for (Map.Entry<BlockPos, Network> e : networks.entrySet()) {
Network n = e.getValue();
if (n.connectors.contains(p)) {
System.out.println("Reloading nodes for network " + n);
n.reloadAllNodes(world);
System.out.println("Done reloading nodes");
}
}
}
public synchronized static void addCable(BlockView world, BlockPos p) {
public static void addCable(BlockView world, BlockPos p) {
Set<Network> connectedNetworks = new HashSet<>();
for (BlockPos pos : new BlockPos[]{ p.up(), p.down(), p.north(), p.east(), p.south(), p.west() }){
for (Map.Entry<BlockPos, Network> e : networks.entrySet()){
for (BlockPos pos : new BlockPos[]{p.up(), p.down(), p.north(), p.east(), p.south(), p.west()}) {
for (Map.Entry<BlockPos, Network> e : networks.entrySet()) {
Network n = e.getValue();
if (n.connectors.contains(pos) || n.cables.contains(pos)){
if (n.connectors.contains(pos) || n.cables.contains(pos)) {
connectedNetworks.add(n);
}
}
@ -53,19 +50,22 @@ public class Network { @@ -53,19 +50,22 @@ public class Network {
Set<BlockPos> known = new HashSet<>();
known.addAll(n.cables);
known.addAll(n.connectors);
System.out.println("Loading new nodes for network " + n);
getConnectedBlocks(world, p, known, n.cables, n.connectors);
System.out.println("Done reloading nodes");
}
}
public synchronized static void addConnector(BlockView world, BlockPos p) {
public static void addConnector(BlockView world, BlockPos p) {
Set<Network> connectedNetworks = new HashSet<>();
for (BlockPos pos : new BlockPos[]{ p.up(), p.down(), p.north(), p.east(), p.south(), p.west() }){
for (Map.Entry<BlockPos, Network> e : networks.entrySet()){
Network n = e.getValue();
if (n.connectors.contains(pos) || n.cables.contains(pos)){
connectedNetworks.add(n);
for (BlockPos pos : new BlockPos[]{p.up(), p.down(), p.north(), p.east(), p.south(), p.west()}) {
Block b = world.getBlockState(pos).getBlock();
if (b instanceof ControllerBlock) {
connectedNetworks.add(((ControllerBlockEntity) world.getBlockEntity(pos)).network);
} else {
for (Map.Entry<BlockPos, Network> e : networks.entrySet()) {
Network n = e.getValue();
if (n.connectors.contains(pos) || n.cables.contains(pos)) {
connectedNetworks.add(n);
}
}
}
}
@ -74,19 +74,10 @@ public class Network { @@ -74,19 +74,10 @@ public class Network {
Set<BlockPos> known = new HashSet<>();
known.addAll(n.cables);
known.addAll(n.connectors);
System.out.println("Loading new nodes for network " + n);
getConnectedBlocks(world, p, known, n.cables, n.connectors);
System.out.println("Done reloading nodes");
}
}
public synchronized void reloadAllNodes(BlockView world) {
cables = new HashSet<>();
connectors = new HashSet<>();
ControllerBlockEntity be = ((ControllerBlockEntity)world.getBlockEntity(controller));
getConnectedBlocks(be.getWorld(), controller, cables, connectors);
}
public static void getConnectedBlocks(BlockView world, BlockPos origin, Set<BlockPos> cables, Set<BlockPos> connectors) {
getConnectedBlocks(world, origin, new HashSet<>(), cables, connectors);
}
@ -104,7 +95,7 @@ public class Network { @@ -104,7 +95,7 @@ public class Network {
}
// Search connected
while (!toSearch.isEmpty()){
while (!toSearch.isEmpty()) {
BlockPos p = toSearch.removeFirst();
for (BlockPos p2 : Arrays.asList(p.up(), p.down(), p.north(), p.south(), p.east(), p.west())) {
if (exclude.contains(p2)) {
@ -123,6 +114,26 @@ public class Network { @@ -123,6 +114,26 @@ public class Network {
}
}
private static void findProviderTypes(Class<?> cls, Set<Class<? extends BaseProvider>> ret) {
tMap.putIfAbsent(cls, ret);
if (cls != Object.class && cls != null) {
if (BaseProvider.class.isAssignableFrom(cls)) {
ret.add((Class<? extends BaseProvider>) cls);
}
findProviderTypes(cls.getSuperclass(), ret);
for (Class<?> itf : cls.getInterfaces()) {
findProviderTypes(itf, ret);
}
}
}
public void reloadAllNodes(BlockView world) {
cables = new HashSet<>();
connectors = new HashSet<>();
ControllerBlockEntity be = ((ControllerBlockEntity) world.getBlockEntity(controller));
getConnectedBlocks(be.getWorld(), controller, cables, connectors);
}
public void setController(BlockPos pos) {
controller = pos;
networks.put(pos, this);
@ -148,19 +159,4 @@ public class Network { @@ -148,19 +159,4 @@ public class Network {
});
return providers;
}
// Ugly hack by Pyrofab
private static Map<Class<?>, Set<Class<? extends BaseProvider>>> tMap = new HashMap<>();