Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add laser compatibility with ae2 certus quartz cluster #226

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -212,19 +212,27 @@ public void finishBlockWork(BlockPos processed) {
super.finishBlockWork(processed);
}
}

public static List<ItemStack> getLootDrops(BlockState state, ServerWorld world, BlockPos pos, @Nullable BlockEntity blockEntity, int yieldAddons) {

return getLootDrops(state, world, pos, blockEntity, yieldAddons, null);
}

public static List<ItemStack> getLootDrops(BlockState state, ServerWorld world, BlockPos pos, @Nullable BlockEntity blockEntity, int yieldAddons, @Nullable PlayerEntity entity) {

var sampleTool = new ItemStack(Items.NETHERITE_PICKAXE);
sampleTool.set(DataComponentTypes.UNBREAKABLE, new UnbreakableComponent(false));
var fortuneEntry = world.getRegistryManager().get(RegistryKeys.ENCHANTMENT).getEntry(Enchantments.FORTUNE).get();
sampleTool.addEnchantment(fortuneEntry, Math.min(yieldAddons, 3));

var builder = new LootContextParameterSet.Builder(world).add(LootContextParameters.ORIGIN, Vec3d.ofCenter(pos)).add(LootContextParameters.TOOL, sampleTool).addOptional(LootContextParameters.BLOCK_ENTITY, blockEntity);

var builder = new LootContextParameterSet.Builder(world).add(LootContextParameters.ORIGIN, Vec3d.ofCenter(pos))
.add(LootContextParameters.TOOL, sampleTool)
.addOptional(LootContextParameters.BLOCK_ENTITY, blockEntity);
if (entity != null)
builder.addOptional(LootContextParameters.THIS_ENTITY, entity);
return state.getDroppedStacks(builder);
}
@Override

@Override
protected void doProgress(boolean moving) {
super.doProgress(moving);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,29 +69,29 @@
import static rearth.oritech.block.base.block.MultiblockMachine.ASSEMBLED;

public class LaserArmBlockEntity extends BlockEntity implements GeoBlockEntity, BlockEntityTicker<LaserArmBlockEntity>, EnergyApi.BlockProvider, ScreenProvider, ExtendedScreenHandlerFactory, MultiblockMachineController, MachineAddonController, InventoryProvider, RedstoneAddonBlockEntity.RedstoneControllable {

public static final String LASER_PLAYER_NAME = "oritech_laser";
private static final int BLOCK_BREAK_ENERGY = Oritech.CONFIG.laserArmConfig.blockBreakEnergyBase();

// storage
protected final DynamicEnergyStorage energyStorage = new DynamicEnergyStorage(getDefaultCapacity(), getDefaultInsertRate(), 0, this::markDirty);

public final SimpleInventory inventory = new SimpleInventory(3) {
@Override
public void markDirty() {
LaserArmBlockEntity.this.markDirty();
}
};

protected final InventoryStorage inventoryStorage = InventoryStorage.of(inventory, null);

// animation
protected final AnimatableInstanceCache animatableInstanceCache = GeckoLibUtil.createInstanceCache(this);
private final AnimationController<LaserArmBlockEntity> animationController = getAnimationController();

// multiblock
private final ArrayList<BlockPos> coreBlocksConnected = new ArrayList<>();

// addons
private final List<BlockPos> connectedAddons = new ArrayList<>();
private final List<BlockPos> openSlots = new ArrayList<>();
Expand All @@ -101,12 +101,12 @@ public void markDirty() {
public int yieldAddons = 0;
public int hunterAddons = 0;
public boolean hasCropFilterAddon = false;

// config
private final int range = Oritech.CONFIG.laserArmConfig.range();

private Vec3d laserHead;

// working data
private BlockPos targetDirection;
private BlockPos currentTarget;
Expand All @@ -119,21 +119,21 @@ public void markDirty() {
private boolean redstonePowered;
private ArrayDeque<BlockPos> pendingArea;
private final ArrayDeque<LivingEntity> pendingLivingTargets = new ArrayDeque<>();

// needed only on client
public Vec3d lastRenderPosition;
private PlayerEntity laserPlayerEntity = null;

public LaserArmBlockEntity(BlockPos pos, BlockState state) {
super(BlockEntitiesContent.LASER_ARM_ENTITY, pos, state);
laserHead = Vec3d.of(pos.up()).add(0.5, 0.55, 0.5);
}

@Override
public void tick(World world, BlockPos pos, BlockState state, LaserArmBlockEntity blockEntity) {
if (world.isClient() || !isActive(state))
return;

if (!redstonePowered && energyStorage.getAmount() >= energyRequiredToFire()) {
if (hunterAddons > 0) {
fireAtLivingEntities(world, pos, state, blockEntity);
Expand All @@ -145,7 +145,7 @@ else if (currentTarget != null && !currentTarget.equals(BlockPos.ZERO)) {
findNextBlockBreakTarget();
}
}

if (networkDirty)
updateNetwork();
}
Expand Down Expand Up @@ -188,7 +188,7 @@ private void fireAtLivingEntities(World world, BlockPos pos, BlockState state, L
}

}

public void setRedstonePowered(boolean redstonePowered) {
this.redstonePowered = redstonePowered;
}
Expand All @@ -204,16 +204,19 @@ public int getBlockBreakProgress() {
public int getTargetBlockEnergyNeeded() {
return targetBlockEnergyNeeded;
}

public void finishBlockBreaking(BlockPos targetPos, BlockState targetBlockState) {
progress -= targetBlockEnergyNeeded;

var targetEntity = world.getBlockEntity(targetPos);
List<ItemStack> dropped;
// added getLaserPlayerEntity() to make ae2 certus quartz drop from certus
// quartz clusters because it's expecting an entity in
// LootContextParameters.THIS_ENTITY
if (yieldAddons > 0) {
dropped = DestroyerBlockEntity.getLootDrops(targetBlockState, (ServerWorld) world, targetPos, targetEntity, yieldAddons);
dropped = DestroyerBlockEntity.getLootDrops(targetBlockState, (ServerWorld) world, targetPos, targetEntity, yieldAddons, getLaserPlayerEntity());
} else {
dropped = net.minecraft.block.Block.getDroppedStacks(targetBlockState, (ServerWorld) world, targetPos, targetEntity);
dropped = net.minecraft.block.Block.getDroppedStacks(targetBlockState, (ServerWorld) world, targetPos, targetEntity, getLaserPlayerEntity(), ItemStack.EMPTY);
}

var blockRecipe = tryGetRecipeOfBlock(targetBlockState);
Expand All @@ -223,12 +226,12 @@ public void finishBlockBreaking(BlockPos targetPos, BlockState targetBlockState)
dropped = List.of(new ItemStack(recipe.getResults().get(0).getItem(), farmedCount));
ParticleContent.CHARGING.spawn(world, Vec3d.of(targetPos), 1);
}

// yes, this will discard items that wont fit anymore
for (var stack : dropped) {
this.inventory.addStack(stack);
}

try {
targetBlockState.getBlock().onBreak(world, targetPos, targetBlockState, getLaserPlayerEntity());
} catch (Exception exception) {
Expand All @@ -237,10 +240,10 @@ public void finishBlockBreaking(BlockPos targetPos, BlockState targetBlockState)
world.addBlockBreakParticles(targetPos, world.getBlockState(targetPos));
world.playSound(null, targetPos, targetBlockState.getSoundGroup().getBreakSound(), SoundCategory.BLOCKS, 1f, 1f);
world.breakBlock(targetPos, false);

findNextBlockBreakTarget();
}

private RecipeEntry<OritechRecipe> tryGetRecipeOfBlock(BlockState destroyed) {
var inputItem = destroyed.getBlock().asItem();
var inputInv = new SimpleCraftingInventory(new ItemStack(inputItem));
Expand Down