Skip to content

Instantly share code, notes, and snippets.

@quat1024
Last active March 9, 2018 19:32
Show Gist options
  • Save quat1024/4253dab80e581fb63363b5db903a03af to your computer and use it in GitHub Desktop.
Save quat1024/4253dab80e581fb63363b5db903a03af to your computer and use it in GitHub Desktop.
LKSADaskldklasd
package quaternary.incorporeal.block;
import net.minecraft.block.Block;
import net.minecraft.block.material.MapColor;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.EnumDyeColor;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.*;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import quaternary.incorporeal.Incorporeal;
import quaternary.incorporeal.lexicon.IncorporealLexiData;
import quaternary.incorporeal.tile.TileCorporeaSparkTinkerer;
import vazkii.botania.api.lexicon.ILexiconable;
import vazkii.botania.api.lexicon.LexiconEntry;
import vazkii.botania.api.state.BotaniaStateProps;
import vazkii.botania.common.item.ModItems;
import javax.annotation.Nullable;
public class BlockCorporeaSparkTinkerer extends Block implements ILexiconable {
public BlockCorporeaSparkTinkerer() {
super(Material.ROCK, MapColor.PURPLE);
setRegistryName(new ResourceLocation(Incorporeal.MODID, "corporea_spark_tinkerer"));
setUnlocalizedName(Incorporeal.MODID + ".corporea_spark_tinkerer");
setDefaultState(getDefaultState().withProperty(BotaniaStateProps.POWERED, true));
}
//Messages to tile
@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
ItemStack heldStack = player.getHeldItem(hand);
TileEntity te = world.getTileEntity(pos);
if(te instanceof TileCorporeaSparkTinkerer && heldStack.getItem() == ModItems.dye) { //floral powder
if(!world.isRemote) {
TileCorporeaSparkTinkerer tinkerer = (TileCorporeaSparkTinkerer) te;
EnumDyeColor heldColor = EnumDyeColor.byMetadata(heldStack.getMetadata());
Incorporeal.LOGGER.info("Setting color to " + heldColor);
tinkerer.setNetwork(heldColor);
}
return true;
}
return false;
}
@Override
public void neighborChanged(IBlockState state, World world, BlockPos pos, Block block, BlockPos fromPos) {
boolean worldPower = world.isBlockIndirectlyGettingPowered(pos) > 0;
boolean myPower = state.getValue(BotaniaStateProps.POWERED);
if(worldPower != myPower) {
world.setBlockState(pos, state.withProperty(BotaniaStateProps.POWERED, worldPower), 2);
if(worldPower) {
TileEntity tile = world.getTileEntity(pos);
if(tile instanceof TileCorporeaSparkTinkerer) {
((TileCorporeaSparkTinkerer) tile).doSwap();
}
}
}
}
//State boilerplate
@Override
protected BlockStateContainer createBlockState() {
return new BlockStateContainer(this, BotaniaStateProps.POWERED);
}
@Override
public IBlockState getStateFromMeta(int meta) {
return getDefaultState().withProperty(BotaniaStateProps.POWERED, meta == 1);
}
@Override
public int getMetaFromState(IBlockState state) {
return state.getValue(BotaniaStateProps.POWERED) ? 1 : 0;
}
//Aabbabababb
AxisAlignedBB aabb = new AxisAlignedBB(0d, 0d, 0d, 1d, 0.1875d, 1d);
@Override
public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos) {
return aabb;
}
@Override
public boolean isFullCube(IBlockState state) {
return false;
}
@Override
public boolean isOpaqueCube(IBlockState state) {
return false;
}
//Tile
@Override
public boolean hasTileEntity(IBlockState blah) {
return true;
}
@Nullable
@Override
public TileEntity createTileEntity(World world, IBlockState state) {
return new TileCorporeaSparkTinkerer();
}
//Entry
@Override
public LexiconEntry getEntry(World world, BlockPos blockPos, EntityPlayer entityPlayer, ItemStack itemStack) {
return IncorporealLexiData.corporeaTinkerer;
}
}
package quaternary.incorporeal.tile;
import net.minecraft.item.EnumDyeColor;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.MathHelper;
import net.minecraftforge.fml.relauncher.ReflectionHelper;
import quaternary.incorporeal.Incorporeal;
import quaternary.incorporeal.etc.CorporeaHelper2;
import vazkii.botania.common.entity.EntityCorporeaSpark;
import java.util.*;
public class TileCorporeaSparkTinkerer extends TileEntity {
private EnumDyeColor myNetwork = EnumDyeColor.WHITE;
public TileCorporeaSparkTinkerer() {}
public void doSwap() {
if(world.isRemote) return;
//look for other corporea sparks nearby
List<EntityCorporeaSpark> nearbySparks = new ArrayList<>();
for(EnumFacing offset : EnumFacing.HORIZONTALS) {
EntityCorporeaSpark spork = CorporeaHelper2.getSparkEntityForBlock(world, pos.offset(offset));
if(spork != null) nearbySparks.add(spork);
}
if(nearbySparks.size() == 0) return;
//choose one
Collections.shuffle(nearbySparks);
EntityCorporeaSpark spork = nearbySparks.get(0);
//switch my network color with that one
EnumDyeColor itsNetwork = spork.getNetwork();
Incorporeal.LOGGER.info("my network " + myNetwork);
Incorporeal.LOGGER.info("its network " + itsNetwork);
spork.setNetwork(myNetwork);
myNetwork = itsNetwork;
//this causes the corporea spark to relink
//for some reason just changing the network is not enough...
//also this is private...
ReflectionHelper.setPrivateValue(EntityCorporeaSpark.class, spork, true, "firstTick");
Incorporeal.LOGGER.info("ASDASD");
Incorporeal.LOGGER.info("my network " + myNetwork);
Incorporeal.LOGGER.info("its network " + spork.getNetwork());
}
public void setNetwork(EnumDyeColor color) {
Incorporeal.LOGGER.info("SETTING MY COLOR TO " + color);
myNetwork = color;
Incorporeal.LOGGER.info(myNetwork);
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
myNetwork = EnumDyeColor.byMetadata(MathHelper.clamp(nbt.getInteger("Network"), 0, 15));
super.readFromNBT(nbt);
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
nbt.setInteger("Network", myNetwork.getMetadata());
return super.writeToNBT(nbt);
}
}
[14:26:04] [Server thread/INFO]: <Player569> I right click the tinkerer with red dye
[14:26:07] [Server thread/INFO] [Incorporeal]: Setting color to red
[14:26:07] [Server thread/INFO] [Incorporeal]: SETTING MY COLOR TO red
[14:26:07] [Server thread/INFO] [Incorporeal]: red
[14:26:21] [Server thread/INFO]: <Player569> I power the tinkerer next to a chest with a blue spark on it
[14:26:26] [Server thread/INFO] [Incorporeal]: my network white
[14:26:26] [Server thread/INFO] [Incorporeal]: its network blue
[14:26:26] [Server thread/INFO] [Incorporeal]: ASDASD
[14:26:26] [Server thread/INFO] [Incorporeal]: my network blue
[14:26:26] [Server thread/INFO] [Incorporeal]: its network white
[14:26:34] [Server thread/INFO]: <Player569> It returns to white
[14:26:55] [Server thread/INFO]: <Player569> It's like, I'm setting the contents of the "myNetwork" field, but those changes disappear when I power the spark tinkerer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment