mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2024-07-07 20:34:25 +00:00
Tick pocket computers in item entity form
See #995. And no, just because I'm adding this doesn't mean it's not a terrible issue.
This commit is contained in:
parent
4d10639efb
commit
114261944a
|
@ -17,7 +17,6 @@
|
||||||
import dan200.computercraft.shared.computer.core.ClientComputer;
|
import dan200.computercraft.shared.computer.core.ClientComputer;
|
||||||
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
||||||
import dan200.computercraft.shared.computer.core.ComputerState;
|
import dan200.computercraft.shared.computer.core.ComputerState;
|
||||||
import dan200.computercraft.shared.computer.core.ServerComputer;
|
|
||||||
import dan200.computercraft.shared.computer.items.IComputerItem;
|
import dan200.computercraft.shared.computer.items.IComputerItem;
|
||||||
import dan200.computercraft.shared.network.container.ComputerContainerData;
|
import dan200.computercraft.shared.network.container.ComputerContainerData;
|
||||||
import dan200.computercraft.shared.pocket.apis.PocketAPI;
|
import dan200.computercraft.shared.pocket.apis.PocketAPI;
|
||||||
|
@ -25,6 +24,7 @@
|
||||||
import dan200.computercraft.shared.pocket.inventory.PocketComputerMenuProvider;
|
import dan200.computercraft.shared.pocket.inventory.PocketComputerMenuProvider;
|
||||||
import net.minecraft.client.util.ITooltipFlag;
|
import net.minecraft.client.util.ITooltipFlag;
|
||||||
import net.minecraft.entity.Entity;
|
import net.minecraft.entity.Entity;
|
||||||
|
import net.minecraft.entity.item.ItemEntity;
|
||||||
import net.minecraft.entity.player.PlayerEntity;
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
import net.minecraft.inventory.IInventory;
|
import net.minecraft.inventory.IInventory;
|
||||||
import net.minecraft.item.Item;
|
import net.minecraft.item.Item;
|
||||||
|
@ -83,53 +83,65 @@ public void fillItemCategory( @Nonnull ItemGroup group, @Nonnull NonNullList<Ite
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private boolean tick( @Nonnull ItemStack stack, @Nonnull World world, @Nonnull Entity entity, @Nonnull PocketServerComputer computer )
|
||||||
public void inventoryTick( @Nonnull ItemStack stack, World world, @Nonnull Entity entity, int slotNum, boolean selected )
|
|
||||||
{
|
|
||||||
if( !world.isClientSide )
|
|
||||||
{
|
|
||||||
// Server side
|
|
||||||
IInventory inventory = entity instanceof PlayerEntity ? ((PlayerEntity) entity).inventory : null;
|
|
||||||
PocketServerComputer computer = createServerComputer( world, inventory, entity, stack );
|
|
||||||
if( computer != null )
|
|
||||||
{
|
{
|
||||||
IPocketUpgrade upgrade = getUpgrade( stack );
|
IPocketUpgrade upgrade = getUpgrade( stack );
|
||||||
|
|
||||||
// Ping computer
|
|
||||||
computer.keepAlive();
|
|
||||||
computer.setWorld( world );
|
computer.setWorld( world );
|
||||||
computer.updateValues( entity, stack, upgrade );
|
computer.updateValues( entity, stack, upgrade );
|
||||||
|
|
||||||
|
boolean changed = false;
|
||||||
|
|
||||||
// Sync ID
|
// Sync ID
|
||||||
int id = computer.getID();
|
int id = computer.getID();
|
||||||
if( id != getComputerID( stack ) )
|
if( id != getComputerID( stack ) )
|
||||||
{
|
{
|
||||||
|
changed = true;
|
||||||
setComputerID( stack, id );
|
setComputerID( stack, id );
|
||||||
if( inventory != null ) inventory.setChanged();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sync label
|
// Sync label
|
||||||
String label = computer.getLabel();
|
String label = computer.getLabel();
|
||||||
if( !Objects.equal( label, getLabel( stack ) ) )
|
if( !Objects.equal( label, getLabel( stack ) ) )
|
||||||
{
|
{
|
||||||
|
changed = true;
|
||||||
setLabel( stack, label );
|
setLabel( stack, label );
|
||||||
if( inventory != null ) inventory.setChanged();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update pocket upgrade
|
// Update pocket upgrade
|
||||||
if( upgrade != null )
|
if( upgrade != null ) upgrade.update( computer, computer.getPeripheral( ComputerSide.BACK ) );
|
||||||
|
|
||||||
|
return changed;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void inventoryTick( @Nonnull ItemStack stack, World world, @Nonnull Entity entity, int slotNum, boolean selected )
|
||||||
{
|
{
|
||||||
upgrade.update( computer, computer.getPeripheral( ComputerSide.BACK ) );
|
if( !world.isClientSide )
|
||||||
}
|
{
|
||||||
}
|
IInventory inventory = entity instanceof PlayerEntity ? ((PlayerEntity) entity).inventory : null;
|
||||||
|
PocketServerComputer computer = createServerComputer( world, entity, inventory, stack );
|
||||||
|
computer.keepAlive();
|
||||||
|
|
||||||
|
boolean changed = tick( stack, world, entity, computer );
|
||||||
|
if( changed && inventory != null ) inventory.setChanged();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Client side
|
|
||||||
createClientComputer( stack );
|
createClientComputer( stack );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onEntityItemUpdate( ItemStack stack, ItemEntity entity )
|
||||||
|
{
|
||||||
|
if( entity.level.isClientSide ) return false;
|
||||||
|
|
||||||
|
PocketServerComputer computer = getServerComputer( stack );
|
||||||
|
if( computer != null && tick( stack, entity.level, entity, computer ) ) entity.setItem( stack.copy() );
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@Override
|
@Override
|
||||||
public ActionResult<ItemStack> use( World world, PlayerEntity player, @Nonnull Hand hand )
|
public ActionResult<ItemStack> use( World world, PlayerEntity player, @Nonnull Hand hand )
|
||||||
|
@ -137,22 +149,18 @@ public ActionResult<ItemStack> use( World world, PlayerEntity player, @Nonnull H
|
||||||
ItemStack stack = player.getItemInHand( hand );
|
ItemStack stack = player.getItemInHand( hand );
|
||||||
if( !world.isClientSide )
|
if( !world.isClientSide )
|
||||||
{
|
{
|
||||||
PocketServerComputer computer = createServerComputer( world, player.inventory, player, stack );
|
PocketServerComputer computer = createServerComputer( world, player, player.inventory, stack );
|
||||||
|
|
||||||
boolean stop = false;
|
|
||||||
if( computer != null )
|
|
||||||
{
|
|
||||||
computer.turnOn();
|
computer.turnOn();
|
||||||
|
|
||||||
|
boolean stop = false;
|
||||||
IPocketUpgrade upgrade = getUpgrade( stack );
|
IPocketUpgrade upgrade = getUpgrade( stack );
|
||||||
if( upgrade != null )
|
if( upgrade != null )
|
||||||
{
|
{
|
||||||
computer.updateValues( player, stack, upgrade );
|
computer.updateValues( player, stack, upgrade );
|
||||||
stop = upgrade.onRightClick( world, computer, computer.getPeripheral( ComputerSide.BACK ) );
|
stop = upgrade.onRightClick( world, computer, computer.getPeripheral( ComputerSide.BACK ) );
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if( !stop && computer != null )
|
if( !stop )
|
||||||
{
|
{
|
||||||
boolean isTypingOnly = hand == Hand.OFF_HAND;
|
boolean isTypingOnly = hand == Hand.OFF_HAND;
|
||||||
new ComputerContainerData( computer ).open( player, new PocketComputerMenuProvider( computer, stack, this, hand, isTypingOnly ) );
|
new ComputerContainerData( computer ).open( player, new PocketComputerMenuProvider( computer, stack, this, hand, isTypingOnly ) );
|
||||||
|
@ -210,17 +218,17 @@ public String getCreatorModId( ItemStack stack )
|
||||||
return super.getCreatorModId( stack );
|
return super.getCreatorModId( stack );
|
||||||
}
|
}
|
||||||
|
|
||||||
public PocketServerComputer createServerComputer( final World world, IInventory inventory, Entity entity, @Nonnull ItemStack stack )
|
@Nonnull
|
||||||
|
public PocketServerComputer createServerComputer( World world, Entity entity, @Nullable IInventory inventory, @Nonnull ItemStack stack )
|
||||||
{
|
{
|
||||||
if( world.isClientSide ) return null;
|
if( world.isClientSide ) throw new IllegalStateException( "Cannot call createServerComputer on the client" );
|
||||||
|
|
||||||
PocketServerComputer computer;
|
PocketServerComputer computer;
|
||||||
int instanceID = getInstanceID( stack );
|
int instanceID = getInstanceID( stack );
|
||||||
int sessionID = getSessionID( stack );
|
int sessionID = getSessionID( stack );
|
||||||
int correctSessionID = ComputerCraft.serverComputerRegistry.getSessionID();
|
int correctSessionID = ComputerCraft.serverComputerRegistry.getSessionID();
|
||||||
|
|
||||||
if( instanceID >= 0 && sessionID == correctSessionID &&
|
if( instanceID >= 0 && sessionID == correctSessionID && ComputerCraft.serverComputerRegistry.contains( instanceID ) )
|
||||||
ComputerCraft.serverComputerRegistry.contains( instanceID ) )
|
|
||||||
{
|
{
|
||||||
computer = (PocketServerComputer) ComputerCraft.serverComputerRegistry.get( instanceID );
|
computer = (PocketServerComputer) ComputerCraft.serverComputerRegistry.get( instanceID );
|
||||||
}
|
}
|
||||||
|
@ -238,13 +246,7 @@ public PocketServerComputer createServerComputer( final World world, IInventory
|
||||||
computerID = ComputerCraftAPI.createUniqueNumberedSaveDir( world, "computer" );
|
computerID = ComputerCraftAPI.createUniqueNumberedSaveDir( world, "computer" );
|
||||||
setComputerID( stack, computerID );
|
setComputerID( stack, computerID );
|
||||||
}
|
}
|
||||||
computer = new PocketServerComputer(
|
computer = new PocketServerComputer( world, computerID, getLabel( stack ), instanceID, getFamily() );
|
||||||
world,
|
|
||||||
computerID,
|
|
||||||
getLabel( stack ),
|
|
||||||
instanceID,
|
|
||||||
getFamily()
|
|
||||||
);
|
|
||||||
computer.updateValues( entity, stack, getUpgrade( stack ) );
|
computer.updateValues( entity, stack, getUpgrade( stack ) );
|
||||||
computer.addAPI( new PocketAPI( computer ) );
|
computer.addAPI( new PocketAPI( computer ) );
|
||||||
ComputerCraft.serverComputerRegistry.add( instanceID, computer );
|
ComputerCraft.serverComputerRegistry.add( instanceID, computer );
|
||||||
|
@ -254,15 +256,17 @@ public PocketServerComputer createServerComputer( final World world, IInventory
|
||||||
return computer;
|
return computer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ServerComputer getServerComputer( @Nonnull ItemStack stack )
|
@Nullable
|
||||||
|
public static PocketServerComputer getServerComputer( @Nonnull ItemStack stack )
|
||||||
{
|
{
|
||||||
int session = getSessionID( stack );
|
int session = getSessionID( stack );
|
||||||
if( session != ComputerCraft.serverComputerRegistry.getSessionID() ) return null;
|
if( session != ComputerCraft.serverComputerRegistry.getSessionID() ) return null;
|
||||||
|
|
||||||
int instanceID = getInstanceID( stack );
|
int instanceID = getInstanceID( stack );
|
||||||
return instanceID >= 0 ? ComputerCraft.serverComputerRegistry.get( instanceID ) : null;
|
return instanceID >= 0 ? (PocketServerComputer) ComputerCraft.serverComputerRegistry.get( instanceID ) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
public static ClientComputer createClientComputer( @Nonnull ItemStack stack )
|
public static ClientComputer createClientComputer( @Nonnull ItemStack stack )
|
||||||
{
|
{
|
||||||
int instanceID = getInstanceID( stack );
|
int instanceID = getInstanceID( stack );
|
||||||
|
@ -277,6 +281,7 @@ public static ClientComputer createClientComputer( @Nonnull ItemStack stack )
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
private static ClientComputer getClientComputer( @Nonnull ItemStack stack )
|
private static ClientComputer getClientComputer( @Nonnull ItemStack stack )
|
||||||
{
|
{
|
||||||
int instanceID = getInstanceID( stack );
|
int instanceID = getInstanceID( stack );
|
||||||
|
|
Loading…
Reference in New Issue
Block a user