Add fluid level emitter (#3588)

Fixes #3571
This commit is contained in:
fscan
2018-07-04 19:56:47 +02:00
committed by GitHub
parent 2182b6e37c
commit 7cd8b72bb7
16 changed files with 788 additions and 19 deletions
@@ -0,0 +1,216 @@
package appeng.fluids.client.gui;
import java.io.IOException;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.entity.player.InventoryPlayer;
import appeng.api.config.RedstoneMode;
import appeng.api.config.Settings;
import appeng.client.gui.implementations.GuiUpgradeable;
import appeng.client.gui.widgets.GuiImgButton;
import appeng.client.gui.widgets.GuiNumberBox;
import appeng.core.AEConfig;
import appeng.core.AELog;
import appeng.core.localization.GuiText;
import appeng.core.sync.network.NetworkHandler;
import appeng.core.sync.packets.PacketValueConfig;
import appeng.fluids.client.gui.widgets.GuiFluidSlot;
import appeng.fluids.container.ContainerFluidLevelEmitter;
import appeng.fluids.parts.PartFluidLevelEmitter;
public class GuiFluidLevelEmitter extends GuiUpgradeable
{
private final PartFluidLevelEmitter levelEmitter;
private GuiNumberBox level;
private GuiButton plus1;
private GuiButton plus10;
private GuiButton plus100;
private GuiButton plus1000;
private GuiButton minus1;
private GuiButton minus10;
private GuiButton minus100;
private GuiButton minus1000;
public GuiFluidLevelEmitter( final InventoryPlayer inventoryPlayer, final PartFluidLevelEmitter te )
{
super( new ContainerFluidLevelEmitter( inventoryPlayer, te ) );
this.levelEmitter = te;
}
@Override
public void initGui()
{
super.initGui();
this.level = new GuiNumberBox( this.fontRenderer, this.guiLeft + 24, this.guiTop + 43, 79, this.fontRenderer.FONT_HEIGHT, Long.class );
this.level.setEnableBackgroundDrawing( false );
this.level.setMaxStringLength( 16 );
this.level.setTextColor( 0xFFFFFF );
this.level.setVisible( true );
this.level.setFocused( true );
( (ContainerFluidLevelEmitter) this.inventorySlots ).setTextField( this.level );
final int y = 40;
final int x = 80 + 44;
this.guiSlots.add( new GuiFluidSlot( this.levelEmitter.getConfig(), 0, 0, x, y ) );
}
@Override
protected void addButtons()
{
this.redstoneMode = new GuiImgButton( this.guiLeft - 18, this.guiTop + 28, Settings.REDSTONE_EMITTER, RedstoneMode.LOW_SIGNAL );
final int a = AEConfig.instance().levelByMillyBuckets( 0 );
final int b = AEConfig.instance().levelByMillyBuckets( 1 );
final int c = AEConfig.instance().levelByMillyBuckets( 2 );
final int d = AEConfig.instance().levelByMillyBuckets( 3 );
this.buttonList.add( this.plus1 = new GuiButton( 0, this.guiLeft + 20, this.guiTop + 17, 22, 20, "+" + a ) );
this.buttonList.add( this.plus10 = new GuiButton( 0, this.guiLeft + 48, this.guiTop + 17, 28, 20, "+" + b ) );
this.buttonList.add( this.plus100 = new GuiButton( 0, this.guiLeft + 82, this.guiTop + 17, 32, 20, "+" + c ) );
this.buttonList.add( this.plus1000 = new GuiButton( 0, this.guiLeft + 120, this.guiTop + 17, 38, 20, "+" + d ) );
this.buttonList.add( this.minus1 = new GuiButton( 0, this.guiLeft + 20, this.guiTop + 59, 22, 20, "-" + a ) );
this.buttonList.add( this.minus10 = new GuiButton( 0, this.guiLeft + 48, this.guiTop + 59, 28, 20, "-" + b ) );
this.buttonList.add( this.minus100 = new GuiButton( 0, this.guiLeft + 82, this.guiTop + 59, 32, 20, "-" + c ) );
this.buttonList.add( this.minus1000 = new GuiButton( 0, this.guiLeft + 120, this.guiTop + 59, 38, 20, "-" + d ) );
this.buttonList.add( this.redstoneMode );
}
@Override
public void drawBG( final int offsetX, final int offsetY, final int mouseX, final int mouseY )
{
super.drawBG( offsetX, offsetY, mouseX, mouseY );
this.level.drawTextBox();
}
@Override
protected boolean drawUpgrades()
{
return false;
}
@Override
protected String getBackground()
{
return "guis/lvlemitter.png";
}
@Override
protected GuiText getName()
{
return GuiText.FluidLevelEmitter;
}
@Override
protected void handleButtonVisibility()
{
}
@Override
protected void actionPerformed( final GuiButton btn ) throws IOException
{
super.actionPerformed( btn );
final boolean isPlus = btn == this.plus1 || btn == this.plus10 || btn == this.plus100 || btn == this.plus1000;
final boolean isMinus = btn == this.minus1 || btn == this.minus10 || btn == this.minus100 || btn == this.minus1000;
if( isPlus || isMinus )
{
this.addQty( this.getQty( btn ) );
}
}
private void addQty( final long i )
{
try
{
String Out = this.level.getText();
boolean Fixed = false;
while( Out.startsWith( "0" ) && Out.length() > 1 )
{
Out = Out.substring( 1 );
Fixed = true;
}
if( Fixed )
{
this.level.setText( Out );
}
if( Out.isEmpty() )
{
Out = "0";
}
long result = Long.parseLong( Out );
result += i;
if( result < 0 )
{
result = 0;
}
this.level.setText( Out = Long.toString( result ) );
NetworkHandler.instance().sendToServer( new PacketValueConfig( "FluidLevelEmitter.Value", Out ) );
}
catch( final NumberFormatException e )
{
// nope..
this.level.setText( "0" );
}
catch( final IOException e )
{
AELog.debug( e );
}
}
@Override
protected void keyTyped( final char character, final int key ) throws IOException
{
if( !this.checkHotbarKeys( key ) )
{
if( ( key == 211 || key == 205 || key == 203 || key == 14 || Character.isDigit( character ) ) && this.level.textboxKeyTyped( character, key ) )
{
try
{
String Out = this.level.getText();
boolean Fixed = false;
while( Out.startsWith( "0" ) && Out.length() > 1 )
{
Out = Out.substring( 1 );
Fixed = true;
}
if( Fixed )
{
this.level.setText( Out );
}
if( Out.isEmpty() )
{
Out = "0";
}
NetworkHandler.instance().sendToServer( new PacketValueConfig( "FluidLevelEmitter.Value", Out ) );
}
catch( final IOException e )
{
AELog.debug( e );
}
}
else
{
super.keyTyped( character, key );
}
}
}
}
@@ -0,0 +1,97 @@
package appeng.fluids.container;
import net.minecraft.client.gui.GuiTextField;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import appeng.api.config.RedstoneMode;
import appeng.api.config.SecurityPermissions;
import appeng.api.config.Settings;
import appeng.container.guisync.GuiSync;
import appeng.fluids.parts.PartFluidLevelEmitter;
import appeng.fluids.util.IAEFluidTank;
import appeng.util.Platform;
public class ContainerFluidLevelEmitter extends ContainerFluidConfigurable
{
private final PartFluidLevelEmitter lvlEmitter;
@SideOnly( Side.CLIENT )
private GuiTextField textField;
@GuiSync( 3 )
public long EmitterValue = -1;
public ContainerFluidLevelEmitter( final InventoryPlayer ip, final PartFluidLevelEmitter te )
{
super( ip, te );
this.lvlEmitter = te;
}
@SideOnly( Side.CLIENT )
public void setTextField( final GuiTextField level )
{
this.textField = level;
this.textField.setText( String.valueOf( this.EmitterValue ) );
}
public void setLevel( final long l, final EntityPlayer player )
{
this.lvlEmitter.setReportingValue( l );
this.EmitterValue = l;
}
@Override
protected void setupConfig()
{
}
@Override
protected boolean supportCapacity()
{
return false;
}
@Override
public int availableUpgrades()
{
return 0;
}
@Override
public void detectAndSendChanges()
{
this.verifyPermissions( SecurityPermissions.BUILD, false );
if( Platform.isServer() )
{
this.EmitterValue = this.lvlEmitter.getReportingValue();
this.setRedStoneMode( (RedstoneMode) this.getUpgradeable().getConfigManager().getSetting( Settings.REDSTONE_EMITTER ) );
}
this.standardDetectAndSendChanges();
}
@Override
public void onUpdate( final String field, final Object oldValue, final Object newValue )
{
if( field.equals( "EmitterValue" ) )
{
if( this.textField != null )
{
this.textField.setText( String.valueOf( this.EmitterValue ) );
}
}
}
@Override
public IAEFluidTank getFluidConfigInventory()
{
return this.lvlEmitter.getConfig();
}
}
@@ -0,0 +1,325 @@
package appeng.fluids.parts;
import java.util.Random;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumHand;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import appeng.api.AEApi;
import appeng.api.config.RedstoneMode;
import appeng.api.config.Settings;
import appeng.api.networking.events.MENetworkChannelsChanged;
import appeng.api.networking.events.MENetworkEventSubscribe;
import appeng.api.networking.events.MENetworkPowerStatusChange;
import appeng.api.networking.security.IActionSource;
import appeng.api.networking.storage.IStackWatcher;
import appeng.api.networking.storage.IStackWatcherHost;
import appeng.api.parts.IPartCollisionHelper;
import appeng.api.parts.IPartModel;
import appeng.api.storage.IMEMonitor;
import appeng.api.storage.IStorageChannel;
import appeng.api.storage.channels.IFluidStorageChannel;
import appeng.api.storage.data.IAEFluidStack;
import appeng.api.storage.data.IAEStack;
import appeng.api.storage.data.IItemList;
import appeng.api.util.AECableType;
import appeng.api.util.AEPartLocation;
import appeng.api.util.IConfigManager;
import appeng.core.AppEng;
import appeng.core.sync.GuiBridge;
import appeng.fluids.util.AEFluidInventory;
import appeng.fluids.util.IAEFluidInventory;
import appeng.fluids.util.IAEFluidTank;
import appeng.items.parts.PartModels;
import appeng.me.GridAccessException;
import appeng.parts.PartModel;
import appeng.parts.automation.PartUpgradeable;
import appeng.util.IConfigManagerHost;
import appeng.util.Platform;
public class PartFluidLevelEmitter extends PartUpgradeable implements IStackWatcherHost, IConfigManagerHost, IAEFluidInventory
{
@PartModels
public static final ResourceLocation MODEL_BASE_OFF = new ResourceLocation( AppEng.MOD_ID, "part/level_emitter_base_off" );
@PartModels
public static final ResourceLocation MODEL_BASE_ON = new ResourceLocation( AppEng.MOD_ID, "part/level_emitter_base_on" );
@PartModels
public static final ResourceLocation MODEL_STATUS_OFF = new ResourceLocation( AppEng.MOD_ID, "part/level_emitter_status_off" );
@PartModels
public static final ResourceLocation MODEL_STATUS_ON = new ResourceLocation( AppEng.MOD_ID, "part/level_emitter_status_on" );
@PartModels
public static final ResourceLocation MODEL_STATUS_HAS_CHANNEL = new ResourceLocation( AppEng.MOD_ID, "part/level_emitter_status_has_channel" );
public static final PartModel MODEL_OFF_OFF = new PartModel( MODEL_BASE_OFF, MODEL_STATUS_OFF );
public static final PartModel MODEL_OFF_ON = new PartModel( MODEL_BASE_OFF, MODEL_STATUS_ON );
public static final PartModel MODEL_OFF_HAS_CHANNEL = new PartModel( MODEL_BASE_OFF, MODEL_STATUS_HAS_CHANNEL );
public static final PartModel MODEL_ON_OFF = new PartModel( MODEL_BASE_ON, MODEL_STATUS_OFF );
public static final PartModel MODEL_ON_ON = new PartModel( MODEL_BASE_ON, MODEL_STATUS_ON );
public static final PartModel MODEL_ON_HAS_CHANNEL = new PartModel( MODEL_BASE_ON, MODEL_STATUS_HAS_CHANNEL );
private static final int FLAG_ON = 4;
private boolean prevState = false;
private long lastReportedValue = 0;
private long reportingValue = 0;
private IStackWatcher stackWatcher = null;
private final AEFluidInventory config = new AEFluidInventory( this, 1 );
public PartFluidLevelEmitter( ItemStack is )
{
super( is );
this.getConfigManager().registerSetting( Settings.REDSTONE_EMITTER, RedstoneMode.HIGH_SIGNAL );
}
public long getReportingValue()
{
return this.reportingValue;
}
public void setReportingValue( final long v )
{
this.reportingValue = v;
this.updateState();
}
@Override
public void updateSetting( IConfigManager manager, Enum settingName, Enum newValue )
{
this.configureWatchers();
}
@Override
public void updateWatcher( IStackWatcher newWatcher )
{
this.stackWatcher = newWatcher;
this.configureWatchers();
}
@Override
public void onStackChange( IItemList<?> o, IAEStack<?> fullStack, IAEStack<?> diffStack, IActionSource src, IStorageChannel<?> chan )
{
if( chan == AEApi.instance().storage().getStorageChannel( IFluidStorageChannel.class ) && fullStack.equals( this.config.getFluidInSlot( 0 ) ) )
{
this.lastReportedValue = fullStack.getStackSize();
this.updateState();
}
}
@Override
public void onFluidInventoryChanged( IAEFluidTank inv, int slot )
{
this.configureWatchers();
}
@MENetworkEventSubscribe
public void channelChanged( final MENetworkChannelsChanged c )
{
this.updateState();
}
@MENetworkEventSubscribe
public void powerChanged( final MENetworkPowerStatusChange c )
{
this.updateState();
}
@Override
public int isProvidingStrongPower()
{
return this.prevState ? 15 : 0;
}
@Override
public int isProvidingWeakPower()
{
return this.prevState ? 15 : 0;
}
@Override
protected int populateFlags( final int cf )
{
return cf | ( this.prevState ? FLAG_ON : 0 );
}
private void updateState()
{
final boolean isOn = this.isLevelEmitterOn();
if( this.prevState != isOn )
{
this.getHost().markForUpdate();
final TileEntity te = this.getHost().getTile();
this.prevState = isOn;
Platform.notifyBlocksOfNeighbors( te.getWorld(), te.getPos() );
Platform.notifyBlocksOfNeighbors( te.getWorld(), te.getPos().offset( this.getSide().getFacing() ) );
}
}
private void configureWatchers()
{
if( this.stackWatcher != null )
{
this.stackWatcher.reset();
final IAEFluidStack myStack = this.config.getFluidInSlot( 0 );
if( myStack != null )
{
this.stackWatcher.add( myStack );
}
}
try
{
this.updateReportingValue(
this.getProxy().getStorage().getInventory( AEApi.instance().storage().getStorageChannel( IFluidStorageChannel.class ) ) );
}
catch( GridAccessException e )
{
// x.x
}
}
private void updateReportingValue( final IMEMonitor<IAEFluidStack> monitor )
{
final IAEFluidStack myStack = this.config.getFluidInSlot( 0 );
if( myStack != null )
{
final IAEFluidStack r = monitor.getStorageList().findPrecise( myStack );
if( r == null )
{
this.lastReportedValue = 0;
}
else
{
this.lastReportedValue = r.getStackSize();
}
}
this.updateState();
}
private boolean isLevelEmitterOn()
{
if( Platform.isClient() )
{
return ( this.getClientFlags() & FLAG_ON ) == FLAG_ON;
}
if( !this.getProxy().isActive() )
{
return false;
}
final boolean flipState = this.getConfigManager().getSetting( Settings.REDSTONE_EMITTER ) == RedstoneMode.LOW_SIGNAL;
return flipState ? this.reportingValue > this.lastReportedValue : this.reportingValue <= this.lastReportedValue;
}
@Override
public AECableType getCableConnectionType( final AEPartLocation dir )
{
return AECableType.SMART;
}
@Override
public float getCableConnectionLength( AECableType cable )
{
return 16;
}
@Override
public boolean canConnectRedstone()
{
return true;
}
@Override
public void getBoxes( final IPartCollisionHelper bch )
{
bch.addBox( 7, 7, 11, 9, 9, 16 );
}
@Override
public void randomDisplayTick( final World world, final BlockPos pos, final Random r )
{
if( this.isLevelEmitterOn() )
{
final AEPartLocation d = this.getSide();
final double d0 = d.xOffset * 0.45F + ( r.nextFloat() - 0.5F ) * 0.2D;
final double d1 = d.yOffset * 0.45F + ( r.nextFloat() - 0.5F ) * 0.2D;
final double d2 = d.zOffset * 0.45F + ( r.nextFloat() - 0.5F ) * 0.2D;
world.spawnParticle( EnumParticleTypes.REDSTONE, 0.5 + pos.getX() + d0, 0.5 + pos.getY() + d1, 0.5 + pos.getZ() + d2, 0.0D, 0.0D, 0.0D,
new int[0] );
}
}
@Override
public boolean onPartActivate( final EntityPlayer player, final EnumHand hand, final Vec3d pos )
{
if( !player.isSneaking() )
{
if( Platform.isClient() )
{
return true;
}
Platform.openGUI( player, this.getHost().getTile(), this.getSide(), GuiBridge.GUI_FLUID_LEVEL_EMITTER );
return true;
}
return false;
}
@Override
public IPartModel getStaticModels()
{
if( this.isActive() && this.isPowered() )
{
return this.isLevelEmitterOn() ? MODEL_ON_HAS_CHANNEL : MODEL_OFF_HAS_CHANNEL;
}
else if( this.isPowered() )
{
return this.isLevelEmitterOn() ? MODEL_ON_ON : MODEL_OFF_ON;
}
else
{
return this.isLevelEmitterOn() ? MODEL_ON_OFF : MODEL_OFF_OFF;
}
}
public IAEFluidTank getConfig()
{
return this.config;
}
@Override
public void readFromNBT( final NBTTagCompound data )
{
super.readFromNBT( data );
this.lastReportedValue = data.getLong( "lastReportedValue" );
this.reportingValue = data.getLong( "reportingValue" );
this.prevState = data.getBoolean( "prevState" );
this.config.readFromNBT( data, "config" );
}
@Override
public void writeToNBT( final NBTTagCompound data )
{
super.writeToNBT( data );
data.setLong( "lastReportedValue", this.lastReportedValue );
data.setLong( "reportingValue", this.reportingValue );
data.setBoolean( "prevState", this.prevState );
this.config.writeToNBT( data, "config" );
}
}