Browse Source

reliable item counts

master
Martmists 1 year ago
parent
commit
2087c6b657
10 changed files with 76 additions and 42 deletions
  1. +1
    -1
      settings.gradle
  2. +2
    -1
      src/main/java/com/martmists/ynet/api/ItemProvider.java
  3. +17
    -4
      src/main/java/com/martmists/ynet/blockentities/ControllerBlockEntity.java
  4. +21
    -0
      src/main/java/com/martmists/ynet/blocks/ConnectorBlock.java
  5. +7
    -11
      src/main/java/com/martmists/ynet/event/impl/ItemTickCallback.java
  6. +10
    -18
      src/main/java/com/martmists/ynet/mixin/providers/item/AbstractFurnaceBlockMixin.java
  7. +3
    -3
      src/main/java/com/martmists/ynet/mixin/providers/item/GenericMachineBlockMixin.java
  8. +3
    -3
      src/main/java/com/martmists/ynet/mixin/providers/item/LootableContainerBlockMixin.java
  9. +1
    -1
      src/main/java/com/martmists/ynet/network/Network.java
  10. +11
    -0
      versions/common.gradle

+ 1
- 1
settings.gradle View File

@@ -1,4 +1,4 @@
include "versions:1.15.2"
include "versions:1.16"

rootProject.name = 'ynet'
rootProject.name = 'ynet'

+ 2
- 1
src/main/java/com/martmists/ynet/api/ItemProvider.java View File

@@ -10,6 +10,7 @@ public interface ItemProvider extends BaseProvider {
* @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
* Should at most be itemStack.getCount()
*/
int getItemInputCount(BlockView world, BlockPos pos, ItemStack itemStack);

@@ -31,7 +32,7 @@ public interface ItemProvider extends BaseProvider {
* @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
* May not be an exact existing stack, make sure to check the count
*/
void outputItem(BlockView world, BlockPos pos, ItemStack itemStack);
}

+ 17
- 4
src/main/java/com/martmists/ynet/blockentities/ControllerBlockEntity.java View File

@@ -27,10 +27,12 @@ import java.util.stream.Collectors;
public class ControllerBlockEntity extends BlockEntity implements Tickable {
public Network network;
public Channel[] channels = new Channel[9];
private int t;

public ControllerBlockEntity() {
super(YNetMod.CONTROLLER_BE);
this.network = new Network();
t = 0;
}

public void updateNetwork() {
@@ -54,10 +56,21 @@ public class ControllerBlockEntity extends BlockEntity implements Tickable {
if (network.connectors == null) {
register();
}
for (Channel ch : channels) {
if (ch != null && ch.providerType != null) {
ProviderTickCallback<?> callback = YNetMod.PROVIDERS.get(ch.providerType);
callback.interact(ch.connectorSettings, this);

t++;
if (t > 5) {
t = 0;
for (Channel ch : channels) {
if (ch != null && ch.providerType != null) {
ProviderTickCallback<?> callback = YNetMod.PROVIDERS.get(ch.providerType);
try {
callback.interact(ch.connectorSettings, this);
} catch (ClassCastException e) {
// Invalid world, log warning and reload nodes
e.printStackTrace();
ch.connectorSettings.removeIf(it -> !(world.getBlockState(it.providerPos).getBlock() instanceof BaseProvider));
}
}
}
}
}


+ 21
- 0
src/main/java/com/martmists/ynet/blocks/ConnectorBlock.java View File

@@ -3,6 +3,8 @@ 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.Channel;
import com.martmists.ynet.network.Network;
import net.minecraft.block.Block;
import net.minecraft.block.BlockEntityProvider;
@@ -81,6 +83,25 @@ public class ConnectorBlock extends ConnectingBlock implements BlockEntityProvid
}
}

@Override
public void neighborUpdate(BlockState state, World world, BlockPos pos, Block block, BlockPos neighborPos, boolean moved) {
super.neighborUpdate(state, world, pos, block, neighborPos, moved);
if (!(block instanceof BaseProvider)) {
return;
}

for (Map.Entry<BlockPos, Network> e : Network.networks.entrySet()) {
if (e.getValue().connectors.contains(pos)) {
ControllerBlockEntity be = (ControllerBlockEntity)world.getBlockEntity(e.getKey());
for (Channel c : be.channels) {
if (c != null) {
c.connectorSettings.removeIf(s -> s == null || s.providerPos == neighborPos);
}
}
}
}
}

public BlockState withConnectionProperties(BlockView world, BlockPos pos) {
Block block = world.getBlockState(pos.down()).getBlock();
Block block2 = world.getBlockState(pos.up()).getBlock();


+ 7
- 11
src/main/java/com/martmists/ynet/event/impl/ItemTickCallback.java View File

@@ -39,7 +39,7 @@ public class ItemTickCallback implements ProviderTickCallback<ItemProvider> {
.forEach(config -> {
ItemProvider p = (ItemProvider) world.getBlockState(config.providerPos).getBlock();
for (ItemStack s : p.getItemOutputStacks(world, config.providerPos)) {
entries.add(new Entry(config.providerPos, p, s));
entries.add(new Entry(config.providerPos, p, new ItemStack(s.getItem(), s.getCount())));
}
});

@@ -52,8 +52,9 @@ public class ItemTickCallback implements ProviderTickCallback<ItemProvider> {
Map<ItemProvider, Integer> itemsStored = new HashMap<>();
Map<ItemProvider, Integer> itemsRemoved = new HashMap<>();

boolean found = true;
while (!entries.isEmpty()) {
if (e.items.getCount() <= 0) {
if (e.items.getCount() <= 0 || !found) {
try {
e = entries.get(1);
} catch (IndexOutOfBoundsException exc) {
@@ -62,14 +63,14 @@ public class ItemTickCallback implements ProviderTickCallback<ItemProvider> {
entries.remove(0);
}

found = false;

itemsRemoved.putIfAbsent(e.provider, 0);

if (itemsRemoved.get(e.provider) >= 64) {
e.items.setCount(0);
continue;
}

boolean found = false;
for (ConnectorConfiguration receiverConfig : takeItems) {
ItemProvider receiver = (ItemProvider) world.getBlockState(receiverConfig.providerPos).getBlock();
itemsStored.putIfAbsent(receiver, 0);
@@ -79,7 +80,7 @@ public class ItemTickCallback implements ProviderTickCallback<ItemProvider> {
64 - itemsStored.get(receiver),
64 - itemsRemoved.get(e.provider)));

if (receiverConfig.filter != null) {
if (receiverConfig.filter.length != 0) {
Entry fe = e;
if (Arrays.stream(receiverConfig.filter).noneMatch((obj) -> obj == fe.items.getItem())) {
continue;
@@ -91,17 +92,12 @@ public class ItemTickCallback implements ProviderTickCallback<ItemProvider> {
itemsRemoved.put(e.provider, itemsRemoved.get(e.provider) + count);
int finalCount = e.items.getCount() - count;
e.items.setCount(count);
receiver.inputItem(world, receiverConfig.providerPos, e.items);
e.provider.outputItem(world, e.pos, e.items);
receiver.inputItem(world, e.pos, e.items);
e.items.setCount(finalCount);
found = true;
}
}

if (!found) {
// Remove next
e.items.setCount(0);
}
}
}
}

+ 10
- 18
src/main/java/com/martmists/ynet/mixin/providers/item/AbstractFurnaceBlockMixin.java View File

@@ -13,23 +13,15 @@ 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)) {
// put in fuel slot
ItemStack stack = be.getInvStack(1);
if (stack.isEmpty()) {
return itemStack.getCount();
}
return stack.getMaxCount() - stack.getCount();
ItemStack stack = be.getInvStack(AbstractFurnaceBlockEntity.canUseAsFuel(itemStack) ? 1 : 0);
// put in fuel slot
if (stack.isEmpty()) {
return itemStack.getCount();
} else {
ItemStack stack = be.getInvStack(0);
if (stack.isEmpty()) {
return itemStack.getCount();
if (stack.getItem() == itemStack.getItem()) {
return Math.min(stack.getMaxCount() - stack.getCount(), itemStack.getCount());
} else {
if (stack.getItem() == itemStack.getItem()) {
return stack.getMaxCount() - stack.getCount();
} else {
return 0;
}
return 0;
}
}
}
@@ -40,14 +32,14 @@ public abstract class AbstractFurnaceBlockMixin implements ItemProvider {
if (AbstractFurnaceBlockEntity.canUseAsFuel(itemStack)) {
ItemStack stack = be.getInvStack(1);
if (stack.isEmpty()) {
be.setInvStack(1, itemStack);
be.setInvStack(1, new ItemStack(itemStack.getItem(), itemStack.getCount()));
} else {
stack.setCount(itemStack.getCount() + stack.getCount());
}
} else {
ItemStack stack = be.getInvStack(0);
if (stack.isEmpty()) {
be.setInvStack(0, itemStack);
be.setInvStack(0, new ItemStack(itemStack.getItem(), itemStack.getCount()));
} else {
stack.setCount(itemStack.getCount() + stack.getCount());
}
@@ -56,7 +48,7 @@ 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


+ 3
- 3
src/main/java/com/martmists/ynet/mixin/providers/item/GenericMachineBlockMixin.java View File

@@ -21,7 +21,7 @@ public class GenericMachineBlockMixin implements ItemProvider {
if (hasInventory(be)) {
List<ItemStack> stacks = getInputStacks(be);
if (stacks.stream().anyMatch(ItemStack::isEmpty)) {
return itemStack.getMaxCount();
return itemStack.getCount();
}
int available = 0;
for (ItemStack stack : stacks) {
@@ -29,7 +29,7 @@ public class GenericMachineBlockMixin implements ItemProvider {
available += stack.getMaxCount() - stack.getCount();
}
}
return Math.min(available, itemStack.getMaxCount());
return Math.min(available, itemStack.getCount());
}
return 0;
}
@@ -74,7 +74,7 @@ public class GenericMachineBlockMixin implements ItemProvider {
for (ItemStack stack : stacks) {
if (stack.getItem() == itemStack.getItem()) {
int outputting = Math.min(outputCount, stack.getCount());
stack.setCount(outputting);
stack.setCount(stack.getCount() - outputting);
outputCount -= outputting;
if (stack.getCount() == 0) {
stacks.set(i, ItemStack.EMPTY);


+ 3
- 3
src/main/java/com/martmists/ynet/mixin/providers/item/LootableContainerBlockMixin.java View File

@@ -21,7 +21,7 @@ public abstract class LootableContainerBlockMixin implements ItemProvider {
public int getItemInputCount(BlockView world, BlockPos pos, ItemStack itemStack) {
List<ItemStack> stacks = ((InventoryStacksAccessor) getBlockEntity(world, pos)).callGetInvStackList();
if (stacks.stream().anyMatch(ItemStack::isEmpty)) {
return itemStack.getMaxCount();
return itemStack.getCount();
}
int available = 0;
for (ItemStack stack : stacks) {
@@ -29,7 +29,7 @@ public abstract class LootableContainerBlockMixin implements ItemProvider {
available += stack.getMaxCount() - stack.getCount();
}
}
return Math.min(available, itemStack.getMaxCount());
return Math.min(available, itemStack.getCount());
}

@Override
@@ -72,7 +72,7 @@ public abstract class LootableContainerBlockMixin implements ItemProvider {
for (ItemStack stack : stacks) {
if (stack.getItem() == itemStack.getItem()) {
int outputting = Math.min(outputCount, stack.getCount());
stack.setCount(outputting);
stack.setCount(stack.getCount() - outputting);
outputCount -= outputting;
if (stack.getCount() == 0) {
stacks.set(i, ItemStack.EMPTY);


+ 1
- 1
src/main/java/com/martmists/ynet/network/Network.java View File

@@ -15,7 +15,7 @@ public class Network {
public static Map<Class<?>, Set<Class<? extends BaseProvider>>> tMap = new HashMap<>();
public Set<BlockPos> cables;
public Set<BlockPos> connectors;
private BlockPos controller;
public BlockPos controller;

public synchronized static void removeCable(BlockView world, BlockPos p) {
for (Map.Entry<BlockPos, Network> e : networks.entrySet()) {


+ 11
- 0
versions/common.gradle View File

@@ -50,6 +50,11 @@ sourceSets {
srcDirs = ["../../src/main/resources"]
}
}
api {
java {
srcDirs = ["../../src/main/java/com/martmists/ynet/api"]
}
}
}

jar {
@@ -61,5 +66,11 @@ task sourcesJar(type: Jar, dependsOn: classes) {
from sourceSets.main.allSource
}

task apiJar(type: Jar, dependsOn: classes) {
archiveClassifier.set("api")
from sourceSets.api.allSource
}

jar.dependsOn apiJar
jar.dependsOn sourcesJar
build.dependsOn jar

Loading…
Cancel
Save